Completed
Push — master ( db13e3...4d471d )
by
unknown
300:40 queued 288:23
created

ContentFieldStrategyTest::testHasStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\Tests\ContentThumbnail;
10
11
use ArrayIterator;
12
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
13
use eZ\Publish\API\Repository\Values\Content\Field;
14
use eZ\Publish\API\Repository\Values\Content\Thumbnail;
15
use eZ\Publish\Core\Repository\Strategy\ContentThumbnail\Field\ContentFieldStrategy;
16
use eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\Field\FieldTypeBasedThumbnailStrategy;
17
use PHPUnit\Framework\TestCase;
18
19
class ContentFieldStrategyTest extends TestCase
20
{
21
    private function getFieldTypeBasedThumbnailStrategy(string $fieldTypeIdentifier): FieldTypeBasedThumbnailStrategy
22
    {
23
        return new class($fieldTypeIdentifier) implements FieldTypeBasedThumbnailStrategy {
24
            /** @var string */
25
            private $fieldTypeIdentifier;
26
27
            public function __construct(string $fieldTypeIdentifier)
28
            {
29
                $this->fieldTypeIdentifier = $fieldTypeIdentifier;
30
            }
31
32
            public function getFieldTypeIdentifier(): string
33
            {
34
                return $this->fieldTypeIdentifier;
35
            }
36
37
            public function getThumbnail(Field $field): ?Thumbnail
38
            {
39
                return new Thumbnail([
40
                    'resource' => $field->value,
41
                ]);
42
            }
43
        };
44
    }
45
46
    public function testHasStrategy(): void
47
    {
48
        $contentFieldStrategy = new ContentFieldStrategy(new ArrayIterator([
49
            $this->getFieldTypeBasedThumbnailStrategy('example'),
50
        ]));
51
52
        $this->assertTrue($contentFieldStrategy->hasStrategy('example'));
53
        $this->assertFalse($contentFieldStrategy->hasStrategy('something_else'));
54
    }
55
56
    public function testAddStrategy(): void
57
    {
58
        $contentFieldStrategy = new ContentFieldStrategy(new ArrayIterator());
59
60
        $this->assertFalse($contentFieldStrategy->hasStrategy('example'));
61
62
        $contentFieldStrategy->addStrategy('example', $this->getFieldTypeBasedThumbnailStrategy('example'));
63
64
        $this->assertTrue($contentFieldStrategy->hasStrategy('example'));
65
    }
66
67
    public function testSetStrategies(): void
68
    {
69
        $contentFieldStrategy = new ContentFieldStrategy(new ArrayIterator([
70
            $this->getFieldTypeBasedThumbnailStrategy('previous'),
71
        ]));
72
73
        $this->assertTrue($contentFieldStrategy->hasStrategy('previous'));
74
75
        $contentFieldStrategy->setStrategies([
76
            $this->getFieldTypeBasedThumbnailStrategy('new-example-1'),
77
            $this->getFieldTypeBasedThumbnailStrategy('new-example-2'),
78
        ]);
79
80
        $this->assertFalse($contentFieldStrategy->hasStrategy('previous'));
81
        $this->assertTrue($contentFieldStrategy->hasStrategy('new-example-1'));
82
        $this->assertTrue($contentFieldStrategy->hasStrategy('new-example-2'));
83
    }
84
85
    public function testGetThumbnailFound(): void
86
    {
87
        $contentFieldStrategy = new ContentFieldStrategy(new ArrayIterator([
88
            $this->getFieldTypeBasedThumbnailStrategy('example'),
89
        ]));
90
91
        $field = new Field([
92
            'fieldTypeIdentifier' => 'example',
93
            'value' => 'example-value',
94
        ]);
95
96
        $thumbnail = $contentFieldStrategy->getThumbnail($field);
97
98
        $this->assertInstanceOf(Thumbnail::class, $thumbnail);
99
        $this->assertEquals('example-value', $thumbnail->resource);
100
    }
101
102
    public function testGetThumbnailNotFound(): void
103
    {
104
        $contentFieldStrategy = new ContentFieldStrategy(new ArrayIterator([
105
            $this->getFieldTypeBasedThumbnailStrategy('something-else'),
106
        ]));
107
108
        $field = new Field([
109
            'fieldTypeIdentifier' => 'example',
110
            'value' => 'example-value',
111
        ]);
112
113
        $this->expectException(NotFoundException::class);
114
115
        $contentFieldStrategy->getThumbnail($field);
116
    }
117
}
118