1
0
mirror of https://github.com/flarum/core.git synced 2025-07-24 18:21:33 +02:00

Eager loading extender (#2724)

* Eager loading extender
* Add tests for the eager loading extender
This commit is contained in:
Sami Mazouz
2021-03-25 15:36:39 +01:00
committed by GitHub
parent 4cc9aeeb28
commit e60bf67c61
8 changed files with 255 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ namespace Flarum\Tests\integration\extenders;
use Carbon\Carbon;
use Flarum\Api\Controller\AbstractShowController;
use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Api\Controller\ListUsersController;
use Flarum\Api\Controller\ShowDiscussionController;
use Flarum\Api\Controller\ShowForumController;
use Flarum\Api\Controller\ShowPostController;
@@ -22,6 +23,7 @@ use Flarum\Api\Serializer\PostSerializer;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\Discussion\Discussion;
use Flarum\Extend;
use Flarum\Post\Post;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use Flarum\User\User;
@@ -47,6 +49,11 @@ class ApiControllerTest extends TestCase
['id' => 2, 'title' => 'Custom Discussion Title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 3, '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],
],
'posts' => [
['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' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'discussionRenamed', 'content' => '<t><p>can i haz relationz?</p></t>'],
['id' => 3, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'discussionRenamed', 'content' => '<t><p>can i haz relationz?</p></t>'],
],
]);
}
@@ -652,6 +659,150 @@ class ApiControllerTest extends TestCase
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals([2, 1, 3], Arr::pluck($payload['data'], 'id'));
}
/**
* @test
*/
public function custom_first_level_relation_is_not_loaded_by_default()
{
$users = null;
$this->extend(
(new Extend\Model(User::class))
->hasOne('firstLevelRelation', Post::class, 'user_id'),
(new Extend\ApiController(ListUsersController::class))
->prepareDataForSerialization(function ($controller, $data) use (&$users) {
$users = $data;
return [];
})
);
$this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])
);
$this->assertTrue($users->filter->relationLoaded('firstLevelRelation')->isEmpty());
}
/**
* @test
*/
public function custom_first_level_relation_is_loaded_if_added()
{
$users = null;
$this->extend(
(new Extend\Model(User::class))
->hasOne('firstLevelRelation', Post::class, 'user_id'),
(new Extend\ApiController(ListUsersController::class))
->load('firstLevelRelation')
->prepareDataForSerialization(function ($controller, $data) use (&$users) {
$users = $data;
return [];
})
);
$this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])
);
$this->assertFalse($users->filter->relationLoaded('firstLevelRelation')->isEmpty());
}
/**
* @test
*/
public function custom_second_level_relation_is_not_loaded_by_default()
{
$users = null;
$this->extend(
(new Extend\Model(User::class))
->hasOne('firstLevelRelation', Post::class, 'user_id'),
(new Extend\Model(Post::class))
->belongsTo('secondLevelRelation', Discussion::class),
(new Extend\ApiController(ListUsersController::class))
->prepareDataForSerialization(function ($controller, $data) use (&$users) {
$users = $data;
return [];
})
);
$this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])
);
$this->assertTrue($users->pluck('firstLevelRelation')->filter->relationLoaded('secondLevelRelation')->isEmpty());
}
/**
* @test
*/
public function custom_second_level_relation_is_loaded_if_added()
{
$users = null;
$this->extend(
(new Extend\Model(User::class))
->hasOne('firstLevelRelation', Post::class, 'user_id'),
(new Extend\Model(Post::class))
->belongsTo('secondLevelRelation', Discussion::class),
(new Extend\ApiController(ListUsersController::class))
->load(['firstLevelRelation', 'firstLevelRelation.secondLevelRelation'])
->prepareDataForSerialization(function ($controller, $data) use (&$users) {
$users = $data;
return [];
})
);
$this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])
);
$this->assertFalse($users->pluck('firstLevelRelation')->filter->relationLoaded('secondLevelRelation')->isEmpty());
}
/**
* @test
*/
public function custom_second_level_relation_is_not_loaded_when_first_level_is_not()
{
$users = null;
$this->extend(
(new Extend\Model(User::class))
->hasOne('firstLevelRelation', Post::class, 'user_id'),
(new Extend\Model(Post::class))
->belongsTo('secondLevelRelation', Discussion::class),
(new Extend\ApiController(ListUsersController::class))
->load(['firstLevelRelation.secondLevelRelation'])
->prepareDataForSerialization(function ($controller, $data) use (&$users) {
$users = $data;
return [];
})
);
$this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])
);
$this->assertTrue($users->pluck('firstLevelRelation')->filter->relationLoaded('secondLevelRelation')->isEmpty());
}
}
class CustomDiscussionSerializer extends DiscussionSerializer