Skip to content

Json schema draft test suite #24

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

Open
wants to merge 8 commits into
base: master
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
24 changes: 21 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,26 @@ language: php
sudo: false

env:
global:
- CC_TEST_REPORTER_ID=5e32818628fac9eb11d34e2b35289f88169610cc4a98c6f170c74923342284f1
- TEST_SUITE="PHPModelGenerator" FILTER="." CC_TEST_REPORTER_ID=5e32818628fac9eb11d34e2b35289f88169610cc4a98c6f170c74923342284f1
- TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft3\]"
- TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft3/optional\]"
- TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft4\]"
- TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft4/optional\]"
- TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft6\]"
- TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft6/optional\]"
- TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft7\]"
- TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft7/optional\]"

jobs:
allow_failures:
- env: TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft3\]"
- env: TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft3/optional\]"
- env: TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft4\]"
- env: TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft4/optional\]"
- env: TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft6\]"
- env: TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft6/optional\]"
- env: TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft7\]"
- env: TEST_SUITE="JsonSchemaTestSuite" FILTER="Draft suite\[draft7/optional\]"

php:
- 7.2
Expand All @@ -28,7 +46,7 @@ before_script:
- mkdir -p build/logs

script:
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml --testdox
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml --testdox --testsuite="$TEST_SUITE" --filter="$FILTER"

after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
Expand Down
17 changes: 16 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.4"
"phpunit/phpunit": "^8.5 || ^9.4",
"json-schema/json-schema-test-suite": "2.0.0"
},
"suggest": {
"symplify/easy-coding-standard": "Allows pretty printing of the generated code"
Expand All @@ -29,6 +30,20 @@
"PHPModelGenerator\\": "src"
}
},
"repositories": [
{
"type": "package",
"package": {
"name": "json-schema/json-schema-test-suite",
"version": "2.0.0",
"source": {
"type": "git",
"url": "https://github.com/json-schema/JSON-Schema-Test-Suite",
"reference": "2.0.0"
}
}
}
],
"autoload-dev": {
"psr-4": {
"PHPModelGenerator\\Tests\\": "tests",
Expand Down
8 changes: 5 additions & 3 deletions docs/source/generator/postProcessor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ By default additional properties are not included in serialized models. If the *
Custom Post Processors
----------------------

You can implement custom post processors to accomplish your tasks. Each post processor must implement the **PHPModelGenerator\\SchemaProcessor\\PostProcessor\\PostProcessorInterface**. If you have implemented a post processor add the post processor to your `ModelGenerator` and the post processor will be executed for each class.
You can implement custom post processors to accomplish your tasks. Each post processor must extend the class **PHPModelGenerator\\SchemaProcessor\\PostProcessor\\PostProcessor**. If you have implemented a post processor add the post processor to your `ModelGenerator` and the post processor will be executed for each class.

A custom post processor which adds a custom trait to the generated model (eg. a trait adding methods for an active record pattern implementation) may look like:

Expand All @@ -153,9 +153,9 @@ A custom post processor which adds a custom trait to the generated model (eg. a
namespace MyApp\Model\Generator\PostProcessor;

use MyApp\Model\ActiveRecordTrait;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;

class ActiveRecordPostProcessor implements PostProcessorInterface
class ActiveRecordPostProcessor extends PostProcessor
{
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
{
Expand Down Expand Up @@ -184,3 +184,5 @@ What can you do inside your custom post processor?
If a setter for a property is called with the same value which is already stored internally (consequently no update of the property is required), the setters will return directly and as a result of that the setter hooks will not be executed.

This behaviour also applies also to properties changed via the *populate* method added by the `PopulatePostProcessor <#populatepostprocessor>`__ and the *setAdditionalProperty* method added by the `AdditionalPropertiesAccessorPostProcessor <#additionalpropertiesaccessorpostprocessor>`__

To execute code before/after the processing of the schemas override the methods **preProcess** and **postProcess** inside your custom post processor.
14 changes: 10 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
<directory>src</directory>
</include>
</coverage>
<testsuite name="PHPModelGenerator">
<directory>tests</directory>
<exclude>tests/manual</exclude>
</testsuite>
<testsuites>
<testsuite name="PHPModelGenerator">
<directory>tests</directory>
<exclude>tests/manual</exclude>
<exclude>tests/JsonSchemaTestSuite</exclude>
</testsuite>
<testsuite name="JsonSchemaTestSuite">
<directory>tests/JsonSchemaTestSuite</directory>
</testsuite>
</testsuites>
</phpunit>
4 changes: 2 additions & 2 deletions src/Model/RenderJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PHPModelGenerator\Exception\ValidationException;
use PHPModelGenerator\Model\Validator\AbstractComposedPropertyValidator;
use PHPModelGenerator\SchemaProcessor\Hook\SchemaHookResolver;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\Utils\RenderHelper;

/**
Expand Down Expand Up @@ -51,7 +51,7 @@ public function __construct(
}

/**
* @param PostProcessorInterface[] $postProcessors
* @param PostProcessor[] $postProcessors
* @param GeneratorConfiguration $generatorConfiguration
*/
public function postProcess(array $postProcessors, GeneratorConfiguration $generatorConfiguration)
Expand Down
8 changes: 4 additions & 4 deletions src/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPModelGenerator\Exception\SchemaException;
use PHPModelGenerator\Model\GeneratorConfiguration;
use PHPModelGenerator\SchemaProcessor\PostProcessor\Internal\CompositionValidationPostProcessor;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\SchemaProcessor\PostProcessor\SerializationPostProcessor;
use PHPModelGenerator\SchemaProcessor\RenderQueue;
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
Expand All @@ -27,7 +27,7 @@ class ModelGenerator
{
/** @var GeneratorConfiguration */
protected $generatorConfiguration;
/** @var PostProcessorInterface[] */
/** @var PostProcessor[] */
protected $postProcessors = [];

/**
Expand All @@ -48,11 +48,11 @@ public function __construct(GeneratorConfiguration $generatorConfiguration = nul
}

/**
* @param PostProcessorInterface $postProcessor
* @param PostProcessor $postProcessor
*
* @return $this
*/
public function addPostProcessor(PostProcessorInterface $postProcessor): self
public function addPostProcessor(PostProcessor $postProcessor): self
{
$this->postProcessors[] = $postProcessor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @package PHPModelGenerator\SchemaProcessor\PostProcessor
*/
class AdditionalPropertiesAccessorPostProcessor implements PostProcessorInterface
class AdditionalPropertiesAccessorPostProcessor extends PostProcessor
{
/** @var bool */
private $addForModelsWithoutAdditionalPropertiesDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
use PHPModelGenerator\Model\Validator\AbstractComposedPropertyValidator;
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\SchemaProcessor\PostProcessor\RenderedMethod;
use PHPModelGenerator\Utils\RenderHelper;

Expand All @@ -25,7 +25,7 @@
*
* @package PHPModelGenerator\SchemaProcessor\PostProcessor\Internal
*/
class CompositionValidationPostProcessor implements PostProcessorInterface
class CompositionValidationPostProcessor extends PostProcessor
{
/**
* @param Schema $schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @package PHPModelGenerator\SchemaProcessor\PostProcessor
*/
class PopulatePostProcessor implements PostProcessorInterface
class PopulatePostProcessor extends PostProcessor
{
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
{
Expand Down
33 changes: 33 additions & 0 deletions src/SchemaProcessor/PostProcessor/PostProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types = 1);

namespace PHPModelGenerator\SchemaProcessor\PostProcessor;

use PHPModelGenerator\Model\GeneratorConfiguration;
use PHPModelGenerator\Model\Schema;

abstract class PostProcessor
{
/**
* Have fun doin' crazy stuff with the schema
*
* @param Schema $schema
* @param GeneratorConfiguration $generatorConfiguration
*/
abstract public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void;

/**
* Overwrite this function to execute code before the schemas are processed by the post processor
*/
public function preProcess(): void
{
}

/**
* Overwrite this function to execute code after the schemas are processed by the post processor
*/
public function postProcess(): void
{
}
}
19 changes: 0 additions & 19 deletions src/SchemaProcessor/PostProcessor/PostProcessorInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @package PHPModelGenerator\SchemaProcessor\PostProcessor
*/
class SerializationPostProcessor implements PostProcessorInterface
class SerializationPostProcessor extends PostProcessor
{
/**
* Add serialization support to the provided schema
Expand Down
12 changes: 10 additions & 2 deletions src/SchemaProcessor/RenderQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PHPModelGenerator\Exception\RenderException;
use PHPModelGenerator\Model\GeneratorConfiguration;
use PHPModelGenerator\Model\RenderJob;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;

/**
* Class RenderQueue
Expand Down Expand Up @@ -37,7 +37,7 @@ public function addRenderJob(RenderJob $renderJob): self
*
* @param string $destination
* @param GeneratorConfiguration $generatorConfiguration
* @param PostProcessorInterface[] $postProcessors
* @param PostProcessor[] $postProcessors
*
* @throws FileSystemException
* @throws RenderException
Expand All @@ -47,11 +47,19 @@ public function execute(
GeneratorConfiguration $generatorConfiguration,
array $postProcessors
): void {
foreach ($postProcessors as $postProcessor) {
$postProcessor->preProcess();
}

foreach ($this->jobs as $job) {
$job->postProcess($postProcessors, $generatorConfiguration);
$job->render($destination, $generatorConfiguration);
}

foreach ($postProcessors as $postProcessor) {
$postProcessor->postProcess();
}

$this->jobs = [];
}
}
4 changes: 2 additions & 2 deletions tests/Basic/BasicSchemaGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PHPModelGenerator\Model\Schema;
use PHPModelGenerator\ModelGenerator;
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;

/**
Expand Down Expand Up @@ -66,7 +66,7 @@ public function testGetterAndSetterAreGeneratedForMutableObjects(bool $implicitN
public function testSetterLogicIsNotExecutedWhenValueIsIdentical(): void
{
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator): void {
$modelGenerator->addPostProcessor(new class () implements PostProcessorInterface {
$modelGenerator->addPostProcessor(new class () extends PostProcessor {
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
{
$schema->addSchemaHook(new class () implements SetterBeforeValidationHookInterface {
Expand Down
4 changes: 2 additions & 2 deletions tests/Basic/SchemaHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use PHPModelGenerator\SchemaProcessor\Hook\SchemaHookInterface;
use PHPModelGenerator\SchemaProcessor\Hook\SetterAfterValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;

/**
Expand Down Expand Up @@ -184,7 +184,7 @@ public function setterAfterValidationHookDataProvider(): array
protected function addSchemaHook(SchemaHookInterface $schemaHook): void
{
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator) use ($schemaHook): void {
$modelGenerator->addPostProcessor(new class ($schemaHook) implements PostProcessorInterface {
$modelGenerator->addPostProcessor(new class ($schemaHook) extends PostProcessor {
private $schemaHook;

public function __construct(SchemaHookInterface $schemaHook)
Expand Down
Loading