testResolveManyMixedResponses()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2017 American Express Travel Related Services Company, Inc.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15
 * or implied. See the License for the specific language governing
16
 * permissions and limitations under the License.
17
 */
18
19
declare(strict_types=1);
20
21
namespace AmericanExpressTest\HyperledgerFabricClient\Peer;
22
23
use AmericanExpress\HyperledgerFabricClient\Peer\UnaryCallResolver;
24
use AmericanExpress\HyperledgerFabricClient\Proposal\Response;
25
use AmericanExpress\HyperledgerFabricClient\Proposal\ResponseCollection;
26
use Grpc\UnaryCall;
27
use Hyperledger\Fabric\Protos\Peer\ProposalResponse;
28
use PHPUnit\Framework\TestCase;
29
30
/**
31
 * @covers \AmericanExpress\HyperledgerFabricClient\Peer\UnaryCallResolver
32
 */
33
class UnaryCallResolverTest extends TestCase
34
{
35
    /**
36
     * @var UnaryCall|\PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private $unaryCall;
39
40
    /**
41
     * @var UnaryCallResolver
42
     */
43
    private $sut;
44
45
    protected function setUp()
46
    {
47
        $this->unaryCall = $this->getMockBuilder(UnaryCall::class)
48
            ->disableOriginalConstructor()
49
            ->getMock();
50
51
        $this->sut = new UnaryCallResolver();
52
    }
53
54
    public function testResolveOneProposalResponse()
55
    {
56
        $this->unaryCall->method('wait')
0 ignored issues
show
Bug introduced by
The method method() does not exist on Grpc\UnaryCall. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        $this->unaryCall->/** @scrutinizer ignore-call */ 
57
                          method('wait')

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...
57
            ->willReturn([
58
                $proposalResponse = new ProposalResponse(),
0 ignored issues
show
Unused Code introduced by
The assignment to $proposalResponse is dead and can be removed.
Loading history...
59
                [ 'code' => 0 ]
60
            ]);
61
62
        $response = $this->sut->resolveOne($this->unaryCall);
63
64
        self::assertInstanceOf(Response::class, $response);
65
        self::assertFalse($response->isException());
66
    }
67
68
    public function testResolveOneException()
69
    {
70
        $this->unaryCall->method('wait')
71
            ->willReturn([
72
                null,
73
                [
74
                    'code' => 14,
75
                    'details' => 'Connect failed',
76
                    'metadata' => [],
77
                ],
78
            ]);
79
80
        $response = $this->sut->resolveOne($this->unaryCall);
81
82
        self::assertInstanceOf(Response::class, $response);
83
        self::assertTrue($response->isException());
84
    }
85
86
    public function testResolveManyProposalResponses()
87
    {
88
        $this->unaryCall->method('wait')
89
            ->willReturn([
90
                new ProposalResponse(),
91
                [ 'code' => 0 ]
92
            ]);
93
94
        $responses = $this->sut->resolveMany($this->unaryCall, $this->unaryCall);
95
96
        self::assertInstanceOf(ResponseCollection::class, $responses);
97
        self::assertTrue($responses->hasProposalResponses());
98
        self::assertCount(2, $responses->getProposalResponses());
99
        self::assertFalse($responses->hasExceptions());
100
        self::assertCount(0, $responses->getExceptions());
101
    }
102
103
    public function testResolveManyExceptions()
104
    {
105
        $this->unaryCall->method('wait')
106
            ->willReturn([
107
                null,
108
                [
109
                    'code' => 14,
110
                    'details' => 'Connect failed',
111
                    'metadata' => [],
112
                ],
113
            ]);
114
115
        $responses = $this->sut->resolveMany($this->unaryCall, $this->unaryCall);
116
117
        self::assertInstanceOf(ResponseCollection::class, $responses);
118
        self::assertFalse($responses->hasProposalResponses());
119
        self::assertCount(0, $responses->getProposalResponses());
120
        self::assertTrue($responses->hasExceptions());
121
        self::assertCount(2, $responses->getExceptions());
122
    }
123
124
    public function testResolveManyMixedResponses()
125
    {
126
        $this->unaryCall->method('wait')
127
            ->willReturnOnConsecutiveCalls(
128
                [
129
                    new ProposalResponse(),
130
                    [ 'code' => 0 ]
131
                ],
132
                [
133
                    null,
134
                    [
135
                        'code' => 14,
136
                        'details' => 'Connect failed',
137
                        'metadata' => [],
138
                    ],
139
                ]
140
            );
141
142
        $responses = $this->sut->resolveMany($this->unaryCall, $this->unaryCall);
143
144
        self::assertInstanceOf(ResponseCollection::class, $responses);
145
        self::assertTrue($responses->hasProposalResponses());
146
        self::assertCount(1, $responses->getProposalResponses());
147
        self::assertTrue($responses->hasExceptions());
148
        self::assertCount(1, $responses->getExceptions());
149
    }
150
}
151