forked from thephpleague/factory-muffin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DoctrineTest.php
142 lines (120 loc) · 4.31 KB
/
DoctrineTest.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
<?php
/*
* This file is part of Factory Muffin.
*
* (c) Graham Campbell <[email protected]>
* (c) Scott Robertson <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use League\FactoryMuffin\FactoryMuffin;
use League\FactoryMuffin\Faker\Facade as Faker;
use League\FactoryMuffin\Stores\RepositoryStore;
/**
* This is doctrine test class.
*
* @author Michael Bodnarchuk <[email protected]>
* @author Graham Campbell <[email protected]>
*/
class DoctrineTest extends AbstractTestCase
{
const USER_ENTITY = 'League\FactoryMuffin\Test\User';
const CAT_ENTITY = 'League\FactoryMuffin\Test\Cat';
protected static $em;
public static function setupBeforeClass()
{
$dbParams = [
'driver' => 'pdo_sqlite',
'memory' => true,
];
$config = Setup::createAnnotationMetadataConfiguration([__DIR__.'/entities'], true);
static::$em = EntityManager::create($dbParams, $config);
static::$fm = new FactoryMuffin(new RepositoryStore(static::$em));
$schemaTool = new SchemaTool(static::$em);
$classes = [
static::$em->getClassMetadata(self::USER_ENTITY),
static::$em->getClassMetadata(self::CAT_ENTITY),
];
$schemaTool->dropSchema($classes);
$schemaTool->createSchema($classes);
static::$fm->define(self::USER_ENTITY)->setDefinitions([
'name' => Faker::firstNameMale(),
'email' => Faker::email(),
]);
static::$fm->getDefinition(self::USER_ENTITY)->setCallback(function ($model) {
static::$em->refresh($model);
});
static::$fm->define(self::CAT_ENTITY)->setDefinitions([
'name' => Faker::firstNameFemale(),
'user' => 'entity|'.self::USER_ENTITY,
]);
Faker::setLocale('en_GB');
static::$fm->seed(5, self::USER_ENTITY);
static::$fm->seed(50, self::CAT_ENTITY);
}
public static function tearDownAfterClass()
{
static::$fm->deleteSaved();
static::$fm = new FactoryMuffin();
}
public function testNumberOfCats()
{
$cats = [];
$users = static::$em->getRepository(self::USER_ENTITY)->findAll();
foreach ($users as $user) {
$userCats = $user->getCats();
foreach ($userCats as $cat) {
$cats[] = $cat;
}
}
$this->assertCount(50, $cats);
$this->assertInstanceOf(self::CAT_ENTITY, $cats[0]);
}
public function testNumberOfCatOwners()
{
$users = [];
$cats = static::$em->getRepository(self::CAT_ENTITY)->findAll();
foreach ($cats as $cat) {
$users[] = $cat->getUser();
}
$this->assertCount(50, $users);
$this->assertInstanceOf(self::USER_ENTITY, $users[0]);
}
public function testUserProperties()
{
$user = self::$em->find(self::USER_ENTITY, 1);
$this->assertGreaterThan(1, strlen($user->getName()));
$this->assertGreaterThan(5, strlen($user->getEmail()));
$this->assertContains('@', $user->getEmail());
$this->assertContains('.', $user->getEmail());
}
public function testCatProperties()
{
$cat = self::$em->find(self::CAT_ENTITY, 1);
$this->assertGreaterThan(1, strlen($cat->getName()));
$this->assertInstanceOf(self::USER_ENTITY, $cat->getUser());
}
public function testSavedObjects()
{
$reflection = new ReflectionClass(static::$fm);
$store = $reflection->getProperty('store');
$store->setAccessible(true);
$value = $store->getValue(static::$fm);
// 50 cats + 50 corresponding users + 5 users without cats
$this->assertCount(105, $value->saved());
$this->assertCount(0, $value->pending());
}
public function testDeleteSaved()
{
$cat = self::$fm->create(self::CAT_ENTITY);
$user = $cat->getUser();
$this->assertInstanceOf(self::USER_ENTITY, $user);
static::tearDownAfterClass();
$users = static::$em->getRepository(self::USER_ENTITY)->findAll();
$this->assertCount(0, $users);
}
}