Test suite finally working
This commit is contained in:
parent
d9be2c7696
commit
3d254a9822
6 changed files with 788 additions and 416 deletions
|
@ -29,7 +29,10 @@
|
|||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"preferred-install": "dist",
|
||||
"sort-packages": true
|
||||
"sort-packages": true,
|
||||
"platform": {
|
||||
"php": "7.1.33"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
|
@ -46,7 +49,7 @@
|
|||
},
|
||||
"require-dev": {
|
||||
"phpstan/phpstan": "^0.11.5",
|
||||
"symfony/browser-kit": "^4.4",
|
||||
"phpunit/phpunit": "7.5"
|
||||
"phpunit/phpunit": "7.5",
|
||||
"symfony/dom-crawler": "^4.4"
|
||||
}
|
||||
}
|
||||
|
|
963
composer.lock
generated
963
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -1,26 +0,0 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
|
@ -22,24 +22,45 @@ class LoginControllerTest extends TestCase
|
|||
$response = $this->post(route('login.show'));
|
||||
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
$this->assertSame(route('login.show'), $response->getHeader('Location'));
|
||||
$this->assertSame(route('login.show'), $response->getHeaderLine('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());
|
||||
//
|
||||
//
|
||||
// }
|
||||
/** @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)]);
|
||||
|
||||
$response = $this->get(route('login.show'));
|
||||
$form = $this->getCrawler($response)
|
||||
->selectButton('Login')
|
||||
->form([
|
||||
'username' => 'admin@example.com',
|
||||
'password' => 'admin',
|
||||
], 'POST');
|
||||
|
||||
$response = $this->submitForm($form);
|
||||
$this->assertSame(302, $response->getStatusCode());
|
||||
$this->assertSame(route('home'), $response->getHeaderLine('Location'));
|
||||
|
||||
$response = $this->get(route('home'));
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_hide_register_by_default()
|
||||
{
|
||||
$response = $this->get(route('login.show'));
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
$this->assertStringNotContainsString('Register', $this->getCrawler($response)->text());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_show_register_when_enabled()
|
||||
{
|
||||
$this->database()->query("INSERT INTO `settings`(`key`, `value`) VALUES ('register_enabled', 'on')");
|
||||
|
||||
$response = $this->get(route('login.show'));
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
$this->assertStringContainsString('Register', $this->getCrawler($response)->text());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,50 +2,16 @@
|
|||
|
||||
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;
|
||||
use WithApplication;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->client = new Client($this->createApplication());
|
||||
$this->client->followRedirects(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param array $parameters
|
||||
* @return object|Response
|
||||
*/
|
||||
public function get(string $uri, array $parameters = [])
|
||||
{
|
||||
$this->client->request('GET', $uri, $parameters);
|
||||
return $this->client->getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param array $parameters
|
||||
* @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');
|
||||
parent::setUp();
|
||||
$_SESSION = []; // ugly workaround to the the session superglobal between tests
|
||||
$this->createApplication();
|
||||
}
|
||||
}
|
||||
|
|
107
tests/WithApplication.php
Normal file
107
tests/WithApplication.php
Normal file
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use App\Database\DB;
|
||||
use App\Database\Migrator;
|
||||
use GuzzleHttp\Psr7\ServerRequest;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use Symfony\Component\DomCrawler\Form;
|
||||
|
||||
trait WithApplication
|
||||
{
|
||||
/** @var \Slim\App $app */
|
||||
protected $app;
|
||||
|
||||
public function createApplication()
|
||||
{
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $method
|
||||
* @param $uri
|
||||
* @param array $parameters
|
||||
* @param array $headers
|
||||
* @param null $body
|
||||
* @param array $cookies
|
||||
* @param array $files
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function request($method, $uri, $parameters = [], $headers = [], $body = null, $cookies = [], $files = [])
|
||||
{
|
||||
$request = (new ServerRequest($method, $uri, $headers, $body))
|
||||
->withParsedBody($parameters)
|
||||
->withQueryParams($parameters)
|
||||
->withUploadedFiles(ServerRequest::normalizeFiles($files))
|
||||
->withCookieParams($cookies);
|
||||
|
||||
$response = $this->app->handle($request);
|
||||
|
||||
if ($response->getBody()->isSeekable()) {
|
||||
$response->getBody()->rewind();
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param array $parameters
|
||||
* @param array $cookies
|
||||
* @param array $headers
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function get(string $uri, array $parameters = [], $cookies = [], $headers = [], $files = [])
|
||||
{
|
||||
return $this->request('GET', $uri, $parameters, $headers, null, $cookies, $files);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param array $parameters
|
||||
* @param array $cookies
|
||||
* @param array $headers
|
||||
* @param array $files
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function post(string $uri, array $parameters = [], $cookies = [], $headers = [], $files = [])
|
||||
{
|
||||
return $this->request('POST', $uri, $parameters, $headers, http_build_query($parameters), $cookies, $files);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Form $form
|
||||
* @param array $headers
|
||||
* @param array $cookies
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function submitForm(Form $form, $headers = [], $cookies = [])
|
||||
{
|
||||
return $this->request($form->getMethod(), $form->getUri(), $form->getValues(), $headers, http_build_query($form->getValues()), $cookies, $form->getFiles());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DB
|
||||
*/
|
||||
public function database()
|
||||
{
|
||||
return $this->app->getContainer()->get('database');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
* @return Crawler
|
||||
*/
|
||||
public function getCrawler($response)
|
||||
{
|
||||
return new Crawler($response->getBody()->getContents());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue