1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\Persistence\Event; |
6
|
|
|
|
7
|
|
|
use Doctrine\Persistence\Event\PreUpdateEventArgs; |
8
|
|
|
use Doctrine\Persistence\ObjectManager; |
9
|
|
|
use Doctrine\Tests\DoctrineTestCase; |
10
|
|
|
use Doctrine\Tests\Persistence\TestObject; |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
class PreUpdateEventArgsTest extends DoctrineTestCase |
14
|
|
|
{ |
15
|
|
|
public function testPreUpdateEventArgs() : void |
16
|
|
|
{ |
17
|
|
|
$expectedEntityChangeset = [ |
18
|
|
|
'name' => ['old', 'new'], |
19
|
|
|
'active' => [0, 1], |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
$event = $this->createTestPreUpdateEventArgs(); |
23
|
|
|
|
24
|
|
|
self::assertSame($expectedEntityChangeset, $event->getEntityChangeSet()); |
25
|
|
|
|
26
|
|
|
self::assertTrue($event->hasChangedField('name')); |
27
|
|
|
self::assertTrue($event->hasChangedField('active')); |
28
|
|
|
self::assertFalse($event->hasChangedField('email')); |
29
|
|
|
|
30
|
|
|
self::assertSame('old', $event->getOldValue('name')); |
31
|
|
|
self::assertSame('new', $event->getNewValue('name')); |
32
|
|
|
|
33
|
|
|
$event->setNewValue('name', 'changed new'); |
34
|
|
|
|
35
|
|
|
self::assertSame('changed new', $event->getNewValue('name')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetOldValueThrowsInvalidArgumentExceptionOnInvalidField() : void |
39
|
|
|
{ |
40
|
|
|
$event = $this->createTestPreUpdateEventArgs(); |
41
|
|
|
|
42
|
|
|
$this->expectException(InvalidArgumentException::class); |
43
|
|
|
$this->expectExceptionMessage('Field "does_not_exist" is not a valid field of the entity "Doctrine\Tests\Persistence\TestObject'); |
44
|
|
|
|
45
|
|
|
$event->getOldValue('does_not_exist'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testGetNewValueThrowsInvalidArgumentExceptionOnInvalidField() : void |
49
|
|
|
{ |
50
|
|
|
$event = $this->createTestPreUpdateEventArgs(); |
51
|
|
|
|
52
|
|
|
$this->expectException(InvalidArgumentException::class); |
53
|
|
|
$this->expectExceptionMessage('Field "does_not_exist" is not a valid field of the entity "Doctrine\Tests\Persistence\TestObject'); |
54
|
|
|
|
55
|
|
|
$event->getNewValue('does_not_exist'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testSetNewValueThrowsInvalidArgumentExceptionOnInvalidField() : void |
59
|
|
|
{ |
60
|
|
|
$event = $this->createTestPreUpdateEventArgs(); |
61
|
|
|
|
62
|
|
|
$this->expectException(InvalidArgumentException::class); |
63
|
|
|
$this->expectExceptionMessage('Field "does_not_exist" is not a valid field of the entity "Doctrine\Tests\Persistence\TestObject'); |
64
|
|
|
|
65
|
|
|
$event->setNewValue('does_not_exist', 'new value'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function createTestPreUpdateEventArgs() : PreUpdateEventArgs |
69
|
|
|
{ |
70
|
|
|
$entity = new TestObject(); |
71
|
|
|
|
72
|
|
|
$objectManager = $this->createMock(ObjectManager::class); |
73
|
|
|
|
74
|
|
|
$entityChangeset = [ |
75
|
|
|
'name' => ['old', 'new'], |
76
|
|
|
'active' => [0, 1], |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
return new PreUpdateEventArgs($entity, $objectManager, $entityChangeset); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|