Passed
Push — master ( bf1af5...e52ce1 )
by Ryan
14:07
created

HandlerStackTest::testOrder()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2017 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie\Test\Unit;
12
13
use SimplePie\Enum\FeedType;
14
use SimplePie\HandlerStack;
15
use SimplePie\Middleware\Xml\Atom;
16
17
/**
18
 * @coversNothing
19
 */
20
class HandlerStackTest extends AbstractTestCase
21
{
22
    // public function testBasicAppend()
23
    // {
24
    //     $stack = (new HandlerStack())
25
    //         ->append(new Atom());
26
    // }
27
28
    public function testAppendClosure(): void
29
    {
30
        $stack = (new HandlerStack())
31
            ->append(new Atom())
32
            ->appendClosure(FeedType::ALL, static function (): void {
33
            })
34
            ->appendClosure(FeedType::JSON, static function (): void {
35
            })
36
            ->appendClosure(FeedType::HTML, static function (): void {
37
            })
38
            ->appendClosure(FeedType::XML, static function (): void {
39
            });
40
41
        $this->assertSame(2, \count($stack->debugStack()['json']));
42
        $this->assertSame(2, \count($stack->debugStack()['html']));
43
        $this->assertSame(3, \count($stack->debugStack()['xml']));
44
    }
45
46
    public function testOrder(): void
47
    {
48
        $stack = (new HandlerStack())
49
            ->appendClosure(FeedType::ALL, static function (): void {
50
            }, 'start')
51
            ->appendClosure(FeedType::JSON, static function (): void {
52
            }, 'appendJson')
53
            ->appendClosure(FeedType::HTML, static function (): void {
54
            }, 'appendHtml')
55
            ->appendClosure(FeedType::XML, static function (): void {
56
            }, 'appendXml')
57
            ->prependClosure(FeedType::JSON, static function (): void {
58
            }, 'prependJson')
59
            ->prependClosure(FeedType::HTML, static function (): void {
60
            }, 'prependHtml')
61
            ->prependClosure(FeedType::XML, static function (): void {
62
            }, 'prependXml');
63
64
        $order = ['prependXml', 'start', 'appendXml'];
65
66
        foreach ($stack->debugStack()['xml'] as $middleware) {
67
            $match = \array_shift($order);
68
            $this->assertSame(1, \preg_match('/' . $match . '/', $middleware));
69
        }
70
    }
71
72
    public function testMiddlewareException(): void
73
    {
74
        $this->expectException(\SimplePie\Exception\MiddlewareException::class);
75
        $this->expectExceptionMessage('The middleware `Closure` could not be assigned to a feed type.');
76
77
        $stack = (new HandlerStack())
78
            ->appendClosure('bogus', static function (): void {
79
            });
80
    }
81
}
82