forked from lox/pheasant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelationshipTest.php
113 lines (92 loc) · 3.5 KB
/
RelationshipTest.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
<?php
namespace Pheasant\Tests\Relationships;
use \Pheasant\Tests\Examples\Hero;
use \Pheasant\Tests\Examples\Power;
use \Pheasant\Tests\Examples\SecretIdentity;
class RelationshipTest extends \Pheasant\Tests\MysqlTestCase
{
public function setUp()
{
parent::setUp();
$migrator = new \Pheasant\Migrate\Migrator();
$migrator
->create('hero', Hero::schema())
->create('power', Power::schema())
->create('secretidentity', SecretIdentity::schema())
;
}
public function testOneToManyViaPropertySetting()
{
$hero = new Hero(array('alias'=>'Spider Man'));
$hero->save();
$this->assertEquals(count($hero->Powers), 0);
// save via property access
$power = new Power(array('description'=>'Spider Senses'));
$power->heroid = $hero->id;
$power->save();
$this->assertEquals(count($hero->Powers), 1);
$this->assertTrue($hero->Powers[0]->equals($power));
}
public function testOneToManyViaArrayAccess()
{
$hero = new Hero(array('alias'=>'Spider Man'));
$hero->save();
$this->assertEquals(count($hero->Powers), 0);
// save via adding
$power = new Power(array('description'=>'Super-human Strength'));
$hero->Powers[] = $power;
$power->save();
$this->assertEquals(count($hero->Powers), 1);
$this->assertEquals($power->heroid, 1);
$this->assertTrue($hero->Powers[0]->equals($power));
}
public function testHasOneRelationship()
{
$hero = new Hero(array('alias'=>'Spider Man'));
$hero->save();
$identity = new SecretIdentity(array('realname'=>'Peter Parker'));
$identity->Hero = $hero;
$identity->save();
$this->assertEquals($hero->identityid, $identity->id);
$this->assertTrue($hero->SecretIdentity->equals($identity));
$this->assertTrue($identity->Hero->equals($hero));
}
public function testPropertyReferencesResolvedInMapping()
{
$identity = new SecretIdentity(array('realname'=>'Peter Parker'));
$hero = new Hero(array('alias'=>'Spider Man'));
// set the identityid before it's been saved, still null
$hero->identityid = $identity->id;
$identity->save();
$hero->save();
$this->assertEquals($identity->id, 1);
$this->assertEquals($hero->identityid, 1);
}
public function testFilteringCollectionsReturnedByRelationships()
{
$spiderman = Hero::createHelper('Spider Man', 'Peter Parker', array(
'Super-human Strength', 'Spider Senses'
));
$superman = Hero::createHelper('Super Man', 'Clark Kent', array(
'Super-human Strength', 'Invulnerability'
));
$batman = Hero::createHelper('Batman', 'Bruce Wayne', array(
'Richness', 'Super-human Intellect'
));
$this->assertCount(2, $spiderman->Powers);
$this->assertCount(1, $spiderman->Powers->filter('description LIKE ?', 'Super-human%')->toArray());
}
public function testEmptyRelationshipsWithAllowEmpty()
{
$hero = new Hero(array('alias'=>'Spider Man'));
$hero->save();
$this->assertNull($hero->SecretIdentity);
}
public function testEmptyRelationshipsWithoutAllowEmpty()
{
$power = new Power(array('description'=>'Spider Senses'));
$power->save();
$this->setExpectedException('\Pheasant\Exception');
$foo = $power->Hero;
}
}