1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Component\Resource\Tests\Factory; |
13
|
|
|
|
14
|
|
|
use Lug\Component\Resource\Factory\Factory; |
15
|
|
|
use Lug\Component\Resource\Factory\FactoryInterface; |
16
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
17
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
18
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author GeLo <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class FactoryTest extends \PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Factory |
27
|
|
|
*/ |
28
|
|
|
private $factory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
32
|
|
|
*/ |
33
|
|
|
private $resource; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|PropertyAccessorInterface |
37
|
|
|
*/ |
38
|
|
|
private $propertyAccessor; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function setUp() |
44
|
|
|
{ |
45
|
|
|
$this->resource = $this->createResourceMock(); |
46
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
47
|
|
|
|
48
|
|
|
$this->factory = new Factory($this->resource, $this->propertyAccessor); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testInheritance() |
52
|
|
|
{ |
53
|
|
|
$this->assertInstanceOf(FactoryInterface::class, $this->factory); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testCreateWithOptions() |
57
|
|
|
{ |
58
|
|
|
$this->resource |
59
|
|
|
->expects($this->once()) |
60
|
|
|
->method('getModel') |
61
|
|
|
->will($this->returnValue($model = Fixture::class)); |
62
|
|
|
|
63
|
|
|
$fixture = $this->factory->create(['name' => $name = 'foo']); |
64
|
|
|
|
65
|
|
|
$this->assertInstanceOf($model, $fixture); |
66
|
|
|
$this->assertSame($name, $fixture->getName()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testCreateWithoutOptions() |
70
|
|
|
{ |
71
|
|
|
$this->resource |
72
|
|
|
->expects($this->once()) |
73
|
|
|
->method('getModel') |
74
|
|
|
->will($this->returnValue($model = Fixture::class)); |
75
|
|
|
|
76
|
|
|
$fixture = $this->factory->create(); |
77
|
|
|
|
78
|
|
|
$this->assertInstanceOf($model, $fixture); |
79
|
|
|
$this->assertNull($fixture->getName()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
84
|
|
|
*/ |
85
|
|
|
private function createResourceMock() |
86
|
|
|
{ |
87
|
|
|
return $this->createMock(ResourceInterface::class); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @author GeLo <[email protected]> |
93
|
|
|
*/ |
94
|
|
|
class Fixture |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
/** |
97
|
|
|
* @var string |
98
|
|
|
*/ |
99
|
|
|
private $name; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getName() |
105
|
|
|
{ |
106
|
|
|
return $this->name; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $name |
111
|
|
|
*/ |
112
|
|
|
public function setName($name) |
113
|
|
|
{ |
114
|
|
|
$this->name = $name; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.