1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebaks\CrudTest\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use Sebaks\Crud\Controller\ReadController; |
6
|
|
|
use Sebaks\Crud\View\Model\ReadViewModel; |
7
|
|
|
|
8
|
|
View Code Duplication |
class ReadControllerTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
private $controller; |
11
|
|
|
private $criteriaMock; |
12
|
|
|
private $repositoryMock; |
13
|
|
|
private $view; |
14
|
|
|
private $eventMock; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
$this->criteriaMock = $this->getMock('T4webDomainInterface\Infrastructure\CriteriaInterface'); |
19
|
|
|
$this->repositoryMock = $this->getMock('T4webDomainInterface\Infrastructure\RepositoryInterface'); |
20
|
|
|
|
21
|
|
|
$this->eventMock = $this->getMock('Zend\Mvc\MvcEvent'); |
22
|
|
|
|
23
|
|
|
$this->view = new ReadViewModel(); |
24
|
|
|
|
25
|
|
|
$this->controller = new ReadController( |
26
|
|
|
$this->criteriaMock, |
27
|
|
|
$this->repositoryMock, |
28
|
|
|
$this->view |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testOnDispatchSuccess() |
33
|
|
|
{ |
34
|
|
|
$entityMock = $this->getMock('T4webDomainInterface\EntityInterface'); |
35
|
|
|
|
36
|
|
|
$this->repositoryMock->method('find') |
37
|
|
|
->with($this->criteriaMock) |
38
|
|
|
->willReturn($entityMock); |
39
|
|
|
|
40
|
|
|
$this->eventMock->method('setResult') |
41
|
|
|
->with($this->view); |
42
|
|
|
|
43
|
|
|
$actualViewModel = $this->controller->onDispatch($this->eventMock); |
44
|
|
|
|
45
|
|
|
$this->assertSame($this->view, $actualViewModel); |
46
|
|
|
$this->assertSame($entityMock, $actualViewModel->getEntity()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testOnDispatchFail() |
50
|
|
|
{ |
51
|
|
|
$this->repositoryMock->method('find') |
52
|
|
|
->with($this->criteriaMock) |
53
|
|
|
->willReturn(null); |
54
|
|
|
|
55
|
|
|
$this->eventMock->expects($this->never()) |
56
|
|
|
->method('setResult'); |
57
|
|
|
|
58
|
|
|
$this->eventMock->method('getRouteMatch') |
59
|
|
|
->willReturn($this->getMockBuilder('Zend\Mvc\Router\RouteMatch') |
60
|
|
|
->setMethods(['setParam']) |
61
|
|
|
->disableOriginalConstructor() |
62
|
|
|
->getMock()); |
63
|
|
|
|
64
|
|
|
$this->controller->setEvent($this->eventMock); |
65
|
|
|
$actualViewModel = $this->controller->onDispatch($this->eventMock); |
66
|
|
|
|
67
|
|
|
$this->assertInstanceOf('Zend\View\Model\ViewModel', $actualViewModel); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.