Skip to content

Commit 80727d2

Browse files
committed
Updates
1 parent 5df627d commit 80727d2

File tree

6 files changed

+54
-31
lines changed

6 files changed

+54
-31
lines changed

.github/workflows/Test.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ jobs:
3535
php-version: ${{ matrix.php }}
3636
extensions: json
3737
coverage: pcov
38-
#tools: cs2pr
3938
env:
4039
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4140

@@ -46,7 +45,7 @@ jobs:
4645
run: composer install --prefer-dist --no-interaction --no-suggest
4746

4847
- name: Run phpcs
49-
run: composer cs-check #| cs2pr
48+
run: composer cs-check -- -v
5049

5150
- name: Setup PCOV
5251
if: matrix.php == '7.1'
@@ -55,7 +54,7 @@ jobs:
5554
vendor/bin/pcov clobber
5655
5756
- name: Execute tests
58-
run: ./vendor/bin/phpunit -v --configuration ./phpunit.xml.dist --coverage-clover=coverage.xml
57+
run: composer test -- -v --coverage-clover=coverage.xml
5958

6059
- name: Run codecov
6160
uses: codecov/codecov-action@v1

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"minimum-stability": "stable",
5050
"scripts": {
5151
"cs-check": "phpcs",
52-
"cs-fix": "phpcbf"
52+
"cs-fix": "phpcbf",
53+
"test": "phpunit"
5354
},
5455
"support": {
5556
"email": "[email protected]",

src/AccessHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public static function keyExists($collection, $key, bool $magicIsAllowed = false
6161
}
6262

6363
if (is_int($key) && $key < 0) {
64-
$key = abs((int)$key);
64+
$key = abs($key);
6565
}
6666

6767
if (is_array($collection) || $collection instanceof ArrayAccess) {
6868
return array_key_exists($key, $collection);
6969
}
7070

7171
if (is_object($collection)) {
72-
return property_exists($collection, (string)$key);
72+
return property_exists($collection, $key);
7373
}
7474

7575
return false;

src/JSONPathToken.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ class JSONPathToken
2626
public const T_SLICE = 'slice';
2727
public const T_INDEXES = 'indexes';
2828

29-
/** @var string */
29+
/**
30+
* @var string
31+
*/
3032
public $type;
3133

32-
/** @var mixed */
34+
/**
35+
* @var mixed
36+
*/
3337
public $value;
3438

3539
/**

tests/QueryTest.php

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
namespace Flow\JSONPath\Test;
1212

13-
use Exception;
14-
use Flow\JSONPath\JSONPath;
13+
use Flow\JSONPath\{JSONPath, JSONPathException};
1514
use PHPUnit\Framework\{ExpectationFailedException, TestCase};
1615

1716
use function fwrite;
@@ -33,44 +32,62 @@ class QueryTest extends TestCase
3332
* printed to the console (using STDERR), so we know, what's going on.
3433
*
3534
* @see https://cburgmer.github.io/json-path-comparison
36-
*
37-
* @small
3835
* @dataProvider queryDataProvider
39-
*
4036
* @param string $query
4137
* @param string $selector
4238
* @param string $data
4339
* @param string $consensus
4440
* @param bool $allowFail
45-
* @throws Exception
41+
* @param bool $skip
42+
* @throws JSONPathException
4643
*/
4744
public function testQueries(
4845
string $query,
4946
string $selector,
5047
string $data,
5148
string $consensus,
52-
bool $allowFail = false
49+
bool $allowFail = false,
50+
bool $skip = false
5351
): void {
52+
if ($skip) {
53+
// Avoid "This test did not perform any assertions"
54+
// but do not use markTestSkipped, to prevent unnecessary
55+
// console outputs
56+
self::assertTrue(true);
57+
58+
return;
59+
}
60+
61+
$results = json_encode((new JSONPath(json_decode($data, true)))->find($selector));
62+
5463
if ($allowFail) {
5564
try {
56-
self::assertEquals(
57-
$consensus,
58-
json_encode((new JSONPath(json_decode($data, true)))->find($selector))
59-
);
65+
self::assertEquals($consensus, $results);
6066
} catch (ExpectationFailedException $e) {
6167
$comparisonFailure = $e->getComparisonFailure();
6268

6369
fwrite(STDERR, "Query: {$query}\n{$comparisonFailure->toString()}");
6470
}
6571
} else {
66-
self::assertEquals(
67-
$consensus,
68-
json_encode((new JSONPath(json_decode($data, true)))->find($selector))
69-
);
72+
self::assertEquals($consensus, $results);
7073
}
7174
}
7275

7376
/**
77+
* Returns a list of queries, test data and expected results.
78+
*
79+
* A hand full of queries may run forever, thus they should
80+
* be skipped.
81+
*
82+
* Queries that are currently known as "problematic" are:
83+
*
84+
* - array_slice_with_negative_step_and_start_greater_than_end
85+
* - array_slice_with_open_end_and_negative_step
86+
* - array_slice_with_large_number_for_start
87+
* - array_slice_with_large_number_for_end
88+
* - array_slice_with_open_start_and_negative_step
89+
* - array_slice_with_negative_step_only
90+
*
7491
* @return string[]
7592
* @todo Finish this list
7693
*/
@@ -113,13 +130,14 @@ public function queryDataProvider(): array
113130
'["first", "second", "third", "forth", "fifth"]',
114131
'[]', // Unknown consensus, might be ["third","second","first"]
115132
],
116-
//[
117-
// 'Array slice with large number for start',
118-
// '$[-113667776004:2]',
119-
// '["first","second","third","forth","fifth"]',
120-
// '[]', // Unknown consensus, might be ["first","second"],
121-
// true
122-
//]
133+
[
134+
'Array slice with large number for start',
135+
'$[-113667776004:2]',
136+
'["first","second","third","forth","fifth"]',
137+
'[]', // Unknown consensus, might be ["first","second"],
138+
true, // Allow fail
139+
true // Skip
140+
]
123141
];
124142
}
125143
}

tests/Traits/TestDataTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Flow\JSONPath\Test\Traits;
1212

13+
use ArrayAccess;
1314
use RuntimeException;
1415

1516
trait TestDataTrait
@@ -19,7 +20,7 @@ trait TestDataTrait
1920
*
2021
* @param string $type
2122
* @param bool|int $asArray
22-
* @return mixed
23+
* @return array|ArrayAccess|null
2324
*/
2425
protected function getData(string $type, $asArray = true)
2526
{

0 commit comments

Comments
 (0)