Skip to content

Commit

Permalink
Repository maintenance, migrate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Dec 13, 2024
1 parent 8bcc583 commit 0919fd9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/leanness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
php-version: "${{ matrix.php }}"

- name: Install Composer dependencies
run: composer update --no-progress --no-suggest --prefer-dist --optimize-autoloader
run: composer update --no-progress --prefer-dist --optimize-autoloader

- name: Validate package leanness
run: composer run-script validate-gitattributes
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
php-version: "${{ matrix.php }}"

- name: Install Composer dependencies
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Coding styles
run: composer run-script cs-lint
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
php-version: "${{ matrix.php }}"

- name: Install Composer dependencies
run: composer update --no-progress --no-suggest --prefer-dist --optimize-autoloader
run: composer update --no-progress --prefer-dist --optimize-autoloader

- name: Tests
run: composer run-script test
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "8.*",
"phpunit/phpunit": "^11.4.4||^10.5.25||^8.0",
"friendsofphp/php-cs-fixer": "^3.0",
"stolt/lean-package-validator": "^3.0"
},
Expand Down
23 changes: 11 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Json Pointer Test Suite">
<directory>tests/</directory>
</testsuite>
<testsuite name="Json Pointer Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>

128 changes: 54 additions & 74 deletions tests/Rs/Json/PointerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace Rs\Json;

use ArrayObject;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Rs\Json\Pointer;
use Rs\Json\Pointer\InvalidJsonException;
Expand All @@ -11,21 +13,18 @@

class PointerTest extends TestCase
{
/**
* @dataProvider invalidJsonProvider
* @test
*/
#[Test]
#[DataProvider('invalidJsonProvider')]
public function constructShouldThrowExpectedExceptionWhenUsingInvalidJson($invalidJson)
{
$this->expectException(InvalidJsonException::class);
$this->expectExceptionMessage('Cannot operate on invalid Json.');

$jsonPointer = new Pointer($invalidJson);
}
/**
* @test
* @dataProvider invalidPointerCharProvider
*/

#[Test]
#[DataProvider('invalidPointerCharProvider')]
public function getShouldThrowExpectedExceptionWhenPointerStartsWithInvalidPointerChar($invalidPointerChar)
{
$this->expectException(InvalidPointerException::class);
Expand All @@ -34,10 +33,9 @@ public function getShouldThrowExpectedExceptionWhenPointerStartsWithInvalidPoint
$jsonPointer = new Pointer('{"a": 1}');
$jsonPointer->get($invalidPointerChar);
}
/**
* @test
* @dataProvider nonStringPointerProvider
*/

#[Test]
#[DataProvider('nonStringPointerProvider')]
public function getShouldThrowExpectedExceptionWhenPointerIsNotAString($nonStringPointer)
{
$this->expectException(InvalidPointerException::class);
Expand All @@ -46,10 +44,9 @@ public function getShouldThrowExpectedExceptionWhenPointerIsNotAString($nonStrin
$jsonPointer = new Pointer('{"a": 1}');
$jsonPointer->get($nonStringPointer);
}
/**
* @dataProvider nonWalkableJsonProvider
* @test
*/

#[Test]
#[DataProvider('nonWalkableJsonProvider')]
public function getShouldThrowExpectedExceptionWhenUsingNonWalkableJson($nonWalkableJson)
{
$this->expectException(NonWalkableJsonException::class);
Expand All @@ -58,9 +55,8 @@ public function getShouldThrowExpectedExceptionWhenUsingNonWalkableJson($nonWalk
$jsonPointer = new Pointer($nonWalkableJson);
$jsonPointer->get('/');
}
/**
* @test
*/

#[Test]
public function getShouldReturnGivenJsonWhenUsingOnlyARootPointer()
{
$givenJson = '{"status":["done","started","planned"]}';
Expand All @@ -73,9 +69,8 @@ public function getShouldReturnGivenJsonWhenUsingOnlyARootPointer()
'Unexpected mismatch between given and pointed Json'
);
}
/**
* @test
*/

#[Test]
public function getShouldNotCastEmptyObjectsToArrays()
{
$givenJson = '{"foo":{"bar":{},"baz":"qux"}}';
Expand All @@ -85,9 +80,8 @@ public function getShouldNotCastEmptyObjectsToArrays()
$this->assertTrue(($pointedJson instanceof \stdClass));
$this->assertTrue(($pointedJson->bar instanceof \stdClass));
}
/**
* @test
*/

#[Test]
public function getShouldNotEscapeUnicode()
{
$givenJson = '{"status":["第","二","个"]}';
Expand All @@ -100,9 +94,8 @@ public function getShouldNotEscapeUnicode()
'Escaped unicode between given and pointed Json'
);
}
/**
* @test
*/

#[Test]
public function getPointerShouldReturnFedPointer()
{
$givenJson = '{"status": ["done", "started", "planned"]}';
Expand All @@ -112,40 +105,36 @@ public function getPointerShouldReturnFedPointer()

$this->assertEquals($pointer, $jsonPointer->getPointer());
}
/**
* @test
*/

#[Test]
public function getShouldReturnExpectedValueOfSecondElementBelowNamedPointer()
{
$givenJson = '{"status": ["done", "started", "planned"]}';
$jsonPointer = new Pointer($givenJson);

$this->assertEquals('started', $jsonPointer->get('/status/1'));
}
/**
* @test
*/

#[Test]
public function getShouldReturnExpectedValueOfFourthElement()
{
$givenJson = '["done", "started", "planned","pending","archived"]';
$jsonPointer = new Pointer($givenJson);

$this->assertEquals('pending', $jsonPointer->get('/3'));
}
/**
* @test
*/

#[Test]
public function getShouldReturnExpectedValueOfFourthElementWithNoEscapeUnicode()
{
$givenJson = '["done", "started", "planned","第二个","archived"]';
$jsonPointer = new Pointer($givenJson);

$this->assertEquals('第二个', $jsonPointer->get('/3'));
}
/**
* @test
* @dataProvider nonexistentValueProvider
*/

#[Test]
#[DataProvider('nonexistentValueProvider')]
public function getShouldThrowExpectedExceptionWhenNonexistentValueIsReferenced($givenJson, $givenPointer)
{
$this->expectException(NonexistentValueReferencedException::class);
Expand All @@ -154,40 +143,36 @@ public function getShouldThrowExpectedExceptionWhenNonexistentValueIsReferenced(
$jsonPointer = new Pointer($givenJson);
$jsonPointer->get($givenPointer);
}
/**
* @test
*/

#[Test]
public function getShouldReturnNullAsAValidValue()
{
$givenJson = '{"a":{"b":null}}';
$jsonPointer = new Pointer($givenJson);

$this->assertNull($jsonPointer->get('/a/b'));
}
/**
* @test
*/

#[Test]
public function getShouldReturnExpectedValueOfSecondElementBelowDeepNamedPointer()
{
$givenJson = '{"categories":{"a":{"a1":{"a1a":["a1aa"],"a1b":["a1bb"]},"a2":["a2a","a2b"]}}}';
$jsonPointer = new Pointer($givenJson);

$this->assertEquals('a2b', $jsonPointer->get('/categories/a/a2/1'));
}
/**
* @test
*/

#[Test]
public function getShouldReturnExpectedValueOfPointerWithSlashInKey()
{
$givenJson = '{"test/foo.txt":{"size":1521,"description":"Text File"}}';
$jsonPointer = new Pointer($givenJson);

$this->assertEquals(1521, $jsonPointer->get('/test%2Ffoo.txt/size'));
}
/**
* @test
* @dataProvider lastArrayElementsTestDataProvider
*/

#[Test]
#[DataProvider('lastArrayElementsTestDataProvider')]
public function getShouldReturnLastArrayElementWhenHypenIsGiven($testData)
{
$givenJson = $testData['given-json'];
Expand All @@ -198,9 +183,8 @@ public function getShouldReturnLastArrayElementWhenHypenIsGiven($testData)
$jsonPointer->get($testData['given-pointer'])
);
}
/**
* @test
*/

#[Test]
public function getShouldTraverseToObjectPropertiesAfterArrayIndex()
{
$givenJson = '{"foo": {"bar": {"baz": [ {"bar":"baz"}, {"bar":"qux"} ] }}}';
Expand All @@ -209,9 +193,7 @@ public function getShouldTraverseToObjectPropertiesAfterArrayIndex()
$this->assertEquals('baz', $jsonPointer->get('/foo/bar/baz/0/bar'));
$this->assertEquals('qux', $jsonPointer->get('/foo/bar/baz/1/bar'));
}
/**
* @test
*/
#[Test]
public function referenceTokenGettingEvaluated()
{
$givenJson = '{"a/b/c": 1, "m~n": 8, "a": {"b": {"c": 12} } }';
Expand All @@ -221,20 +203,18 @@ public function referenceTokenGettingEvaluated()
$this->assertEquals(8, $jsonPointer->get('/m~0n'));
$this->assertEquals(12, $jsonPointer->get('/a/b/c'));
}
/**
* @dataProvider specSpecialCaseProvider
* @test
*/

#[Test]
#[DataProvider('specSpecialCaseProvider')]
public function specialCasesFromSpecAreMatched($expectedValue, $pointer)
{
$givenJson = '{"foo":["bar","baz"],"":0,"a/b":1,"c%d":2,"e^f":3,"g|h":4,"k\"l":6," ":7,"m~n":8}';
$jsonPointer = new Pointer($givenJson);

$this->assertSame($expectedValue, $jsonPointer->get($pointer));
}
/**
* @test
*/

#[Test]
public function getShouldReturnEmptyJson()
{
$givenJson = $expectedValue = '[]';
Expand All @@ -246,7 +226,7 @@ public function getShouldReturnEmptyJson()
/**
* @return array
*/
public function invalidPointerCharProvider()
public static function invalidPointerCharProvider(): array
{
return array(
array('*'),
Expand All @@ -257,7 +237,7 @@ public function invalidPointerCharProvider()
/**
* @return array
*/
public function lastArrayElementsTestDataProvider()
public static function lastArrayElementsTestDataProvider(): array
{
return array(
array(array(
Expand All @@ -275,7 +255,7 @@ public function lastArrayElementsTestDataProvider()
/**
* @return array
*/
public function specSpecialCaseProvider()
public static function specSpecialCaseProvider(): array
{
return array(
array('{"foo":["bar","baz"],"":0,"a\/b":1,"c%d":2,"e^f":3,"g|h":4,"k\"l":6," ":7,"m~n":8}', ''),
Expand All @@ -294,7 +274,7 @@ public function specSpecialCaseProvider()
/**
* @return array
*/
public function nonexistentValueProvider()
public static function nonexistentValueProvider(): array
{
return array(
array('["done", "started", "planned","pending","archived"]', '/6'),
Expand All @@ -306,7 +286,7 @@ public function nonexistentValueProvider()
/**
* @return array
*/
public function nonStringPointerProvider()
public static function nonStringPointerProvider(): array
{
return array(
array(array()),
Expand All @@ -318,7 +298,7 @@ public function nonStringPointerProvider()
/**
* @return array
*/
public function invalidJsonProvider()
public static function invalidJsonProvider(): array
{
return array(
array('['),
Expand All @@ -330,7 +310,7 @@ public function invalidJsonProvider()
/**
* @return array
*/
public function nonWalkableJsonProvider()
public static function nonWalkableJsonProvider(): array
{
return array(
array('6'),
Expand Down

0 comments on commit 0919fd9

Please sign in to comment.