Completed
Branch master (910c19)
by Dmitri
01:43
created

LookupKeyStorageCommandTest::it_finds_key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 24
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\Tests\Command\Storage;
6
7
use Damax\Bundle\ApiAuthBundle\Command\Storage\LookupKeyCommand;
8
use Damax\Bundle\ApiAuthBundle\Key\Key;
9
use Damax\Bundle\ApiAuthBundle\Key\Storage\KeyNotFound;
10
use Damax\Bundle\ApiAuthBundle\Key\Storage\Storage;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use Symfony\Component\Console\Command\Command;
13
14
/**
15
 * @group integration
16
 * @group console
17
 */
18
class LookupKeyStorageCommandTest extends StorageCommandTestCase
19
{
20
    /**
21
     * @var Storage|MockObject
22
     */
23
    private $storage;
24
25
    protected function createCommand(): Command
26
    {
27
        $this->storage = $this->createMock(Storage::class);
28
29
        return new LookupKeyCommand($this->storage);
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function it_finds_key()
36
    {
37
        $this->storage
38
            ->expects($this->once())
39
            ->method('get')
40
            ->with('XYZ')
41
            ->willReturn(new Key('XYZ', 'john.doe', 60))
42
        ;
43
44
        $code = $this->tester->execute(['command' => 'damax:api-auth:storage:lookup-key', 'key' => 'XYZ']);
45
46
        $output = <<<CONSOLE
47
48
 ---------- ---------- 
49
  Key        XYZ       
50
  Identity   john.doe  
51
  TTL        60        
52
 ---------- ---------- 
53
54
55
CONSOLE;
56
57
        $this->assertSame(0, $code);
58
        $this->assertEquals($output, $this->tester->getDisplay());
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function it_fails_to_find_key()
65
    {
66
        $this->storage
67
            ->expects($this->once())
68
            ->method('get')
69
            ->with('XYZ')
70
            ->willThrowException(new KeyNotFound())
71
        ;
72
73
        $code = $this->tester->execute(['command' => 'damax:api-auth:storage:lookup-key', 'key' => 'XYZ']);
74
75
        $this->assertSame(1, $code);
76
        $this->assertEquals('[ERROR] Key not found.', trim($this->tester->getDisplay()));
77
    }
78
}
79