Completed
Branch master (95b3ba)
by Ryan
28:02 queued 13:03
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
class HandlerStackTest extends AbstractTestCase
18
{
19
    // public function testBasicAppend()
20
    // {
21
    //     $stack = (new HandlerStack())
22
    //         ->append(new Atom());
23
    // }
24
25
    public function testAppendClosure(): void
26
    {
27
        $stack = (new HandlerStack())
28
            ->append(new Atom())
29
            ->appendClosure(FeedType::ALL, function (): void {
30
            })
31
            ->appendClosure(FeedType::JSON, function (): void {
32
            })
33
            ->appendClosure(FeedType::HTML, function (): void {
34
            })
35
            ->appendClosure(FeedType::XML, function (): void {
36
            });
37
38
        $this->assertSame(2, \count($stack->debugStack()['json']));
39
        $this->assertSame(2, \count($stack->debugStack()['html']));
40
        $this->assertSame(3, \count($stack->debugStack()['xml']));
41
    }
42
43
    public function testOrder(): void
44
    {
45
        $stack = (new HandlerStack())
46
            ->appendClosure(FeedType::ALL, function (): void {
47
            }, 'start')
48
            ->appendClosure(FeedType::JSON, function (): void {
49
            }, 'appendJson')
50
            ->appendClosure(FeedType::HTML, function (): void {
51
            }, 'appendHtml')
52
            ->appendClosure(FeedType::XML, function (): void {
53
            }, 'appendXml')
54
            ->prependClosure(FeedType::JSON, function (): void {
55
            }, 'prependJson')
56
            ->prependClosure(FeedType::HTML, function (): void {
57
            }, 'prependHtml')
58
            ->prependClosure(FeedType::XML, function (): void {
59
            }, 'prependXml');
60
61
        $order = ['prependXml', 'start', 'appendXml'];
62
63
        foreach ($stack->debugStack()['xml'] as $middleware) {
64
            $match = \array_shift($order);
65
            $this->assertSame(1, \preg_match('/' . $match . '/', $middleware));
66
        }
67
    }
68
69
    /**
70
     * @expectedException \SimplePie\Exception\MiddlewareException
71
     * @expectedExceptionMessage The middleware `Closure` could not be assigned to a feed type.
72
     */
73
    public function testMiddlewareException(): void
74
    {
75
        $stack = (new HandlerStack())
76
            ->appendClosure('bogus', function (): void {
77
            });
78
    }
79
}
80