Skip to content

Commit 6d90a3a

Browse files
authored
Merge pull request #15 from sirbrillig/add/do-arrays-match
Add helper function `do_arrays_match()`
2 parents f5a0f52 + f1a8555 commit 6d90a3a

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

API.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ $value = wp_update_post( 'hello' );
9595
$this->assertEquals( 'hello', $value );
9696
```
9797

98+
- `do_arrays_match( $a, $b )`: Compare two arrays allowing usage of `match_array()`.
99+
100+
```php
101+
$array = [ 'baz' => 'boo', 'foo' => 'bar' ];
102+
$this->assertTrue( \Spies\do_arrays_match( $array, \Spies\match_array( [ 'foo' => 'bar' ] ) ) );
103+
```
104+
98105
# Spy
99106

100107
### Static methods

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,15 @@ a spy was actually called with:
379379

380380
See the [API document](API.md) for the full list of custom assertions available.
381381

382+
## Assertion Helpers
383+
384+
For any assertion, even those not involving Spies or Stubs, it can be helpful to compare partial arrays in the same manner as `match_array()`. You can use the helper function `do_arrays_match()` to do this:
385+
386+
```php
387+
$array = [ 'baz' => 'boo', 'foo' => 'bar' ];
388+
$this->assertTrue( \Spies\do_arrays_match( $array, \Spies\match_array( [ 'foo' => 'bar' ] ) ) );
389+
```
390+
382391
# Spying and Mocking existing functions
383392

384393
PHP does not allow mocking existing functions. However, there is a library called [Patchwork](http://patchwork2.org/) which allows this. If that library is loaded, it will be used by Spies. The library must be loaded *before* Spies. One way to do this is to use a test bootstrap file.

src/Spies/Helpers.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ private static function do_vals_match( $a, $b ) {
3939
}
4040
return false;
4141
}
42+
43+
public static function do_arrays_match( $a, $b ) {
44+
return self::do_vals_match( $a, $b );
45+
}
4246
}

src/Spies/functions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ function match_array( $array ) {
5454
function passed_arg( $index ) {
5555
return new \Spies\PassedArgument( $index );
5656
}
57+
58+
function do_arrays_match( $array1, $array2 ) {
59+
return \Spies\Helpers::do_arrays_match( $array1, $array2 );
60+
}

tests/HelpersTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
class HelpersTest extends \Spies\TestCase {
4+
public function test_do_arrays_match_returns_true_if_arrays_are_the_same() {
5+
$array1 = [ 'a', 'b', 'c' ];
6+
$array2 = [ 'a', 'b', 'c' ];
7+
$this->assertTrue( \Spies\do_arrays_match( $array1, $array2 ) );
8+
}
9+
10+
public function test_do_arrays_match_returns_false_if_arrays_are_different() {
11+
$array1 = [ 'a', 'b', 'c' ];
12+
$array2 = [ 'b', 'c' ];
13+
$this->assertFalse( \Spies\do_arrays_match( $array1, $array2 ) );
14+
}
15+
16+
public function test_do_arrays_match_returns_true_if_using_match_array_and_arrays_match() {
17+
$array1 = [ 'a', 'b', 'c' ];
18+
$array2 = [ 'b', 'c' ];
19+
$this->assertTrue( \Spies\do_arrays_match( $array1, \Spies\match_array( $array2 ) ) );
20+
}
21+
22+
public function test_do_arrays_match_returns_false_if_using_match_array_and_arrays_differ() {
23+
$array1 = [ 'a', 'b', 'c' ];
24+
$array2 = [ 'd', 'c' ];
25+
$this->assertFalse( \Spies\do_arrays_match( $array1, \Spies\match_array( $array2 ) ) );
26+
}
27+
}
28+

tests/StubTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public function test_stub_when_called_with_sets_a_conditional_return_value_on_th
6161
$this->assertEquals( 6, test_stub( 'bar' ) );
6262
}
6363

64+
public function test_stub_when_called_with_match_array_sets_a_conditional_return_value_on_the_stub() {
65+
\Spies\mock_function( 'test_stub' )->will_return( 4 );
66+
\Spies\mock_function( 'test_stub' )->when_called->with( \Spies\match_array( [ 'type' => 'Bokoblin' ] ) )->will_return( 5 );
67+
\Spies\mock_function( 'test_stub' )->when_called->with( \Spies\match_array( [ 'type' => 'Moblin' ] ) )->will_return( 6 );
68+
$this->assertEquals( 5, test_stub( [ 'name' => 'Bobo', 'type' => 'Bokoblin' ] ) );
69+
$this->assertEquals( 6, test_stub( [ 'name' => 'Grup', 'type' => 'Moblin' ] ) );
70+
$this->assertEquals( 4, test_stub( [ 'name' => 'Corb', 'type' => 'Lizafos' ] ) );
71+
}
72+
6473
public function test_stub_with_conditional_returns_will_return_unconditional_value_when_called_with_unexpected_parameters() {
6574
\Spies\mock_function( 'test_stub' )->when_called->with( 'foo' )->will_return( 5 );
6675
\Spies\mock_function( 'test_stub' )->will_return( 7 );

0 commit comments

Comments
 (0)