Passed
Pull Request — master (#12)
by romain
09:45
created

CategoryPatchMessageSpec   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 42.11 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 24
loc 57
rs 10
c 0
b 0
f 0

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 spec\Yproximite\Api\Message\Article;
4
5
use PhpSpec\ObjectBehavior;
6
7
use Yproximite\Api\Model\Article\Category;
8
use Yproximite\Api\Model\Article\CategoryTranslation;
9
use Yproximite\Api\Message\Article\CategoryPatchMessage;
10
use Yproximite\Api\Message\Article\CategoryTranslationMessage;
11
12
class CategoryPatchMessageSpec extends ObjectBehavior
13
{
14
    function it_is_initializable()
15
    {
16
        $this->shouldHaveType(CategoryPatchMessage::class);
17
    }
18
19
    function it_should_build()
20
    {
21
        $translation = new CategoryTranslationMessage();
22
        $translation->setLocale('en');
23
        $translation->setTitle('English title');
24
        $translation->setDescription('English description');
25
26
        $this->setParentRootId(11);
27
        $this->setEnabled(true);
28
        $this->addTranslation($translation);
29
30
        $transData = [
31
            'title'       => 'English title',
32
            'description' => 'English description',
33
        ];
34
35
        $data = [
36
            'parentRootId' => 11,
37
            'enabled'      => true,
38
            'translations' => ['en' => $transData],
39
        ];
40
41
        $this->build()->shouldReturn($data);
42
    }
43
44
    function it_should_create_from_category(
45
        Category $category,
46
        CategoryTranslation $translation
47
    ) {
48
        $translation->getLocale()->willReturn('en');
49
        $translation->getTitle()->willReturn('English title');
50
        $translation->getDescription()->willReturn('English description');
51
52
        $category->getId()->willReturn(1);
53
        $category->isEnabled()->willReturn(true);
54
        $category->getTranslations()->willReturn([$translation]);
55
56
        $transMessage = new CategoryTranslationMessage();
57
        $transMessage->setLocale('en');
58
        $transMessage->setTitle('English title');
59
        $transMessage->setDescription('English description');
60
61
        $message = new CategoryPatchMessage();
62
        $message->setId(1);
63
        $message->setEnabled(true);
64
        $message->addTranslation($transMessage);
65
66
        $this::createFromCategory($category)->shouldBeLike($message);
67
    }
68
}
69