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 823e6cbd2e
commit b92695a9c2
10 changed files with 96 additions and 158 deletions

View File

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

View File

@ -27,8 +27,13 @@ class ApiSerializerTest extends TestCase
{ {
use RetrievesAuthorizedUsers; use RetrievesAuthorizedUsers;
protected function prepDb() /**
* @inheritDoc
*/
protected function setUp(): void
{ {
parent::setUp();
$this->prepareDatabase([ $this->prepareDatabase([
'users' => [ 'users' => [
$this->normalUser() $this->normalUser()
@ -42,8 +47,6 @@ class ApiSerializerTest extends TestCase
['id' => 1, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'discussionRenamed', 'content' => '<t><p>can i haz relationz?</p></t>'], ['id' => 1, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'discussionRenamed', 'content' => '<t><p>can i haz relationz?</p></t>'],
], ],
]); ]);
$this->app();
} }
/** /**
@ -329,8 +332,6 @@ class ApiSerializerTest extends TestCase
->hasMany('customSerializerRelation', DiscussionSerializer::class) ->hasMany('customSerializerRelation', DiscussionSerializer::class)
); );
$this->prepDb();
$request = $this->request('GET', '/api/users/2', [ $request = $this->request('GET', '/api/users/2', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
]); ]);
@ -356,8 +357,6 @@ class ApiSerializerTest extends TestCase
->hasOne('customSerializerRelation', DiscussionSerializer::class) ->hasOne('customSerializerRelation', DiscussionSerializer::class)
); );
$this->prepDb();
$request = $this->request('GET', '/api/users/2', [ $request = $this->request('GET', '/api/users/2', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
]); ]);
@ -385,8 +384,6 @@ class ApiSerializerTest extends TestCase
}) })
); );
$this->prepDb();
$request = $this->request('GET', '/api/users/2', [ $request = $this->request('GET', '/api/users/2', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
]); ]);
@ -412,8 +409,6 @@ class ApiSerializerTest extends TestCase
->relationship('customSerializerRelation', CustomRelationshipInvokableClass::class) ->relationship('customSerializerRelation', CustomRelationshipInvokableClass::class)
); );
$this->prepDb();
$request = $this->request('GET', '/api/users/2', [ $request = $this->request('GET', '/api/users/2', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
]); ]);
@ -439,8 +434,6 @@ class ApiSerializerTest extends TestCase
->hasMany('anotherCustomRelation', DiscussionSerializer::class) ->hasMany('anotherCustomRelation', DiscussionSerializer::class)
); );
$this->prepDb();
$request = $this->request('GET', '/api/users/2', [ $request = $this->request('GET', '/api/users/2', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
]); ]);
@ -472,8 +465,6 @@ class ApiSerializerTest extends TestCase
}) })
); );
$this->prepDb();
$request = $this->request('GET', '/api/users/2', [ $request = $this->request('GET', '/api/users/2', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
]); ]);

View File

@ -79,8 +79,6 @@ class CsrfTest extends TestCase
->exemptRoute('users.create') ->exemptRoute('users.create')
); );
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('POST', '/api/users', [ $this->request('POST', '/api/users', [
'json' => [ 'json' => [

View File

@ -25,23 +25,23 @@ class ModelTest extends TestCase
{ {
use RetrievesAuthorizedUsers; use RetrievesAuthorizedUsers;
protected function prepDb() /**
* @inheritDoc
*/
protected function setUp(): void
{ {
parent::setUp();
$this->prepareDatabase([ $this->prepareDatabase([
'users' => [ 'users' => [
$this->normalUser(), $this->normalUser(),
], ],
]); ]);
$this->app();
} }
protected function prepPostsHierarchy() protected function prepPostsHierarchy()
{ {
$this->prepareDatabase([ $this->prepareDatabase([
'users' => [
$this->normalUser(),
],
'discussions' => [ 'discussions' => [
['id' => 1, 'title' => 'Discussion with post', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 1, 'comment_count' => 1, 'is_private' => 0], ['id' => 1, 'title' => 'Discussion with post', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 1, 'comment_count' => 1, 'is_private' => 0],
], ],
@ -49,8 +49,6 @@ class ModelTest extends TestCase
['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'discussionRenamed', 'content' => '<t><p>can i haz relationz?</p></t>'], ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'discussionRenamed', 'content' => '<t><p>can i haz relationz?</p></t>'],
], ],
]); ]);
$this->app();
} }
/** /**
@ -58,7 +56,7 @@ class ModelTest extends TestCase
*/ */
public function custom_relationship_does_not_exist_by_default() public function custom_relationship_does_not_exist_by_default()
{ {
$this->prepDB(); $this->app();
$user = User::find(1); $user = User::find(1);
@ -76,7 +74,7 @@ class ModelTest extends TestCase
->hasOne('customRelation', Discussion::class, 'user_id') ->hasOne('customRelation', Discussion::class, 'user_id')
); );
$this->prepDB(); $this->app();
$user = User::find(1); $user = User::find(1);
@ -93,7 +91,7 @@ class ModelTest extends TestCase
->hasMany('customRelation', Discussion::class, 'user_id') ->hasMany('customRelation', Discussion::class, 'user_id')
); );
$this->prepDB(); $this->app();
$user = User::find(1); $user = User::find(1);
@ -110,7 +108,7 @@ class ModelTest extends TestCase
->belongsTo('customRelation', Discussion::class, 'user_id') ->belongsTo('customRelation', Discussion::class, 'user_id')
); );
$this->prepDB(); $this->app();
$user = User::find(1); $user = User::find(1);
@ -129,7 +127,7 @@ class ModelTest extends TestCase
}) })
); );
$this->prepDB(); $this->app();
$user = User::find(1); $user = User::find(1);
@ -146,7 +144,7 @@ class ModelTest extends TestCase
->relationship('customRelation', CustomRelationClass::class) ->relationship('customRelation', CustomRelationClass::class)
); );
$this->prepDB(); $this->app();
$user = User::find(1); $user = User::find(1);
@ -170,7 +168,8 @@ class ModelTest extends TestCase
['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1] ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1]
] ]
]); ]);
$this->prepDB();
$this->app();
$user = User::find(1); $user = User::find(1);
@ -190,6 +189,8 @@ class ModelTest extends TestCase
$this->prepPostsHierarchy(); $this->prepPostsHierarchy();
$this->app();
$post = CommentPost::find(1); $post = CommentPost::find(1);
$this->assertInstanceOf(Discussion::class, $post->ancestor); $this->assertInstanceOf(Discussion::class, $post->ancestor);
@ -210,6 +211,8 @@ class ModelTest extends TestCase
$this->prepPostsHierarchy(); $this->prepPostsHierarchy();
$this->app();
$post = DiscussionRenamedPost::find(1); $post = DiscussionRenamedPost::find(1);
$this->assertInstanceOf(Discussion::class, $post->ancestor); $this->assertInstanceOf(Discussion::class, $post->ancestor);
@ -229,6 +232,9 @@ class ModelTest extends TestCase
); );
$this->prepPostsHierarchy(); $this->prepPostsHierarchy();
$this->app();
$post = DiscussionRenamedPost::find(1); $post = DiscussionRenamedPost::find(1);
$this->assertInstanceOf(User::class, $post->ancestor); $this->assertInstanceOf(User::class, $post->ancestor);
@ -247,7 +253,7 @@ class ModelTest extends TestCase
}) })
); );
$this->prepDB(); $this->app();
$group = Group::find(1); $group = Group::find(1);

View File

@ -21,8 +21,13 @@ class ModelUrlTest extends TestCase
{ {
use RetrievesAuthorizedUsers; use RetrievesAuthorizedUsers;
protected function prepDb() /**
* @inheritDoc
*/
protected function setUp(): void
{ {
parent::setUp();
$userClass = User::class; $userClass = User::class;
$this->prepareDatabase([ $this->prepareDatabase([
'users' => [ 'users' => [
@ -39,8 +44,6 @@ class ModelUrlTest extends TestCase
*/ */
public function uses_default_driver_by_default() public function uses_default_driver_by_default()
{ {
$this->prepDb();
$slugManager = $this->app()->getContainer()->make(SlugManager::class); $slugManager = $this->app()->getContainer()->make(SlugManager::class);
$testUser = User::find(1); $testUser = User::find(1);
@ -56,8 +59,6 @@ class ModelUrlTest extends TestCase
{ {
$this->extend((new Extend\ModelUrl(User::class))->addSlugDriver('testDriver', TestSlugDriver::class)); $this->extend((new Extend\ModelUrl(User::class))->addSlugDriver('testDriver', TestSlugDriver::class));
$this->prepDb();
$slugManager = $this->app()->getContainer()->make(SlugManager::class); $slugManager = $this->app()->getContainer()->make(SlugManager::class);
$testUser = User::find(1); $testUser = User::find(1);

View File

@ -23,8 +23,13 @@ class ModelVisibilityTest extends TestCase
{ {
use RetrievesAuthorizedUsers; use RetrievesAuthorizedUsers;
protected function prepDb() /**
* @inheritDoc
*/
protected function setUp(): void
{ {
parent::setUp();
$this->prepareDatabase([ $this->prepareDatabase([
'discussions' => [ 'discussions' => [
['id' => 1, 'title' => 'Empty discussion', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => null, 'comment_count' => 0, 'is_private' => 0], ['id' => 1, 'title' => 'Empty discussion', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => null, 'comment_count' => 0, 'is_private' => 0],
@ -39,8 +44,6 @@ class ModelVisibilityTest extends TestCase
$this->normalUser(), $this->normalUser(),
] ]
]); ]);
$this->app();
} }
/** /**
@ -48,7 +51,7 @@ class ModelVisibilityTest extends TestCase
*/ */
public function user_can_see_posts_by_default() public function user_can_see_posts_by_default()
{ {
$this->prepDb(); $this->app();
$actor = User::find(2); $actor = User::find(2);
@ -69,7 +72,7 @@ class ModelVisibilityTest extends TestCase
}, 'view') }, 'view')
); );
$this->prepDb(); $this->app();
$actor = User::find(2); $actor = User::find(2);
@ -90,7 +93,7 @@ class ModelVisibilityTest extends TestCase
}, 'view') }, 'view')
); );
$this->prepDb(); $this->app();
$actor = User::find(2); $actor = User::find(2);
@ -115,7 +118,7 @@ class ModelVisibilityTest extends TestCase
}, 'view') }, 'view')
); );
$this->prepDb(); $this->app();
$actor = User::find(2); $actor = User::find(2);
@ -140,7 +143,7 @@ class ModelVisibilityTest extends TestCase
}, 'viewPrivate') }, 'viewPrivate')
); );
$this->prepDb(); $this->app();
$actor = User::find(2); $actor = User::find(2);
@ -169,7 +172,7 @@ class ModelVisibilityTest extends TestCase
}) })
); );
$this->prepDb(); $this->app();
$actor = User::find(2); $actor = User::find(2);

View File

@ -28,8 +28,13 @@ class PolicyTest extends TestCase
// Request body to hide discussions sent in tests. // Request body to hide discussions sent in tests.
protected $hideQuery = ['authenticatedAs' => 2, 'json' => ['data' => ['attributes' => ['isHidden' => true]]]]; protected $hideQuery = ['authenticatedAs' => 2, 'json' => ['data' => ['attributes' => ['isHidden' => true]]]];
private function prepDb() /**
* @inheritDoc
*/
protected function setUp(): void
{ {
parent::setUp();
$this->prepareDatabase([ $this->prepareDatabase([
'users' => [ 'users' => [
$this->normalUser(), $this->normalUser(),
@ -41,8 +46,6 @@ class PolicyTest extends TestCase
['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>a normal reply - too-obscure</p></t>'], ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>a normal reply - too-obscure</p></t>'],
] ]
]); ]);
$this->app();
} }
/** /**
@ -50,8 +53,6 @@ class PolicyTest extends TestCase
*/ */
public function unrelated_user_cant_hide_discussion_by_default() public function unrelated_user_cant_hide_discussion_by_default()
{ {
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('PATCH', '/api/discussions/1', $this->hideQuery) $this->request('PATCH', '/api/discussions/1', $this->hideQuery)
); );
@ -69,8 +70,6 @@ class PolicyTest extends TestCase
->modelPolicy(Discussion::class, CustomPolicy::class) ->modelPolicy(Discussion::class, CustomPolicy::class)
); );
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('PATCH', '/api/discussions/1', $this->hideQuery) $this->request('PATCH', '/api/discussions/1', $this->hideQuery)
); );
@ -89,8 +88,6 @@ class PolicyTest extends TestCase
->modelPolicy(Discussion::class, CustomPolicy::class) ->modelPolicy(Discussion::class, CustomPolicy::class)
); );
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('PATCH', '/api/discussions/1', $this->hideQuery) $this->request('PATCH', '/api/discussions/1', $this->hideQuery)
); );
@ -110,8 +107,6 @@ class PolicyTest extends TestCase
->modelPolicy(Discussion::class, CustomPolicy::class) ->modelPolicy(Discussion::class, CustomPolicy::class)
); );
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('PATCH', '/api/discussions/1', $this->hideQuery) $this->request('PATCH', '/api/discussions/1', $this->hideQuery)
); );
@ -132,8 +127,6 @@ class PolicyTest extends TestCase
->modelPolicy(Discussion::class, ForceAllowHidePolicy::class) ->modelPolicy(Discussion::class, ForceAllowHidePolicy::class)
); );
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('PATCH', '/api/discussions/1', $this->hideQuery) $this->request('PATCH', '/api/discussions/1', $this->hideQuery)
); );
@ -146,7 +139,7 @@ class PolicyTest extends TestCase
*/ */
public function regular_user_cant_start_discussions_by_default() public function regular_user_cant_start_discussions_by_default()
{ {
$this->prepDb(); $this->app();
$user = User::find(2); $user = User::find(2);
@ -163,7 +156,7 @@ class PolicyTest extends TestCase
->globalPolicy(GlobalStartDiscussionPolicy::class) ->globalPolicy(GlobalStartDiscussionPolicy::class)
); );
$this->prepDb(); $this->app();
$user = User::find(2); $user = User::find(2);
@ -180,7 +173,7 @@ class PolicyTest extends TestCase
->globalPolicy(GlobalStartDiscussionPolicy::class) ->globalPolicy(GlobalStartDiscussionPolicy::class)
); );
$this->prepDb(); $this->app();
$user = User::find(2); $user = User::find(2);
@ -192,7 +185,7 @@ class PolicyTest extends TestCase
*/ */
public function unrelated_user_cant_hide_post_by_default() public function unrelated_user_cant_hide_post_by_default()
{ {
$this->prepDb(); $this->app();
$user = User::find(2); $user = User::find(2);
@ -207,7 +200,7 @@ class PolicyTest extends TestCase
$this->extend( $this->extend(
(new Extend\Policy)->modelPolicy(CommentPost::class, CommentPostChildClassPolicy::class) (new Extend\Policy)->modelPolicy(CommentPost::class, CommentPostChildClassPolicy::class)
); );
$this->prepDb(); $this->app();
$user = User::find(2); $user = User::find(2);
@ -223,7 +216,7 @@ class PolicyTest extends TestCase
(new Extend\Policy)->modelPolicy(Post::class, PostParentClassPolicy::class), (new Extend\Policy)->modelPolicy(Post::class, PostParentClassPolicy::class),
(new Extend\Policy)->modelPolicy(CommentPost::class, CommentPostChildClassPolicy::class) (new Extend\Policy)->modelPolicy(CommentPost::class, CommentPostChildClassPolicy::class)
); );
$this->prepDb(); $this->app();
$user = User::find(2); $user = User::find(2);

View File

@ -17,8 +17,13 @@ class SettingsTest extends TestCase
{ {
use RetrievesAuthorizedUsers; use RetrievesAuthorizedUsers;
protected function prepDb() /**
* @inheritDoc
*/
protected function setUp(): void
{ {
parent::setUp();
$this->prepareDatabase([ $this->prepareDatabase([
'users' => [ 'users' => [
$this->normalUser() $this->normalUser()
@ -35,8 +40,6 @@ class SettingsTest extends TestCase
*/ */
public function custom_setting_isnt_serialized_by_default() public function custom_setting_isnt_serialized_by_default()
{ {
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('GET', '/api', [ $this->request('GET', '/api', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
@ -58,8 +61,6 @@ class SettingsTest extends TestCase
->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting') ->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting')
); );
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('GET', '/api', [ $this->request('GET', '/api', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
@ -84,8 +85,6 @@ class SettingsTest extends TestCase
}) })
); );
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('GET', '/api', [ $this->request('GET', '/api', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
@ -108,8 +107,6 @@ class SettingsTest extends TestCase
->serializeToForum('customPrefix.customSetting2', 'custom-prefix.custom_setting2', CustomInvokableClass::class) ->serializeToForum('customPrefix.customSetting2', 'custom-prefix.custom_setting2', CustomInvokableClass::class)
); );
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('GET', '/api', [ $this->request('GET', '/api', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
@ -132,8 +129,6 @@ class SettingsTest extends TestCase
->serializeToForum('customPrefix.noCustomSetting', 'custom-prefix.no_custom_setting', null, 'customDefault') ->serializeToForum('customPrefix.noCustomSetting', 'custom-prefix.no_custom_setting', null, 'customDefault')
); );
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('GET', '/api', [ $this->request('GET', '/api', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
@ -158,8 +153,6 @@ class SettingsTest extends TestCase
}, 'customDefault') }, 'customDefault')
); );
$this->prepDb();
$response = $this->send( $response = $this->send(
$this->request('GET', '/api', [ $this->request('GET', '/api', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,

View File

@ -17,8 +17,13 @@ class ThrottleApiTest extends TestCase
{ {
use RetrievesAuthorizedUsers; use RetrievesAuthorizedUsers;
protected function prepDb(): void /**
* @inheritDoc
*/
protected function setUp(): void
{ {
parent::setUp();
$this->prepareDatabase([ $this->prepareDatabase([
'users' => [ 'users' => [
$this->normalUser(), $this->normalUser(),
@ -31,8 +36,6 @@ class ThrottleApiTest extends TestCase
*/ */
public function list_discussions_not_restricted_by_default() public function list_discussions_not_restricted_by_default()
{ {
$this->prepDb();
$response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2])); $response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2]));
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
@ -49,8 +52,6 @@ class ThrottleApiTest extends TestCase
} }
})); }));
$this->prepDb();
$response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2])); $response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2]));
$this->assertEquals(429, $response->getStatusCode()); $this->assertEquals(429, $response->getStatusCode());
@ -74,10 +75,6 @@ class ThrottleApiTest extends TestCase
}) })
); );
$this->prepDb();
$this->prepDb();
$response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2])); $response = $this->send($this->request('GET', '/api/discussions', ['authenticatedAs' => 2]));
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());

View File

@ -20,8 +20,13 @@ class UserTest extends TestCase
{ {
use RetrievesAuthorizedUsers; use RetrievesAuthorizedUsers;
protected function prepDb() /**
* @inheritDoc
*/
protected function setUp(): void
{ {
parent::setUp();
$this->prepareDatabase([ $this->prepareDatabase([
'users' => [ 'users' => [
$this->normalUser(), $this->normalUser(),
@ -30,8 +35,6 @@ class UserTest extends TestCase
['key' => 'display_name_driver', 'value' => 'custom'], ['key' => 'display_name_driver', 'value' => 'custom'],
] ]
]); ]);
$this->app();
} }
protected function registerTestPreference() protected function registerTestPreference()
@ -47,7 +50,7 @@ class UserTest extends TestCase
*/ */
public function username_display_name_driver_used_by_default() public function username_display_name_driver_used_by_default()
{ {
$this->prepDb(); $this->app();
$user = User::find(1); $user = User::find(1);
@ -64,7 +67,7 @@ class UserTest extends TestCase
->displayNameDriver('custom', CustomDisplayNameDriver::class) ->displayNameDriver('custom', CustomDisplayNameDriver::class)
); );
$this->prepDb(); $this->app();
$user = User::find(1); $user = User::find(1);
@ -76,7 +79,8 @@ class UserTest extends TestCase
*/ */
public function user_has_permissions_for_expected_groups_if_no_processors_added() public function user_has_permissions_for_expected_groups_if_no_processors_added()
{ {
$this->prepDb(); $this->app();
$user = User::find(2); $user = User::find(2);
$this->assertContains('viewUserList', $user->getPermissions()); $this->assertContains('viewUserList', $user->getPermissions());
@ -93,7 +97,8 @@ class UserTest extends TestCase
}); });
})); }));
$this->prepDb(); $this->app();
$user = User::find(2); $user = User::find(2);
$this->assertNotContains('viewUserList', $user->getPermissions()); $this->assertNotContains('viewUserList', $user->getPermissions());
@ -106,7 +111,8 @@ class UserTest extends TestCase
{ {
$this->extend((new Extend\User)->permissionGroups(CustomGroupProcessorClass::class)); $this->extend((new Extend\User)->permissionGroups(CustomGroupProcessorClass::class));
$this->prepDb(); $this->app();
$user = User::find(2); $user = User::find(2);
$this->assertNotContains('viewUserList', $user->getPermissions()); $this->assertNotContains('viewUserList', $user->getPermissions());
@ -118,7 +124,8 @@ class UserTest extends TestCase
public function can_add_user_preference() public function can_add_user_preference()
{ {
$this->registerTestPreference(); $this->registerTestPreference();
$this->prepDb();
$this->app();
/** @var User $user */ /** @var User $user */
$user = User::find(2); $user = User::find(2);
@ -131,7 +138,8 @@ class UserTest extends TestCase
public function can_store_user_preference() public function can_store_user_preference()
{ {
$this->registerTestPreference(); $this->registerTestPreference();
$this->prepDb();
$this->app();
/** @var User $user */ /** @var User $user */
$user = User::find(2); $user = User::find(2);
@ -147,7 +155,8 @@ class UserTest extends TestCase
public function storing_user_preference_modified_by_transformer() public function storing_user_preference_modified_by_transformer()
{ {
$this->registerTestPreference(); $this->registerTestPreference();
$this->prepDb();
$this->app();
/** @var User $user */ /** @var User $user */
$user = User::find(2); $user = User::find(2);