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

Integration tests #16

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<phpunit bootstrap="vendor/autoload.php"
<phpunit bootstrap="tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true"
stopOnFailure="false"
syntaxCheck="false">
<testsuites for="Json Patch">
<testsuite name="unit">
Expand Down
14 changes: 14 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require 'vendor/autoload.php';

// Json Patch integration test files revision
// Update this from time to time
$revision = 'c91f7e8';
$repository = 'https://raw.githubusercontent.com/json-patch/json-patch-tests';

$specTests = file_get_contents($repository.'/'.$revision.'/spec_tests.json');
$tests = file_get_contents($repository.'/'.$revision.'/tests.json');

file_put_contents('tests/integration/specs.json', $specTests);
file_put_contents('tests/integration/tests.json', $tests);
2 changes: 2 additions & 0 deletions tests/integration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
specs.json
tests.json
118 changes: 118 additions & 0 deletions tests/integration/SpecTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

use Rs\Json\Patch;
use Rs\Json\Patch\FailedTestException;
use Rs\Json\Patch\InvalidOperationException;

class SpecTest extends PHPUnit_Framework_TestCase
{
/**
* @test
* @dataProvider nonErrorTestsProvider
*
* @param $targetDocument
* @param $patchDocument
* @param $expectedDocument
*/
public function itSucceedsAllNormalTests($targetDocument, $patchDocument, $expectedDocument)
{
// Arrange

// Act
$patch = new Patch($targetDocument, $patchDocument);
$patchedDocument = $patch->apply();

// Assert
if (!$expectedDocument) {
return;
}

$this->assertJsonStringEqualsJsonString(
$expectedDocument,
$patchedDocument
);
}

/**
* @test
* @dataProvider erroringTestsProvider
*
* @param $targetDocument
* @param $patchDocument
* @param string $error
* @param string $comment
*/
public function itFailsOnErroringTests($targetDocument, $patchDocument, $error, $comment = null)
{
// Arrange
if ($comment) {
$this->setName($comment);
}

$exception = null;

// Act
$patch = new Patch($targetDocument, $patchDocument);

try {
$patch->apply();
} catch (Exception $thrown) {
$exception = $thrown;
}

// Assert
$this->assertNotNull($exception, 'An exception should have been thrown');
$this->assertContains(
get_class($exception),
array('Rs\Json\Patch\InvalidOperationException', 'Rs\Json\Patch\FailedTestException')
);
}

/**
* @param object $test
*
* @return array
*/
public function mapper($test)
{
return array(
json_encode($test->doc),
json_encode($test->patch),
isset($test->error) ? $test->error : (isset($test->expected) ? json_encode($test->expected) : null),
);
}

/**
* @return array
*/
public function nonErrorTestsProvider()
{
return array_map(array($this, 'mapper'), array_filter($this->allTestsProvider(), function ($test) {
return !isset($test->error);
}));
}

/**
* @return array
*/
public function erroringTestsProvider()
{
return array_map(array($this, 'mapper'), array_filter($this->allTestsProvider(), function ($test) {
return isset($test->error);
}));
}

/**
* @return array
*/
public function allTestsProvider()
{
$tests = json_decode(file_get_contents('tests/integration/specs.json'))
+ json_decode(file_get_contents('tests/integration/tests.json'));

return array_filter($tests, function ($test) {
// If `disabled` is set and set to true, then don't include this test.
return isset($test->disabled) ? (!$test->disabled) : true;
});
}
}