Completed
Push — master ( 576a4c...bc864b )
by Dennis
01:49
created

SeedCollectionTest::mockSeed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
namespace Dennis\Seeder\Tests\Unit\Collection;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2018 Dennis Römmich <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
use Dennis\Seeder\Collection\SeedCollection;
28
use TYPO3\CMS\Core\Tests\UnitTestCase;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * SeedCollectionTest
33
 *
34
 * @author Dennis Römmich<[email protected]>
35
 * @copyright Copyright belongs to the respective authors
36
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
37
 */
38
class SeedCollectionTest extends UnitTestCase
0 ignored issues
show
Deprecated Code introduced by
The class TYPO3\CMS\Core\Tests\UnitTestCase has been deprecated with message: will be removed once TYPO3 9 LTS is released

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
39
{
40
    /**
41
     * @var \Dennis\Seeder\SeedCollection
42
     */
43
    protected $subject;
44
45
    public function setUp()
46
    {
47
        $this->subject = new SeedCollection();
48
    }
49
50
    /**
51
     * @method attach
52
     * @test
53
     */
54
    public function attachAttachesNewItemIfItNotExist()
55
    {
56
        $seed = $this->getMock(\Dennis\Seeder\Seed::class);
57
        $this->subject->attach($seed);
58
        $this->assertSame($seed, $this->subject->current()['NEW1']);
59
    }
60
61
    /**
62
     * @method attach
63
     * @test
64
     */
65 View Code Duplication
    public function attachAttachesOnlyOneSeedIfSeedsAreIdentical()
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...
66
    {
67
        $seed = $this->getMock(\Dennis\Seeder\Seed::class);
68
        $this->subject->attach($seed);
69
        $this->subject->attach($seed);
70
        $this->assertSame([
71
            0 => 'NEW1',
72
        ], array_keys($this->subject->current()));
73
    }
74
75
    /**
76
     * @method attach
77
     * @test
78
     */
79 View Code Duplication
    public function attachAttachesMultipleSeedsIfSeedsAreDifferent()
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...
80
    {
81
        $seed = $this->getMock(\Dennis\Seeder\Seed::class);
82
        $this->subject->attach($seed);
83
        $this->subject->attach(clone $seed);
84
        $this->assertSame([
85
            0 => 'NEW1',
86
            1 => 'NEW2',
87
        ], array_keys($this->subject->current()));
88
    }
89
90
    /**
91
     * @param $name
92
     * @return \PHPUnit_Framework_MockObject_MockObject
93
     */
94
    protected function mockSeed($name)
95
    {
96
        $seed = $this->getMock(\Dennis\Seeder\Seed::class);
97
        $seed->method('getTitle')->willReturn($name);
98
        $seed->method('getTarget')->willReturn(strtolower(preg_replace('/\B([A-Z])/', '_$1', $name)));
99
        $seed->method('getProperties')->willReturn(['pid' => 123]);
100
101
        return $seed;
102
    }
103
104
    /**
105
     * @test
106
     * @method get
107
     */
108 View Code Duplication
    public function getReturnsArrayWithTwoKeysWhenClassNameFooBar()
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...
109
    {
110
        $seeder = $this->getMock(\Dennis\Seeder\Seeder::class);
111
        $seeder->method('getClass')->willReturn('FooBar');
112
        $fooBarSeed = $this->mockSeed('FooBar');
113
        $fooSeed = $this->mockSeed('Foo');
114
        $barSeed = $this->mockSeed('Bar');
115
116
        // NEW1
117
        $this->subject->attach($fooBarSeed);
118
        // NEW2
119
        $this->subject->attach($fooSeed);
120
        // NEW3
121
        $this->subject->attach(clone $fooBarSeed);
122
        // NEW4
123
        $this->subject->attach($barSeed);
124
125
        $this->subject->amount = 4;
0 ignored issues
show
Bug introduced by
Accessing amount on the interface Dennis\Seeder\SeedCollection suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
126
        $this->assertSame([
127
            0 => 'NEW1',
128
            1 => 'NEW3',
129
        ], array_keys($this->subject->get($seeder)));
130
    }
131
132
    /**
133
     * @test
134
     * @method get
135
     */
136 View Code Duplication
    public function getReturnsArrayWithOneKeyWhenClassNameFoo()
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...
137
    {
138
        $seeder = $this->getMock(\Dennis\Seeder\Seeder::class);
139
        $seeder->method('getClass')->willReturn('Foo');
140
        $fooBarSeed = $this->mockSeed('FooBar');
141
        $fooSeed = $this->mockSeed('Foo');
142
        $barSeed = $this->mockSeed('Bar');
143
144
        // NEW1
145
        $this->subject->attach($fooBarSeed);
146
        // NEW2
147
        $this->subject->attach($fooSeed);
148
        // NEW3
149
        $this->subject->attach($fooBarSeed);
150
        // NEW4
151
        $this->subject->attach($barSeed);
152
153
        $this->subject->amount = 4;
0 ignored issues
show
Bug introduced by
Accessing amount on the interface Dennis\Seeder\SeedCollection suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
154
        $this->assertSame([
155
            0 => 'NEW2',
156
        ], array_keys($this->subject->get($seeder)));
157
    }
158
159
    /**
160
     * @test
161
     * @method get
162
     */
163 View Code Duplication
    public function getReturnsArrayWithOneKeyWhenClassNameFooBar()
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...
164
    {
165
        $seeder = $this->getMock(\Dennis\Seeder\Seeder::class);
166
        $seeder->method('getClass')->willReturn('FooBar');
167
        $fooBarSeed = $this->mockSeed('FooBar');
168
        $fooSeed = $this->mockSeed('Foo');
169
        $barSeed = $this->mockSeed('Bar');
170
171
        // NEW1
172
        $this->subject->attach($fooBarSeed);
173
        // NEW2
174
        $this->subject->attach($fooSeed);
175
        // NEW3
176
        $this->subject->attach($fooBarSeed);
177
        // NEW4
178
        $this->subject->attach($barSeed);
179
180
        $this->subject->amount = 4;
0 ignored issues
show
Bug introduced by
Accessing amount on the interface Dennis\Seeder\SeedCollection suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
181
        $this->assertSame([
182
            0 => 'NEW1',
183
        ], array_keys($this->subject->get($seeder)));
184
    }
185
186
    /**
187
     * @method clear
188
     * @test
189
     */
190
    public function clearSeedCollectionRemovesAllSeeds()
191
    {
192
        $seed = $this->getMock(\Dennis\Seeder\Seed::class);
193
        $this->subject->attach($seed);
194
        $this->subject->clear();
195
196
        $this->assertSame([], $this->subject->toArray());
197
        $this->assertSame(0, $this->subject->count());
198
        $this->assertSame(false, $this->subject->current());
199
        $this->assertSame(false, $this->subject->valid());
200
        $this->assertSame(false, $this->subject->next());
201
        $this->assertSame(null, $this->subject->key());
202
        $this->assertSame(false, $this->subject->rewind());
203
    }
204
205
    /**
206
     * @method clear
207
     * @test
208
     */
209
    public function clearSeedCollectionSetCounterToZero()
210
    {
211
        $seed = $this->getMock(\Dennis\Seeder\Seed::class);
212
        $this->subject->attach($seed);
213
        $this->subject->clear();
214
        $this->subject->attach($seed);
215
        $this->subject->attach(clone $seed);
216
        $this->assertSame([
217
            0 => 'NEW1',
218
            1 => 'NEW2',
219
        ], array_keys($this->subject->current()));
220
    }
221
222
    /**
223
     * @method toArray
224
     * @test
225
     */
226
    public function toArrayReturnsCorrectArray()
227
    {
228
        $seed = $this->getMock(\Dennis\Seeder\Seed::class);
229
        $seed->method('getTitle')->willReturn('FooBar');
230
        $seed->method('getTarget')->willReturn('foo_bar');
231
        $seed->method('getProperties')->willReturn(['pid' => 123]);
232
        $this->subject->attach($seed);
233
        $this->subject->attach(clone $seed);
234
        $this->assertSame([
235
            'foo_bar' => [
236
                'NEW1' => [
237
                    'pid' => 123
238
                ],
239
                'NEW2' => [
240
                    'pid' => 123
241
                ],
242
            ],
243
        ], $this->subject->toArray());
244
    }
245
246
    public function tearDown()
247
    {
248
        $this->subject->clear();
249
    }
250
}
251