Merge remote-tracking branch 'origin/master'

# Conflicts:
#	tests/Feature/LoginControllerTest.php
#	tests/TestCase.php
This commit is contained in:
Sergio 2020-09-29 09:47:35 +02:00
commit d9be2c7696
4 changed files with 91 additions and 29 deletions

26
tests/BootApplication.php Normal file
View file

@ -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;
}
}

View file

@ -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
{
private $app;
public function __construct(App $app, $server = [], History $history = null, CookieJar $cookieJar = null)
{
parent::__construct($server, $history, $cookieJar);
$this->app = $app;
}
protected function doRequest($request)
{
/** @var \Slim\App $app */
$app = require BASE_DIR.'bootstrap/app.php';
$response = $this->app->handle(new ServerRequest($request->getMethod(), $request->getUri(), [], $request->getContent()));
$migrator = new Migrator($app->getContainer()->get('database'), BASE_DIR.'resources/schemas');
$migrator->migrate();
$body = $response->getBody();
$response = $app->handle(new ServerRequest($request->getMethod(), $request->getUri(), [], $request->getContent()));
return new Response($response->getBody()->getContents(), $response->getStatusCode(), $response->getHeaders());
if ($body->isSeekable()) {
$body->rewind();
}
return new Response($body->getContents(), $response->getStatusCode(), $response->getHeaders());
}
}

View file

@ -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());
//
//
// }
}

View file

@ -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');
}
}