testResponseNot200()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Command;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\TransferException;
7
use JMS\Serializer\SerializerBuilder;
8
use JMS\Serializer\SerializerInterface;
9
use Loevgaard\DandomainAltapayBundle\Command\ConsumeWebhookQueueCommand;
10
use Loevgaard\DandomainAltapayBundle\Entity\WebhookExchange;
11
use Loevgaard\DandomainAltapayBundle\Entity\WebhookExchangeRepository;
12
use Loevgaard\DandomainAltapayBundle\Entity\WebhookQueueItem;
13
use PHPUnit\Framework\TestCase;
14
use Psr\Http\Message\ResponseInterface;
15
use Symfony\Component\Console\Application;
16
use Symfony\Component\Console\Tester\CommandTester;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
class ConsumeWebhookQueueCommandTest extends TestCase
20
{
21
    public function testExecute1()
22
    {
23
        $webhookUrl = 'https://www.example.com';
24
25
        $webhookExchange = new WebhookExchange($webhookUrl);
26
27
        $webhookQueueItemToConsume = new WebhookQueueItem('content', $webhookExchange);
28
29
        $webhookQueueItemToNotConsume1 = new WebhookQueueItem('content', $webhookExchange);
30
        $webhookQueueItemToNotConsume1->setStatus(WebhookQueueItem::STATUS_SUCCESS);
31
32
        $webhookQueueItemToNotConsume2 = new WebhookQueueItem('content', $webhookExchange);
33
        $webhookQueueItemToNotConsume2->setNextTry((new \DateTimeImmutable())->add(new \DateInterval('P1D')));
34
35
        $webhookExchange
36
            ->addWebhookQueueItem($webhookQueueItemToConsume)
37
            ->addWebhookQueueItem($webhookQueueItemToNotConsume1)
38
            ->addWebhookQueueItem($webhookQueueItemToNotConsume2);
39
40
        $webhookExchangeRepository = $this->getWebhookExchangeRepository(['findAll', 'flush']);
41
        $webhookExchangeRepository
42
            ->method('findAll')
43
            ->willReturn([$webhookExchange]);
44
45
        $command = $this->getCommand($webhookExchangeRepository);
46
47
        $commandTester = $this->execute($command);
48
49
        $this->assertTrue($webhookQueueItemToConsume->isStatus(WebhookQueueItem::STATUS_SUCCESS));
50
        $this->assertSame(0, $webhookQueueItemToNotConsume1->getTries());
51
        $this->assertSame(0, $webhookQueueItemToNotConsume2->getTries());
52
        $this->assertSame(0, $commandTester->getStatusCode(), 'Returns 0 on success');
53
    }
54
55 View Code Duplication
    public function testThrowGeneralException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $webhookExchange = new WebhookExchange('https://www.example.com');
58
59
        $webhookQueueItem = new WebhookQueueItem('content', $webhookExchange);
60
61
        $webhookExchange
62
            ->addWebhookQueueItem($webhookQueueItem);
63
64
        $webhookExchangeRepository = $this->getWebhookExchangeRepository(['findAll', 'flush']);
65
        $webhookExchangeRepository
66
            ->method('findAll')
67
            ->willReturn([$webhookExchange]);
68
69
        $command = $this->getCommand($webhookExchangeRepository);
70
71
        $client = $this->createMock(ClientInterface::class);
72
        $client->method('send')->willThrowException(new \Exception());
73
74
        $command->setHttpClient($client);
0 ignored issues
show
Documentation introduced by
$client is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<GuzzleHttp\ClientInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
76
        $commandTester = $this->execute($command);
77
78
        $this->assertTrue($webhookQueueItem->isStatus(WebhookQueueItem::STATUS_ERROR));
79
        $this->assertSame(0, $commandTester->getStatusCode(), 'Returns 0 on success');
80
    }
81
82 View Code Duplication
    public function testThrowTransferException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $webhookExchange = new WebhookExchange('https://www.example.com');
85
86
        $webhookQueueItem = new WebhookQueueItem('content', $webhookExchange);
87
88
        $webhookExchange
89
            ->addWebhookQueueItem($webhookQueueItem);
90
91
        $webhookExchangeRepository = $this->getWebhookExchangeRepository(['findAll', 'flush']);
92
        $webhookExchangeRepository
93
            ->method('findAll')
94
            ->willReturn([$webhookExchange]);
95
96
        $command = $this->getCommand($webhookExchangeRepository);
97
98
        $client = $this->createMock(ClientInterface::class);
99
        $client->method('send')->willThrowException(new TransferException());
100
101
        $command->setHttpClient($client);
0 ignored issues
show
Documentation introduced by
$client is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<GuzzleHttp\ClientInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
103
        $commandTester = $this->execute($command);
104
105
        $this->assertTrue($webhookQueueItem->isStatus(WebhookQueueItem::STATUS_ERROR));
106
        $this->assertSame(0, $commandTester->getStatusCode(), 'Returns 0 on success');
107
    }
108
109
    public function testResponseNot200()
110
    {
111
        $webhookExchange = new WebhookExchange('https://www.example.com');
112
113
        $webhookQueueItem = new WebhookQueueItem('content', $webhookExchange);
114
115
        $webhookExchange
116
            ->addWebhookQueueItem($webhookQueueItem);
117
118
        $webhookExchangeRepository = $this->getWebhookExchangeRepository(['findAll', 'flush']);
119
        $webhookExchangeRepository
120
            ->method('findAll')
121
            ->willReturn([$webhookExchange]);
122
123
        $command = $this->getCommand($webhookExchangeRepository);
124
125
        $response = $this->createMock(ResponseInterface::class);
126
        $response
127
            ->method('getStatusCode')
128
            ->willReturn(400);
129
130
        $response
131
            ->method('getHeaders')
132
            ->willReturn([]);
133
134
        $client = $this->createMock(ClientInterface::class);
135
        $client->method('send')->willReturn($response);
136
137
        $command->setHttpClient($client);
0 ignored issues
show
Documentation introduced by
$client is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<GuzzleHttp\ClientInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
139
        $commandTester = $this->execute($command);
140
141
        $this->assertTrue($webhookQueueItem->isStatus(WebhookQueueItem::STATUS_ERROR));
142
        $this->assertSame(0, $commandTester->getStatusCode(), 'Returns 0 on success');
143
    }
144
145
    private function execute(ConsumeWebhookQueueCommand $command)
146
    {
147
        $application = new Application();
148
        $application->setAutoExit(false);
149
        $application->add($command);
150
151
        $command = $application->find('loevgaard:dandomain:altapay:consume-webhook-queue');
152
        $commandTester = new CommandTester($command);
153
        $commandTester->execute([
154
            'command' => $command->getName(),
155
        ]);
156
157
        return $commandTester;
158
    }
159
160
    /**
161
     * @param null $webhookExchangeRepository
162
     * @param null $serializer
163
     * @param null $container
164
     *
165
     * @return ConsumeWebhookQueueCommand
166
     */
167
    private function getCommand($webhookExchangeRepository = null, $serializer = null, $container = null): ConsumeWebhookQueueCommand
168
    {
169
        if (!$webhookExchangeRepository) {
170
            $webhookExchangeRepository = $this->getWebhookExchangeRepository();
171
        }
172
173
        if (!$serializer) {
174
            $serializer = $this->getSerializer();
175
        }
176
177
        if (!$container) {
178
            $container = $this->getContainer();
179
        }
180
181
        $command = new ConsumeWebhookQueueCommand($webhookExchangeRepository, $serializer);
182
        $command->setContainer($container);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->getContainer() on line 178 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Symfony\Bundle\Framework...Command::setContainer() does only seem to accept null|object<Symfony\Comp...ion\ContainerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
183
184
        return $command;
185
    }
186
187
    /**
188
     * @param array $methods
189
     *
190
     * @return WebhookExchangeRepository|\PHPUnit_Framework_MockObject_MockObject
191
     */
192
    private function getWebhookExchangeRepository(array $methods = [])
193
    {
194
        $mockBuilder = $this->getMockBuilder(WebhookExchangeRepository::class)->disableOriginalConstructor();
195
196
        if (!empty($methods)) {
197
            $mockBuilder->setMethods($methods);
198
        }
199
200
        return $mockBuilder->getMock();
201
    }
202
203
    private function getSerializer(): SerializerInterface
204
    {
205
        return SerializerBuilder::create()->build();
206
    }
207
208
    /**
209
     * @return \PHPUnit_Framework_MockObject_MockObject|ContainerInterface
210
     */
211
    private function getContainer()
212
    {
213
        return $this->createMock(ContainerInterface::class);
214
    }
215
}
216