Completed
Push — master ( 2604a0...14e09a )
by Joachim
02:14
created

ConsumeWebhookQueueCommandTest::testExecute1()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Tests\Command;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Exception\TransferException;
8
use JMS\Serializer\SerializerBuilder;
9
use JMS\Serializer\SerializerInterface;
10
use Loevgaard\DandomainAltapayBundle\Command\ConsumeWebhookQueueCommand;
11
use Loevgaard\DandomainAltapayBundle\Entity\WebhookExchange;
12
use Loevgaard\DandomainAltapayBundle\Entity\WebhookExchangeRepository;
13
use Loevgaard\DandomainAltapayBundle\Entity\WebhookQueueItem;
14
use PHPUnit\Framework\TestCase;
15
use Psr\Http\Message\ResponseInterface;
16
use Symfony\Component\Console\Application;
17
use Symfony\Component\Console\Tester\CommandTester;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
class ConsumeWebhookQueueCommandTest extends TestCase
21
{
22
    public function testExecute1()
23
    {
24
        $webhookUrl = 'https://www.example.com';
25
26
        $webhookExchange = new WebhookExchange($webhookUrl);
27
28
        $webhookQueueItemToConsume = new WebhookQueueItem('content', $webhookExchange);
29
30
        $webhookQueueItemToNotConsume1 = new WebhookQueueItem('content', $webhookExchange);
31
        $webhookQueueItemToNotConsume1->setStatus(WebhookQueueItem::STATUS_SUCCESS);
32
33
        $webhookQueueItemToNotConsume2 = new WebhookQueueItem('content', $webhookExchange);
34
        $webhookQueueItemToNotConsume2->setNextTry((new \DateTimeImmutable())->add(new \DateInterval('P1D')));
35
36
        $webhookExchange
37
            ->addWebhookQueueItem($webhookQueueItemToConsume)
38
            ->addWebhookQueueItem($webhookQueueItemToNotConsume1)
39
            ->addWebhookQueueItem($webhookQueueItemToNotConsume2);
40
41
        $webhookExchangeRepository = $this->getWebhookExchangeRepository(['findAll', 'flush']);
42
        $webhookExchangeRepository
43
            ->method('findAll')
44
            ->willReturn([$webhookExchange]);
45
46
        $command = $this->getCommand($webhookExchangeRepository);
47
48
        $commandTester = $this->execute($command);
49
50
        $this->assertTrue($webhookQueueItemToConsume->isStatus(WebhookQueueItem::STATUS_SUCCESS));
51
        $this->assertSame(0, $webhookQueueItemToNotConsume1->getTries());
52
        $this->assertSame(0, $webhookQueueItemToNotConsume2->getTries());
53
        $this->assertSame(0, $commandTester->getStatusCode(), 'Returns 0 on success');
54
    }
55
56 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...
57
    {
58
        $webhookExchange = new WebhookExchange('https://www.example.com');
59
60
        $webhookQueueItem = new WebhookQueueItem('content', $webhookExchange);
61
62
        $webhookExchange
63
            ->addWebhookQueueItem($webhookQueueItem);
64
65
        $webhookExchangeRepository = $this->getWebhookExchangeRepository(['findAll', 'flush']);
66
        $webhookExchangeRepository
67
            ->method('findAll')
68
            ->willReturn([$webhookExchange]);
69
70
        $command = $this->getCommand($webhookExchangeRepository);
71
72
        $client = $this->createMock(ClientInterface::class);
73
        $client->method('send')->willThrowException(new \Exception());
74
75
        $command->setHttpClient($client);
76
77
        $commandTester = $this->execute($command);
78
79
        $this->assertTrue($webhookQueueItem->isStatus(WebhookQueueItem::STATUS_ERROR));
80
        $this->assertSame(0, $commandTester->getStatusCode(), 'Returns 0 on success');
81
    }
82
83 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...
84
    {
85
        $webhookExchange = new WebhookExchange('https://www.example.com');
86
87
        $webhookQueueItem = new WebhookQueueItem('content', $webhookExchange);
88
89
        $webhookExchange
90
            ->addWebhookQueueItem($webhookQueueItem);
91
92
        $webhookExchangeRepository = $this->getWebhookExchangeRepository(['findAll', 'flush']);
93
        $webhookExchangeRepository
94
            ->method('findAll')
95
            ->willReturn([$webhookExchange]);
96
97
        $command = $this->getCommand($webhookExchangeRepository);
98
99
        $client = $this->createMock(ClientInterface::class);
100
        $client->method('send')->willThrowException(new TransferException());
101
102
        $command->setHttpClient($client);
103
104
        $commandTester = $this->execute($command);
105
106
        $this->assertTrue($webhookQueueItem->isStatus(WebhookQueueItem::STATUS_ERROR));
107
        $this->assertSame(0, $commandTester->getStatusCode(), 'Returns 0 on success');
108
    }
109
110
    public function testResponseNot200()
111
    {
112
        $webhookExchange = new WebhookExchange('https://www.example.com');
113
114
        $webhookQueueItem = new WebhookQueueItem('content', $webhookExchange);
115
116
        $webhookExchange
117
            ->addWebhookQueueItem($webhookQueueItem);
118
119
        $webhookExchangeRepository = $this->getWebhookExchangeRepository(['findAll', 'flush']);
120
        $webhookExchangeRepository
121
            ->method('findAll')
122
            ->willReturn([$webhookExchange]);
123
124
        $command = $this->getCommand($webhookExchangeRepository);
125
126
        $response = $this->createMock(ResponseInterface::class);
127
        $response
128
            ->method('getStatusCode')
129
            ->willReturn(400);
130
131
        $response
132
            ->method('getHeaders')
133
            ->willReturn([]);
134
135
        $client = $this->createMock(ClientInterface::class);
136
        $client->method('send')->willReturn($response);
137
138
        $command->setHttpClient($client);
139
140
        $commandTester = $this->execute($command);
141
142
        $this->assertTrue($webhookQueueItem->isStatus(WebhookQueueItem::STATUS_ERROR));
143
        $this->assertSame(0, $commandTester->getStatusCode(), 'Returns 0 on success');
144
    }
145
146
    private function execute(ConsumeWebhookQueueCommand $command)
147
    {
148
        $application = new Application();
149
        $application->setAutoExit(false);
150
        $application->add($command);
151
152
        $command = $application->find('loevgaard:dandomain:altapay:consume-webhook-queue');
153
        $commandTester = new CommandTester($command);
154
        $commandTester->execute([
155
            'command' => $command->getName(),
156
        ]);
157
158
        return $commandTester;
159
    }
160
161
    /**
162
     * @param null $webhookExchangeRepository
163
     * @param null $serializer
164
     * @param null $container
165
     *
166
     * @return ConsumeWebhookQueueCommand
167
     */
168
    private function getCommand($webhookExchangeRepository = null, $serializer = null, $container = null): ConsumeWebhookQueueCommand
169
    {
170
        if (!$webhookExchangeRepository) {
171
            $webhookExchangeRepository = $this->getWebhookExchangeRepository();
172
        }
173
174
        if (!$serializer) {
175
            $serializer = $this->getSerializer();
176
        }
177
178
        if (!$container) {
179
            $container = $this->getContainer();
180
        }
181
182
        $command = new ConsumeWebhookQueueCommand($webhookExchangeRepository, $serializer);
183
        $command->setContainer($container);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->getContainer() on line 179 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...
184
185
        return $command;
186
    }
187
188
    /**
189
     * @param array $methods
190
     *
191
     * @return WebhookExchangeRepository|\PHPUnit_Framework_MockObject_MockObject
192
     */
193
    private function getWebhookExchangeRepository(array $methods = [])
194
    {
195
        $mockBuilder = $this->getMockBuilder(WebhookExchangeRepository::class)->disableOriginalConstructor();
196
197
        if (!empty($methods)) {
198
            $mockBuilder->setMethods($methods);
199
        }
200
201
        return $mockBuilder->getMock();
202
    }
203
204
    private function getSerializer(): SerializerInterface
205
    {
206
        return SerializerBuilder::create()->build();
207
    }
208
209
    /**
210
     * @return \PHPUnit_Framework_MockObject_MockObject|ContainerInterface
211
     */
212
    private function getContainer()
213
    {
214
        return $this->createMock(ContainerInterface::class);
215
    }
216
}
217