CloudFrontTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testLegacyCloudFront() 0 20 1
A testLegacyException() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Tests\CDN;
15
16
use Aws\CloudFront\CloudFrontClient;
17
use PHPUnit\Framework\TestCase;
18
use Sonata\MediaBundle\CDN\CloudFront;
19
20
/**
21
 * @author Javier Spagnoletti <[email protected]>
22
 */
23
class CloudFrontTest extends TestCase
24
{
25
    /**
26
     * @group legacy
27
     */
28
    public function testLegacyCloudFront(): void
29
    {
30
        $client = $this->createMock(CloudFrontClientSpy::class);
31
32
        $client->expects($this->exactly(3))->method('createInvalidation')->willReturn(new CloudFrontResultSpy());
33
34
        $cloudFront = $this->getMockBuilder(CloudFront::class)
35
            ->setConstructorArgs(['/foo', 'secret', 'key', 'xxxxxxxxxxxxxx'])
36
            ->onlyMethods([])
37
            ->getMock();
38
        $cloudFront->setClient($client);
39
40
        $this->assertSame('/foo/bar.jpg', $cloudFront->getPath('bar.jpg', true));
41
42
        $path = '/mypath/file.jpg';
43
44
        $cloudFront->flushByString($path);
45
        $cloudFront->flush($path);
46
        $cloudFront->flushPaths([$path]);
47
    }
48
49
    /**
50
     * @group legacy
51
     */
52
    public function testLegacyException(): void
53
    {
54
        $this->expectException(\RuntimeException::class);
55
        $this->expectExceptionMessage('Unable to flush : ');
56
57
        $client = $this->createMock(CloudFrontClientSpy::class);
58
        $client->expects($this->once())->method('createInvalidation')->willReturn(new CloudFrontResultSpy(true));
59
        $cloudFront = $this->getMockBuilder(CloudFront::class)
60
            ->setConstructorArgs(['/foo', 'secret', 'key', 'xxxxxxxxxxxxxx'])
61
            ->onlyMethods([])
62
            ->getMock();
63
        $cloudFront->setClient($client);
64
        $cloudFront->flushPaths(['boom']);
65
    }
66
}
67
68
class CloudFrontClientSpy extends CloudFrontClient
69
{
70
    public function createInvalidation(): CloudFrontResultSpy
71
    {
72
        return new CloudFrontResultSpy();
73
    }
74
}
75
76
final class CloudFrontResultSpy
77
{
78
    private $fail;
79
80
    public function __construct(bool $fail = false)
81
    {
82
        $this->fail = $fail;
83
    }
84
85
    public function get($data): string
86
    {
87
        if ('Status' !== $data || $this->fail) {
88
            return '';
89
        }
90
91
        return 'InProgress';
92
    }
93
}
94