Completed
Push — master ( 96e397...a65e1b )
by Dennis
03:31
created

getReturnsArrayWithOneKeyWhenClassNameFoo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
namespace TildBJ\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 TildBJ\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 \TildBJ\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(\TildBJ\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
    public function attachAttachesOnlyOneSeedIfSeedsAreIdentical()
66
    {
67
        $seed = $this->getMock(\TildBJ\Seeder\Seed::class);
68
        $this->subject->attach($seed);
69
        $this->subject->attach($seed);
70
        $this->assertSame([
71
            0 => 'NEW1',
72
            1 => 'NEW2',
73
        ], array_keys($this->subject->current()));
74
    }
75
76
    /**
77
     * @param $name
78
     * @return \PHPUnit_Framework_MockObject_MockObject
79
     */
80
    protected function mockSeed($name)
81
    {
82
        $seed = $this->getMock(\TildBJ\Seeder\Seed::class);
83
        $seed->method('getTitle')->willReturn($name);
84
        $seed->method('getTarget')->willReturn(strtolower(preg_replace('/\B([A-Z])/', '_$1', $name)));
85
        $seed->method('getProperties')->willReturn(['pid' => 123]);
86
87
        return $seed;
88
    }
89
90
    /**
91
     * @test
92
     * @method get
93
     */
94
    public function getReturnsArrayWithTwoKeysWhenClassNameFooBar()
95
    {
96
        $seeder = $this->getMock(\TildBJ\Seeder\Seeder::class);
97
        $seeder->method('getClass')->willReturn('FooBar');
98
        $fooBarSeed = $this->mockSeed('FooBar');
99
        $fooSeed = $this->mockSeed('Foo');
100
        $barSeed = $this->mockSeed('Bar');
101
102
        // NEW1
103
        $this->subject->attach($fooBarSeed);
104
        // NEW2
105
        $this->subject->attach($fooSeed);
106
        // NEW3
107
        $this->subject->attach(clone $fooBarSeed);
108
        // NEW4
109
        $this->subject->attach($barSeed);
110
111
        $this->subject->amount = 4;
0 ignored issues
show
Bug introduced by
Accessing amount on the interface TildBJ\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...
112
        $this->assertSame([
113
            0 => 'NEW1',
114
            1 => 'NEW3',
115
        ], array_keys($this->subject->get($seeder)));
116
    }
117
118
    /**
119
     * @test
120
     * @method get
121
     */
122
    public function getReturnsArrayWithOneKeyWhenClassNameFoo()
123
    {
124
        $seeder = $this->getMock(\TildBJ\Seeder\Seeder::class);
125
        $seeder->method('getClass')->willReturn('Foo');
126
        $fooBarSeed = $this->mockSeed('FooBar');
127
        $fooSeed = $this->mockSeed('Foo');
128
        $barSeed = $this->mockSeed('Bar');
129
130
        // NEW1
131
        $this->subject->attach($fooBarSeed);
132
        // NEW2
133
        $this->subject->attach($fooSeed);
134
        // NEW3
135
        $this->subject->attach($fooBarSeed);
136
        // NEW4
137
        $this->subject->attach($barSeed);
138
139
        $this->subject->amount = 4;
0 ignored issues
show
Bug introduced by
Accessing amount on the interface TildBJ\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...
140
        $this->assertSame([
141
            0 => 'NEW2',
142
        ], array_keys($this->subject->get($seeder)));
143
    }
144
145
    /**
146
     * @test
147
     * @method get
148
     */
149
    public function getReturnsArrayWithOneKeyWhenClassNameFooBar()
150
    {
151
        $seeder = $this->getMock(\TildBJ\Seeder\Seeder::class);
152
        $seeder->method('getClass')->willReturn('FooBar');
153
        $fooBarSeed = $this->mockSeed('FooBar');
154
        $fooSeed = $this->mockSeed('Foo');
155
        $barSeed = $this->mockSeed('Bar');
156
157
        // NEW1
158
        $this->subject->attach($fooBarSeed);
159
        // NEW2
160
        $this->subject->attach($fooSeed);
161
        // NEW3
162
        $this->subject->attach($fooBarSeed);
163
        // NEW4
164
        $this->subject->attach($barSeed);
165
166
        $this->subject->amount = 4;
0 ignored issues
show
Bug introduced by
Accessing amount on the interface TildBJ\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...
167
        $this->assertSame([
168
            0 => 'NEW1',
169
            1 => 'NEW3',
170
        ], array_keys($this->subject->get($seeder)));
171
    }
172
173
    /**
174
     * @method clear
175
     * @test
176
     */
177
    public function clearSeedCollectionRemovesAllSeeds()
178
    {
179
        $seed = $this->getMock(\TildBJ\Seeder\Seed::class);
180
        $this->subject->attach($seed);
181
        $this->subject->clear();
182
183
        $this->assertSame([], $this->subject->toArray());
184
        $this->assertSame(0, $this->subject->count());
185
        $this->assertSame(false, $this->subject->current());
186
        $this->assertSame(false, $this->subject->valid());
187
        $this->assertSame(false, $this->subject->next());
188
        $this->assertSame(null, $this->subject->key());
189
        $this->assertSame(false, $this->subject->rewind());
190
    }
191
192
    /**
193
     * @method clear
194
     * @test
195
     */
196
    public function clearSeedCollectionSetCounterToZero()
197
    {
198
        $seed = $this->getMock(\TildBJ\Seeder\Seed::class);
199
        $this->subject->attach($seed);
200
        $this->subject->clear();
201
        $this->subject->attach($seed);
202
        $this->subject->attach(clone $seed);
203
        $this->assertSame([
204
            0 => 'NEW1',
205
            1 => 'NEW2',
206
        ], array_keys($this->subject->current()));
207
    }
208
209
    /**
210
     * @method toArray
211
     * @test
212
     */
213
    public function toArrayReturnsCorrectArray()
214
    {
215
        $seed = $this->getMock(\TildBJ\Seeder\Seed::class);
216
        $seed->method('getTitle')->willReturn('FooBar');
217
        $seed->method('getTarget')->willReturn('foo_bar');
218
        $seed->method('getProperties')->willReturn(['pid' => 123]);
219
        $this->subject->attach($seed);
220
        $this->subject->attach(clone $seed);
221
        $this->assertSame([
222
            'foo_bar' => [
223
                'NEW1' => [
224
                    'pid' => 123
225
                ],
226
                'NEW2' => [
227
                    'pid' => 123
228
                ],
229
            ],
230
        ], $this->subject->toArray());
231
    }
232
233
    public function tearDown()
234
    {
235
        $this->subject->clear();
236
    }
237
}
238