forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseEloquentMorphTest.php
executable file
·115 lines (88 loc) · 4.04 KB
/
DatabaseEloquentMorphTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
use Mockery as m;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
class DatabaseEloquentMorphTest extends PHPUnit_Framework_TestCase {
public function tearDown()
{
m::close();
}
public function testMorphOneSetsProperConstraints()
{
$relation = $this->getOneRelation();
}
public function testMorphOneEagerConstraintsAreProperlyAdded()
{
$relation = $this->getOneRelation();
$relation->getQuery()->shouldReceive('whereIn')->once()->with('table.morph_id', array(1, 2));
$relation->getQuery()->shouldReceive('where')->once()->with('table.morph_type', get_class($relation->getParent()));
$model1 = new EloquentMorphResetModelStub;
$model1->id = 1;
$model2 = new EloquentMorphResetModelStub;
$model2->id = 2;
$relation->addEagerConstraints(array($model1, $model2));
}
/**
* Note that the tests are the exact same for morph many because the classes share this code...
* Will still test to be safe.
*/
public function testMorphManySetsProperConstraints()
{
$relation = $this->getManyRelation();
}
public function testMorphManyEagerConstraintsAreProperlyAdded()
{
$relation = $this->getManyRelation();
$relation->getQuery()->shouldReceive('whereIn')->once()->with('table.morph_id', array(1, 2));
$relation->getQuery()->shouldReceive('where')->once()->with('table.morph_type', get_class($relation->getParent()));
$model1 = new EloquentMorphResetModelStub;
$model1->id = 1;
$model2 = new EloquentMorphResetModelStub;
$model2->id = 2;
$relation->addEagerConstraints(array($model1, $model2));
}
public function testCreateFunctionOnMorph()
{
// Doesn't matter which relation type we use since they share the code...
$relation = $this->getOneRelation();
$created = m::mock('Illuminate\Database\Eloquent\Model');
$created->shouldReceive('setAttribute')->once()->with('morph_id', 1);
$created->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
$relation->getRelated()->shouldReceive('newInstance')->once()->with(array('name' => 'taylor'))->andReturn($created);
$created->shouldReceive('save')->once()->andReturn(true);
$this->assertEquals($created, $relation->create(array('name' => 'taylor')));
}
protected function getOneRelation()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder');
$builder->shouldReceive('where')->once()->with('table.morph_id', '=', 1);
$related = m::mock('Illuminate\Database\Eloquent\Model');
$builder->shouldReceive('getModel')->andReturn($related);
$parent = m::mock('Illuminate\Database\Eloquent\Model');
$parent->shouldReceive('getAttribute')->with('id')->andReturn(1);
$parent->shouldReceive('getMorphClass')->andReturn(get_class($parent));
$builder->shouldReceive('where')->once()->with('table.morph_type', get_class($parent));
return new MorphOne($builder, $parent, 'table.morph_type', 'table.morph_id', 'id');
}
protected function getManyRelation()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder');
$builder->shouldReceive('where')->once()->with('table.morph_id', '=', 1);
$related = m::mock('Illuminate\Database\Eloquent\Model');
$builder->shouldReceive('getModel')->andReturn($related);
$parent = m::mock('Illuminate\Database\Eloquent\Model');
$parent->shouldReceive('getAttribute')->with('id')->andReturn(1);
$parent->shouldReceive('getMorphClass')->andReturn(get_class($parent));
$builder->shouldReceive('where')->once()->with('table.morph_type', get_class($parent));
return new MorphMany($builder, $parent, 'table.morph_type', 'table.morph_id', 'id');
}
}
class EloquentMorphResetModelStub extends Illuminate\Database\Eloquent\Model {}
class EloquentMorphResetBuilderStub extends Illuminate\Database\Eloquent\Builder {
public function __construct() { $this->query = new EloquentRelationQueryStub; }
public function getModel() { return new EloquentMorphResetModelStub; }
public function isSoftDeleting() { return false; }
}
class EloquentMorphQueryStub extends Illuminate\Database\Query\Builder {
public function __construct() {}
}