mirror of
https://github.com/flarum/core.git
synced 2025-07-19 07:41:22 +02:00
Tweak model extender tests
- Format code - Reorder methods - Test a different scenario to avoid the use of sleep() Refs #2100.
This commit is contained in:
@@ -46,14 +46,68 @@ class ModelTest extends TestCase
|
|||||||
$user->customRelation();
|
$user->customRelation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function custom_hasOne_relationship_exists_if_added()
|
||||||
|
{
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\Model(User::class))
|
||||||
|
->hasMany('customRelation', Discussion::class, 'user_id')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->prepDB();
|
||||||
|
|
||||||
|
$user = User::find(1);
|
||||||
|
|
||||||
|
$this->assertEquals([], $user->customRelation()->get()->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function custom_hasMany_relationship_exists_if_added()
|
||||||
|
{
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\Model(User::class))
|
||||||
|
->hasMany('customRelation', Discussion::class, 'user_id')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->prepDB();
|
||||||
|
|
||||||
|
$user = User::find(1);
|
||||||
|
|
||||||
|
$this->assertEquals([], $user->customRelation()->get()->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function custom_belongsTo_relationship_exists_if_added()
|
||||||
|
{
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\Model(User::class))
|
||||||
|
->belongsTo('customRelation', Discussion::class, 'user_id')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->prepDB();
|
||||||
|
|
||||||
|
$user = User::find(1);
|
||||||
|
|
||||||
|
$this->assertEquals([], $user->customRelation()->get()->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
public function custom_relationship_exists_if_added()
|
public function custom_relationship_exists_if_added()
|
||||||
{
|
{
|
||||||
$this->extend((new Extend\Model(User::class))->relationship('customRelation', function (User $user) {
|
$this->extend(
|
||||||
return $user->hasMany(Discussion::class, 'user_id');
|
(new Extend\Model(User::class))
|
||||||
}));
|
->relationship('customRelation', function (User $user) {
|
||||||
|
return $user->hasMany(Discussion::class, 'user_id');
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
$this->prepDB();
|
$this->prepDB();
|
||||||
|
|
||||||
@@ -67,12 +121,14 @@ class ModelTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function custom_relationship_exists_and_can_return_instances_if_added()
|
public function custom_relationship_exists_and_can_return_instances_if_added()
|
||||||
{
|
{
|
||||||
$this->extend((new Extend\Model(User::class))->relationship('customRelation', function (User $user) {
|
$this->extend(
|
||||||
return $user->hasMany(Discussion::class, 'user_id');
|
(new Extend\Model(User::class))
|
||||||
}));
|
->relationship('customRelation', function (User $user) {
|
||||||
|
return $user->hasMany(Discussion::class, 'user_id');
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
$this->prepDB();
|
$this->prepDB();
|
||||||
|
|
||||||
$this->prepareDatabase([
|
$this->prepareDatabase([
|
||||||
'discussions' => [
|
'discussions' => [
|
||||||
['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]
|
||||||
@@ -90,9 +146,12 @@ class ModelTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function custom_relationship_does_not_exist_if_added_to_unrelated_model()
|
public function custom_relationship_does_not_exist_if_added_to_unrelated_model()
|
||||||
{
|
{
|
||||||
$this->extend((new Extend\Model(User::class))->relationship('customRelation', function (User $user) {
|
$this->extend(
|
||||||
return $user->hasMany(Discussion::class, 'user_id');
|
(new Extend\Model(User::class))
|
||||||
}));
|
->relationship('customRelation', function (User $user) {
|
||||||
|
return $user->hasMany(Discussion::class, 'user_id');
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
$this->prepDB();
|
$this->prepDB();
|
||||||
$this->prepareDatabase([
|
$this->prepareDatabase([
|
||||||
@@ -107,48 +166,6 @@ class ModelTest extends TestCase
|
|||||||
$group->customRelation();
|
$group->customRelation();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function custom_hasOne_relationship_exists_if_added()
|
|
||||||
{
|
|
||||||
$this->extend((new Extend\Model(User::class))->hasMany('customRelation', Discussion::class, 'user_id'));
|
|
||||||
|
|
||||||
$this->prepDB();
|
|
||||||
|
|
||||||
$user = User::find(1);
|
|
||||||
|
|
||||||
$this->assertEquals([], $user->customRelation()->get()->toArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function custom_hasMany_relationship_exists_if_added()
|
|
||||||
{
|
|
||||||
$this->extend((new Extend\Model(User::class))->hasMany('customRelation', Discussion::class, 'user_id'));
|
|
||||||
|
|
||||||
$this->prepDB();
|
|
||||||
|
|
||||||
$user = User::find(1);
|
|
||||||
|
|
||||||
$this->assertEquals([], $user->customRelation()->get()->toArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function custom_belongsTo_relationship_exists_if_added()
|
|
||||||
{
|
|
||||||
$this->extend((new Extend\Model(User::class))->belongsTo('customRelation', Discussion::class, 'user_id'));
|
|
||||||
|
|
||||||
$this->prepDB();
|
|
||||||
|
|
||||||
$user = User::find(1);
|
|
||||||
|
|
||||||
$this->assertEquals([], $user->customRelation()->get()->toArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
@@ -166,7 +183,10 @@ class ModelTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function custom_default_attribute_works_if_set()
|
public function custom_default_attribute_works_if_set()
|
||||||
{
|
{
|
||||||
$this->extend((new Extend\Model(Group::class))->default('name_singular', 'Custom Default'));
|
$this->extend(
|
||||||
|
(new Extend\Model(Group::class))
|
||||||
|
->default('name_singular', 'Custom Default')
|
||||||
|
);
|
||||||
|
|
||||||
$this->app();
|
$this->app();
|
||||||
|
|
||||||
@@ -180,18 +200,21 @@ class ModelTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function custom_default_attribute_evaluated_at_runtime_if_callable()
|
public function custom_default_attribute_evaluated_at_runtime_if_callable()
|
||||||
{
|
{
|
||||||
$time = Carbon::now();
|
$this->extend(
|
||||||
$this->extend((new Extend\Model(Group::class))->default('name_singular', function () {
|
(new Extend\Model(Group::class))
|
||||||
return Carbon::now();
|
->default('counter', function () {
|
||||||
}));
|
static $counter = 0;
|
||||||
|
return ++$counter;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
$this->app();
|
$this->app();
|
||||||
|
|
||||||
sleep(2);
|
$group1 = new Group;
|
||||||
|
$group2 = new Group;
|
||||||
|
|
||||||
$group = new Group;
|
$this->assertEquals(1, $group1->counter);
|
||||||
|
$this->assertEquals(2, $group2->counter);
|
||||||
$this->assertGreaterThanOrEqual($time->diffInSeconds($group->name_singular), 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -199,7 +222,10 @@ class ModelTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function custom_default_attribute_doesnt_work_if_set_on_unrelated_model()
|
public function custom_default_attribute_doesnt_work_if_set_on_unrelated_model()
|
||||||
{
|
{
|
||||||
$this->extend((new Extend\Model(Group::class))->default('name_singular', 'Custom Default'));
|
$this->extend(
|
||||||
|
(new Extend\Model(Group::class))
|
||||||
|
->default('name_singular', 'Custom Default')
|
||||||
|
);
|
||||||
|
|
||||||
$this->app();
|
$this->app();
|
||||||
|
|
||||||
@@ -225,7 +251,10 @@ class ModelTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function custom_date_attribute_can_be_set()
|
public function custom_date_attribute_can_be_set()
|
||||||
{
|
{
|
||||||
$this->extend((new Extend\Model(Post::class))->dateAttribute('custom'));
|
$this->extend(
|
||||||
|
(new Extend\Model(Post::class))
|
||||||
|
->dateAttribute('custom')
|
||||||
|
);
|
||||||
|
|
||||||
$this->app();
|
$this->app();
|
||||||
|
|
||||||
@@ -239,7 +268,10 @@ class ModelTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function custom_date_attribute_doesnt_work_if_set_on_unrelated_model()
|
public function custom_date_attribute_doesnt_work_if_set_on_unrelated_model()
|
||||||
{
|
{
|
||||||
$this->extend((new Extend\Model(Post::class))->dateAttribute('custom'));
|
$this->extend(
|
||||||
|
(new Extend\Model(Post::class))
|
||||||
|
->dateAttribute('custom')
|
||||||
|
);
|
||||||
|
|
||||||
$this->app();
|
$this->app();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user