Passed
Pull Request — master (#71)
by Matthieu
05:14 queued 01:08
created

RequestApiCommandTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AtlassianConnectBundle\Tests\Command;
6
7
use AtlassianConnectBundle\Command\RequestAPICommand;
8
use AtlassianConnectBundle\Entity\TenantInterface;
9
use AtlassianConnectBundle\Repository\TenantRepositoryInterface;
10
use AtlassianConnectBundle\Service\AtlassianRestClientInterface;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\Console\Application;
13
use Symfony\Component\Console\Tester\CommandTester;
14
15
final class RequestApiCommandTest extends TestCase
16
{
17
    /** @var TenantRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject */
18
    private $tenantRepository;
19
20
    /** @var AtlassianRestClientInterface|\PHPUnit\Framework\MockObject\MockObject */
21
    private $restClient;
22
23
    private CommandTester $commandTester;
24
25
    protected function setUp(): void
26
    {
27
        $this->tenantRepository = $this->createMock(TenantRepositoryInterface::class);
28
        $this->restClient = $this->createMock(AtlassianRestClientInterface::class);
29
        $application = new Application();
30
        $application->add(new RequestAPICommand($this->tenantRepository, $this->restClient));
31
        $this->commandTester = new CommandTester($application->find('ac:request-api'));
32
    }
33
34
    public function testExecute(): void
35
    {
36
        $this->tenantRepository->expects($this->once())
37
            ->method('findById')
38
            ->with('1')
39
            ->willReturn($this->createMock(TenantInterface::class));
40
41
        $this->restClient
42
            ->expects($this->once())
43
            ->method('get')
44
            ->with('/resource')
45
            ->willReturn('{"message": "ok"}');
46
47
        $this->commandTester->execute(['rest-url' => '/resource', '--tenant-id' => '1'], []);
48
        $this->assertStringContainsString('{"message": "ok"}', $this->commandTester->getDisplay());
49
    }
50
51
    public function testByClientKey(): void
52
    {
53
        $this->tenantRepository->expects($this->once())
54
            ->method('findByClientKey')
55
            ->with('key')
56
            ->willReturn($this->createMock(TenantInterface::class));
57
58
        $this->restClient
59
            ->expects($this->once())
60
            ->method('get')
61
            ->with('/resource')
62
            ->willReturn('{"message": "ok"}');
63
64
        $this->commandTester->execute(['rest-url' => '/resource', '--client-key' => 'key'], []);
65
        $this->assertStringContainsString('{"message": "ok"}', $this->commandTester->getDisplay());
66
    }
67
68
    public function testFails(): void
69
    {
70
        $this->expectException(\RuntimeException::class);
71
        $this->commandTester->execute(['rest-url' => '/resource'], []);
72
    }
73
}
74