Skip to content

Bump github/codeql-action from 1 to 2 #7

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

Closed
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
25 changes: 8 additions & 17 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
report_file: results.sarif

- name: Upload Security Analysis results to GitHub
uses: github/codeql-action/upload-sarif@v1
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: results.sarif

Expand Down Expand Up @@ -51,22 +51,13 @@ jobs:
strategy:
matrix:
php:
- 7.1
# - 7.2
# - 7.3
# - 7.4
# - 8.0
- 7.4
- 8.0
include:
- php: 7.1
phpunit: 7.5.20
# - php: 7.2
# phpunit: 8.5.13
# - php: 7.3
# phpunit: 9.5.0
# - php: 7.4
# phpunit: 9.5.0
# - php: 8.0
# phpunit: 9.5.0
- php: 7.4
phpunit: 9.5.0
- php: 8.0
phpunit: 9.5.0

steps:
- uses: actions/checkout@v2
Expand All @@ -81,7 +72,7 @@ jobs:
with:
php_version: ${{ matrix.php }}

- uses: php-actions/phpunit@v2
- uses: php-actions/phpunit@v9
with:
php_version: ${{ matrix.php }}
version: ${{ matrix.phpunit }}
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.0",
"php": "^7.4|^8.0",
"justinrainbow/json-schema": "^5.0",
"mtdowling/jmespath.php": "^2.3"
"mtdowling/jmespath.php": "^2.3",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^6",
"phpunit/phpunit": "^9",
"codacy/coverage": "dev-master",
"symfony/http-foundation": "^2.8|^3.0"
},
Expand Down
22 changes: 9 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="phpunit-json-assertions Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>

</include>
<report>
<clover outputFile="./build/logs/clover.xml"/>
</report>
</coverage>
</phpunit>
44 changes: 14 additions & 30 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace EnricoStahn\JsonAssert;

use JmesPath\Env;
use JsonSchema\Constraints\Factory;
use JsonSchema\SchemaStorage;
use JsonSchema\Validator;
Expand All @@ -26,9 +27,9 @@
trait Assert
{
/**
* @var SchemaStorage
* @var ?SchemaStorage
*/
private static $schemaStorage = null;
private static ?SchemaStorage $schemaStorage = null;

/**
* Asserts that json content is valid according to the provided schema file.
Expand All @@ -37,10 +38,10 @@ trait Assert
*
* static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json')
*
* @param string|null $schema Path to the schema file
* @param array|object $content JSON array or object
* @param ?string $schema Path to the schema file
*/
public static function assertJsonMatchesSchema($content, $schema = null)
public static function assertJsonMatchesSchema($content, ?string $schema = null): void
{
if (self::$schemaStorage === null) {
self::$schemaStorage = new SchemaStorage();
Expand All @@ -60,7 +61,7 @@ public static function assertJsonMatchesSchema($content, $schema = null)
$validator = new Validator(new Factory(self::$schemaStorage));
$validator->validate($content, $schemaObject);

$message = '- Property: %s, Contraint: %s, Message: %s';
$message = '- Property: %s, Constraint: %s, Message: %s';
$messages = array_map(function ($exception) use ($message) {
return sprintf($message, $exception['property'], $exception['constraint'], $exception['message']);
}, $validator->getErrors());
Expand All @@ -69,30 +70,13 @@ public static function assertJsonMatchesSchema($content, $schema = null)
\PHPUnit\Framework\Assert::assertTrue($validator->isValid(), implode("\n", $messages));
}

/**
* Asserts that json content is valid according to the provided schema file.
*
* Example:
*
* static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json')
*
* @param string|null $schema Path to the schema file
* @param array|object $content JSON array or object
*
* @deprecated This will be removed in the next major version (4.x).
*/
public static function assertJsonMatchesSchemaDepr($schema, $content)
{
self::assertJsonMatchesSchema($content, $schema);
}

/**
* Asserts that json content is valid according to the provided schema string.
*
* @param string $schema Schema data
* @param array|object $content JSON content
*/
public static function assertJsonMatchesSchemaString($schema, $content)
public static function assertJsonMatchesSchemaString(string $schema, $content): void
{
$file = tempnam(sys_get_temp_dir(), 'json-schema-');
file_put_contents($file, $schema);
Expand All @@ -107,17 +91,17 @@ public static function assertJsonMatchesSchemaString($schema, $content)
*
* static::assertJsonValueEquals(33, 'foo.bar[0]', $json);
*
* @param mixed $expected Expected value
* @param string $expression Expression to retrieve the result
* (e.g. locations[?state == 'WA'].name | sort(@))
* @param array|object $json JSON Content
* @param mixed $expected Expected value
* @param string $expression Expression to retrieve the result
* (e.g. locations[?state == 'WA'].name | sort(@))
* @param array|object|string $json JSON Content
*/
public static function assertJsonValueEquals($expected, $expression, $json)
public static function assertJsonValueEquals($expected, string $expression, $json): void
{
$result = \JmesPath\Env::search($expression, $json);
$result = Env::search($expression, $json);

\PHPUnit\Framework\Assert::assertEquals($expected, $result);
\PHPUnit\Framework\Assert::assertInternalType(strtolower(gettype($expected)), $result);
\PHPUnit\Framework\Assert::assertEquals(gettype($expected), gettype($result));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Extension/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ trait Symfony
* @param string $schema Path to the schema file
* @param Response $response JSON array or object
*/
public static function assertJsonMatchesSchema($schema, Response $response)
public static function assertJsonMatchesSchema(string $schema, Response $response): void
{
Assert::assertJsonMatchesSchemaDepr($schema, json_decode($response->getContent()));
Assert::assertJsonMatchesSchema(json_decode($response->getContent()), $schema);
}

/**
Expand All @@ -37,7 +37,7 @@ public static function assertJsonMatchesSchema($schema, Response $response)
* @param string $schema Schema data
* @param Response $response JSON content
*/
public static function assertJsonMatchesSchemaString($schema, Response $response)
public static function assertJsonMatchesSchemaString(string $schema, Response $response): void
{
Assert::assertJsonMatchesSchemaString($schema, json_decode($response->getContent()));
}
Expand All @@ -54,7 +54,7 @@ public static function assertJsonMatchesSchemaString($schema, Response $response
* (e.g. locations[?state == 'WA'].name | sort(@))
* @param Response $response JSON Content
*/
public static function assertJsonValueEquals($expected, $expression, $response)
public static function assertJsonValueEquals($expected, string $expression, Response $response): void
{
Assert::assertJsonValueEquals($expected, $expression, json_decode($response->getContent()));
}
Expand All @@ -67,7 +67,7 @@ public static function assertJsonValueEquals($expected, $expression, $response)
*
* @see \Bazinga\Bundle\RestExtraBundle\Test\WebTestCase::assertJsonResponse()
*/
public static function assertJsonResponse(Response $response, $statusCode = 200)
public static function assertJsonResponse(Response $response, int $statusCode = 200): void
{
\PHPUnit\Framework\Assert::assertEquals(
$statusCode,
Expand Down
8 changes: 4 additions & 4 deletions tests/AssertTraitImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ class AssertTraitImpl extends TestCase
{
use JsonAssert;

public function setUp()
public function setUp(): void
{
self::$schemaStorage = new SchemaStorage();
}

/**
* @param string $id
* @param string $schema
* @param string $id
* @param array|object $schema
*
* @return SchemaStorage
*/
public function testWithSchemaStore($id, $schema)
public function testWithSchemaStore(string $id, $schema): SchemaStorage
{
self::$schemaStorage->addSchema($id, $schema);

Expand Down
51 changes: 27 additions & 24 deletions tests/AssertTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;

class AssertTraitTest extends TestCase
{
Expand All @@ -25,24 +26,30 @@ public function testAssertJsonMatchesSchemaSimple()
{
$content = json_decode(file_get_contents(Utils::getJsonPath('assertJsonMatchesSchema_simple.json')));

AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json'), $content);
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json'));
}

public function testAssertJsonMatchesSchema()
{
$content = json_decode('{"foo":123}');

AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('test.schema.json'), $content);
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json'));
}

/**
* @expectedException \PHPUnit\Framework\ExpectationFailedException
*/
public function testAssertJsonMatchesSchemaFail()
{
$this->expectException(ExpectationFailedException::class);
$content = json_decode('{"foo":"123"}');

AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('test.schema.json'), $content);
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json'));
}

public function testAssertThrowsFileNotFoundException()
{
$this->expectException(FileNotFoundException::class);
$content = json_decode('{"foo":"123"}');

AssertTraitImpl::assertJsonMatchesSchema($content, 'not-found.json');
}

public function testAssertJsonMatchesSchemaFailMessage()
Expand All @@ -52,10 +59,10 @@ public function testAssertJsonMatchesSchemaFailMessage()
$exception = null;

try {
AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('test.schema.json'), $content);
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json'));
} catch (ExpectationFailedException $exception) {
self::assertContains('- Property: foo, Contraint: type, Message: String value found, but an integer is required', $exception->getMessage());
self::assertContains('- Response: {"foo":"123"}', $exception->getMessage());
self::assertStringContainsString('- Property: foo, Constraint: type, Message: String value found, but an integer is required', $exception->getMessage());
self::assertStringContainsString('- Response: {"foo":"123"}', $exception->getMessage());
}

self::assertInstanceOf('\PHPUnit\Framework\ExpectationFailedException', $exception);
Expand All @@ -68,17 +75,15 @@ public function testAssertJsonMatchesSchemaWithRefs()
{
$content = json_decode('{"code":123, "message":"Nothing works."}');

AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('error.schema.json'), $content);
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('error.schema.json'));
}

/**
* @expectedException \PHPUnit\Framework\ExpectationFailedException
*/
public function testAssertJsonMatchesSchemaWithRefsFails()
{
$this->expectException(ExpectationFailedException::class);
$content = json_decode('{"code":"123", "message":"Nothing works."}');

AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('error.schema.json'), $content);
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('error.schema.json'));
}

public function testAssertJsonMatchesSchemaString()
Expand All @@ -97,7 +102,7 @@ public function testAssertJsonMatchesSchemaString()
* @param string $expression
* @param mixed $value
*/
public function testAssertJsonValueEquals($expression, $value)
public function testAssertJsonValueEquals(string $expression, $value)
{
$content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json')));

Expand All @@ -109,28 +114,26 @@ public function testAssertWithSchemaStore()
$obj = new AssertTraitImpl();
$obj->setUp();

$schemastore = $obj->testWithSchemaStore('foobar', (object) ['type' => 'string']);
$schemaStore = $obj->testWithSchemaStore('foobar', (object) ['type' => 'string']);

self::assertInstanceOf('JsonSchema\SchemaStorage', $schemastore);
self::assertEquals($schemastore->getSchema('foobar'), (object) ['type' => 'string']);
self::assertInstanceOf('JsonSchema\SchemaStorage', $schemaStore);
self::assertEquals($schemaStore->getSchema('foobar'), (object) ['type' => 'string']);
}

public function assertJsonValueEqualsProvider()
public function assertJsonValueEqualsProvider(): array
{
return [
['foo', '123'],
['a.b.c[0].d[1][0]', 1],
];
}

/**
* @expectedException \PHPUnit\Framework\ExpectationFailedException
*/
public function testAssertJsonValueEqualsFailsOnWrongDataType()
{
$this->expectException(ExpectationFailedException::class);
$content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json')));

AssertTraitImpl::assertJsonValueEquals($content, 'a.b.c[0].d[1][0]', '1');
AssertTraitImpl::assertJsonValueEquals($content, 'a.b.c[0].d[1][0]', '{}');
}

/**
Expand All @@ -141,7 +144,7 @@ public function testGetJsonObject($expected, $actual)
self::assertEquals($expected, AssertTraitImpl::getJsonObject($actual));
}

public function jsonObjectProvider()
public function jsonObjectProvider(): array
{
return [
[[], []],
Expand Down
Loading