Completed
Push — master ( 1f884a...4c414e )
by Nicolas
04:48
created

TaggableTraitTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 31.86 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 36
loc 113
wmc 9
lcom 1
cbo 3
rs 10

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 Modules\Tag\Tests\Integration;
4
5
use Modules\Page\Entities\Page;
6
use Modules\Page\Repositories\PageRepository;
7
use Modules\Tag\Repositories\TagRepository;
8
use Modules\Tag\Tests\BaseTestCase;
9
10
class TaggableTraitTest extends BaseTestCase
11
{
12
    /**
13
     * @var TagRepository
14
     */
15
    private $tag;
16
    /**
17
     * @var PageRepository
18
     */
19
    private $page;
20
21
    public function setUp()
22
    {
23
        parent::setUp();
24
25
        $this->tag = app(TagRepository::class);
26
        $this->page = app(PageRepository::class);
27
    }
28
29
    /** @test */
30
    public function it_creates_tags_on_creation_of_related_model()
31
    {
32
        $page = $this->createPage();
33
34
        $page->setTags(['my first tag']);
35
36
        $this->assertCount(1, $this->tag->all());
37
    }
38
39
    /** @test */
40
    public function it_can_create_multiple_tags_at_once()
41
    {
42
        $page = $this->createPage();
43
44
        $page->setTags(['my first tag', 'second tag', 'third tag']);
45
46
        $this->assertCount(3, $this->tag->all());
47
    }
48
49
    /** @test */
50
    public function it_links_tags_to_entity()
51
    {
52
        $page = $this->createPage();
53
54
        $page->setTags(['my first tag']);
55
56
        $page = $this->page->findHomepage();
57
58
        $this->assertCount(1, $page->tags);
59
    }
60
61
    /** @test */
62
    public function it_removes_tags_from_link_with_entity()
63
    {
64
        $this->createPage(['original tag']);
65
66
        $page = $this->page->findHomepage();
67
        $page->setTags(['my first tag']);
68
69
        $page = $this->page->findHomepage();
70
        $this->assertCount(1, $page->tags);
71
    }
72
73
    /** @test */
74
    public function it_can_get_pages_with_one_of_specified_tags()
75
    {
76
        $this->createPage(['original tag']);
77
        $this->createPage(['original-tag']);
78
        $this->createPage(['random tag']);
79
80
        $this->assertCount(2, Page::withTag(['original-tag', 'some-other-tag'])->get());
81
    }
82
83
    /** @test */
84
    public function it_gets_pages_with_all_specified_tags()
85
    {
86
        $this->createPage(['original tag']);
87
        $this->createPage(['original-tag']);
88
        $this->createPage(['random tag', 'original-tag']);
89
90
        $this->assertCount(1, Page::whereTag(['original-tag', 'random-tag'])->get());
91
    }
92
93
    /** @test */
94
    public function it_gets_all_tags_for_a_namespace()
95
    {
96
        $this->createPage(['original tag', 'other tag', 'random tag']);
97
98
        $this->tag->create([
99
            'namespace' => 'asgardcms/media',
100
            'en' => [
101
                'slug' => 'media-tag',
102
                'name' => 'media tag',
103
            ],
104
        ]);
105
106
        $this->assertCount(3, Page::allTags()->get());
107
    }
108
109
    private function createPage(array $tags = [])
110
    {
111
        return $this->page->create([
112
            'is_home' => 1,
113
            'template' => 'default',
114
            'en' => [
115
                'title' => 'My Page',
116
                'slug' => 'my-page',
117
                'body' => 'My Page Body',
118
            ],
119
            'tags' => $tags,
120
        ]);
121
    }
122
}
123