Skip to content

Commit

Permalink
Merge pull request #18 from sirbrillig/add/match-pattern-string
Browse files Browse the repository at this point in the history
Add match_pattern
  • Loading branch information
sirbrillig authored Sep 25, 2017
2 parents 408d180 + bc19e32 commit 08f4c3b
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 0 deletions.
15 changes: 15 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ expect_spy( $spy )->to_have_been_called->with( any() );
finish_spying();
```

- `match_pattern()`: Used as an argument to `Expectation->with()` or `Spy()->with()` to mean "any string argument matching this PCRE pattern". Shortcut for `new MatchPattern()`.

```php
$spy = get_spy_for( 'run_experiment' );
run_experiment( 'slartibartfast' );
expect_spy( $spy )->to_have_been_called->with( match_pattern( '/bart/' ) );
finish_spying();
```

```php
mock_function( 'slartibartfast' )->when_called->with( match_pattern( '/bart/' ) )->will_return( 14 );
$id = run_experiment( 'slartibartfast' );
$this->assertEquals( 14, $id );
```

- `match_array()`: Used as an argument to `Expectation->with()` or `Spy()->with()` to mean "any argument with these values". Shortcut for `new MatchArray()`.

```php
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ function test_calculation() {
}
```

If you need to match just part of a string, you can use `\Spies\match_pattern()`.

```php
$spy = \Spies\get_spy_for( 'run_experiment' );
run_experiment( 'slartibartfast' );
\Spies\expect_spy( $spy )->to_have_been_called->with( \Spies\match_pattern( '/bart/' ) );
\Spies\finish_spying();
```

You can also use `\Spies\match_array()` to match elements of an array while ignoring other parts:

```php
Expand Down
6 changes: 6 additions & 0 deletions src/Spies/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ private static function do_vals_match( $a, $b ) {
if ( $a instanceof \Spies\AnyValue || $b instanceof \Spies\AnyValue ) {
return true;
}
if ( $a instanceof \Spies\MatchPattern && $a->is_match( $b ) ) {
return true;
}
if ( $b instanceof \Spies\MatchPattern && $b->is_match( $a ) ) {
return true;
}
if ( $a instanceof \Spies\MatchArray && $a->is_match( $b ) ) {
return true;
}
Expand Down
21 changes: 21 additions & 0 deletions src/Spies/MatchPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Spies;

class MatchPattern {
public function __construct( $pattern ) {
$this->expected_pattern = $pattern;
}

public function is_match( $actual ) {
if ( ! is_string( $actual ) ) {
return false;
}
// Convert 0 and FALSE to false and 1 to true
if ( preg_match( $this->expected_pattern, $actual ) ) {
return true;
}
return false;
}
}


4 changes: 4 additions & 0 deletions src/Spies/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function match_array( $array ) {
return new \Spies\MatchArray( $array );
}

function match_pattern( $pattern ) {
return new \Spies\MatchPattern( $pattern );
}

function passed_arg( $index ) {
return new \Spies\PassedArgument( $index );
}
Expand Down
12 changes: 12 additions & 0 deletions tests/AssertionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public function test_assert_spy_was_called_with_is_true_when_called_with_args()
$this->assertSpyWasCalledWith( $spy, [ 'a', 'b', 'c' ] );
}

public function test_assert_that_was_called_with_is_true_when_called_with_match_pattern_as_argument() {
$spy = \Spies\make_spy();
$spy( 'slartibartfast' );
$this->assertThat( $spy, $this->wasCalledWith( [ \Spies\match_pattern( '/Bart/i' ) ] ) );
}

public function test_assert_that_was_called_with_is_false_when_called_with_match_pattern_as_argument() {
$spy = \Spies\make_spy();
$spy( 'bar' );
$this->assertThat( $spy, $this->logicalNot( $this->wasCalledWith( [ \Spies\match_pattern( '/Bart/i' ) ] ) ) );
}

public function test_assert_that_was_called_with_is_true_when_called_with_match_array_as_argument() {
$spy = \Spies\make_spy();
$spy( [ 'foo' => 'bar', 'baz' => 'boo' ] );
Expand Down
15 changes: 15 additions & 0 deletions tests/ExpectationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ public function test_with_is_not_met_if_the_spy_is_called_and_function_returns_f
$this->assertFalse( $expectation->verify() );
}

public function test_with_match_pattern_is_met_if_the_spy_is_called_with_matching_pattern() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\match_pattern( '/Bart/i' ) );
$spy( 'foo', 'slartibartfast' );
$expectation->verify();
}

public function test_with_match_pattern_is_not_met_if_the_spy_is_called_with_differing_pattern() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\match_pattern( '/Bart/i' ) );
$expectation->silent_failures = true;
$spy( 'foo', 'slartiblargfast' );
$this->assertFalse( $expectation->verify() );
}

public function test_with_match_array_is_met_if_the_spy_is_called_with_matching_key_values() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\match_array( [ 'foo' => 'bar', 'flim' => 'flam' ] ) );
Expand Down
9 changes: 9 additions & 0 deletions tests/StubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public function test_stub_when_called_with_sets_a_conditional_return_value_on_th
$this->assertEquals( 6, test_stub( 'bar' ) );
}

public function test_stub_when_called_with_match_pattern_sets_a_conditional_return_value_on_the_stub() {
\Spies\mock_function( 'test_stub' )->will_return( 4 );
\Spies\mock_function( 'test_stub' )->when_called->with( \Spies\match_pattern( '/boko/i' ) )->will_return( 5 );
\Spies\mock_function( 'test_stub' )->when_called->with( \Spies\match_pattern( '/mob/i' ) )->will_return( 6 );
$this->assertEquals( 5, test_stub( 'Bokoblin' ) );
$this->assertEquals( 6, test_stub( 'Moblin' ) );
$this->assertEquals( 4, test_stub( 'Lizafos' ) );
}

public function test_stub_when_called_with_match_array_sets_a_conditional_return_value_on_the_stub() {
\Spies\mock_function( 'test_stub' )->will_return( 4 );
\Spies\mock_function( 'test_stub' )->when_called->with( \Spies\match_array( [ 'type' => 'Bokoblin' ] ) )->will_return( 5 );
Expand Down

0 comments on commit 08f4c3b

Please sign in to comment.