Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #13 #14

Merged
merged 5 commits into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog for Behat API Extension
=================================

v1.0.3
------
__N/A__

Bug fixes:

* #13: Checking multi-dimensional arrays

v1.0.2
------
__2016-09-15__
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@
"scripts": {
"clean": "rm -rf build",
"lint": "for file in `git ls-files '*php'`; do php -l $file; done",
"test-phpunit": "vendor/bin/phpunit",
"test-behat": "vendor/bin/behat --strict",
"test": [
"vendor/bin/phpunit",
"vendor/bin/behat --strict"
"@test-phpunit",
"@test-behat"
],
"start-server": "php -S localhost:8080 -t ./features/bootstrap > server.log 2>&1 &"
}
Expand Down
26 changes: 26 additions & 0 deletions features/bootstrap/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,31 @@
], 500);
});

$app->match('/issue-13', function(Application $app) {
return new JsonResponse([
'customer' => [
'id' => '12345',
'name' => 'Behat Testing API',
'images' => [
[
'id' => '5678',
'filename_client' => 'tech.ai',
'filename_preview' => 'testimage-converted.png',
'filename_print' => 'testimage.ai',
'url' => '\/media\/testimage-converted.png',
'created_time' => '2016-10-10 07 => 28 => 42'
], [
'id' => '7890',
'filename_client' => 'demo.ai',
'filename_preview' => 'demoimage-converted.png',
'filename_print' => 'demoimage.ai',
'url' => '\/media\/demoimage-converted.png',
'created_time' => '2016-10-10 07 => 38 => 22'
],
],
],
]);
});

// Run the application
$app->run();
46 changes: 46 additions & 0 deletions features/issue-13.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Feature: Fix issue #13
In order to check multidimensional arrays
As an extension
I need to recursively compare arrays

Background:
Given a file named "behat.yml" with:
"""
default:
formatters:
progress: ~
extensions:
Imbo\BehatApiExtension:
base_uri: http://localhost:8080

suites:
default:
contexts: ['Imbo\BehatApiExtension\Context\ApiContext']
"""

Scenario: Recursively compare arrays
Given a file named "features/issue-13.feature" with:
"""
Feature: Recursively check arrays
Scenario: Check the value for a sub-array
When I request "/issue-13"
Then the response code is 200
And the response body contains:
'''
{
"customer": {
"images[0]": {
"filename_client": "tech.ai"
}
}
}
'''
"""
When I run "behat features/issue-13.feature"
Then it should pass with:
"""
...

1 scenario (1 passed)
3 steps (3 passed)
"""
6 changes: 6 additions & 0 deletions src/ArrayContainsComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public function compare(array $haystack, array $needle, $path = null) {
continue;
}

if (is_array($value) && is_array($haystack[$key][$index])) {
// Recursively compare the haystack against the needle
$this->compare($haystack[$key][$index], $value);
continue;
}

if ($value !== $haystack[$key][$index]) {
throw new InvalidArgumentException(sprintf(
'Item on index %d in array at haystak key "%s" does not match value %s',
Expand Down
29 changes: 29 additions & 0 deletions tests/ArrayContainsComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,35 @@ public function getValuesToCompare() {
'willFail' => false,
],

// @see https://github.com/imbo/behat-api-extension/issues/13
'match sub-arrays using indexes' => [
'haystack' => [
'foo' => [
'bar' => [
[
'foo' => 'bar',
'baz' => 'bat',
],
[
'foo' => 'bar',
'baz' => 'bat',
],
],
],
],
'needle' => [
'foo' => [
'bar[0]' => [
'foo' => 'bar',
],
'bar[1]' => [
'baz' => 'bat',
]
]
],
'willFail' => false,
],

'MATCH ALL THE THINGS!!!' => [
'haystack' => [
'null' => null,
Expand Down