Completed
Push — master ( b2c31d...142309 )
by Vitaliy
01:49
created

HttpProtocolChainVisitorTest::shouldNotCallNextCheckedIfPreviouslyCheckWithError()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the AppleApnPush package
5
 *
6
 * (c) Vitaliy Zhuk <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code
10
 */
11
12
namespace Tests\Apple\ApnPush\Protocol\Http\Visitor;
13
14
use Apple\ApnPush\Model\Notification;
15
use Apple\ApnPush\Model\Payload;
16
use Apple\ApnPush\Protocol\Http\Request;
17
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolChainVisitor;
18
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolVisitorInterface;
19
use PHPUnit\Framework\TestCase;
20
21
class HttpProtocolChainVisitorTest extends TestCase
22
{
23
    /**
24
     * @test
25
     */
26
    public function shouldSuccessCallsWithPriority()
27
    {
28
        $notification = self::createMock(Notification::class);
29
        $request = self::createMock(Request::class);
30
31
        $visitor1 = $this->createVisitor();
32
        $visitor2 = $this->createVisitor();
33
        $visitor3 = $this->createVisitor();
34
35
        $calls = [];
36
37
        $visitor3->expects(self::exactly(2))
38
            ->method('visit')
39
            ->with($notification, $request)
40
            ->willReturnCallback(function () use (&$calls, $request) {
41
                $calls[] = 3;
42
43
                return $request;
44
            });
45
46
        $visitor1->expects(self::exactly(2))
47
            ->method('visit')
48
            ->with($notification, $request)
49
            ->willReturnCallback(function () use (&$calls, $request) {
50
                $calls[] = 1;
51
52
                return $request;
53
            });
54
55
        $visitor2->expects(self::exactly(2))
56
            ->method('visit')
57
            ->with($notification, $request)
58
            ->willReturnCallback(function () use (&$calls, $request) {
59
                $calls[] = 2;
60
61
                return $request;
62
            });
63
64
        $chain = new HttpProtocolChainVisitor();
65
        $chain->add($visitor3, -1);
66
        $chain->add($visitor1, 0);
67
        $chain->add($visitor2, 1);
68
69
        $chain->visit($notification, $request);
70
        // Call to second iteration
71
        $chain->visit($notification, $request);
72
73
        self::assertEquals([
74
            2, 1, 3,
75
            2, 1, 3,
76
        ], $calls);
77
    }
78
79
    /**
80
     * @test
81
     *
82
     * @expectedException \InvalidArgumentException
83
     */
84
    public function shouldNotCallNextCheckedIfPreviouslyCheckWithError()
85
    {
86
        $notification = self::createMock(Notification::class);
87
        $request = self::createMock(Request::class);
88
89
        $visitor1 = $this->createVisitor();
90
        $visitor2 = $this->createVisitor();
91
        $visitor3 = $this->createVisitor();
92
93
        $visitor1->expects(self::once())
94
            ->method('visit')
95
            ->with($notification, $request)
96
            ->willThrowException(new \InvalidArgumentException());
97
98
        $visitor2->expects(self::never())
99
            ->method('visit');
100
101
        $visitor3->expects(self::never())
102
            ->method('visit');
103
104
        $chain = new HttpProtocolChainVisitor();
105
        $chain->add($visitor1, 3);
106
        $chain->add($visitor2, 2);
107
        $chain->add($visitor3, 1);
108
109
        $chain->visit($notification, $request);
110
    }
111
112
    /**
113
     * Create visitor mock
114
     *
115
     * @return HttpProtocolVisitorInterface|\PHPUnit_Framework_MockObject_MockObject
116
     */
117
    private function createVisitor()
118
    {
119
        $className = sprintf(
120
            'HttpProtocolVisitorInterface_%s',
121
            md5(uniqid(random_int(0, 9999), true))
122
        );
123
124
        $visitor = $this->getMockBuilder(HttpProtocolVisitorInterface::class)
125
            ->setMockClassName($className);
126
127
        return $visitor->getMock();
128
    }
129
}
130