1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
6
|
|
|
use Doctrine\Persistence\ObjectManager; |
7
|
|
|
use Doctrine\Persistence\ObjectRepository; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Zenstruck\Foundry\PersistenceManager; |
10
|
|
|
use Zenstruck\Foundry\Proxy; |
11
|
|
|
use Zenstruck\Foundry\RepositoryProxy; |
12
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category; |
13
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post; |
14
|
|
|
use Zenstruck\Foundry\Tests\ResetGlobals; |
15
|
|
|
use function Zenstruck\Foundry\create; |
16
|
|
|
use function Zenstruck\Foundry\create_many; |
17
|
|
|
use function Zenstruck\Foundry\faker; |
18
|
|
|
use function Zenstruck\Foundry\instantiate; |
19
|
|
|
use function Zenstruck\Foundry\instantiate_many; |
20
|
|
|
use function Zenstruck\Foundry\repository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Kevin Bond <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class FunctionsTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
use ResetGlobals; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
*/ |
32
|
|
|
public function faker(): void |
33
|
|
|
{ |
34
|
|
|
$this->assertIsString(faker()->name); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @test |
39
|
|
|
*/ |
40
|
|
|
public function instantiate(): void |
41
|
|
|
{ |
42
|
|
|
$object = instantiate(Post::class, ['title' => 'title', 'body' => 'body']); |
43
|
|
|
|
44
|
|
|
$this->assertInstanceOf(Post::class, $object); |
45
|
|
|
$this->assertSame('title', $object->getTitle()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @test |
50
|
|
|
*/ |
51
|
|
|
public function instantiate_many(): void |
52
|
|
|
{ |
53
|
|
|
$objects = instantiate_many(3, Category::class); |
54
|
|
|
|
55
|
|
|
$this->assertCount(3, $objects); |
56
|
|
|
$this->assertInstanceOf(Category::class, $objects[0]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @test |
61
|
|
|
*/ |
62
|
|
|
public function create(): void |
63
|
|
|
{ |
64
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
65
|
|
|
$registry |
66
|
|
|
->expects($this->once()) |
67
|
|
|
->method('getManagerForClass') |
68
|
|
|
->with(Category::class) |
69
|
|
|
->willReturn($this->createMock(ObjectManager::class)) |
70
|
|
|
; |
71
|
|
|
|
72
|
|
|
PersistenceManager::register($registry); |
73
|
|
|
|
74
|
|
|
$object = create(Category::class); |
75
|
|
|
|
76
|
|
|
$this->assertInstanceOf(Proxy::class, $object); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @test |
81
|
|
|
*/ |
82
|
|
|
public function create_many(): void |
83
|
|
|
{ |
84
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
85
|
|
|
$registry |
86
|
|
|
->expects($this->exactly(3)) |
87
|
|
|
->method('getManagerForClass') |
88
|
|
|
->with(Category::class) |
89
|
|
|
->willReturn($this->createMock(ObjectManager::class)) |
90
|
|
|
; |
91
|
|
|
|
92
|
|
|
PersistenceManager::register($registry); |
93
|
|
|
|
94
|
|
|
$objects = create_many(3, Category::class); |
95
|
|
|
|
96
|
|
|
$this->assertCount(3, $objects); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @test |
101
|
|
|
*/ |
102
|
|
|
public function repository(): void |
103
|
|
|
{ |
104
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
105
|
|
|
$registry |
106
|
|
|
->expects($this->once()) |
107
|
|
|
->method('getRepository') |
108
|
|
|
->with(Category::class) |
109
|
|
|
->willReturn($this->createMock(ObjectRepository::class)) |
110
|
|
|
; |
111
|
|
|
|
112
|
|
|
PersistenceManager::register($registry); |
113
|
|
|
|
114
|
|
|
$this->assertInstanceOf(RepositoryProxy::class, repository(new Category())); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|