Passed
Pull Request — master (#1)
by Peter
07:07
created

OptionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetValueReturnsEmptyStringIfValueIsUnset() 0 9 1
A testGetValueReturnsEmptyStringIfValueIsNull() 0 9 1
A createElement() 0 15 1
A testRender() 0 16 1
A renderProvider() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Form\Component;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Html\Helper\ArrayHelper;
9
use AbterPhp\Framework\Html\INode;
10
use AbterPhp\Framework\TestDouble\Html\Component\StubAttributeFactory;
11
use AbterPhp\Framework\TestDouble\I18n\MockTranslatorFactory;
12
use PHPUnit\Framework\TestCase;
13
14
class OptionTest extends TestCase
15
{
16
    /**
17
     * @return array
18
     */
19
    public function renderProvider()
20
    {
21
        $attribs = StubAttributeFactory::createAttributes();
22
        $str     = ArrayHelper::toAttributes($attribs);
23
24
        return [
25
            'simple'           => ['abc', 'ABC', false, [], null, null, "<option value=\"abc\">ABC</option>"],
26
            'attributes'       => ['abc', 'ABC', false, $attribs, null, null, "<option$str value=\"abc\">ABC</option>"],
27
            'w/o translations' => ['abc', 'ABC', false, [], [], null, "<option value=\"abc\">ABC</option>"],
28
            'custom tag'       => ['abc', 'ABC', false, [], null, 'foo', "<foo value=\"abc\">ABC</foo>"],
29
            'w translations'   => ['abc', 'ABC', false, [], ['ABC' => '+'], null, "<option value=\"abc\">+</option>"],
30
        ];
31
    }
32
33
    /**
34
     * @dataProvider renderProvider
35
     *
36
     * @param string                    $value
37
     * @param INode[]|INode|string|null $content
38
     * @param string[][]                $attributes
39
     * @param string[]|null             $translations
40
     * @param string|null               $tag
41
     * @param string                    $expectedResult
42
     */
43
    public function testRender(
44
        string $value,
45
        $content,
46
        bool $isSelected,
47
        array $attributes,
48
        ?array $translations,
49
        ?string $tag,
50
        string $expectedResult
51
    ) {
52
        $sut = $this->createElement($value, $content, $isSelected, $attributes, $translations, $tag);
53
54
        $actualResult1 = (string)$sut;
55
        $actualResult2 = (string)$sut;
56
57
        $this->assertSame($expectedResult, $actualResult1);
58
        $this->assertSame($expectedResult, $actualResult2);
59
    }
60
61
    /**
62
     * @param string                    $value
63
     * @param INode[]|INode|string|null $content
64
     * @param bool                      $isSelected
65
     * @param string[][]                $attributes
66
     * @param string[]|null             $translations
67
     * @param string|null               $tag
68
     *
69
     * @return Option
70
     */
71
    protected function createElement(
72
        string $value,
73
        $content,
74
        bool $isSelected,
75
        array $attributes,
76
        ?array $translations,
77
        ?string $tag
78
    ): Option {
79
        $translatorMock = MockTranslatorFactory::createSimpleTranslator($this, $translations);
80
81
        $option = new Option($value, $content, $isSelected, [], $attributes, $tag);
82
83
        $option->setTranslator($translatorMock);
84
85
        return $option;
86
    }
87
88
    public function testGetValueReturnsEmptyStringIfValueIsUnset()
89
    {
90
        $sut = new Option('foo', 'Foo');
91
92
        $sut->unsetAttribute(Html5::ATTR_VALUE);
93
94
        $actualResult = $sut->getValue();
95
96
        $this->assertSame('', $actualResult);
97
    }
98
99
    public function testGetValueReturnsEmptyStringIfValueIsNull()
100
    {
101
        $sut = new Option('foo', 'Foo');
102
103
        $sut->setAttribute(Html5::ATTR_VALUE, null);
104
105
        $actualResult = $sut->getValue();
106
107
        $this->assertSame('', $actualResult);
108
    }
109
}
110