1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\SerializerBuilder; |
6
|
|
|
use JMS\Serializer\SerializerInterface; |
7
|
|
|
use Loevgaard\DandomainAltapayBundle\Command\EnqueueWebhooksCommand; |
8
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\Event; |
9
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\EventRepository; |
10
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\WebhookExchange; |
11
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\WebhookExchangeRepository; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\Console\Application; |
14
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
16
|
|
|
|
17
|
|
|
class EnqueueWebhooksCommandTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function testNoWebhookUrls() |
20
|
|
|
{ |
21
|
|
|
$command = $this->getCommand(); |
22
|
|
|
|
23
|
|
|
$application = new Application(); |
24
|
|
|
$application->setAutoExit(false); |
25
|
|
|
$application->add($command); |
26
|
|
|
|
27
|
|
|
$command = $application->find('loevgaard:dandomain:altapay:enqueue-webhooks'); |
28
|
|
|
$commandTester = new CommandTester($command); |
29
|
|
|
$exitCode = $commandTester->execute([ |
30
|
|
|
'command' => $command->getName(), |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
$this->assertSame('No webhook URLs defined', trim($commandTester->getDisplay())); |
34
|
|
|
$this->assertSame(0, $exitCode, 'Returns 0 if there are not webhook URLs'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testExecute() |
38
|
|
|
{ |
39
|
|
|
$webhookUrl = 'https://www.example.com'; |
40
|
|
|
|
41
|
|
|
$container = $this->getContainer(); |
42
|
|
|
$container |
43
|
|
|
->method('getParameter') |
44
|
|
|
->with('loevgaard_dandomain_altapay.webhook_urls') |
45
|
|
|
->willReturn([$webhookUrl]); |
46
|
|
|
|
47
|
|
|
$webhookExchangeRepository = $this->getWebhookExchangeRepository(); |
48
|
|
|
$webhookExchangeRepository |
49
|
|
|
->method('findByUrlOrCreate') |
50
|
|
|
->with($webhookUrl) |
51
|
|
|
->willReturn(new WebhookExchange($webhookUrl)); |
52
|
|
|
|
53
|
|
|
$event = new Event('event', 'event_body'); |
54
|
|
|
$event->setId(1); |
55
|
|
|
$eventRepository = $this->getEventRepository(); |
56
|
|
|
$eventRepository |
57
|
|
|
->method('findRecentEvents') |
58
|
|
|
->willReturn([$event]); |
59
|
|
|
|
60
|
|
|
$command = $this->getCommand($webhookExchangeRepository, $eventRepository, null, $container); |
61
|
|
|
|
62
|
|
|
$application = new Application(); |
63
|
|
|
$application->setAutoExit(false); |
64
|
|
|
$application->add($command); |
65
|
|
|
|
66
|
|
|
$command = $application->find('loevgaard:dandomain:altapay:enqueue-webhooks'); |
67
|
|
|
$commandTester = new CommandTester($command); |
68
|
|
|
$exitCode = $commandTester->execute([ |
69
|
|
|
'command' => $command->getName(), |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$this->assertSame(0, $exitCode, 'Returns 0 on success'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param null $webhookExchangeRepository |
77
|
|
|
* @param null $eventRepository |
78
|
|
|
* @param null $serializer |
79
|
|
|
* @param null $container |
80
|
|
|
* |
81
|
|
|
* @return EnqueueWebhooksCommand |
82
|
|
|
*/ |
83
|
|
|
private function getCommand($webhookExchangeRepository = null, $eventRepository = null, $serializer = null, $container = null) |
84
|
|
|
{ |
85
|
|
|
if (!$webhookExchangeRepository) { |
86
|
|
|
$webhookExchangeRepository = $this->getWebhookExchangeRepository(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!$eventRepository) { |
90
|
|
|
$eventRepository = $this->getEventRepository(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!$serializer) { |
94
|
|
|
$serializer = $this->getSerializer(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!$container) { |
98
|
|
|
$container = $this->getContainer(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$command = new EnqueueWebhooksCommand($webhookExchangeRepository, $eventRepository, $serializer); |
102
|
|
|
$command->setContainer($container); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return $command; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return WebhookExchangeRepository|\PHPUnit_Framework_MockObject_MockObject |
109
|
|
|
*/ |
110
|
|
|
private function getWebhookExchangeRepository() |
111
|
|
|
{ |
112
|
|
|
return $this->createMock(WebhookExchangeRepository::class); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return EventRepository|\PHPUnit_Framework_MockObject_MockObject |
117
|
|
|
*/ |
118
|
|
|
private function getEventRepository() |
119
|
|
|
{ |
120
|
|
|
return $this->createMock(EventRepository::class); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function getSerializer(): SerializerInterface |
124
|
|
|
{ |
125
|
|
|
return SerializerBuilder::create()->build(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ContainerInterface |
130
|
|
|
*/ |
131
|
|
|
private function getContainer() |
132
|
|
|
{ |
133
|
|
|
return $this->createMock(ContainerInterface::class); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.