1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LKu\DoctrineGuidTest; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventManager; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
9
|
|
|
use Doctrine\ORM\Events; |
10
|
|
|
use Doctrine\ORM\Id\AbstractIdGenerator; |
11
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
12
|
|
|
use LKu\DoctrineGuid\EventSubscriber; |
13
|
|
|
use LKu\DoctrineGuidTest\Asset\Entity; |
14
|
|
|
use PHPUnit_Framework_Assert; |
15
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
16
|
|
|
use PHPUnit_Framework_TestCase; |
17
|
|
|
use ReflectionProperty; |
18
|
|
|
use Zend\ServiceManager\ServiceManager; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Test case for {@see \LKu\DoctrineGuid\EventSubscriber} |
22
|
|
|
*/ |
23
|
|
|
class EventSubscriberTest extends PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
/** @var AbstractIdGenerator|PHPUnit_Framework_MockObject_MockObject */ |
26
|
|
|
protected $guidGenerator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
protected function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->guidGenerator = $this->getMockBuilder('Doctrine\ORM\Id\AbstractIdGenerator')->getMockForAbstractClass(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @covers \LKu\DoctrineGuid\EventSubscriber::__construct |
38
|
|
|
*/ |
39
|
|
|
public function testConstructor() |
40
|
|
|
{ |
41
|
|
|
$subscriber = new EventSubscriber($this->guidGenerator); |
42
|
|
|
|
43
|
|
|
$this->assertSame($this->guidGenerator, PHPUnit_Framework_Assert::readAttribute($subscriber, 'guidGenerator')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @covers \LKu\DoctrineGuid\EventSubscriber::getSubscribedEvents |
48
|
|
|
*/ |
49
|
|
|
public function testSubscribedEvents() |
50
|
|
|
{ |
51
|
|
|
$subscriber = new EventSubscriber($this->guidGenerator); |
52
|
|
|
|
53
|
|
|
$this->assertTrue(in_array(Events::prePersist, $subscriber->getSubscribedEvents())); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @covers \LKu\DoctrineGuid\EventSubscriber::prePersist |
58
|
|
|
*/ |
59
|
|
|
public function testPrePersist() |
60
|
|
|
{ |
61
|
|
|
$testGuid = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'; |
62
|
|
|
$this->guidGenerator->expects($this->once()) |
63
|
|
|
->method('generate') |
64
|
|
|
->will($this->returnValue($testGuid)); |
65
|
|
|
|
66
|
|
|
$subscriber = new EventSubscriber($this->guidGenerator); |
67
|
|
|
|
68
|
|
|
$entity = new Entity(null); |
69
|
|
|
|
70
|
|
|
$propertyReflection = new ReflectionProperty('LKu\DoctrineGuidTest\Asset\Entity', 'guid'); |
71
|
|
|
$propertyReflection->setAccessible(true); |
72
|
|
|
|
73
|
|
|
$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo') |
74
|
|
|
->disableOriginalConstructor() |
75
|
|
|
->getMock(); |
76
|
|
|
$metadata->expects($this->any()) |
77
|
|
|
->method('getFieldNames') |
78
|
|
|
->will($this->returnValue(['guid'])); |
79
|
|
|
$metadata->expects($this->any()) |
80
|
|
|
->method('getTypeOfField') |
81
|
|
|
->will($this->returnValue(Type::GUID)); |
82
|
|
|
$metadata->expects($this->any()) |
83
|
|
|
->method('getReflectionProperty') |
84
|
|
|
->will($this->returnValue($propertyReflection)); |
85
|
|
|
|
86
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock(); |
87
|
|
|
$em->expects($this->any()) |
88
|
|
|
->method('getClassMetadata') |
89
|
|
|
->will($this->returnValue($metadata)); |
90
|
|
|
|
91
|
|
|
// generate new GUID if not exists |
92
|
|
|
$args = new LifecycleEventArgs($entity, $em); |
93
|
|
|
$subscriber->prePersist($args); |
94
|
|
|
$this->assertEquals($testGuid, $entity->getGuid()); |
95
|
|
|
|
96
|
|
|
// don't update GUID |
97
|
|
|
$entity = new Entity('test'); |
98
|
|
|
$args = new LifecycleEventArgs($entity, $em); |
99
|
|
|
$subscriber->prePersist($args); |
100
|
|
|
$this->assertEquals('test', $entity->getGuid()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|