Completed
Branchmaster (e55173)
created

TagTest::testArticleRaw()   A

↳ Parent: TagTest

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 10
Code Lines 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace Redaxscript\Tests\Template;
3
4
use Redaxscript\Language;
5
use Redaxscript\Registry;
6
use Redaxscript\Request;
7
use Redaxscript\Template;
8
use Redaxscript\Tests\TestCaseAbstract;
9
use org\bovigo\vfs\vfsStream as Stream;
10
use org\bovigo\vfs\vfsStreamFile as StreamFile;
11
use org\bovigo\vfs\vfsStreamWrapper as StreamWrapper;
12
13
/**
14
 * TagTest
15
 *
16
 * @since 2.3.0
17
 *
18
 * @package Redaxscript
19
 * @category Tests
20
 * @author Henry Ruhs
21
 */
22
23
class TagTest extends TestCaseAbstract
24
{
25
	/**
26
	 * instance of the registry class
27
	 *
28
	 * @var object
29
	 */
30
31
	protected $_registry;
32
33
	/**
34
	 * instance of the language class
35
	 *
36
	 * @var object
37
	 */
38
39
	protected $_language;
40
41
	/**
42
	 * instance of the request class
43
	 *
44
	 * @var object
45
	 */
46
47
	protected $_request;
48
49
	/**
50
	 * setUp
51
	 *
52
	 * @since 2.6.0
53
	 */
54
55
	public function setUp()
56
	{
57
		$this->_registry = Registry::getInstance();
58
		$this->_language = Language::getInstance();
59
		$this->_request = Request::getInstance();
60
	}
61
62
	/**
63
	 * testBase
64
	 *
65
	 * @since 3.0.0
66
	 */
67
68
	public function testBase()
69
	{
70
		/* actual */
71
72
		$actual = Template\Tag::base();
73
74
		/* compare */
75
76
		$this->assertString($actual);
77
	}
78
79
	/**
80
	 * testTitle
81
	 *
82
	 * @since 3.0.0
83
	 */
84
85
	public function testTitle()
86
	{
87
		/* actual */
88
89
		$actual = Template\Tag::title();
90
91
		/* compare */
92
93
		$this->assertString($actual);
94
	}
95
96
	/**
97
	 * testLink
98
	 *
99
	 * @since 3.0.0
100
	 */
101
102
	public function testLink()
103
	{
104
		/* actual */
105
106
		$actual = Template\Tag::link();
107
108
		/* compare */
109
110
		$this->assertInstanceOf('Redaxscript\Head\Link', $actual);
111
	}
112
113
	/**
114
	 * testMeta
115
	 *
116
	 * @since 3.0.0
117
	 */
118
119
	public function testMeta()
120
	{
121
		/* actual */
122
123
		$actual = Template\Tag::meta();
124
125
		/* compare */
126
127
		$this->assertInstanceOf('Redaxscript\Head\Meta', $actual);
128
	}
129
130
	/**
131
	 * testScript
132
	 *
133
	 * @since 3.0.0
134
	 */
135
136
	public function testScript()
137
	{
138
		/* actual */
139
140
		$actual = Template\Tag::script();
141
142
		/* compare */
143
144
		$this->assertInstanceOf('Redaxscript\Head\Script', $actual);
145
	}
146
147
	/**
148
	 * testStyle
149
	 *
150
	 * @since 3.0.0
151
	 */
152
153
	public function testStyle()
154
	{
155
		/* actual */
156
157
		$actual = Template\Tag::style();
158
159
		/* compare */
160
161
		$this->assertInstanceOf('Redaxscript\Head\Style', $actual);
162
	}
163
164
	/**
165
	 * testBreadcrumb
166
	 *
167
	 * @since 2.3.0
168
	 */
169
170
	public function testBreadcrumb()
171
	{
172
		/* actual */
173
174
		$actual = Template\Tag::breadcrumb();
175
176
		/* compare */
177
178
		$this->assertString($actual);
179
	}
180
181
	/**
182
	 * testConsoleLine
183
	 *
184
	 * @since 3.0.0
185
	 */
186
187
	public function testConsoleLine()
188
	{
189
		/* setup */
190
191
		$this->_request->setPost('argv', 'help');
192
193
		/* actual */
194
195
		$actual = Template\Tag::consoleLine();
196
197
		/* compare */
198
199
		$this->assertString($actual);
200
	}
201
202
	/**
203
	 * testConsoleLineInvalid
204
	 *
205
	 * @since 3.0.0
206
	 */
207
208
	public function testConsoleLineInvalid()
209
	{
210
		/* setup */
211
212
		$this->_request->setPost('argv', 'invalidCommand');
213
214
		/* actual */
215
216
		$actual = Template\Tag::consoleLine();
217
218
		/* compare */
219
220
		$this->assertNull($actual);
221
	}
222
223
	/**
224
	 * testConsoleForm
225
	 *
226
	 * @since 3.0.0
227
	 */
228
229
	public function testConsoleForm()
230
	{
231
		/* actual */
232
233
		$actual = Template\Tag::consoleForm();
234
235
		/* compare */
236
237
		$this->assertString($actual);
238
	}
239
240
	/**
241
	 * testSearchForm
242
	 *
243
	 * @since 3.0.0
244
	 */
245
246
	public function testSearchForm()
247
	{
248
		/* actual */
249
250
		$actual = Template\Tag::searchForm();
251
252
		/* compare */
253
254
		$this->assertString($actual);
255
	}
256
257
	/**
258
	 * testPartial
259
	 *
260
	 * @since 2.3.0
261
	 */
262
263
	public function testPartial()
264
	{
265
		/* setup */
266
267
		Stream::setup('root');
268
		$file = new StreamFile('partial.phtml');
269
		StreamWrapper::getRoot()->addChild($file);
0 ignored issues
show
Bug introduced by redaxmedia
It seems like you code against a concrete implementation and not the interface org\bovigo\vfs\vfsStreamContent as the method addChild() does only exist in the following implementations of said interface: org\bovigo\vfs\DotDirectory, org\bovigo\vfs\vfsStreamDirectory.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
270
271
		/* actual */
272
273
		$actual = Template\Tag::partial(Stream::url('root/partial.phtml'));
274
275
		/* compare */
276
277
		$this->assertString($actual);
278
	}
279
280
	/**
281
	 * testRegistry
282
	 *
283
	 * @since 2.6.0
284
	 */
285
286
	public function testRegistry()
287
	{
288
		/* setup */
289
290
		$this->_registry->set('testKey', 'testValue');
291
292
		/* actual */
293
294
		$actual = Template\Tag::registry('testKey');
295
296
		/* compare */
297
298
		$this->assertEquals('testValue', $actual);
299
	}
300
301
	/**
302
	 * testLanguage
303
	 *
304
	 * @since 2.6.0
305
	 */
306
307
	public function testLanguage()
308
	{
309
		/* setup */
310
311
		$this->_language->set('testKey', 'testValue');
312
313
		/* actual */
314
315
		$actual = Template\Tag::language('testKey');
316
317
		/* compare */
318
319
		$this->assertEquals('testValue', $actual);
320
	}
321
322
	/**
323
	 * testSetting
324
	 *
325
	 * @since 2.6.0
326
	 */
327
328
	public function testSetting()
329
	{
330
		/* actual */
331
332
		$actual = Template\Tag::setting('charset');
333
334
		/* compare */
335
336
		$this->assertEquals('utf-8', $actual);
337
	}
338
339
	/**
340
	 * testArticleRaw
341
	 *
342
	 * @since 3.0.0
343
	 */
344
345
	public function testArticleRaw()
346
	{
347
		/* actual */
348
349
		$actual = Template\Tag::articleRaw();
350
351
		/* compare */
352
353
		$this->assertInstanceOf('Redaxscript\Db', $actual);
354
	}
355
356
	/**
357
	 * testExtraRaw
358
	 *
359
	 * @since 3.0.0
360
	 */
361
362
	public function testExtraRaw()
363
	{
364
		/* actual */
365
366
		$actual = Template\Tag::extraRaw();
367
368
		/* compare */
369
370
		$this->assertInstanceOf('Redaxscript\Db', $actual);
371
	}
372
}
373