Completed
Push — master ( 7bfd57...e0f6b9 )
by Alex
02:26
created

PresenterUnitTest::getSetMessagesDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Application\Tests;
3
4
use Mezon\Transport\HttpRequestParams;
5
use Mezon\Router\Router;
6
use Mezon\HtmlTemplate\HtmlTemplate;
7
use Mezon\Application\ViewInterface;
8
use Mezon\Application\View;
9
use Mezon\Application\AbstractPresenter;
10
11
class PresenterUnitTest extends \PHPUnit\Framework\TestCase
12
{
13
14
    /**
15
     * Testing constructor
16
     */
17
    public function testConstructor(): void
18
    {
19
        // setupp
20
        $presenter = new TestingPresenter(new TestingView(), 'Test');
21
22
        $this->assertEquals('Test', $presenter->getPresenterName(), 'Invalid constructor call');
23
    }
24
25
    /**
26
     * Testing render
27
     */
28
    public function testRender(): void
29
    {
30
        // setupp
31
        $presenter = new TestingPresenter(new TestingView(), 'Test');
32
33
        $this->assertEquals('computed content', $presenter->run(), 'Invalid controller execution');
34
        $this->assertEquals('computed content 2', $presenter->run('test2'), 'Invalid controller execution');
35
    }
36
37
    /**
38
     * Testing default controller
39
     */
40
    public function testDefault(): void
41
    {
42
        // setupp
43
        $presenter = new TestingPresenter(new TestingView());
44
45
        // assertionss
46
        $this->expectExceptionMessage('Presenter Default was not found');
47
48
        // test bodyy
49
        $presenter->run();
50
    }
51
52
    /**
53
     * Method tests buildRoute
54
     */
55
    public function testBuildRoute(): void
56
    {
57
        // setup
58
        $presenter = new TestingPresenter(new TestingView());
59
60
        // test body
61
        $route = $presenter->buildRoute('/test/', 'POST', 'controllerTest');
0 ignored issues
show
Deprecated Code introduced by
The function Mezon\Application\Presenter::buildRoute() has been deprecated: Deprecated since 2020-08-28, use Application::buildRoute ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
        $route = /** @scrutinizer ignore-deprecated */ $presenter->buildRoute('/test/', 'POST', 'controllerTest');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
62
63
        // assertions
64
        $this->assertEquals('/test/', $route['route']);
65
        $this->assertEquals('POST', $route['method']);
66
        $this->assertInstanceOf(TestingPresenter::class, $route['callback'][0]);
67
        $this->assertEquals('controllerTest', $route['callback'][1]);
68
    }
69
70
    /**
71
     * Testing method setPresenterName
72
     */
73
    public function testSetPresenterName(): void
74
    {
75
        // setup
76
        $presenter = new TestingPresenter(new TestingView());
77
78
        // test body
79
        $presenter->setPresenterName('SomeName');
80
81
        // assertions
82
        $this->assertEquals('SomeName', $presenter->getPresenterName());
83
    }
84
85
    /**
86
     * Testing method getRequestParamsFetcher
87
     */
88
    public function testGetParamsFetcher(): void
89
    {
90
        // setup
91
        $router = new Router();
92
        $presenter = new TestingPresenter(new TestingView(), '', new HttpRequestParams($router));
93
94
        // test body
95
        $fetcher = $presenter->getRequestParamsFetcher();
96
97
        // assertions
98
        $this->assertInstanceOf(HttpRequestParams::class, $fetcher);
99
    }
100
101
    /**
102
     * Testing method getRequestParamsFetcher with exception
103
     */
104
    public function testGetParamsFetcherWithException(): void
105
    {
106
        // setup
107
        $presenter = new TestingPresenter(new TestingView());
108
109
        // assertions
110
        $this->expectException(\Exception::class);
111
112
        // test body
113
        $presenter->getRequestParamsFetcher();
114
    }
115
116
    /**
117
     * Testing get/set method
118
     */
119
    public function testNullViewInPresenterConstructor(): void
120
    {
121
        // setup
122
        $presenter = new TestingPresenter(null);
123
124
        // test body
125
        $presenter->setErrorCode(11);
126
        $presenter->setErrorMessage('msg1');
127
128
        // assertions
129
        $this->assertEquals(11, $presenter->getErrorCode());
130
        $this->assertEquals('msg1', $presenter->getErrorMessage());
131
    }
132
133
    public function getSetMessagesDataProvider(): array
134
    {
135
        return [
136
            [
137
                new TestingPresenter()
138
            ],
139
            [
140
                new TestingPresenter(new TestingView(new HtmlTemplate(__DIR__, 'index')))
141
            ]
142
        ];
143
    }
144
145
    /**
146
     * Testing get/set method
147
     *
148
     * @dataProvider getSetMessagesDataProvider
149
     */
150
    public function testGetSetMessages(AbstractPresenter $presenter): void
151
    {
152
        // test body
153
        $presenter->setErrorCode(12);
154
        $presenter->setErrorMessage('msg2');
155
        $presenter->setSuccessMessage('msg4');
156
157
        // assertions
158
        $this->assertEquals(12, $presenter->getErrorCode());
159
        $this->assertEquals('msg2', $presenter->getErrorMessage());
160
        $this->assertEquals('msg4', $presenter->getSuccessMessage());
161
    }
162
163
    /**
164
     * Data provider for the test testSetViewParameter
165
     *
166
     * @return array
167
     */
168
    public function setViewParemeterDataProvider(): array
169
    {
170
        return [
171
            [
172
                new TestingView(new HtmlTemplate(__DIR__, 'index')),
173
                'val'
174
            ],
175
            [
176
                null,
177
                null
178
            ]
179
        ];
180
    }
181
182
    /**
183
     * Testing method setViewParameter
184
     *
185
     * @param
186
     *            ?ViewInterface View
187
     * @param mixed $var
188
     *            expected variable name
189
     * @dataProvider setViewParemeterDataProvider
190
     */
191
    public function testSetViewParameter(?ViewInterface $view, $var): void
192
    {
193
        // setup
194
        $presenter = new TestingPresenter($view);
195
196
        // test body
197
        $presenter->setViewParameter('var', 'val', true);
198
199
        // assertions
200
        $this->assertEquals($var, $presenter->getViewParameter('var'));
201
    }
202
}
203