-
-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathInvalidArgumentExceptionTest.php
More file actions
228 lines (191 loc) · 9.81 KB
/
InvalidArgumentExceptionTest.php
File metadata and controls
228 lines (191 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* @package Functional-php
* @author Lars Strojny <lstrojny@php.net>
* @copyright 2011-2021 Lars Strojny
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/lstrojny/functional-php
*/
namespace Functional\Tests\Exceptions;
use Functional\Exceptions\InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class InvalidArgumentExceptionTest extends TestCase
{
public function testCallbackExceptionWithUndefinedStaticMethod(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("func() expects parameter 1 to be a valid callback, method 'stdClass::method' not found or invalid method name");
InvalidArgumentException::assertCallback(['stdClass', 'method'], 'func', 1);
}
public function testCallbackExceptionWithUndefinedFunction(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("func() expects parameter 1 to be a valid callback, function 'undefinedFunction' not found or invalid function name");
InvalidArgumentException::assertCallback('undefinedFunction', 'func', 1);
}
public function testCallbackExceptionWithUndefinedMethod(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("func() expects parameter 2 to be a valid callback, method 'stdClass->method' not found or invalid method name");
InvalidArgumentException::assertCallback([new \stdClass(), 'method'], 'func', 2);
}
public function testCallbackExceptionWithIncorrectArrayIndex(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("func() expects parameter 1 to be a valid callback, method 'stdClass->method' not found or invalid method name");
InvalidArgumentException::assertCallback([1 => new \stdClass(), 2 => 'method'], 'func', 1);
}
public function testCallbackExceptionWithObject(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expected parameter 1 to be a valid callback, no array, string, closure or functor given');
InvalidArgumentException::assertCallback(new \stdClass(), 'func', 1);
}
public function testExceptionIfStringIsPassedAsList(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("func() expects parameter 4 to be array or instance of Traversable, string given");
InvalidArgumentException::assertCollection('string', 'func', 4);
}
public function testExceptionIfObjectIsPassedAsList(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("func() expects parameter 2 to be array or instance of Traversable, stdClass given");
InvalidArgumentException::assertCollection(new \stdClass(), 'func', 2);
}
public function testAssertArrayAccessValidCase(): void
{
$validObject = new \ArrayObject();
InvalidArgumentException::assertArrayAccess($validObject, "func", 4);
$this->addToAssertionCount(1);
}
public function testAssertArrayAccessWithString(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects parameter 4 to be array or instance of ArrayAccess, string given');
InvalidArgumentException::assertArrayAccess('string', "func", 4);
}
public function testAssertArrayAccessWithStandardClass(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects parameter 2 to be array or instance of ArrayAccess, stdClass given');
InvalidArgumentException::assertArrayAccess(new \stdClass(), "func", 2);
}
public function testExceptionIfInvalidMethodName(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('foo() expects parameter 2 to be string, stdClass given');
InvalidArgumentException::assertMethodName(new \stdClass(), "foo", 2);
}
public function testExceptionIfInvalidPropertyName(): void
{
InvalidArgumentException::assertPropertyName('property', 'func', 2);
InvalidArgumentException::assertPropertyName(0, 'func', 2);
InvalidArgumentException::assertPropertyName(0.2, 'func', 2);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects parameter 2 to be a valid property name or array index, stdClass given');
InvalidArgumentException::assertPropertyName(new \stdClass(), "func", 2);
}
public function testNoExceptionThrownWithPositiveInteger(): void
{
$this->expectNotToPerformAssertions();
InvalidArgumentException::assertPositiveInteger('2', 'foo', 1);
InvalidArgumentException::assertPositiveInteger(2, 'foo', 1);
}
public function testExceptionIfNegativeIntegerInsteadOfPositiveInteger(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects parameter 2 to be positive integer, negative integer given');
InvalidArgumentException::assertPositiveInteger(-1, 'func', 2);
}
public function testExceptionIfStringInsteadOfPositiveInteger(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects parameter 2 to be positive integer, string given');
InvalidArgumentException::assertPositiveInteger('str', 'func', 2);
}
public function testAssertIntegerAccessWithString(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects parameter 4 to be integer, string given');
InvalidArgumentException::assertInteger('string', "func", 4);
}
public function testAssertIntegerAccessWithObject(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects parameter 4 to be integer, stdClass given');
InvalidArgumentException::assertInteger(new \stdClass(), "func", 4);
}
public function testAssertBooleanAccessWithString(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects parameter 4 to be boolean, string given');
InvalidArgumentException::assertBoolean('string', "func", 4);
}
public function testAssertBooleanAccessWithObject(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects parameter 4 to be boolean, stdClass given');
InvalidArgumentException::assertBoolean(new \stdClass(), "func", 4);
}
public function testAssertPairWithPair(): void
{
$this->expectNotToPerformAssertions();
InvalidArgumentException::assertPair([1, 2], "func", 1);
InvalidArgumentException::assertPair(['1', 2], "func", 1);
InvalidArgumentException::assertPair([1, '2'], "func", 1);
InvalidArgumentException::assertPair([new \stdClass(), '2'], "func", 1);
}
public function testAssertPairWithEmptyArray(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects paramter 1 to be a pair (array with two elements)');
InvalidArgumentException::assertPair([], "func", 1);
}
public function testAssertPairWithInvalidArray(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects paramter 1 to be a pair (array with two elements)');
InvalidArgumentException::assertPair(['one'], "func", 1);
}
public function testAssertPairWithTwoCharacterString(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects paramter 1 to be a pair (array with two elements)');
InvalidArgumentException::assertPair('ab', "func", 1);
}
public function testAssertPairWithThreeCharacterString(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects paramter 1 to be a pair (array with two elements)');
InvalidArgumentException::assertPair('abc', "func", 1);
}
public function testAssertStringableValid(): void
{
$this->expectNotToPerformAssertions();
InvalidArgumentException::assertStringable('abc', "func", 1);
InvalidArgumentException::assertStringable(1, "func", 1);
InvalidArgumentException::assertStringable(1.2, "func", 1);
InvalidArgumentException::assertStringable(1.2, "func", 1);
InvalidArgumentException::assertStringable(
new class
{
public function __toString()
{
}
},
"func",
1
);
}
public function testAssertStringableInvalid(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('func() expects paramter 1 to be a string or an object with a __toString method (got type boolean)');
InvalidArgumentException::assertStringable(false, "func", 1);
$this->expectExceptionMessage('func() expects paramter 1 to be a string or an object with a __toString method (got type array)');
InvalidArgumentException::assertStringable([], "func", 1);
$this->expectExceptionMessage('func() expects paramter 1 to be a string or an object with a __toString method (got type stdClass)');
InvalidArgumentException::assertStringable(new stdClass(), "func", 1);
}
}