forked from cycle/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClasslessHasOneCyclicTest.php
158 lines (138 loc) · 4.63 KB
/
ClasslessHasOneCyclicTest.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/
declare(strict_types=1);
namespace Cycle\ORM\Tests\Classless;
use Cycle\ORM\Heap\Heap;
use Cycle\ORM\Mapper\StdMapper;
use Cycle\ORM\Relation;
use Cycle\ORM\Schema;
use Cycle\ORM\Select;
use Cycle\ORM\Tests\BaseTest;
use Cycle\ORM\Tests\Traits\TableTrait;
use Cycle\ORM\Transaction;
abstract class ClasslessHasOneCyclicTest extends BaseTest
{
use TableTrait;
public function setUp()
{
parent::setUp();
$this->makeTable('cyclic', [
'id' => 'primary',
'name' => 'string',
'parent_id' => 'integer,nullable'
]);
$this->getDatabase()->table('cyclic')->insertMultiple(
['parent_id', 'name'],
[
[null, 'first'],
[1, 'second'],
[3, 'self-reference'],
]
);
$this->orm = $this->withSchema(new Schema([
'cyclic' => [
Schema::MAPPER => StdMapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'cyclic',
Schema::PRIMARY_KEY => 'id',
Schema::FIND_BY_KEYS => ['parent_id'],
Schema::COLUMNS => ['id', 'parent_id', 'name'],
Schema::SCHEMA => [],
Schema::RELATIONS => [
'cyclic' => [
Relation::TYPE => Relation::HAS_ONE,
Relation::TARGET => 'cyclic',
Relation::SCHEMA => [
Relation::CASCADE => true,
Relation::NULLABLE => true,
Relation::INNER_KEY => 'id',
Relation::OUTER_KEY => 'parent_id',
],
]
]
],
]));
}
public function testFetchCyclic()
{
$selector = new Select($this->orm, 'cyclic');
$selector->load('cyclic')->orderBy('cyclic.id');
$this->assertEquals([
[
'id' => '1',
'parent_id' => null,
'name' => 'first',
'cyclic' => [
'id' => '2',
'parent_id' => '1',
'name' => 'second',
],
],
[
'id' => '2',
'parent_id' => '1',
'name' => 'second',
'cyclic' => null,
],
[
'id' => '3',
'parent_id' => '3',
'name' => 'self-reference',
'cyclic' => [
'id' => '3',
'parent_id' => '3',
'name' => 'self-reference',
],
],
], $selector->fetchData());
}
public function testFetchCyclicRelation()
{
$selector = new Select($this->orm, 'cyclic');
list($a, $b, $c) = $selector->load('cyclic')->orderBy('cyclic.id')->fetchAll();
$this->assertSame($b, $a->cyclic);
$this->assertSame(null, $b->cyclic);
$this->assertSame($c, $c->cyclic);
}
public function testUpdateCyclic()
{
$selector = new Select($this->orm, 'cyclic');
$c = $selector->load('cyclic')->wherePK(3)->fetchOne();
$this->assertEquals('self-reference', $c->name);
$c->name = 'updated';
$tr = new Transaction($this->orm);
$tr->persist($c);
$tr->run();
$selector = new Select($this->orm->withHeap(new Heap()), 'cyclic');
$c = $selector->load('cyclic')->wherePK(3)->fetchOne();
$this->assertEquals('updated', $c->name);
$this->assertSame($c, $c->cyclic);
}
public function testCyclicWithoutLoad()
{
$selector = new Select($this->orm, 'cyclic');
$c = $selector->wherePK(3)->fetchOne();
$this->assertEquals('self-reference', $c->name);
$this->assertSame($c, $c->cyclic);
}
public function testCreateCyclic()
{
$c = $this->orm->make('cyclic');
$c->name = "new";
$c->cyclic = $c;
$this->captureWriteQueries();
$tr = new Transaction($this->orm);
$tr->persist($c);
$tr->run();
$this->assertNumWrites(2);
$selector = new Select($this->orm->withHeap(new Heap()), 'cyclic');
$c = $selector->load('cyclic')->wherePK(4)->fetchOne();
$this->assertEquals('new', $c->name);
$this->assertSame($c, $c->cyclic);
}
}