Passed
Push — master ( 40eec0...6215ec )
by Darío
02:02
created

ControllerTest::testStoppingExecution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Home::about() 0 3 1
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
/*
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
}