-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHintableModelTest.php
159 lines (135 loc) · 5.63 KB
/
HintableModelTest.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
159
<?php
declare(strict_types=1);
namespace Mvorisek\Atk4\Hintable\Tests\Data;
use Atk4\Core\AtkPhpunit;
use Atk4\Data\Exception;
/**
* @coversDefaultClass \Mvorisek\Atk4\Hintable\Data\HintableModelTrait
*/
class HintableModelTest extends AtkPhpunit\TestCase
{
public function testFieldName(): void
{
$model = new Model\Simple();
$this->assertSame('simple', $model->table);
$this->assertSame('x', $model->fieldName()->x);
$this->assertSame('x', Model\Simple::hinting()->fieldName()->x);
$model = new Model\Standard();
$this->assertSame('prefix_standard', $model->table);
$this->assertSame('x', $model->fieldName()->x);
$this->assertSame('yy', $model->fieldName()->y);
$this->assertSame('id', $model->fieldName()->id);
$this->assertSame('name', $model->fieldName()->_name);
$this->assertSame('simpleOne', $model->fieldName()->simpleOne);
$this->assertSame('simpleMany', $model->fieldName()->simpleMany);
}
public function testFieldNameUndeclaredException(): void
{
$model = new Model\Simple();
$this->expectException(Exception::class);
$model->fieldName()->y; // @phpstan-ignore-line
}
protected function createDatabaseForRefTest(): \Atk4\Data\Persistence
{
$db = new \Atk4\Data\Persistence\Array_();
$simpleA = (new Model\Simple($db))
->set(Model\Simple::hinting()->fieldName()->x, 'a')
->save();
$simpleB1 = (new Model\Simple($db))
->set(Model\Simple::hinting()->fieldName()->x, 'b1')
->save();
$simpleB2 = (new Model\Simple($db))
->set(Model\Simple::hinting()->fieldName()->x, 'b2')
->save();
$standardTemplate = (new Model\Standard($db))
->set(Model\Standard::hinting()->fieldName()->x, 'xx')
->set(Model\Standard::hinting()->fieldName()->y, 'yy')
->set(Model\Standard::hinting()->fieldName()->_name, 'zz')
->set(Model\Standard::hinting()->fieldName()->dtImmutable, new \DateTime('2000-1-1 12:00:00'))
->set(Model\Standard::hinting()->fieldName()->dtInterface, new \DateTimeImmutable('2000-2-1 12:00:00'))
->set(Model\Standard::hinting()->fieldName()->dtMulti, new \DateTimeImmutable('2000-3-1 12:00:00'));
$standardA = (clone $standardTemplate)
->set(Model\Standard::hinting()->fieldName()->simpleOneId, $simpleA->id)
->save();
$standardB = (clone $standardTemplate)
->set(Model\Standard::hinting()->fieldName()->simpleOneId, $simpleB2->id)
->save();
$simpleA
->set(Model\Simple::hinting()->fieldName()->refId, $standardA->id)
->save();
$simpleB1
->set(Model\Simple::hinting()->fieldName()->refId, $standardB->id)
->save();
$simpleB2
->set(Model\Simple::hinting()->fieldName()->refId, $standardB->id)
->save();
return $db;
}
public function testRefBasic(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Simple($db);
$this->assertSame(1, (clone $model)->load(1)->ref->id);
$this->assertSame(2, (clone $model)->load(2)->ref->id);
$this->assertSame(2, (clone $model)->load(3)->ref->id);
}
public function testRefNoData(): void
{
$model = new Model\Standard();
$model->invokeInit();
$this->assertInstanceOf(Model\Simple::class, $model->simpleOne);
// TODO seems like a bug in atk4/data
$this->markTestSkipped(); // @phpstan-ignore-next-line
$model = new Model\Standard();
$model->invokeInit();
$this->assertInstanceOf(Model\Simple::class, $model->simpleMany);
}
public function testRefOne(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Standard($db);
$this->assertInstanceOf(Model\Simple::class, $model->simpleOne);
$this->assertSame(1, (clone $model)->simpleOne->loadAny()->id);
$this->assertSame(3, (clone $model)->load(2)->simpleOne->id);
}
public function testRefMany(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Standard($db);
$this->assertInstanceOf(Model\Simple::class, $model->simpleMany);
$this->assertSame(1, $model->simpleMany->loadAny()->id);
// $this->assertSame(2, $model->load(2)->simpleMany->loadAny()->id);
$this->assertSame([2 => 2, 3 => 3], array_map(function (Model\Simple $model) {
return $model->id;
}, iterator_to_array($model->load(2)->simpleMany)));
}
public function testRefManyIsUnload(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Standard($db);
$this->assertNull($model->load(2)->simpleMany->id);
}
public function testRefOneLoadOneException(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Standard($db);
$this->expectException(Exception::class);
$model->simpleOne->loadOne();
}
public function testRefManyLoadOneException(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Standard($db);
$this->expectException(Exception::class);
$model->simpleMany->loadOne();
}
public function testPhpstanModelIteratorAggregate(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Simple($db);
$this->assertIsString((clone $model)->loadAny()->x);
foreach ($model as $modelItem) {
$this->assertIsString($modelItem->x);
}
}
}