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

HelpTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testRender() 0 14 1
A createElement() 0 13 1
A renderProvider() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Form\Extra;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Html\Helper\ArrayHelper;
9
use AbterPhp\Framework\TestDouble\Html\Component\StubAttributeFactory;
10
use AbterPhp\Framework\TestDouble\I18n\MockTranslatorFactory;
11
use PHPUnit\Framework\TestCase;
12
13
class HelpTest extends TestCase
14
{
15
    /**
16
     * @return array
17
     */
18
    public function renderProvider()
19
    {
20
        $attributes = StubAttributeFactory::createAttributes();
21
22
        $finalAttribs = ArrayHelper::mergeAttributes([Html5::ATTR_CLASS => [Help::CLASS_HELP_BLOCK]], $attributes);
23
        $str          = ArrayHelper::toAttributes($finalAttribs);
24
25
        return [
26
            'simple'               => ['ABC', [], null, null, '<div class="help-block">ABC</div>'],
27
            'attributes'           => ['ABC', $attributes, [], null, "<div$str>ABC</div>"],
28
            'missing translations' => ['ABC', [], [], null, '<div class="help-block">ABC</div>'],
29
            'found translations'   => ['ABC', [], ['ABC' => 'CBA'], null, '<div class="help-block">CBA</div>'],
30
        ];
31
    }
32
33
    /**
34
     * @dataProvider renderProvider
35
     *
36
     * @param string        $content
37
     * @param array         $attributes
38
     * @param string[]|null $translations
39
     * @param string|null   $tag
40
     * @param string        $expectedResult
41
     */
42
    public function testRender(
43
        string $content,
44
        array $attributes,
45
        ?array $translations,
46
        ?string $tag,
47
        string $expectedResult
48
    ) {
49
        $sut = $this->createElement($content, $attributes, $translations, $tag);
50
51
        $actualResult   = (string)$sut;
52
        $repeatedResult = (string)$sut;
53
54
        $this->assertSame($actualResult, $repeatedResult);
55
        $this->assertSame($expectedResult, $actualResult);
56
    }
57
58
    /**
59
     * @param string        $content
60
     * @param array         $attributes
61
     * @param string[]|null $translations
62
     * @param string|null   $tag
63
     *
64
     * @return Help
65
     */
66
    private function createElement(
67
        string $content,
68
        array $attributes,
69
        ?array $translations,
70
        ?string $tag
71
    ): Help {
72
        $translatorMock = MockTranslatorFactory::createSimpleTranslator($this, $translations);
73
74
        $help = new Help($content, [], $attributes, $tag);
75
76
        $help->setTranslator($translatorMock);
77
78
        return $help;
79
    }
80
}
81