Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
This is the PHP port of Hamcrest Matchers
2
=========================================
3
 
4
[![Build Status](https://travis-ci.org/hamcrest/hamcrest-php.png?branch=master)](https://travis-ci.org/hamcrest/hamcrest-php)
5
 
6
Hamcrest is a matching library originally written for Java, but
7
subsequently ported to many other languages.  hamcrest-php is the
8
official PHP port of Hamcrest and essentially follows a literal
9
translation of the original Java API for Hamcrest, with a few
10
Exceptions, mostly down to PHP language barriers:
11
 
12
  1. `instanceOf($theClass)` is actually `anInstanceOf($theClass)`
13
 
14
  2. `both(containsString('a'))->and(containsString('b'))`
15
     is actually `both(containsString('a'))->andAlso(containsString('b'))`
16
 
17
  3. `either(containsString('a'))->or(containsString('b'))`
18
     is actually `either(containsString('a'))->orElse(containsString('b'))`
19
 
20
  4. Unless it would be non-semantic for a matcher to do so, hamcrest-php
21
     allows dynamic typing for it's input, in "the PHP way". Exception are
22
     where semantics surrounding the type itself would suggest otherwise,
23
     such as stringContains() and greaterThan().
24
 
25
  5. Several official matchers have not been ported because they don't
26
     make sense or don't apply in PHP:
27
 
28
       - `typeCompatibleWith($theClass)`
29
       - `eventFrom($source)`
30
       - `hasProperty($name)` **
31
       - `samePropertyValuesAs($obj)` **
32
 
33
  6. When most of the collections matchers are finally ported, PHP-specific
34
     aliases will probably be created due to a difference in naming
35
     conventions between Java's Arrays, Collections, Sets and Maps compared
36
     with PHP's Arrays.
37
 
38
---
39
** [Unless we consider POPO's (Plain Old PHP Objects) akin to JavaBeans]
40
     - The POPO thing is a joke.  Java devs coin the term POJO's (Plain Old
41
       Java Objects).
42
 
43
 
44
Usage
45
-----
46
 
47
Hamcrest matchers are easy to use as:
48
 
49
```php
50
Hamcrest_MatcherAssert::assertThat('a', Hamcrest_Matchers::equalToIgnoringCase('A'));
51
```
52
 
53
Alternatively, you can use the global proxy-functions:
54
 
55
```php
56
$result = true;
57
// with an identifier
58
assertThat("result should be true", $result, equalTo(true));
59
 
60
// without an identifier
61
assertThat($result, equalTo(true));
62
 
63
// evaluate a boolean expression
64
assertThat($result === true);
65
 
66
// with syntactic sugar is()
67
assertThat(true, is(true));
68
```
69
 
70
:warning: **NOTE:** the global proxy-functions aren't autoloaded by default, so you will need to load them first:
71
 
72
```php
73
\Hamcrest\Util::registerGlobalFunctions();
74
```
75
 
76
For brevity, all of the examples below use the proxy-functions.
77
 
78
 
79
Documentation
80
-------------
81
A tutorial can be found on the [Hamcrest site](https://code.google.com/archive/p/hamcrest/wikis/TutorialPHP.wiki).
82
 
83
 
84
Available Matchers
85
------------------
86
* [Array](../master/README.md#array)
87
* [Collection](../master/README.md#collection)
88
* [Object](../master/README.md#object)
89
* [Numbers](../master/README.md#numbers)
90
* [Type checking](../master/README.md#type-checking)
91
* [XML](../master/README.md#xml)
92
 
93
 
94
### Array
95
 
96
* `anArray` - evaluates an array
97
```php
98
assertThat([], anArray());
99
```
100
 
101
* `hasItemInArray` - check if item exists in array
102
```php
103
$list = range(2, 7, 2);
104
$item = 4;
105
assertThat($list, hasItemInArray($item));
106
```
107
 
108
* `hasValue` - alias of hasItemInArray
109
 
110
* `arrayContainingInAnyOrder` - check if array contains elements in any order
111
```php
112
assertThat([2, 4, 6], arrayContainingInAnyOrder([6, 4, 2]));
113
assertThat([2, 4, 6], arrayContainingInAnyOrder([4, 2, 6]));
114
```
115
 
116
* `containsInAnyOrder` - alias of arrayContainingInAnyOrder
117
 
118
* `arrayContaining` - An array with elements that match the given matchers in the same order.
119
```php
120
assertThat([2, 4, 6], arrayContaining([2, 4, 6]));
121
assertthat([2, 4, 6], not(arrayContaining([6, 4, 2])));
122
```
123
 
124
* `contains` - check array in same order
125
```php
126
assertThat([2, 4, 6], contains([2, 4, 6]));
127
```
128
 
129
* `hasKeyInArray` - check if array has given key
130
```php
131
assertThat(['name'=> 'foobar'], hasKeyInArray('name'));
132
```
133
 
134
* `hasKey` - alias of hasKeyInArray
135
 
136
* `hasKeyValuePair` - check if arary has given key, value pair
137
```php
138
assertThat(['name'=> 'foobar'], hasKeyValuePair('name', 'foobar'));
139
```
140
* `hasEntry` - same as hasKeyValuePair
141
 
142
* `arrayWithSize` - check array has given size
143
```php
144
assertthat([2, 4, 6], arrayWithSize(3));
145
```
146
* `emptyArray` - check if array is emtpy
147
```php
148
assertThat([], emptyArray());
149
```
150
 
151
* `nonEmptyArray`
152
```php
153
assertThat([1], nonEmptyArray());
154
```
155
 
156
### Collection
157
 
158
* `emptyTraversable` - check if traversable is empty
159
```php
160
$empty_it = new EmptyIterator;
161
assertThat($empty_it, emptyTraversable());
162
```
163
 
164
* `nonEmptyTraversable` - check if traversable isn't empty
165
```php
166
$non_empty_it = new ArrayIterator(range(1, 10));
167
assertThat($non_empty_it, nonEmptyTraversable());
168
a
169
```
170
 
171
* `traversableWithSize`
172
```php
173
$non_empty_it = new ArrayIterator(range(1, 10));
174
assertThat($non_empty_it, traversableWithSize(count(range(1, 10))));
175
`
176
```
177
 
178
### Core
179
 
180
* `allOf` - Evaluates to true only if ALL of the passed in matchers evaluate to true.
181
```php
182
assertThat([2,4,6], allOf(hasValue(2), arrayWithSize(3)));
183
```
184
 
185
* `anyOf` - Evaluates to true if ANY of the passed in matchers evaluate to true.
186
```php
187
assertThat([2, 4, 6], anyOf(hasValue(8), hasValue(2)));
188
```
189
 
190
* `noneOf` - Evaluates to false if ANY of the passed in matchers evaluate to true.
191
```php
192
assertThat([2, 4, 6], noneOf(hasValue(1), hasValue(3)));
193
```
194
 
195
* `both` + `andAlso` - This is useful for fluently combining matchers that must both pass.
196
```php
197
assertThat([2, 4, 6], both(hasValue(2))->andAlso(hasValue(4)));
198
```
199
 
200
* `either` + `orElse` - This is useful for fluently combining matchers where either may pass,
201
```php
202
assertThat([2, 4, 6], either(hasValue(2))->orElse(hasValue(4)));
203
```
204
 
205
* `describedAs` - Wraps an existing matcher and overrides the description when it fails.
206
```php
207
$expected = "Dog";
208
$found = null;
209
// this assertion would result error message as Expected: is not null but: was null
210
//assertThat("Expected {$expected}, got {$found}", $found, is(notNullValue()));
211
// and this assertion would result error message as Expected: Dog but: was null
212
//assertThat($found, describedAs($expected, notNullValue()));
213
```
214
 
215
* `everyItem` - A matcher to apply to every element in an array.
216
```php
217
assertThat([2, 4, 6], everyItem(notNullValue()));
218
```
219
 
220
* `hasItem` - check array has given item, it can take a matcher argument
221
```php
222
assertThat([2, 4, 6], hasItem(equalTo(2)));
223
```
224
 
225
* `hasItems` - check array has givem items, it can take multiple matcher as arguments
226
```php
227
assertThat([1, 3, 5], hasItems(equalTo(1), equalTo(3)));
228
```
229
 
230
### Object
231
 
232
* `hasToString` - check `__toString` or `toString` method
233
```php
234
class Foo {
235
    public $name = null;
236
 
237
    public function __toString() {
238
        return "[Foo]Instance";
239
    }
240
}
241
$foo = new Foo;
242
assertThat($foo, hasToString(equalTo("[Foo]Instance")));
243
```
244
 
245
* `equalTo` - compares two instances using comparison operator '=='
246
```php
247
$foo = new Foo;
248
$foo2 = new Foo;
249
assertThat($foo, equalTo($foo2));
250
```
251
 
252
* `identicalTo` - compares two instances using identity operator '==='
253
```php
254
assertThat($foo, is(not(identicalTo($foo2))));
255
```
256
 
257
* `anInstanceOf` - check instance is an instance|sub-class of given class
258
```php
259
assertThat($foo, anInstanceOf(Foo::class));
260
```
261
 
262
* `any` - alias of `anInstanceOf`
263
 
264
* `nullValue` check null
265
```php
266
assertThat(null, is(nullValue()));
267
```
268
 
269
* `notNullValue` check not null
270
```php
271
assertThat("", notNullValue());
272
```
273
 
274
* `sameInstance` - check for same instance
275
```php
276
assertThat($foo, is(not(sameInstance($foo2))));
277
assertThat($foo, is(sameInstance($foo)));
278
```
279
 
280
* `typeOf`- check type
281
```php
282
assertThat(1, typeOf("integer"));
283
```
284
 
285
* `notSet` - check if instance property is not set
286
```php
287
assertThat($foo, notSet("name"));
288
```
289
 
290
* `set` - check if instance property is set
291
```php
292
$foo->name = "bar";
293
assertThat($foo, set("name"));
294
```
295
 
296
### Numbers
297
 
298
* `closeTo` - check value close to a range
299
```php
300
assertThat(3, closeTo(3, 0.5));
301
```
302
 
303
* `comparesEqualTo` - check with '=='
304
```php
305
assertThat(2, comparesEqualTo(2));
306
```
307
 
308
* `greaterThan` - check '>'
309
```
310
assertThat(2, greaterThan(1));
311
```
312
 
313
* `greaterThanOrEqualTo`
314
```php
315
assertThat(2, greaterThanOrEqualTo(2));
316
```
317
 
318
* `atLeast` - The value is >= given value
319
```php
320
assertThat(3, atLeast(2));
321
```
322
* `lessThan`
323
```php
324
assertThat(2, lessThan(3));
325
```
326
 
327
* `lessThanOrEqualTo`
328
```php
329
assertThat(2, lessThanOrEqualTo(3));
330
```
331
 
332
* `atMost` - The value is <= given value
333
```php
334
assertThat(2, atMost(3));
335
```
336
 
337
### String
338
 
339
* `emptyString` - check for empty string
340
```php
341
assertThat("", emptyString());
342
```
343
 
344
* `isEmptyOrNullString`
345
```php
346
assertThat(null, isEmptyOrNullString());
347
```
348
 
349
* `nullOrEmptyString`
350
```php
351
assertThat("", nullOrEmptyString());
352
```
353
 
354
* `isNonEmptyString`
355
```php
356
assertThat("foo", isNonEmptyString());
357
```
358
 
359
* `nonEmptyString`
360
```php
361
assertThat("foo", nonEmptyString());
362
```
363
 
364
* `equalToIgnoringCase`
365
```php
366
assertThat("Foo", equalToIgnoringCase("foo"));
367
```
368
* `equalToIgnoringWhiteSpace`
369
```php
370
assertThat(" Foo ", equalToIgnoringWhiteSpace("Foo"));
371
```
372
 
373
* `matchesPattern` - matches with regex pattern
374
```php
375
assertThat("foobarbaz", matchesPattern('/(foo)(bar)(baz)/'));
376
```
377
 
378
* `containsString` - check for substring
379
```php
380
assertThat("foobar", containsString("foo"));
381
```
382
 
383
* `containsStringIgnoringCase`
384
```php
385
assertThat("fooBar", containsStringIgnoringCase("bar"));
386
```
387
 
388
* `stringContainsInOrder`
389
```php
390
assertThat("foo", stringContainsInOrder("foo"));
391
```
392
 
393
* `endsWith` - check string that ends with given value
394
```php
395
assertThat("foo", endsWith("oo"));
396
```
397
 
398
* `startsWith` - check string that starts with given value
399
```php
400
assertThat("bar", startsWith("ba"));
401
```
402
 
403
### Type-checking
404
 
405
* `arrayValue` - check array type
406
```php
407
assertThat([], arrayValue());
408
```
409
 
410
* `booleanValue`
411
```php
412
assertThat(true, booleanValue());
413
```
414
* `boolValue` - alias of booleanValue
415
 
416
* `callableValue` - check if value is callable
417
```php
418
$func = function () {};
419
assertThat($func, callableValue());
420
```
421
* `doubleValue`
422
```php
423
assertThat(3.14, doubleValue());
424
```
425
 
426
* `floatValue`
427
```php
428
assertThat(3.14, floatValue());
429
```
430
 
431
* `integerValue`
432
```php
433
assertThat(1, integerValue());
434
```
435
 
436
* `intValue` - alias of `integerValue`
437
 
438
* `numericValue` - check if value is numeric
439
```php
440
assertThat("123", numericValue());
441
```
442
 
443
* `objectValue` - check for object
444
```php
445
$obj = new stdClass;
446
assertThat($obj, objectValue());
447
```
448
* `anObject`
449
```php
450
assertThat($obj, anObject());
451
```
452
 
453
* `resourceValue` - check resource type
454
```php
455
$fp = fopen("/tmp/foo", "w+");
456
assertThat($fp, resourceValue());
457
```
458
 
459
* `scalarValue` - check for scaler value
460
```php
461
assertThat(1, scalarValue());
462
```
463
 
464
* `stringValue`
465
```php
466
assertThat("", stringValue());
467
```
468
 
469
### XML
470
 
471
* `hasXPath` - check xml with a xpath
472
```php
473
$xml = <<<XML
474
<books>
475
  <book>
476
    <isbn>1</isbn>
477
  </book>
478
  <book>
479
    <isbn>2</isbn>
480
  </book>
481
</books>
482
XML;
483
 
484
$doc = new DOMDocument;
485
$doc->loadXML($xml);
486
assertThat($doc, hasXPath("book", 2));
487
```
488