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

ChainExtractorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\Tests\Extractor;
6
7
use Damax\Bundle\ApiAuthBundle\Extractor\ChainExtractor;
8
use Damax\Bundle\ApiAuthBundle\Extractor\Extractor;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\HttpFoundation\Request;
12
13
class ChainExtractorTest extends TestCase
14
{
15
    /**
16
     * @var Extractor|MockObject
17
     */
18
    private $extractor1;
19
20
    /**
21
     * @var Extractor|MockObject
22
     */
23
    private $extractor2;
24
25
    /**
26
     * @var ChainExtractor
27
     */
28
    private $extractor;
29
30
    protected function setUp()
31
    {
32
        $this->extractor1 = $this->createMock(Extractor::class);
33
        $this->extractor2 = $this->createMock(Extractor::class);
34
        $this->extractor = new ChainExtractor([$this->extractor1, $this->extractor2]);
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function it_contains_internal_extractors()
41
    {
42
        $this->assertAttributeCount(2, 'extractors', $this->extractor);
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function it_extracts_key_from_first_extractor()
49
    {
50
        $request = new Request();
51
52
        $this->extractor1
53
            ->expects($this->once())
54
            ->method('extractKey')
55
            ->with($this->identicalTo($request))
56
            ->willReturn('XYZ')
57
        ;
58
        $this->extractor2
59
            ->expects($this->never())
60
            ->method('extractKey')
61
        ;
62
63
        $this->assertEquals('XYZ', $this->extractor->extractKey($request));
64
    }
65
66
    /**
67
     * @test
68
     */
69
    public function it_extracts_key_from_second_extractor()
70
    {
71
        $request = new Request();
72
73
        $this->extractor1
74
            ->expects($this->once())
75
            ->method('extractKey')
76
            ->with($this->identicalTo($request))
77
78
        ;
79
        $this->extractor2
80
            ->expects($this->once())
81
            ->method('extractKey')
82
            ->with($this->identicalTo($request))
83
            ->willReturn('XYZ')
84
        ;
85
86
        $this->assertEquals('XYZ', $this->extractor->extractKey($request));
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function it_has_no_key_to_extract()
93
    {
94
        $request = new Request();
95
96
        $this->extractor1
97
            ->expects($this->once())
98
            ->method('extractKey')
99
            ->with($this->identicalTo($request))
100
101
        ;
102
        $this->extractor2
103
            ->expects($this->once())
104
            ->method('extractKey')
105
            ->with($this->identicalTo($request))
106
        ;
107
108
        $this->assertNull($this->extractor->extractKey($request));
109
    }
110
}
111