Issues (99)

test/Mvc/ControllerTest.php (2 issues)

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 Drone\Mvc\Exception\MethodNotFoundException;
15
use Drone\Mvc\Exception\PrivateMethodExecutionException;
16
use PHPUnit\Framework\TestCase;
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
        $result = $ctrl->about();
33
34
        $expected = ["greeting" => "Hello World!"];
35
        $this->assertSame($expected, $result);
36
37
        /*
38
         * Implicit method execution
39
         */
40
41
        $ctrl = new \App\Controller\Home;
42
        $ctrl->setMethod('about');
43
        $result = $ctrl->execute();
44
45
        $expected = ["greeting" => "Hello World!"];
46
        $this->assertSame($expected, $result);
47
    }
48
49
    /**
50
     * Tests if we can execute the controller on private methods
51
     *
52
     * @return null
53
     */
54
    public function testExecutingWhenMethodIsPrivate()
55
    {
56
        $ctrl = new \App\Controller\Home;
57
58
        $errorObject = null;
59
60
        try {
61
            $ctrl->setMethod('doSomething');
62
            $ctrl->execute();
63
        } catch (\Exception $e) {
64
            $errorObject = ($e instanceof PrivateMethodExecutionException);
65
        } finally {
66
            $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...
67
        }
68
    }
69
70
    /**
71
     * Tests if we can execute the controller on non-existing methods
72
     *
73
     * @return null
74
     */
75
    public function testExecutingWhenMethodDoesNotExists()
76
    {
77
        $ctrl = new \App\Controller\Home;
78
79
        $errorObject = null;
80
81
        try {
82
            $ctrl->setMethod('notFound');
83
            $ctrl->execute();
84
        } catch (\Exception $e) {
85
            $errorObject = ($e instanceof MethodNotFoundException);
86
        } finally {
87
            $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...
88
        }
89
    }
90
}
91
92
/*
93
|--------------------------------------------------------------------------
94
| Controller class
95
|--------------------------------------------------------------------------
96
|
97
| This is a simple controller implementing AbstractController.
98
|
99
*/
100
101
namespace App\Controller;
102
103
use Drone\Mvc\AbstractController;
104
105
class Home extends AbstractController
106
{
107
    public function about()
108
    {
109
        return ["greeting" => "Hello World!"];
110
    }
111
112
    private function doSomething()
113
    {
114
        return ["result" => "45dEf7f8EF"];
115
    }
116
}
117