1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 08:27:42 +02:00

Tests: remove prepDb workaround

Previously, the `prepareDatabase` method would directly modify the database, booting the app in the process. This would prevent any extenders from being applied, since `->extend()` has no effect once the app is booted.

Since the new implementation of `prepareDatabase` simply registers seed data to be applied during app boot, the workaround of sticking this seed data into `prepDb` is no longer necessary, and seed data common to all test cases in a class can be provided in `setUp`.

When needed, app boot is explicitly triggered in individual test cases by calling `$this->app()`.
This commit is contained in:
Alexander Skvortsov
2021-01-06 22:34:32 -05:00
parent 0a8816938a
commit ae280016e7
10 changed files with 96 additions and 158 deletions

View File

@@ -31,8 +31,13 @@ class ApiControllerTest extends TestCase
{
use RetrievesAuthorizedUsers;
protected function prepDb()
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->normalUser()
@@ -43,8 +48,6 @@ class ApiControllerTest extends TestCase
['id' => 3, 'title' => 'Custom Discussion Title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'first_post_id' => 0, 'comment_count' => 1, 'is_private' => 0],
],
]);
$this->app();
}
/**
@@ -59,8 +62,6 @@ class ApiControllerTest extends TestCase
})
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions/1', [
'authenticatedAs' => 1,
@@ -82,8 +83,6 @@ class ApiControllerTest extends TestCase
->prepareDataForSerialization(CustomPrepareDataSerializationInvokableClass::class)
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions/1', [
'authenticatedAs' => 1,
@@ -110,8 +109,6 @@ class ApiControllerTest extends TestCase
})
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
@@ -136,8 +133,6 @@ class ApiControllerTest extends TestCase
->prepareDataForSerialization(CustomInvokableClassArgsReference::class)
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
@@ -163,8 +158,6 @@ class ApiControllerTest extends TestCase
})
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions/1', [
'authenticatedAs' => 1,
@@ -194,8 +187,6 @@ class ApiControllerTest extends TestCase
})
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions/1', [
'authenticatedAs' => 1,
@@ -221,8 +212,6 @@ class ApiControllerTest extends TestCase
})
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions/1', [
'authenticatedAs' => 1,
@@ -252,8 +241,6 @@ class ApiControllerTest extends TestCase
})
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions/1', [
'authenticatedAs' => 1,
@@ -270,8 +257,6 @@ class ApiControllerTest extends TestCase
*/
public function custom_serializer_doesnt_work_by_default()
{
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions/1', [
'authenticatedAs' => 1,
@@ -293,8 +278,6 @@ class ApiControllerTest extends TestCase
->setSerializer(CustomDiscussionSerializer::class)
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions/1', [
'authenticatedAs' => 1,
@@ -315,8 +298,6 @@ class ApiControllerTest extends TestCase
(new Extend\ApiController(ShowPostController::class))
->setSerializer(CustomPostSerializer::class, CustomApiControllerInvokableClass::class)
);
$this->prepDb();
$this->prepareDatabase([
'posts' => [
['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>'],
@@ -329,8 +310,6 @@ class ApiControllerTest extends TestCase
])
);
echo $response->getBody();
$payload = json_decode($response->getBody(), true);
$this->assertArrayHasKey('customSerializer', $payload['data']['attributes']);
@@ -348,8 +327,6 @@ class ApiControllerTest extends TestCase
})
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/users/2', [
'authenticatedAs' => 1,
@@ -366,8 +343,6 @@ class ApiControllerTest extends TestCase
*/
public function custom_relationship_not_included_by_default()
{
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/users/2', [
'authenticatedAs' => 1,
@@ -394,8 +369,6 @@ class ApiControllerTest extends TestCase
->addInclude('customApiControllerRelation')
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/users/2', [
'authenticatedAs' => 1,
@@ -421,8 +394,6 @@ class ApiControllerTest extends TestCase
->addOptionalInclude('customApiControllerRelation2')
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/users/2', [
'authenticatedAs' => 1,
@@ -441,8 +412,6 @@ class ApiControllerTest extends TestCase
*/
public function custom_relationship_included_by_default()
{
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/users/2', [
'authenticatedAs' => 1,
@@ -464,8 +433,6 @@ class ApiControllerTest extends TestCase
->removeInclude('groups')
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/users/2', [
'authenticatedAs' => 1,
@@ -492,8 +459,6 @@ class ApiControllerTest extends TestCase
->removeOptionalInclude('customApiControllerRelation2')
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/users/2', [
'authenticatedAs' => 1,
@@ -510,8 +475,6 @@ class ApiControllerTest extends TestCase
*/
public function custom_limit_doesnt_work_by_default()
{
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions', [
'authenticatedAs' => 1,
@@ -533,8 +496,6 @@ class ApiControllerTest extends TestCase
->setLimit(1)
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions', [
'authenticatedAs' => 1,
@@ -556,8 +517,6 @@ class ApiControllerTest extends TestCase
->setMaxLimit(1)
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions', [
'authenticatedAs' => 1,
@@ -576,8 +535,6 @@ class ApiControllerTest extends TestCase
*/
public function custom_sort_field_doesnt_exist_by_default()
{
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions', [
'authenticatedAs' => 1,
@@ -601,8 +558,6 @@ class ApiControllerTest extends TestCase
})
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions', [
'authenticatedAs' => 1,
@@ -624,8 +579,6 @@ class ApiControllerTest extends TestCase
->addSortField('userId')
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions', [
'authenticatedAs' => 1,
@@ -645,8 +598,6 @@ class ApiControllerTest extends TestCase
*/
public function custom_sort_field_exists_by_default()
{
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions', [
'authenticatedAs' => 1,
@@ -668,8 +619,6 @@ class ApiControllerTest extends TestCase
->removeSortField('createdAt')
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions', [
'authenticatedAs' => 1,
@@ -692,8 +641,6 @@ class ApiControllerTest extends TestCase
->setSort(['userId' => 'desc'])
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/discussions', [
'authenticatedAs' => 1,