1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebaks\CrudTest\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use Sebaks\Crud\Controller\UpdateController; |
6
|
|
|
use Sebaks\Crud\View\Model\UpdateViewModel; |
7
|
|
|
|
8
|
|
|
class UpdateControllerTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
private $controller; |
11
|
|
|
private $id; |
12
|
|
|
private $data; |
|
|
|
|
13
|
|
|
private $updaterMock; |
14
|
|
|
private $view; |
15
|
|
|
private $eventMock; |
16
|
|
|
|
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
$this->id = 22; |
20
|
|
|
$this->data = [ |
21
|
|
|
'foo' => 'bar' |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
$this->updaterMock = $this->getMockBuilder('T4webDomainInterface\Service\UpdaterInterface') |
25
|
|
|
->disableOriginalConstructor() |
26
|
|
|
->getMock(); |
27
|
|
|
|
28
|
|
|
$this->eventMock = $this->getMock('Zend\Mvc\MvcEvent'); |
29
|
|
|
|
30
|
|
|
$this->view = new UpdateViewModel(); |
31
|
|
|
|
32
|
|
|
$this->controller = new UpdateController( |
33
|
|
|
$this->id, |
34
|
|
|
$this->data, |
35
|
|
|
$this->updaterMock, |
36
|
|
|
$this->view |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testOnDispatchSuccess() |
41
|
|
|
{ |
42
|
|
|
$entityMock = $this->getMock('T4webDomainInterface\EntityInterface'); |
43
|
|
|
|
44
|
|
|
$this->updaterMock->method('update') |
45
|
|
|
->with($this->id, $this->data) |
46
|
|
|
->willReturn($entityMock); |
47
|
|
|
|
48
|
|
|
$this->eventMock->method('setResult') |
49
|
|
|
->with($this->view); |
50
|
|
|
|
51
|
|
|
$actualViewModel = $this->controller->onDispatch($this->eventMock); |
52
|
|
|
|
53
|
|
|
$this->assertSame($this->view, $actualViewModel); |
54
|
|
|
$this->assertSame($entityMock, $actualViewModel->getEntity()); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testOnDispatchNotFound() |
58
|
|
|
{ |
59
|
|
|
$this->updaterMock->method('update') |
60
|
|
|
->with($this->id, $this->data) |
61
|
|
|
->willReturn(null); |
62
|
|
|
|
63
|
|
|
$this->eventMock->expects($this->never()) |
64
|
|
|
->method('setResult'); |
65
|
|
|
|
66
|
|
|
$this->eventMock->method('getRouteMatch') |
67
|
|
|
->willReturn($this->getMockBuilder('Zend\Mvc\Router\RouteMatch') |
68
|
|
|
->setMethods(['setParam']) |
69
|
|
|
->disableOriginalConstructor() |
70
|
|
|
->getMock()); |
71
|
|
|
|
72
|
|
|
$this->controller->setEvent($this->eventMock); |
73
|
|
|
$actualViewModel = $this->controller->onDispatch($this->eventMock); |
74
|
|
|
|
75
|
|
|
$this->assertInstanceOf('Zend\View\Model\ViewModel', $actualViewModel); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testOnDispatchValidationFail() |
79
|
|
|
{ |
80
|
|
|
$entityMock = $this->getMock('T4webDomainInterface\EntityInterface'); |
81
|
|
|
|
82
|
|
|
$this->updaterMock->method('update') |
83
|
|
|
->with($this->id, $this->data) |
84
|
|
|
->willReturn($entityMock); |
85
|
|
|
|
86
|
|
|
$this->eventMock->expects($this->once()) |
87
|
|
|
->method('setResult') |
88
|
|
|
->with($this->view); |
89
|
|
|
|
90
|
|
|
$this->updaterMock->method('hasErrors') |
91
|
|
|
->willReturn(true); |
92
|
|
|
|
93
|
|
|
$this->updaterMock->method('getErrors') |
94
|
|
|
->willReturn(['errorField' => 'error message']); |
95
|
|
|
|
96
|
|
|
$this->controller->setEvent($this->eventMock); |
97
|
|
|
$actualViewModel = $this->controller->onDispatch($this->eventMock); |
98
|
|
|
|
99
|
|
|
$this->assertSame($this->view, $actualViewModel); |
100
|
|
|
$this->assertSame($this->data, $actualViewModel->getInputData()); |
|
|
|
|
101
|
|
|
$this->assertSame($entityMock, $actualViewModel->getEntity()); |
|
|
|
|
102
|
|
|
$this->assertEquals(['errorField' => 'error message'], $actualViewModel->getErrors()); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |