1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DroneTest\Util; |
12
|
|
|
|
13
|
|
|
use Drone\Mvc\AbstractController; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Drone\Mvc\Exception\PrivateMethodExecutionException; |
16
|
|
|
use Drone\Mvc\Exception\MethodNotFoundException; |
17
|
|
|
|
18
|
|
|
class ControllerTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Tests controller execution |
22
|
|
|
* |
23
|
|
|
* @return null |
24
|
|
|
*/ |
25
|
|
|
public function testInstantiationAndMethodExecution() |
26
|
|
|
{ |
27
|
|
|
/* |
28
|
|
|
* Explicit method execution |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
$ctrl = new \App\Controller\Home; |
32
|
|
|
$params = $ctrl->about(); |
33
|
|
|
|
34
|
|
|
$expected = ["greeting" => "Hello World!"]; |
35
|
|
|
$this->assertSame($expected, $params); |
36
|
|
|
|
37
|
|
|
/* |
38
|
|
|
* Implicit method execution |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
$ctrl = new \App\Controller\Home; |
42
|
|
|
$ctrl->setMethod('about'); |
43
|
|
|
$ctrl->execute(); |
44
|
|
|
$params = $ctrl->getParams(); |
45
|
|
|
|
46
|
|
|
$expected = ["greeting" => "Hello World!"]; |
47
|
|
|
$this->assertSame($expected, $params); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Tests if we can execute the controller on private methods |
52
|
|
|
* |
53
|
|
|
* @return null |
54
|
|
|
*/ |
55
|
|
|
public function testExecutingWhenMethodIsPrivate() |
56
|
|
|
{ |
57
|
|
|
$ctrl = new \App\Controller\Home; |
58
|
|
|
|
59
|
|
|
$errorObject = null; |
60
|
|
|
|
61
|
|
|
try { |
62
|
|
|
$ctrl->setMethod('doSomething'); |
63
|
|
|
$ctrl->execute(); |
64
|
|
|
} |
65
|
|
|
catch (\Exception $e) |
66
|
|
|
{ |
67
|
|
|
$errorObject = ($e instanceof PrivateMethodExecutionException); |
68
|
|
|
} |
69
|
|
|
finally |
70
|
|
|
{ |
71
|
|
|
$this->assertTrue($errorObject, $e->getMessage()); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tests if we can execute the controller on non-existing methods |
77
|
|
|
* |
78
|
|
|
* @return null |
79
|
|
|
*/ |
80
|
|
|
public function testExecutingWhenMethodDoesNotExists() |
81
|
|
|
{ |
82
|
|
|
$ctrl = new \App\Controller\Home; |
83
|
|
|
|
84
|
|
|
$errorObject = null; |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
$ctrl->setMethod('notFound'); |
88
|
|
|
$ctrl->execute(); |
89
|
|
|
} |
90
|
|
|
catch (\Exception $e) |
91
|
|
|
{ |
92
|
|
|
$errorObject = ($e instanceof MethodNotFoundException); |
93
|
|
|
} |
94
|
|
|
finally |
95
|
|
|
{ |
96
|
|
|
$this->assertTrue($errorObject, $e->getMessage()); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/* |
102
|
|
|
|-------------------------------------------------------------------------- |
103
|
|
|
| Controller class |
104
|
|
|
|-------------------------------------------------------------------------- |
105
|
|
|
| |
106
|
|
|
| This is a simple controller implementing AbstractController. |
107
|
|
|
| |
108
|
|
|
*/ |
109
|
|
|
|
110
|
|
|
namespace App\Controller; |
111
|
|
|
|
112
|
|
|
use Drone\Mvc\AbstractController; |
113
|
|
|
|
114
|
|
|
class Home extends AbstractController |
115
|
|
|
{ |
116
|
|
|
public function about() |
117
|
|
|
{ |
118
|
|
|
return ["greeting" => "Hello World!"]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function doSomething() |
122
|
|
|
{ |
123
|
|
|
return ["result" => "45dEf7f8EF"]; |
124
|
|
|
} |
125
|
|
|
} |