Jelajahi Sumber

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	tests/Feature/LoginControllerTest.php
#	tests/TestCase.php
Sergio 4 tahun lalu
induk
melakukan
d9be2c7696
4 mengubah file dengan 91 tambahan dan 29 penghapusan
  1. 26 0
      tests/BootApplication.php
  2. 17 8
      tests/Client.php
  3. 29 6
      tests/Feature/LoginControllerTest.php
  4. 19 15
      tests/TestCase.php

+ 26 - 0
tests/BootApplication.php

@@ -0,0 +1,26 @@
+<?php
+
+
+namespace Tests;
+
+use App\Database\Migrator;
+
+trait BootApplication
+{
+    protected $app;
+
+    public function createApplication(bool $rebuild = false)
+    {
+        if (!$rebuild && $this->app !== null) {
+            return $this->app;
+        }
+
+        /** @var \Slim\App $app */
+        $this->app = require BASE_DIR.'bootstrap/app.php';
+
+        $migrator = new Migrator($this->app->getContainer()->get('database'), BASE_DIR.'resources/schemas');
+        $migrator->migrate();
+
+        return $this->app;
+    }
+}

+ 17 - 8
tests/Client.php

@@ -3,23 +3,32 @@
 
 namespace Tests;
 
-use App\Database\Migrator;
 use GuzzleHttp\Psr7\ServerRequest;
+use Slim\App;
 use Symfony\Component\BrowserKit\AbstractBrowser;
+use Symfony\Component\BrowserKit\CookieJar;
+use Symfony\Component\BrowserKit\History;
 use Symfony\Component\BrowserKit\Response;
 
 class Client extends AbstractBrowser
 {
-    protected function doRequest($request)
+    private $app;
+
+    public function __construct(App $app, $server = [], History $history = null, CookieJar $cookieJar = null)
     {
-        /** @var \Slim\App $app */
-        $app = require BASE_DIR.'bootstrap/app.php';
+        parent::__construct($server, $history, $cookieJar);
+        $this->app = $app;
+    }
 
-        $migrator = new Migrator($app->getContainer()->get('database'), BASE_DIR.'resources/schemas');
-        $migrator->migrate();
+    protected function doRequest($request)
+    {
+        $response = $this->app->handle(new ServerRequest($request->getMethod(), $request->getUri(), [], $request->getContent()));
 
-        $response = $app->handle(new ServerRequest($request->getMethod(), $request->getUri(), [], $request->getContent()));
+        $body = $response->getBody();
 
-        return new Response($response->getBody()->getContents(), $response->getStatusCode(), $response->getHeaders());
+        if ($body->isSeekable()) {
+            $body->rewind();
+        }
+        return new Response($body->getContents(), $response->getStatusCode(), $response->getHeaders());
     }
 }

+ 29 - 6
tests/Feature/LoginControllerTest.php

@@ -8,15 +8,38 @@ use Tests\TestCase;
 class LoginControllerTest extends TestCase
 {
 
-    public function test_it_loads_the_login_page(): void
+    /** @test */
+    public function it_loads_the_login_page()
     {
-        $response = $this->get('/login');
-        self::assertSame(200, $response->getStatusCode());
+        $response = $this->get(route('login.show'));
+
+        $this->assertSame(200, $response->getStatusCode());
     }
 
-    public function test_it_redirect_back_to_login_page_with_no_credentials(): void
+    /** @test */
+    public function it_redirect_to_login_with_no_data()
     {
-        $response = $this->post('/login');
-        self::assertSame(200, $response->getStatusCode());
+        $response = $this->post(route('login.show'));
+
+        $this->assertSame(302, $response->getStatusCode());
+        $this->assertSame(route('login.show'), $response->getHeader('Location'));
     }
+
+//    /** @test */
+//    public function it_login_with_correct_data()
+//    {
+//        $this->database()->query("INSERT INTO `users` (`email`, `username`, `password`, `is_admin`, `user_code`) VALUES ('admin@example.com', 'admin', ?, 1, ?)", [password_hash('admin', PASSWORD_DEFAULT), humanRandomString(5)]);
+//
+//        $loginPage = $this->client->request('GET', route('login.show'));
+//        $form = $loginPage->selectButton('Login')->form([
+//            'username' => 'admin@example.com',
+//            'password' => 'admin',
+//        ], 'POST');
+//
+//        $this->client->submit($form);
+//        $this->client->followRedirect();
+//        dd($this->client->getResponse());
+//
+//
+//    }
 }

+ 19 - 15
tests/TestCase.php

@@ -2,46 +2,50 @@
 
 namespace Tests;
 
+use App\Database\DB;
 use PHPUnit\Framework\TestCase as BaseTestCase;
 use Symfony\Component\BrowserKit\Response;
 
 abstract class TestCase extends BaseTestCase
 {
+    use BootApplication;
+
     /** @var Client */
     protected $client;
 
     protected function setUp()
     {
-        $this->client = new Client();
+        $this->client = new Client($this->createApplication());
+        $this->client->followRedirects(false);
     }
 
     /**
      * @param  string  $uri
      * @param  array  $parameters
-     * @param  array  $files
-     * @param  array  $server
-     * @param  string|null  $content
-     * @param  bool  $changeHistory
-     * @return Response|object
+     * @return object|Response
      */
-    protected function get(string $uri, array $parameters = [], array $files = [], array $server = [], string $content = null, bool $changeHistory = true)
+    public function get(string $uri, array $parameters = [])
     {
-        $this->client->request('GET', $uri, $parameters, $files, $server, $content, $changeHistory);
+        $this->client->request('GET', $uri, $parameters);
         return $this->client->getResponse();
     }
 
     /**
      * @param  string  $uri
      * @param  array  $parameters
-     * @param  array  $files
-     * @param  array  $server
-     * @param  string|null  $content
-     * @param  bool  $changeHistory
-     * @return Response|object
+     * @return object|Response
      */
-    protected function post(string $uri, array $parameters = [], array $files = [], array $server = [], string $content = null, bool $changeHistory = true)
+    public function post(string $uri, array $parameters = [])
     {
-        $this->client->request('POST', $uri, $parameters, $files, $server, $content, $changeHistory);
+        $this->client->request('POST', $uri, $parameters);
         return $this->client->getResponse();
     }
+
+    /**
+     * @return DB
+     */
+    public function database()
+    {
+        return $this->app->getContainer()->get('database');
+    }
 }