Passed
Push — master ( 6b8118...1ac76f )
by Darío
01:44
created

testExecutingWhenMethodDoesNotExists()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 5
nop 0
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
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());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
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());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
97
        }
98
    }
99
100
    /**
101
     * Tests stopping execution
102
     *
103
     * @return null
104
     */
105
    public function testStoppingExecution()
106
    {
107
        $ctrl = new \App\Controller\Home;
108
        $ctrl->setMethod('about');
109
110
        $this->assertTrue($ctrl->executionIsAllowed());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $ctrl->executionIsAllowed() targeting Drone\Mvc\AbstractController::executionIsAllowed() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
111
112
        $ctrl->stopExecution();
113
114
        $this->assertNotTrue($ctrl->executionIsAllowed());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $ctrl->executionIsAllowed() targeting Drone\Mvc\AbstractController::executionIsAllowed() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
115
116
        $ctrl->execute();
117
        $params = $ctrl->getParams();
118
119
        $expected = [];
120
        $this->assertSame($expected, $params);
121
    }
122
}
123
124
/*
125
|--------------------------------------------------------------------------
126
| Controller class
127
|--------------------------------------------------------------------------
128
|
129
| This is a simple controller implementing AbstractController.
130
|
131
*/
132
133
namespace App\Controller;
134
135
use Drone\Mvc\AbstractController;
136
137
class Home extends AbstractController
138
{
139
    public function about()
140
    {
141
        return ["greeting" => "Hello World!"];
142
    }
143
144
    private function doSomething()
145
    {
146
        return ["result" => "45dEf7f8EF"];
147
    }
148
}