Completed
Push — fix_broken_js_unit_test-d6ae96... ( d6ae96 )
by Thomas
21:39
created

TestEntity::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * ownCloud - Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2014 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Db;
23
24
class ConcreteCollection extends Collection {
25
26
}
27
28
class TestEntity extends Entity {
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: columnToProperty, fromParams, fromRow, getUpdatedFields, propertyToColumn, resetUpdatedFields, slugify
Loading history...
29
	private $name;
30
	public function __construct($name)
31
	{
32
		$this->name = $name;
33
	}
34
	public function getName() {
35
		return $this->name;
36
	}
37
}
38
39
class CollectionTest extends \PHPUnit_Framework_TestCase {
40
41
	/**
42
	 * @var \OCA\Calendar\Db\Collection
43
	 */
44
	protected $coll;
45
46
	/**
47
	 * Test entities:
48
	 */
49
	const TestEntityCount=7;
50
	protected $testEntities;
51
52
	public function setUp() {
53
		parent::setUp();
54
		for ($i=0; $i<CollectionTest::TestEntityCount; ++$i)
55
		{
56
			$this->testEntities[] = new TestEntity((string)$i);
57
		}
58
		$this->coll = ConcreteCollection::fromEntity($this->testEntities[1]);
59
	}
60
61
	public function testFromEntity() {
62
63
		$collection = ConcreteCollection::fromEntity($this->testEntities[1]);
64
65
		$this->assertSame($collection->count(), 1);
66
		$this->assertEquals($collection->getObjects()[0], $this->testEntities[1]);
67
	}
68
69
	/**
70
	 * @expectedException PHPUnit_Framework_Error
71
	 */
72
	public function testFailedFromEntity() {
73
74
		ConcreteCollection::fromEntity("No IEntity");
0 ignored issues
show
Documentation introduced by
'No IEntity' is of type string, but the function expects a object<OCA\Calendar\IEntity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
	}
76
77
	public function testFromCollection() {
78
		$collection = ConcreteCollection::fromCollection($this->coll);
79
80
		$this->assertSame ($collection->count(), 1);
81
		$this->assertEquals ($collection->getObjects()[0], $this->testEntities[1]);
82
	}
83
84
	public function testFromArray() {
85
86
		$testEmptyArray = array();
87
		$coll1 = ConcreteCollection::fromArray($testEmptyArray);
88
89
		$this->assertSame ($coll1->count(), 0);
90
91
		$testTwoElementArray = array($this->testEntities[1], $this->testEntities[2]);
92
		$coll2 = ConcreteCollection::fromArray($testTwoElementArray);
93
94
		$this->assertSame ($coll2->count(), 2);
95
		$this->assertEquals ($coll2->getObjects()[0], $this->testEntities[1]);
96
		$this->assertEquals ($coll2->getObjects()[1], $this->testEntities[2]);
97
	}
98
99
	/**
100
	 * @expectedException PHPUnit_Framework_Error
101
	 */
102
	public function testFailedFromArray() {
103
104
		ConcreteCollection::fromEntity(array("No IEntity 1", "No IEntity 2"));
0 ignored issues
show
Documentation introduced by
array('No IEntity 1', 'No IEntity 2') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a object<OCA\Calendar\IEntity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
	}
106
107
	public function testAdd() {
108
109
		$this->coll->add($this->testEntities[0], 0);
110
		$this->assertSame ($this->coll->count(), 2);
111
		$this->assertSame ($this->coll->getObjects()[0], $this->testEntities[0]);
112
		$this->assertSame ($this->coll->getObjects()[1], $this->testEntities[1]);
113
114
115
		$this->coll->add($this->testEntities[2], 1);
116
		$this->assertSame ($this->coll->count(), 3);
117
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[0]);
118
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[2]);
119
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[1]);
120
121
122
		$this->coll->add($this->testEntities[3], 3);
123
		$this->assertSame ($this->coll->count(), 4);
124
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[0]);
125
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[2]);
126
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[1]);
127
		$this->assertEquals ($this->coll->getObjects()[3], $this->testEntities[3]);
128
129
		// TODO: should we have an error if nth is bigger than count ?
130
		$this->coll->add($this->testEntities[4], 5);
131
		$this->assertSame ($this->coll->count(), 5);
132
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[0]);
133
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[2]);
134
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[1]);
135
		$this->assertEquals ($this->coll->getObjects()[3], $this->testEntities[3]);
136
		$this->assertEquals ($this->coll->getObjects()[4], $this->testEntities[4]);
137
138
139
		// TODO: is this wanted to be accepted? have added it to the doc for now
140
		$this->coll->add($this->testEntities[5], -1);
141
		$this->assertSame ($this->coll->count(), 6);
142
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[0]);
143
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[2]);
144
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[1]);
145
		$this->assertEquals ($this->coll->getObjects()[3], $this->testEntities[3]);
146
		$this->assertEquals ($this->coll->getObjects()[4], $this->testEntities[5]);
147
		$this->assertEquals ($this->coll->getObjects()[5], $this->testEntities[4]);
148
149
		$this->coll->add($this->testEntities[6]);
150
		$this->assertSame ($this->coll->count(),7);
151
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[0]);
152
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[2]);
153
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[1]);
154
		$this->assertEquals ($this->coll->getObjects()[3], $this->testEntities[3]);
155
		$this->assertEquals ($this->coll->getObjects()[4], $this->testEntities[5]);
156
		$this->assertEquals ($this->coll->getObjects()[5], $this->testEntities[4]);
157
		$this->assertEquals ($this->coll->getObjects()[6], $this->testEntities[6]);
158
	}
159
160
	/**
161
	 * @expectedException PHPUnit_Framework_Error
162
	 */
163
	public function testFailingAdd() {
164
165
		$this->coll->add("No IEntity");
0 ignored issues
show
Documentation introduced by
'No IEntity' is of type string, but the function expects a object<OCA\Calendar\IEntity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
166
	}
167
168
	public function testAddCollection() {
169
170
		$testTwoElementArray = array($this->testEntities[2], $this->testEntities[3]);
171
		$coll2 = ConcreteCollection::fromArray($testTwoElementArray);
172
173
		$this->coll->addCollection($coll2);
174
		$this->assertSame   ($this->coll->count(), 3);
175
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[1]);
176
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[2]);
177
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[3]);
178
179
		$this->coll->addCollection($coll2, 0);
180
		$this->assertSame   ($this->coll->count(), 5);
181
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[2]);
182
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[3]);
183
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[1]);
184
		$this->assertEquals ($this->coll->getObjects()[3], $this->testEntities[2]);
185
		$this->assertEquals ($this->coll->getObjects()[4], $this->testEntities[3]);
186
187
		$oneElementArray = array($this->testEntities[5]);
188
		$coll3 = ConcreteCollection::fromArray($oneElementArray);
189
		$this->coll->addCollection($coll3, -1);
190
		$this->assertSame   ($this->coll->count(), 6);
191
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[2]);
192
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[3]);
193
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[1]);
194
		$this->assertEquals ($this->coll->getObjects()[3], $this->testEntities[2]);
195
		$this->assertEquals ($this->coll->getObjects()[4], $this->testEntities[5]);
196
		$this->assertEquals ($this->coll->getObjects()[5], $this->testEntities[3]);
197
	}
198
199
	public function testAddObjects() {
200
		$testTwoElementArray = array($this->testEntities[2], $this->testEntities[3]);
201
202
		$this->coll->addObjects($testTwoElementArray);
203
		$this->assertSame   ($this->coll->count(), 3);
204
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[1]);
205
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[2]);
206
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[3]);
207
208
		$this->coll->addObjects($testTwoElementArray, 0);
209
		$this->assertSame   ($this->coll->count(), 5);
210
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[2]);
211
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[3]);
212
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[1]);
213
		$this->assertEquals ($this->coll->getObjects()[3], $this->testEntities[2]);
214
		$this->assertEquals ($this->coll->getObjects()[4], $this->testEntities[3]);
215
216
		$oneElementArray = array($this->testEntities[5]);
217
		$this->coll->addObjects($oneElementArray, -1);
218
		$this->assertSame   ($this->coll->count(), 6);
219
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[2]);
220
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[3]);
221
		$this->assertEquals ($this->coll->getObjects()[2], $this->testEntities[1]);
222
		$this->assertEquals ($this->coll->getObjects()[3], $this->testEntities[2]);
223
		$this->assertEquals ($this->coll->getObjects()[4], $this->testEntities[5]);
224
		$this->assertEquals ($this->coll->getObjects()[5], $this->testEntities[3]);
225
	}
226
227
	public function testSetObjects() {
228
229
		$testTwoElementArray = array($this->testEntities[2], $this->testEntities[3]);
230
231
		$this->coll->setObjects($testTwoElementArray);
232
		$this->assertSame   ($this->coll->count(), 2);
233
		$this->assertEquals ($this->coll->getObjects()[0], $this->testEntities[2]);
234
		$this->assertEquals ($this->coll->getObjects()[1], $this->testEntities[3]);
235
	}
236
237
	public function testSubSet() {
238
		$coll2 = $this->coll->subset();
239
240
		$this->assertEquals($this->coll, $coll2);
241
		$this->assertSame($coll2->count(), 1);
242
243
		$this->coll->add($this->testEntities[2]);
244
		// check that $coll2 hasn't changed:
245
		$this->assertSame($coll2->count(), 1);
246
247
		$coll3 = $this->coll->subset(1);
248
		$this->assertSame   ($coll3->count(), 1);
249
		$this->assertEquals ($coll3->getObjects()[0], $this->testEntities[1]);
250
251
		$coll4 = $this->coll->subset(1, 1);
252
		$this->assertSame   ($coll4->count(), 1);
253
		$this->assertEquals ($coll4->getObjects()[0], $this->testEntities[2]);
254
255
		$coll5 = $this->coll->subset(null, 1);
0 ignored issues
show
Unused Code introduced by
$coll5 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
256
		$this->assertSame   ($coll4->count(), 1);
257
		$this->assertEquals ($coll4->getObjects()[0], $this->testEntities[2]);
258
	}
259
260
	public function testInCollection() {
261
		$this->assertTrue ( $this->coll->inCollection($this->testEntities[1]));
262
		$this->assertFalse( $this->coll->inCollection($this->testEntities[2]));
263
	}
264
265
	public function testSearch() {
266
267
		$coll1 = $this->coll->search("name", "");
268
		$this->assertSame($coll1->count(), 0);
269
270
		$coll1 = $this->coll->search("name", "1");
271
		$this->assertSame($coll1->count(), 1);
272
273
		$this->coll->add($this->testEntities[1]);
274
		$coll1 = $this->coll->search("name", "1");
275
		$this->assertSame($coll1->count(), 2);
276
	}
277
278
	public function testSetProperty() {
279
280
		$mockedEntity = $this->getMock('\OCA\Calendar\Db\Entity');
281
		$mockedEntity->expects($this->once())
282
			->method('setId')
283
			->with($this->equalTo("1.5"));
284
285
		$coll = ConcreteCollection::fromEntity($mockedEntity);
286
		$coll->setProperty('id', '1.5');
287
	}
288
289
	public function testIsValid() {
290
291
		$mockedValid = $this->getMock('\OCA\Calendar\Db\Entity');
292
		$mockedValid->expects($this->any())
293
			->method('isValid')
294
			->will($this->returnValue(true));
295
296
		$mockedInvalid = $this->getMock('\OCA\Calendar\Db\Entity');
297
		$mockedInvalid->expects($this->once())
298
			->method('isValid')
299
			->will($this->returnValue(false));
300
301
		$coll = ConcreteCollection::fromEntity($mockedValid);
302
		$this->assertTrue($coll->isValid());
303
304
		$coll->add($mockedInvalid);
305
		$this->assertFalse($coll->isValid());
306
	}
307
308
	/*
309
	public function testNoDuplicates() {
310
		$this->coll->add($this->testEntities[1]);
311
		$this->assertSame($this->coll->count(), 2);
312
		$collUnique = $this->coll->noDuplicates();
313
		$this->assertSame($collUnique->count(), 1);
314
	}
315
	*/
316
317
	public function testOffsetGet() {
318
		$this->assertSame($this->coll->offsetGet(-1), null);
319
		$this->assertSame($this->coll->offsetGet(0), $this->coll->getObjects()[0]);
320
		$this->assertSame($this->coll->offsetGet(1), null);
321
		$this->assertSame($this->coll->offsetGet('foo'), null);
322
	}
323
324
	public function testOffsetSet() {
325
		$entity0 = & $this->coll->getObjects()[0];
326
		$this->coll->offsetSet(0, 'foo');
327
		$this->assertSame($this->coll->getObjects()[0], $entity0);
328
329
		$entity2 = $this->testEntities[2];
330
		$this->coll->offsetSet(0, $entity2);
331
		$this->assertSame($this->coll->offsetGet(0), $entity2);
332
	}
333
334
	public function testOffsetUnset() {
335
		$this->coll->offsetUnset(0);
336
		$this->assertSame($this->coll->count(), 0);
337
		$this->assertFalse(isset($this->coll->getObjects()[0]));
338
	}
339
340
	public function testReset() {
341
		$this->coll->reset();
342
		$this->assertSame($this->coll->count(), 0);
343
344
		// should also work on empty list
345
		$this->coll->reset();
346
		$this->assertSame($this->coll->count(), 0);
347
	}
348
349
350
	public function testIterate() {
351
		$this->coll->add($this->testEntities[2]);
352
353
		$this->assertEquals($this->coll->key(), 0);
354
		$this->assertEquals($this->coll->current(), $this->testEntities[1]);
355
		$this->assertTrue  ($this->coll->valid());
356
		$this->coll->next();
357
		$this->assertEquals($this->coll->key(), 1);
358
		$this->assertEquals($this->coll->current(), $this->testEntities[2]);
359
		$this->assertTrue  ($this->coll->valid());
360
		$this->coll->next();
361
		$this->assertEquals($this->coll->key(), null);
362
		$this->assertEquals($this->coll->current(), FALSE);
363
		$this->assertFalse ($this->coll->valid());
364
365
		$this->coll->rewind();
366
		$this->assertEquals($this->coll->key(), 0);
367
		$this->assertEquals($this->coll->current(), $this->testEntities[1]);
368
		$this->assertTrue  ($this->coll->valid());
369
	}
370
}
371