|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ducatel\PHPCollection\Test; |
|
4
|
|
|
|
|
5
|
|
|
use Ducatel\PHPCollection\TypedArray; |
|
6
|
|
|
|
|
7
|
|
|
class TypedArrayTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @expectedException \TypeError |
|
11
|
|
|
*/ |
|
12
|
|
|
public function testWithBool() { |
|
13
|
|
|
|
|
14
|
|
|
$typedArray = new TypedArray( function($obj){ return is_bool($obj);}); |
|
15
|
|
|
$this->assertEquals(0, $typedArray->count()); |
|
16
|
|
|
|
|
17
|
|
|
$this->assertFalse($typedArray->add("test")); |
|
|
|
|
|
|
18
|
|
|
$this->assertFalse($typedArray->add(new \stdClass())); |
|
19
|
|
|
|
|
20
|
|
|
$this->assertNotFalse($typedArray->add(true)); |
|
|
|
|
|
|
21
|
|
|
$this->assertEquals(1, $typedArray->count()); |
|
22
|
|
|
$this->assertFalse($typedArray->contains(false)); |
|
23
|
|
|
$this->assertTrue($typedArray->contains(true)); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertNotFalse($typedArray->add(false)); |
|
|
|
|
|
|
26
|
|
|
$this->assertEquals(2, $typedArray->count()); |
|
27
|
|
|
$this->assertTrue($typedArray->contains(false)); |
|
28
|
|
|
$this->assertTrue($typedArray->contains(true)); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertFalse($typedArray->delete(42)); |
|
31
|
|
|
$this->assertFalse($typedArray->delete(6)); |
|
32
|
|
|
|
|
33
|
|
|
$total = $typedArray->count(); |
|
34
|
|
|
$typedArray->delete(0); |
|
35
|
|
|
$this->assertEquals($total -1 , $typedArray->count()); |
|
36
|
|
|
|
|
37
|
|
|
$typedArray->contains(42); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @expectedException \TypeError |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testWithString() { |
|
44
|
|
|
$typedArray = new TypedArray( function($obj){ return is_string($obj);}); |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$this->assertFalse($typedArray->add(true)); |
|
|
|
|
|
|
48
|
|
|
$this->assertFalse($typedArray->add(new \stdClass())); |
|
49
|
|
|
$this->assertFalse($typedArray->add(42)); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$this->assertNotFalse($typedArray->add('plop')); |
|
|
|
|
|
|
52
|
|
|
$this->assertFalse($typedArray->contains('123')); |
|
53
|
|
|
$this->assertFalse($typedArray->contains('PloP')); |
|
54
|
|
|
$this->assertTrue($typedArray->contains('plop')); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertNotFalse($typedArray->add("123")); |
|
|
|
|
|
|
57
|
|
|
$this->assertTrue($typedArray->contains('123')); |
|
58
|
|
|
$this->assertTrue($typedArray->contains('plop')); |
|
59
|
|
|
|
|
60
|
|
|
$typedArray->contains(42); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @expectedException \TypeError |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
public function testWithObject() { |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
$mockBuilder = $this->getMockBuilder('\PDO')->disableOriginalConstructor(); |
|
69
|
|
|
$typedArray = new TypedArray('\PDO'); |
|
70
|
|
|
$this->assertNotFalse($typedArray->add($mockBuilder->getMock())); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertFalse($typedArray->add(true)); |
|
|
|
|
|
|
73
|
|
|
$this->assertFalse($typedArray->add(new \stdClass())); |
|
74
|
|
|
$this->assertFalse($typedArray->add(42)); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$typedArray->delete('plpo'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
View Code Duplication |
public function testWithObjectImplementEquatable() { |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$mock = $this->getMockBuilder('Ducatel\PHPCollection\Base\Equatable')->getMock(); |
|
82
|
|
|
$mock->method('equals') |
|
83
|
|
|
->willReturn(true); |
|
84
|
|
|
|
|
85
|
|
|
$typedArray = new TypedArray('Ducatel\PHPCollection\Base\Equatable'); |
|
86
|
|
|
$this->assertNotFalse($typedArray->add($mock)); |
|
87
|
|
|
$this->assertFalse($typedArray->add(true)); |
|
|
|
|
|
|
88
|
|
|
$this->assertFalse($typedArray->add(new \stdClass())); |
|
89
|
|
|
$this->assertFalse($typedArray->add(42)); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testWithCustomEqualsFunction() { |
|
93
|
|
|
|
|
94
|
|
|
$isStringFct = function($obj){ return is_string($obj);}; |
|
95
|
|
|
$isEquals = function ($obj1, $obj2){ return (strcasecmp($obj1, $obj2) == 0);}; |
|
96
|
|
|
$typedArray = new TypedArray($isStringFct, $isEquals); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertFalse($typedArray->add(true)); |
|
|
|
|
|
|
99
|
|
|
$this->assertFalse($typedArray->add(new \stdClass())); |
|
100
|
|
|
$this->assertFalse($typedArray->add(42)); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$this->assertNotFalse($typedArray->add('plop')); |
|
|
|
|
|
|
103
|
|
|
$this->assertFalse($typedArray->contains('123')); |
|
104
|
|
|
$this->assertTrue($typedArray->contains('plop')); |
|
105
|
|
|
$this->assertTrue($typedArray->contains('PloP')); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
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: