Completed
Pull Request — master (#114)
by Bart
04:23
created

TagGroupTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 119
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A _before() 0 4 1
A testGetRecordDefinition() 0 6 1
A testSaveRecord() 0 11 1
A testDeleteRecord() 0 8 1
A provideTagGroups() 0 11 1
A getMockTagGroup() 0 8 1
A getMockTagGroupDefinition() 0 10 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\models\TagGroup as TagGroupModel;
7
use Codeception\Test\Unit;
8
9
/**
10
 * Class TagGroupTest.
11
 *
12
 * @author    Nerds & Company
13
 * @copyright Copyright (c) 2015-2017, Nerds & Company
14
 * @license   MIT
15
 *
16
 * @see      http://www.nerds.company
17
 */
18
class TagGroupTest extends Unit
19
{
20
    /**
21
     * @var TagGroups
22
     */
23
    private $converter;
24
25
    /**
26
     * Set the converter.
27
     *
28
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
29
     */
30
    protected function _before()
31
    {
32
        $this->converter = new TagGroup();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \NerdsAndCompany\Sch...rters\Models\TagGroup() of type object<NerdsAndCompany\S...erters\Models\TagGroup> is incompatible with the declared type object<NerdsAndCompany\S...rters\Models\TagGroups> of property $converter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
34
35
    //==============================================================================================================
36
    //=================================================  TESTS  ====================================================
37
    //==============================================================================================================
38
39
    /**
40
     * @dataProvider provideTagGroups
41
     *
42
     * @param TagGroupModel $group
43
     * @param array         $definition
44
     */
45
    public function testGetRecordDefinition(TagGroupModel $group, array $definition)
46
    {
47
        $result = $this->converter->getRecordDefinition($group);
48
49
        $this->assertSame($definition, $result);
50
    }
51
52
    /**
53
     * @dataProvider provideTagGroups
54
     *
55
     * @param TagGroupModel $group
56
     * @param array         $definition
57
     */
58
    public function testSaveRecord(TagGroupModel $group, array $definition)
59
    {
60
        Craft::$app->tags->expects($this->exactly(1))
61
                         ->method('saveTagGroup')
62
                         ->with($group)
63
                         ->willReturn(true);
64
65
        $result = $this->converter->saveRecord($group, $definition);
66
67
        $this->assertTrue($result);
68
    }
69
70
    /**
71
     * @dataProvider provideTagGroups
72
     *
73
     * @param TagGroupModel $group
74
     */
75
    public function testDeleteRecord(TagGroupModel $group)
76
    {
77
        Craft::$app->tags->expects($this->exactly(1))
78
                         ->method('deleteTagGroupById')
79
                         ->with($group->id);
80
81
        $this->converter->deleteRecord($group);
82
    }
83
84
    //==============================================================================================================
85
    //==============================================  PROVIDERS  ===================================================
86
    //==============================================================================================================
87
88
    /**
89
     * @return array
90
     */
91
    public function provideTagGroups()
92
    {
93
        $mockTagGroup = $this->getMockTagGroup(1);
94
95
        return [
96
            'valid tag group' => [
97
                'group' => $mockTagGroup,
98
                'definition' => $this->getMockTagGroupDefinition($mockTagGroup),
99
            ],
100
        ];
101
    }
102
103
    //==============================================================================================================
104
    //================================================  HELPERS  ===================================================
105
    //==============================================================================================================
106
107
    /**
108
     * @param int $tagGroupId
109
     *
110
     * @return TagGroupModel
111
     */
112
    private function getMockTagGroup(int $tagGroupId)
113
    {
114
        return new TagGroupModel([
115
            'id' => $tagGroupId,
116
            'handle' => 'tagGroupHandle'.$tagGroupId,
117
            'name' => 'tagGroupName'.$tagGroupId,
118
        ]);
119
    }
120
121
    /**
122
     * @param TagGroupModel $tagGroup
123
     *
124
     * @return array
125
     */
126
    private function getMockTagGroupDefinition(TagGroupModel $tagGroup)
127
    {
128
        return [
129
          'class' => get_class($tagGroup),
130
          'attributes' => [
131
              'name' => 'tagGroupName'.$tagGroup->id,
132
              'handle' => 'tagGroupHandle'.$tagGroup->id,
133
          ],
134
        ];
135
    }
136
}
137