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

EnqueueWebhooksCommandTest::testExecute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 27
nc 1
nop 0
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);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->getContainer() on line 98 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...
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