Completed
Push — master ( 858dca...453367 )
by Henry
06:29
created

TagTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 8.9818
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Redaxscript\Tests\Template;
3
4
use org\bovigo\vfs\vfsStream as Stream;
5
use org\bovigo\vfs\vfsStreamFile as StreamFile;
6
use org\bovigo\vfs\vfsStreamWrapper as StreamWrapper;
7
use Redaxscript\Db;
8
use Redaxscript\Model;
9
use Redaxscript\Template;
10
use Redaxscript\Tests\TestCaseAbstract;
11
12
/**
13
 * TagTest
14
 *
15
 * @since 2.3.0
16
 *
17
 * @package Redaxscript
18
 * @category Tests
19
 * @author Henry Ruhs
20
 *
21
 * @covers Redaxscript\Template\Tag
22
 */
23
24
class TagTest extends TestCaseAbstract
25
{
26
	/**
27
	 * setUp
28
	 *
29
	 * @since 3.1.0
30
	 */
31
32
	public function setUp() : void
33
	{
34
		parent::setUp();
35
		$optionArray = $this->getOptionArray();
0 ignored issues
show
Bug introduced by Henry Ruhs
The method getOptionArray() does not seem to exist on object<Redaxscript\Tests\Template\TagTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
		$installer = $this->installerFactory();
0 ignored issues
show
Bug introduced by redaxmedia
The method installerFactory() does not seem to exist on object<Redaxscript\Tests\Template\TagTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
		$installer->init();
38
		$installer->rawCreate();
39
		$installer->insertSettings($optionArray);
40
		$installer->rawMigrate();
41
		$categoryOne = Db::forTablePrefix('categories')->create();
42
		$categoryOne
43
			->set(
44
			[
45
				'title' => 'Category One',
46
				'alias' => 'category-one'
47
			])
48
			->save();
49
		$articleOne = Db::forTablePrefix('articles')->create();
50
		$articleOne
51
			->set(
52
			[
53
				'title' => 'Article One',
54
				'alias' => 'article-one',
55
				'category' => $categoryOne->id,
56
				'comments' => 1
57
			])
58
			->save();
59
		Db::forTablePrefix('articles')
60
			->create()
61
			->set(
62
			[
63
				'title' => 'Article Two',
64
				'alias' => 'article-two',
65
				'category' => $categoryOne->id
66
			])
67
			->save();
68
		Db::forTablePrefix('comments')
69
			->create()
70
			->set(
71
			[
72
				'author' => 'Comment One',
73
				'text' => 'Comment One',
74
				'article' => $articleOne->id
75
			])
76
			->save();
77
		Db::forTablePrefix('comments')
78
			->create()
79
			->set(
80
			[
81
				'author' => 'Comment Two',
82
				'text' => 'Comment Two',
83
				'article' => $articleOne->id
84
			])
85
			->save();
86
	}
87
88
	/**
89
	 * tearDown
90
	 *
91
	 * @since 3.1.0
92
	 */
93
94
	public function tearDown() : void
95
	{
96
		$this->dropDatabase();
0 ignored issues
show
Bug introduced by Henry Ruhs
The method dropDatabase() does not seem to exist on object<Redaxscript\Tests\Template\TagTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
	}
98
99
	/**
100
	 * testBase
101
	 *
102
	 * @since 3.0.0
103
	 */
104
105
	public function testBase() : void
106
	{
107
		/* actual */
108
109
		$actual = Template\Tag::base();
110
111
		/* compare */
112
113
		$this->assertIsString($actual);
114
	}
115
116
	/**
117
	 * testTitle
118
	 *
119
	 * @since 3.0.0
120
	 */
121
122
	public function testTitle() : void
123
	{
124
		/* actual */
125
126
		$actual = Template\Tag::title('test');
127
128
		/* compare */
129
130
		$this->assertIsString($actual);
131
	}
132
133
	/**
134
	 * testLink
135
	 *
136
	 * @since 3.0.0
137
	 */
138
139
	public function testLink() : void
140
	{
141
		/* actual */
142
143
		$actual = Template\Tag::link();
144
145
		/* compare */
146
147
		$this->assertInstanceOf('Redaxscript\Head\Link', $actual);
148
	}
149
150
	/**
151
	 * testMeta
152
	 *
153
	 * @since 3.0.0
154
	 */
155
156
	public function testMeta() : void
157
	{
158
		/* actual */
159
160
		$actual = Template\Tag::meta();
161
162
		/* compare */
163
164
		$this->assertInstanceOf('Redaxscript\Head\Meta', $actual);
165
	}
166
167
	/**
168
	 * testScript
169
	 *
170
	 * @since 3.0.0
171
	 */
172
173
	public function testScript() : void
174
	{
175
		/* actual */
176
177
		$actual = Template\Tag::script();
178
179
		/* compare */
180
181
		$this->assertInstanceOf('Redaxscript\Head\Script', $actual);
182
	}
183
184
	/**
185
	 * testStyle
186
	 *
187
	 * @since 3.0.0
188
	 */
189
190
	public function testStyle() : void
191
	{
192
		/* actual */
193
194
		$actual = Template\Tag::style();
195
196
		/* compare */
197
198
		$this->assertInstanceOf('Redaxscript\Head\Style', $actual);
199
	}
200
201
	/**
202
	 * testBreadcrumb
203
	 *
204
	 * @since 2.3.0
205
	 */
206
207
	public function testBreadcrumb() : void
208
	{
209
		/* actual */
210
211
		$actual = Template\Tag::breadcrumb();
212
213
		/* compare */
214
215
		$this->assertIsString($actual);
216
	}
217
218
	/**
219
	 * testPartial
220
	 *
221
	 * @since 2.3.0
222
	 */
223
224
	public function testPartial() : void
225
	{
226
		/* setup */
227
228
		Stream::setup('root');
229
		$file = new StreamFile('partial.phtml');
230
		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...
231
232
		/* actual */
233
234
		$actual = Template\Tag::partial(Stream::url('root' . DIRECTORY_SEPARATOR . 'partial.phtml'));
235
236
		/* compare */
237
238
		$this->assertIsString($actual);
239
	}
240
241
	/**
242
	 * testPaginationArticles
243
	 *
244
	 * @since 4.0.0
245
	 */
246
247
	public function testPaginationArticles() : void
248
	{
249
		/* setup */
250
251
		$settingModel = new Model\Setting();
252
		$settingModel->set('limit', 1);
253
254
		/* actual */
255
256
		$actual = Template\Tag::pagination('articles', 1);
257
258
		/* compare */
259
260
		$this->assertIsString($actual);
261
	}
262
263
	/**
264
	 * testPaginationComments
265
	 *
266
	 * @since 4.0.0
267
	 */
268
269
	public function testPaginationComments() : void
270
	{
271
		/* setup */
272
273
		$settingModel = new Model\Setting();
274
		$settingModel->set('limit', 1);
275
276
		/* actual */
277
278
		$actual = Template\Tag::pagination('comments', 1);
279
280
		/* compare */
281
282
		$this->assertIsString($actual);
283
	}
284
285
	/**
286
	 * testPaginationInvalid
287
	 *
288
	 * @since 4.0.0
289
	 */
290
291
	public function testPaginationInvalid() : void
292
	{
293
		/* actual */
294
295
		$actual = Template\Tag::pagination('invalid', 1);
296
297
		/* compare */
298
299
		$this->assertNull($actual);
300
	}
301
302
	/**
303
	 * testNavigationCategories
304
	 *
305
	 * @since 3.3.1
306
	 */
307
308
	public function testNavigationCategories() : void
309
	{
310
		/* actual */
311
312
		$actual = Template\Tag::navigation('categories');
313
314
		/* compare */
315
316
		$this->assertIsString($actual);
317
	}
318
319
	/**
320
	 * testNavigationArticles
321
	 *
322
	 * @since 3.3.1
323
	 */
324
325
	public function testNavigationArticles() : void
326
	{
327
		/* actual */
328
329
		$actual = Template\Tag::navigation('articles');
330
331
		/* compare */
332
333
		$this->assertIsString($actual);
334
	}
335
336
	/**
337
	 * testNavigationComments
338
	 *
339
	 * @since 3.3.1
340
	 */
341
342
	public function testNavigationComments() : void
343
	{
344
		/* actual */
345
346
		$actual = Template\Tag::navigation('comments');
347
348
		/* compare */
349
350
		$this->assertIsString($actual);
351
	}
352
353
	/**
354
	 * testNavigationLanguages
355
	 *
356
	 * @since 3.3.1
357
	 */
358
359
	public function testNavigationLanguages() : void
360
	{
361
		/* actual */
362
363
		$actual = Template\Tag::navigation('languages');
364
365
		/* compare */
366
367
		$this->assertIsString($actual);
368
	}
369
370
	/**
371
	 * testNavigationTemplates
372
	 *
373
	 * @since 3.3.1
374
	 */
375
376
	public function testNavigationTemplates() : void
377
	{
378
		/* actual */
379
380
		$actual = Template\Tag::navigation('templates');
381
382
		/* compare */
383
384
		$this->assertIsString($actual);
385
	}
386
387
	/**
388
	 * testNavigationTemplates
389
	 *
390
	 * @since 3.3.1
391
	 */
392
393
	public function testNavigationInvalid() : void
394
	{
395
		/* actual */
396
397
		$actual = Template\Tag::navigation('invalid');
398
399
		/* compare */
400
401
		$this->assertNull($actual);
402
	}
403
404
	/**
405
	 * testConsole
406
	 *
407
	 * @since 3.0.0
408
	 */
409
410
	public function testConsole() : void
411
	{
412
		/* setup */
413
414
		$this->_request->setStream('argv', 'help');
415
416
		/* actual */
417
418
		$actual = Template\Tag::console();
419
420
		/* compare */
421
422
		$this->assertIsString($actual);
423
	}
424
425
	/**
426
	 * testConsoleInvalid
427
	 *
428
	 * @since 3.0.0
429
	 */
430
431
	public function testConsoleInvalid() : void
432
	{
433
		/* setup */
434
435
		$this->_request->setStream('argv', 'invalidCommand');
436
437
		/* actual */
438
439
		$actual = Template\Tag::console();
440
441
		/* compare */
442
443
		$this->assertNull($actual);
444
	}
445
446
	/**
447
	 * testConsoleForm
448
	 *
449
	 * @since 3.0.0
450
	 */
451
452
	public function testConsoleForm() : void
453
	{
454
		/* actual */
455
456
		$actual = Template\Tag::consoleForm();
457
458
		/* compare */
459
460
		$this->assertIsString($actual);
461
	}
462
463
	/**
464
	 * testCommentForm
465
	 *
466
	 * @since 4.0.0
467
	 */
468
469
	public function testCommentForm() : void
470
	{
471
		/* actual */
472
473
		$actual = Template\Tag::commentForm(1);
474
475
		/* compare */
476
477
		$this->assertIsString($actual);
478
	}
479
480
	/**
481
	 * testSearchForm
482
	 *
483
	 * @since 3.0.0
484
	 */
485
486
	public function testSearchForm() : void
487
	{
488
		/* actual */
489
490
		$actual = Template\Tag::searchForm();
491
492
		/* compare */
493
494
		$this->assertIsString($actual);
495
	}
496
497
}
498