2022-12-11 00:08:59 +00:00
|
|
|
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
|
|
|
|
namespace Tests;
|
2022-12-04 20:13:24 +00:00
|
|
|
use App\Testing\Concerns\MakesHttpRequestsEx;
|
2022-12-04 19:53:27 +00:00
|
|
|
use App\Testing\ScoutFlush;
|
2022-12-04 21:43:29 +00:00
|
|
|
use App\Testing\SyntheticMongoDbTransaction;
|
2022-12-21 11:29:53 +00:00
|
|
|
use Faker\Factory as FakerFactory;
|
2022-12-11 00:08:59 +00:00
|
|
|
use Faker\Generator;
|
2022-12-21 18:33:19 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2022-12-16 11:00:18 +00:00
|
|
|
use Illuminate\Testing\TestResponse;
|
2022-12-11 00:08:59 +00:00
|
|
|
use Laravel\Lumen\Testing\TestCase as LumenTestCase;
|
2018-04-21 12:59:48 +05:00
|
|
|
|
2022-12-11 00:08:59 +00:00
|
|
|
abstract class TestCase extends LumenTestCase
|
2018-04-21 12:59:48 +05:00
|
|
|
{
|
2022-12-04 11:38:11 +00:00
|
|
|
use MakesHttpRequestsEx;
|
2022-12-11 00:08:59 +00:00
|
|
|
protected Generator $faker;
|
2022-12-16 11:00:18 +00:00
|
|
|
protected int $maxResultsPerPage;
|
2022-12-04 19:53:27 +00:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2022-12-21 11:29:53 +00:00
|
|
|
$this->faker = FakerFactory::create();
|
2022-12-16 11:00:18 +00:00
|
|
|
$this->maxResultsPerPage = env("MAX_RESULTS_PER_PAGE", 25);
|
2022-12-04 19:53:27 +00:00
|
|
|
}
|
2022-12-04 11:38:11 +00:00
|
|
|
|
2018-04-21 12:59:48 +05:00
|
|
|
/**
|
|
|
|
* Creates the application.
|
|
|
|
*
|
|
|
|
* @return \Laravel\Lumen\Application
|
|
|
|
*/
|
|
|
|
public function createApplication()
|
|
|
|
{
|
2022-12-04 19:53:27 +00:00
|
|
|
$app = require __DIR__.'/../bootstrap/app.php';
|
|
|
|
$database = env('DB_DATABASE', 'jikan_tests');
|
2022-12-10 21:16:05 +00:00
|
|
|
$app['config']->set('database.connections.mongodb.database', $database === 'jikan' ? 'jikan_tests' : $database);
|
2022-12-21 11:29:53 +00:00
|
|
|
$app->register(TestServiceProvider::class);
|
2022-12-04 19:53:27 +00:00
|
|
|
|
|
|
|
return $app;
|
|
|
|
}
|
|
|
|
|
2022-12-04 20:11:35 +00:00
|
|
|
protected function setUpTraits()
|
2022-12-04 19:53:27 +00:00
|
|
|
{
|
2022-12-04 20:11:35 +00:00
|
|
|
parent::setUpTraits();
|
2022-12-04 19:53:27 +00:00
|
|
|
$uses = array_flip(class_uses_recursive(get_class($this)));
|
|
|
|
|
|
|
|
// we want to empty the search index
|
|
|
|
if (isset($uses[ScoutFlush::class])) {
|
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
|
|
|
$this->runScoutFlush();
|
|
|
|
}
|
2022-12-04 21:43:29 +00:00
|
|
|
|
|
|
|
if (isset($uses[SyntheticMongoDbTransaction::class])) {
|
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
|
|
|
$this->beginDatabaseTransaction();
|
|
|
|
}
|
2018-04-21 12:59:48 +05:00
|
|
|
}
|
2022-12-16 11:00:18 +00:00
|
|
|
|
|
|
|
public function assertPaginationData(int $expectedCount, ?int $expectedTotal = null, ?int $perPage = null): TestResponse
|
|
|
|
{
|
|
|
|
if (is_null($expectedTotal))
|
|
|
|
{
|
|
|
|
$expectedTotal = $expectedCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($perPage))
|
|
|
|
{
|
|
|
|
$perPage = $this->maxResultsPerPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response->assertJsonPath("pagination.items.count", $expectedCount);
|
|
|
|
$this->response->assertJsonPath("pagination.items.total", $expectedTotal);
|
|
|
|
return $this->response->assertJsonPath("pagination.items.per_page", $perPage);
|
|
|
|
}
|
2022-12-21 18:33:19 +00:00
|
|
|
|
|
|
|
public function assertCollectionsStrictlyEqual(Collection $expectedItems, Collection $actualItems): void
|
|
|
|
{
|
|
|
|
$this->assertEquals(0, $expectedItems->diff($actualItems)->count());
|
|
|
|
$this->assertEquals($expectedItems->toArray(), $actualItems->toArray());
|
|
|
|
}
|
2018-04-21 12:59:48 +05:00
|
|
|
}
|