Ver código fonte

Keep an app instace in tests

Sergio Brighenti 4 anos atrás
pai
commit
2eb269d6bf

+ 28 - 0
tests/BootApplication.php

@@ -0,0 +1,28 @@
+<?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 - 1
tests/Feature/LoginControllerTest.php

@@ -11,7 +11,35 @@ class LoginControllerTest extends TestCase
     /** @test */
     public function it_loads_the_login_page()
     {
-        $response = $this->get('/login');
+        $response = $this->get(route('login.show'));
+
         $this->assertSame(200, $response->getStatusCode());
     }
+
+    /** @test */
+    public function it_redirect_to_login_with_no_data()
+    {
+        $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());
+//
+//
+//    }
 }

+ 15 - 3
tests/TestCase.php

@@ -2,23 +2,27 @@
 
 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
-     * @return Response|object
+     * @return object|Response
      */
     public function get(string $uri, array $parameters = [])
     {
@@ -29,11 +33,19 @@ abstract class TestCase extends BaseTestCase
     /**
      * @param  string  $uri
      * @param  array  $parameters
-     * @return Response|object
+     * @return object|Response
      */
     public function post(string $uri, array $parameters = [])
     {
         $this->client->request('POST', $uri, $parameters);
         return $this->client->getResponse();
     }
+
+    /**
+     * @return DB
+     */
+    public function database()
+    {
+        return $this->app->getContainer()->get('database');
+    }
 }