upgraded phpunit and tests to lumen 9

- added namespaces
- used values from https://github.com/laravel/lumen
- updated phpunit config
- moved database migrations in phpunit test listener
This commit is contained in:
pushrbx 2022-12-11 00:08:59 +00:00
parent 628f3e2294
commit d76e81ff3c
23 changed files with 181 additions and 69 deletions

9
bootstrap/tests.php Normal file
View File

@ -0,0 +1,9 @@
<?php
use PackageVersions\Versions;
require_once __DIR__.'/../vendor/autoload.php';
/*
Defines
*/
defined('JIKAN_PARSER_VERSION') or define('JIKAN_PARSER_VERSION', Versions::getVersion('jikan-me/jikan'));

View File

@ -44,23 +44,22 @@
"phpunit/phpunit": "^9.5.26" "phpunit/phpunit": "^9.5.26"
}, },
"autoload": { "autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": { "psr-4": {
"App\\": "app/" "App\\": "app/",
"Database\\Factories\\": "database/factories/"
} }
}, },
"autoload-dev": { "autoload-dev": {
"classmap": [ "psr-4": {
"tests/", "Tests\\": ["tests/"]
"database/" }
]
}, },
"scripts": { "scripts": {
"post-root-package-install": [ "post-root-package-install": [
"php -r \"copy('.env.dist', '.env');\"" "php -r \"copy('.env.dist', '.env');\""
],
"test": [
"php ./vendor/phpunit/phpunit/phpunit"
] ]
}, },
"minimum-stability": "dev", "minimum-stability": "dev",

View File

@ -1,29 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupStaticAttributes="false" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="bootstrap/app.php" bootstrap="bootstrap/tests.php"
colors="true" colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true" verbose="true"
> >
<testsuites> <testsuites>
<testsuite name="Application Test Suite"> <testsuite name="http-integration">
<directory suffix="Test.php">./tests</directory> <directory>./tests/HttpV4/</directory>
</testsuite>
<testsuite name="integration">
<directory>./tests/integration/</directory>
</testsuite>
<testsuite name="unit">
<directory>./tests/unit/</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter> <coverage>
<whitelist processUncoveredFilesFromWhitelist="true"> <include>
<directory suffix=".php">./app</directory> <directory suffix=".php">./app</directory>
</whitelist> </include>
</filter> </coverage>
<listeners>
<listener class="Tests\IntegrationTestListener" />
</listeners>
<php> <php>
<env name="APP_ENV" value="testing"/> <env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="file"/> <env name="CACHE_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="redis"/> <env name="QUEUE_CONNECTION" value="sync"/>
</php> </php>
</phpunit> </phpunit>

View File

@ -1,16 +0,0 @@
<?php
use App\Anime;
use Laravel\Lumen\Testing\DatabaseMigrations;
// silly hack to run the database migrations once
// and for the other test cases we keep truncating the collections in mongo.
class AaDatabaseTest extends TestCase
{
use DatabaseMigrations;
public function testDatabase()
{
$this->assertEquals(0, Anime::count());
}
}

View File

@ -1,6 +1,8 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class AnimeControllerV4Test extends TestCase class AnimeControllerTest extends TestCase
{ {
public function testMain() public function testMain()
{ {

View File

@ -1,4 +1,7 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class CharacterControllerTest extends TestCase class CharacterControllerTest extends TestCase
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use tests\TestCase;
class ClubControllerTest extends TestCase class ClubControllerTest extends TestCase
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class GenreControllerTest extends TestCase class GenreControllerTest extends TestCase
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class MagazineControllerTest extends TestCase class MagazineControllerTest extends TestCase
{ {

View File

@ -1,6 +1,8 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class MangaControllerV4Test extends TestCase class MangaControllerTest extends TestCase
{ {
public function testMain() public function testMain()
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class PersonControllerTest extends TestCase class PersonControllerTest extends TestCase
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class ProducerControllerTest extends TestCase class ProducerControllerTest extends TestCase
{ {

View File

@ -1,4 +1,7 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class RecommendationsControllerTest extends TestCase class RecommendationsControllerTest extends TestCase
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class ReviewsControllerTest extends TestCase class ReviewsControllerTest extends TestCase
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class ScheduleControllerTest extends TestCase class ScheduleControllerTest extends TestCase
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class SearchControllerTest extends TestCase class SearchControllerTest extends TestCase
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class TopControllerTest extends TestCase class TopControllerTest extends TestCase
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class UserControllerTest extends TestCase class UserControllerTest extends TestCase
{ {

View File

@ -1,4 +1,6 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class WatchControllerTest extends TestCase class WatchControllerTest extends TestCase
{ {

View File

@ -1,8 +1,9 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\Integration;
use App\Testing\ScoutFlush;
use App\Anime; use App\Anime;
use App\Testing\ScoutFlush;
use App\Testing\SyntheticMongoDbTransaction; use App\Testing\SyntheticMongoDbTransaction;
use Tests\TestCase;
class AnimeSearchEndpointTest extends TestCase class AnimeSearchEndpointTest extends TestCase
{ {

View File

@ -0,0 +1,80 @@
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests;
use \Throwable;
use PHPUnit\Framework\TestListener;
// fixme: with phpunit 10, this should be replaced with the new event system
class IntegrationTestListener implements TestListener
{
private $app;
public function __construct()
{
$app = require __DIR__.'/../bootstrap/app.php';
$database = env('DB_DATABASE', 'jikan_tests');
$app['config']->set('database.connections.mongodb.database', $database === 'jikan' ? 'jikan_tests' : $database);
$this->app = $app;
}
public function addError(\PHPUnit\Framework\Test $test, Throwable $t, float $time): void
{
}
public function addWarning(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time): void
{
}
public function addFailure(\PHPUnit\Framework\Test $test, \PHPUnit\Framework\AssertionFailedError $e, float $time): void
{
}
public function addIncompleteTest(\PHPUnit\Framework\Test $test, Throwable $t, float $time): void
{
}
public function addRiskyTest(\PHPUnit\Framework\Test $test, Throwable $t, float $time): void
{
}
public function addSkippedTest(\PHPUnit\Framework\Test $test, Throwable $t, float $time): void
{
}
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite): void
{
echo $suite->getName();
if ($suite->getName() == "integration") {
$app = $this->app;
$kernel = $app->make(
'Illuminate\Contracts\Console\Kernel'
);
try {
$kernel->call('migrate:fresh', []);
} catch (\Exception $ex) {
print_r($ex);
}
}
}
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite): void
{
if ($suite->getName() == "integration") {
$app = $this->app;
$kernel = $app->make(
'Illuminate\Contracts\Console\Kernel'
);
$kernel->call('migrate:rollback');
}
}
public function startTest(\PHPUnit\Framework\Test $test): void
{
// TODO: Implement startTest() method.
}
public function endTest(\PHPUnit\Framework\Test $test, float $time): void
{
// TODO: Implement endTest() method.
}
}

View File

@ -1,17 +1,21 @@
<?php <?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests;
use App\Testing\Concerns\MakesHttpRequestsEx; use App\Testing\Concerns\MakesHttpRequestsEx;
use App\Testing\ScoutFlush; use App\Testing\ScoutFlush;
use App\Testing\SyntheticMongoDbTransaction; use App\Testing\SyntheticMongoDbTransaction;
use Faker\Factory;
use Faker\Generator;
use Laravel\Lumen\Testing\TestCase as LumenTestCase;
abstract class TestCase extends Laravel\Lumen\Testing\TestCase abstract class TestCase extends LumenTestCase
{ {
use MakesHttpRequestsEx; use MakesHttpRequestsEx;
protected Faker\Generator $faker; protected Generator $faker;
protected function setUp(): void protected function setUp(): void
{ {
parent::setUp(); parent::setUp();
$this->faker = Faker\Factory::create(); $this->faker = Factory::create();
} }
/** /**

0
tests/Unit/.gitkeep Normal file
View File