-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
file.general.phpt
170 lines (137 loc) · 4.32 KB
/
file.general.phpt
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
160
161
162
163
164
165
166
167
168
169
170
<?php declare(strict_types = 1);
/**
* @testCase
*/
namespace NextrasTests\Orm\Integration\Mapper;
use Nette\Caching\Cache;
use Nette\Caching\Storages\MemoryStorage;
use Nextras\Dbal\Utils\DateTimeImmutable;
use Nextras\Orm\Mapper\Memory\ArrayMapper;
use Nextras\Orm\Model\SimpleModelFactory;
use NextrasTests\Orm\Author;
use NextrasTests\Orm\AuthorsRepository;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\BooksRepository;
use NextrasTests\Orm\EansRepository;
use NextrasTests\Orm\Model;
use NextrasTests\Orm\Publisher;
use NextrasTests\Orm\PublishersRepository;
use NextrasTests\Orm\Tag;
use NextrasTests\Orm\TagFollowersRepository;
use NextrasTests\Orm\TagsRepository;
use NextrasTests\Orm\TestCase;
use Tester\Assert;
require_once __DIR__ . '/../../../bootstrap.php';
class FileMapperTest extends TestCase
{
public function testGeneral(): void
{
/** @var Model $orm */
$orm = $this->createOrm();
$author = new Author();
$author->name = 'The Imp';
$author->web = 'localhost';
$author->born = new DateTimeImmutable('2000-01-01');
$orm->authors->attach($author);
$publisher = new Publisher();
$publisher->name = 'Valyria';
$book = new Book();
$book->author = $author;
$book->title = 'The Wall';
$book->publisher = $publisher;
$book->translator = $author;
$book2 = new Book();
$book2->author = $author;
$book2->title = 'The Wall II';
$book2->publisher = $publisher;
$orm->authors->persistAndFlush($author);
/** @var Model $orm */
$orm = $this->createOrm();
$book3 = new Book();
$book3->author = $orm->authors->getByIdChecked(1);
$book3->title = 'The Wall III';
$book3->publisher = 1;
$book3->tags->set([new Tag('Tag 1'), new Tag('Tag 2'), new Tag('Tag 3')]);
$orm->books->persistAndFlush($book3);
/** @var Model $orm */
$orm = $this->createOrm();
/** @var Author $author */
$author = $orm->authors->findAll()->fetch();
Assert::same('The Imp', $author->name);
Assert::notNull($author->born);
Assert::same('2000-01-01', $author->born->format('Y-m-d'));
Assert::same(3, $author->books->countStored());
Assert::same(3, $author->books->count());
Assert::same(1, $author->translatedBooks->count());
/** @var Book $book */
$book = $orm->books->findBy(['title' => 'The Wall'])->fetch();
Assert::same($author, $book->author);
Assert::same($author, $book->translator);
Assert::same('Valyria', $book->publisher->name);
$book = $orm->books->findBy(['title' => 'The Wall III'])->fetch();
Assert::notNull($book);
Assert::same(3, $book->tags->countStored());
$books = [];
$tag = $orm->tags->findAll()->fetch();
Assert::notNull($tag);
foreach ($tag->books as $innerBook) {
$books[] = $innerBook->title;
}
Assert::same(['The Wall III'], $books);
}
/**
* @return \Nextras\Orm\Model\Model
*/
private function createOrm()
{
$fileName = function ($name): string {
return TEMP_DIR . "/$name.data"; // FileMock::create('');
};
$factory = new SimpleModelFactory(
new Cache(new MemoryStorage()),
[ // @phpstan-ignore-line
// @phpstan-ignore-next-line
'books' => new BooksRepository(new TestFileMapper($fileName('books'))),
// @phpstan-ignore-next-line
'authors' => new AuthorsRepository(new TestFileMapper($fileName('authors'))),
// @phpstan-ignore-next-line
'publishers' => new PublishersRepository(new TestFileMapper($fileName('publishers'))),
// @phpstan-ignore-next-line
'tags' => new TagsRepository(new TestFileMapper($fileName('tags'))),
// @phpstan-ignore-next-line
'tagFollowers' => new TagFollowersRepository(new TestFileMapper($fileName('tags'))),
// @phpstan-ignore-next-line
'eans' => new EansRepository(new TestFileMapper($fileName('eans'))),
]
);
return $factory->create();
}
}
/**
* @template E of \Nextras\Orm\Entity\IEntity
* @extends ArrayMapper<E>
*/
class TestFileMapper extends ArrayMapper
{
/** @var string */
private $fileName;
public function __construct(string $fileName)
{
$this->fileName = $fileName;
}
protected function saveData(array $data): void
{
file_put_contents($this->fileName, serialize($data));
}
protected function readData(): array
{
$fileName = $this->fileName;
if (!file_exists($fileName)) {
return [];
}
$contents = file_get_contents($fileName);
return unserialize($contents !== false ? $contents : '');
}
}
$test = new FileMapperTest();
$test->run();