Completed
Pull Request — master (#37)
by Daniel
08:48 queued 03:58
created

TwigRendererTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 88
Duplicated Lines 19.32 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 17
loc 88
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 17 17 1
A testRender() 0 6 1
A provideViews() 0 53 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Psi\Bridge\ContentType\Twig\Tests\Functional;
4
5
use Psi\Component\ContentType\Standard\View\CollectionType;
6
use Psi\Component\ContentType\Standard\View\NullType;
7
use Psi\Component\ContentType\Standard\View\ObjectType;
8
use Psi\Component\ContentType\Standard\View\ScalarType;
9
use Psi\Component\ContentType\Tests\Functional\Example\Model\Article;
10
use Psi\Component\ContentType\View\View;
11
12
class TwigRendererTest extends TestCase
13
{
14
    private $renderer;
15
16 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
    {
18
        $container = $this->getContainer([
19
            'mapping' => [
20
                Article::class => [
21
                    'alias' => 'article',
22
                    'fields' => [
23
                        'title' => [
24
                            'type' => 'text',
25
                        ],
26
                    ],
27
                ],
28
            ],
29
        ]);
30
        $this->renderer = $container->get('psi_content_type.view.twig.renderer');
31
        $this->viewFactory = $container->get('psi_content_type.view.factory');
0 ignored issues
show
Bug introduced by
The property viewFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
    }
33
34
    /**
35
     * It should renderer a views.
36
     *
37
     * @dataProvider provideViews
38
     */
39
    public function testRender($viewType, $data, array $options, $expected)
40
    {
41
        $view = $this->viewFactory->create($viewType, $data, $options);
42
        $output = $this->renderer->render($view);
43
        $this->assertEquals($expected, $output);
44
    }
45
46
    public function provideViews()
47
    {
48
        $article = new Article();
49
        $article->title = 'Hello';
50
51
        return [
52
            [
53
                ScalarType::class,
54
                'hello',
55
                [],
56
                'hello',
57
            ],
58
            [
59
                ObjectType::class,
60
                $article,
61
                [],
62
                <<<'EOT'
63
    <div>
64
        Hello
65
    </div>
66
67
EOT
68
            ],
69
            [
70
                CollectionType::class,
71
                [
72
                    'hello',
73
                    'goodbye',
74
                ],
75
                [
76
                    'field_type' => 'text',
77
                    'field_options' => [],
78
                ],
79
                <<<'EOT'
80
<ul>
81
            <li>
82
            hello
83
        </li>
84
            <li>
85
            goodbye
86
        </li>
87
    </ul>
88
89
EOT
90
            ],
91
            [
92
                NullType::class,
93
                'nothing',
94
                [],
95
                '',
96
            ],
97
        ];
98
    }
99
}
100