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"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
"App\\": "app/",
"Database\\Factories\\": "database/factories/"
}
},
"autoload-dev": {
"classmap": [
"tests/",
"database/"
]
"psr-4": {
"Tests\\": ["tests/"]
}
},
"scripts": {
"post-root-package-install": [
"php -r \"copy('.env.dist', '.env');\""
],
"test": [
"php ./vendor/phpunit/phpunit/phpunit"
]
},
"minimum-stability": "dev",

View File

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

View File

@ -1,4 +1,7 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\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
{

View File

@ -1,4 +1,6 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\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
{

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()
{

View File

@ -1,4 +1,6 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\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
{

View File

@ -1,4 +1,7 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\TestCase;
class RecommendationsControllerTest extends TestCase
{
@ -45,4 +48,4 @@ 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
{

View File

@ -1,4 +1,6 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\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
{

View File

@ -1,4 +1,6 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\HttpV4\Controllers;
use Tests\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
{

View File

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

View File

@ -1,8 +1,9 @@
<?php
use App\Testing\ScoutFlush;
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
namespace Tests\Integration;
use App\Anime;
use App\Testing\ScoutFlush;
use App\Testing\SyntheticMongoDbTransaction;
use Tests\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\ScoutFlush;
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;
protected Faker\Generator $faker;
protected Generator $faker;
protected function setUp(): void
{
parent::setUp();
$this->faker = Faker\Factory::create();
$this->faker = Factory::create();
}
/**

0
tests/Unit/.gitkeep Normal file
View File