clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
<?php
namespace Shaper\Tests\DataAdaptor;
use PHPUnit\Framework\TestCase;
use Shaper\Util\Context;
/**
* @package Shaper
*
* @coversDefaultClass \Shaper\DataAdaptor\DataAdaptorBase
*/
class DataAdaptorBaseTest extends TestCase {
/**
* @var \Shaper\Util\Context
*/
protected $context;
protected function setUp(): void {
parent::setUp();
$this->context = new Context();
$this->context['key'] = 'lorem';
}
/**
* @covers ::transform
* @covers ::conformsToExpectedInputShape
* @covers ::conformsToInternalShape
*/
public function testtransform() {
$sut = new DataAdaptorFake();
$data = new \stdClass();
$data->lorem = 'caramba!';
$actual = $sut->transform($data, $this->context);
$this->assertSame('caramba!', $actual);
}
/**
* @covers ::transform
*/
public function testtransformErrorInput() {
$sut = new DataAdaptorFake2();
$data = new \stdClass();
$data->lorem = 'caramba!';
$this->expectException(\TypeError::class);
$sut->transform($data, $this->context);
}
/**
* @covers ::transform
*/
public function testtransformErrorOutput() {
$sut = new DataAdaptorFake3();
$data = new \stdClass();
$data->lorem = 'caramba!';
$this->expectException(\TypeError::class);
$sut->transform($data);
}
/**
* @covers ::undoTransform
* @covers ::conformsToInternalShape
* @covers ::conformsToOutputShape
*/
public function testundoTransform() {
$sut = new DataAdaptorFake();
$actual = $sut->undoTransform('caramba!', $this->context);
$expected = new \stdClass();
$expected->lorem = 'caramba!';
$expected->bar = 'default';
$this->assertEquals($expected, $actual);
}
/**
* @covers ::undoTransform
*/
public function testundoTransformErrorInput() {
$sut = new DataAdaptorFake3();
$expected = new \stdClass();
$expected->lorem = 'caramba!';
$expected->bar = 'default';
$this->expectException(\TypeError::class);
$sut->undoTransform('caramba!', $this->context);
}
/**
* @covers ::undoTransform
*/
public function testundoTransformErrorOutput() {
$sut = new DataAdaptorFake4();
$expected = new \stdClass();
$expected->lorem = 'caramba!';
$expected->bar = 'default';
$this->expectException(\TypeError::class);
$sut->undoTransform('caramba!');
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Shaper\Tests\DataAdaptor;
use Shaper\DataAdaptor\DataAdaptorBase;
use Shaper\Util\Context;
use Shaper\Validator\AcceptValidator;
class DataAdaptorFake extends DataAdaptorBase {
protected function doTransform($data, Context $context) {
$key = $context->offsetExists('key') ? $context['key'] : 'lorem';
return $data->{$key};
}
protected function doUndoTransform($data, Context $context) {
$key = $context->offsetExists('key') ? $context['key'] : 'lorem';
return (object) [$key => $data, 'bar' => 'default'];
}
public function getInputValidator() {
return new AcceptValidator();
}
public function getInternalValidator() {
return new AcceptValidator();
}
public function getOutputValidator() {
return new AcceptValidator();
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Shaper\Tests\DataAdaptor;
use Shaper\DataAdaptor\DataAdaptorBase;
use Shaper\Util\Context;
use Shaper\Validator\AcceptValidator;
class DataAdaptorFake2 extends DataAdaptorFake {
public function conformsToExpectedInputShape($data, ?Context $context = NULL) {
return FALSE;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Shaper\Tests\DataAdaptor;
use Shaper\DataAdaptor\DataAdaptorBase;
use Shaper\Util\Context;
use Shaper\Validator\AcceptValidator;
class DataAdaptorFake3 extends DataAdaptorFake {
public function conformsToInternalShape($data, ?Context $context = NULL) {
return FALSE;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Shaper\Tests\DataAdaptor;
use Shaper\DataAdaptor\DataAdaptorBase;
use Shaper\Util\Context;
use Shaper\Validator\AcceptValidator;
class DataAdaptorFake4 extends DataAdaptorFake {
public function conformsToOutputShape($data, ?Context $context = NULL) {
return FALSE;
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Shaper\Tests\Transformation;
use JsonSchema\Validator;
use PHPUnit\Framework\TestCase;
use Shaper\Transformation\TransformationBase;
use Shaper\Util\Context;
use Shaper\Validator\AcceptValidator;
use Shaper\Validator\JsonSchemaValidator;
/**
* @package Shaper
*
* @coversDefaultClass \Shaper\Transformation\TransformationBase
*/
class TransformationBaseTest extends TestCase {
/**
* @covers ::transform
* @covers ::getInputValidator
* @covers ::getOutputValidator
* @covers ::conformsToExpectedInputShape
* @covers ::conformsToOutputShape
*/
public function testTransform() {
$sut = new TransformationFake();
$actual = $sut->transform('foo');
$this->assertSame(42, $actual);
$this->expectException(\TypeError::class);
$sut->transform([], new Context());
}
/**
* @covers ::transform
*/
public function testTransformBeforeError() {
$sut = new TransformationFake();
$this->expectException(\TypeError::class);
$sut->transform([], new Context());
}
/**
* @covers ::transform
*/
public function testTransformAfterError() {
$sut = new TransformationFail();
$this->expectException(\TypeError::class);
$sut->transform('foo', new Context());
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace Shaper\Tests\Transformation;
use Shaper\Util\Context;
class TransformationFail extends TransformationFake {
protected function doTransform($data, Context $context) {
return 'bar';
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Shaper\Tests\Transformation;
use JsonSchema\Validator;
use Shaper\Transformation\TransformationBase;
use Shaper\Util\Context;
use Shaper\Validator\JsonSchemaValidator;
class TransformationFake extends TransformationBase {
public function getInputValidator() {
return new JsonSchemaValidator(['type' => 'string'], new Validator());
}
public function getOutputValidator() {
return new JsonSchemaValidator(['type' => 'number'], new Validator());
}
protected function doTransform($data, Context $context) {
return 42;
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Shaper\Tests\Transformation;
use JsonSchema\Validator;
use PHPUnit\Framework\TestCase;
use Shaper\Transformation\TransformationBase;
use Shaper\Transformation\TransformationsQueue;
use Shaper\Util\Context;
use Shaper\Validator\AcceptValidator;
use Shaper\Validator\JsonSchemaValidator;
class TransformationFake2 extends TransformationBase {
public function getInputValidator() {
return new JsonSchemaValidator(['type' => 'number'], new Validator());
}
public function getOutputValidator() {
$schema = ['type' => 'array', 'items' => ['type' => 'number']];
return new JsonSchemaValidator($schema, new Validator());
}
protected function doTransform($data, Context $context) {
return [$data];
}
}
/**
* @package Shaper
*
* @coversDefaultClass \Shaper\Transformation\TransformationsQueue
*/
class TransformationsQueueTest extends TestCase {
/**
* @var \Shaper\Transformation\TransformationsQueue
*/
protected $sut;
/**
* @var \Shaper\Util\Context
*/
protected $context;
protected function setUp(): void {
parent::setUp();
$this->sut = new TransformationsQueue();
$this->sut->push(new TransformationFake());
$this->sut->push(new TransformationFake2());
$this->context = new Context();
}
/**
* @covers ::transform
*/
public function testTransform() {
$actual = $this->sut->transform('foo', $this->context);
$this->assertEquals([42], $actual);
$actual = $this->sut->transform('foo');
$this->assertEquals([42], $actual);
}
/**
* @covers ::getInputValidator
*/
public function testGetInputValidator() {
$this->assertFalse($this->sut->conformsToExpectedInputShape([], $this->context));
}
/**
* @covers ::getOutputValidator
*/
public function testGetOutputValidator() {
$this->assertFalse($this->sut->conformsToOutputShape(new \stdClass(), $this->context));
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Shaper\Tests\Validator;
use Shaper\Validator\AcceptValidator;
use PHPUnit\Framework\TestCase;
/**
* @package Shaper
*
* @coversDefaultClass \Shaper\Validator\AcceptValidator
*/
class AcceptValidatorTest extends TestCase {
/**
* @covers ::isValid
*/
public function testIsValid() {
$sut = new AcceptValidator();
$this->assertTrue($sut->isValid(NULL));
$this->assertTrue($sut->isValid(TRUE));
$this->assertTrue($sut->isValid(42));
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Shaper\Tests\Validator;
use PHPUnit\Framework\TestCase;
use Shaper\Validator\CollectionOfValidators;
use Shaper\Validator\InstanceofValidator;
/**
* @package Shaper
*
* @coversDefaultClass \Shaper\Validator\CollectionOfValidators
*/
class CollectionOfValidatorsTest extends TestCase {
/**
* @covers ::__construct
*/
public function test__construct() {
$sut = new CollectionOfValidators(new InstanceofValidator(TestCase::class));
$this->assertInstanceOf(CollectionOfValidators::class, $sut);
}
/**
* @covers ::__construct
*/
public function test__constructError() {
$this->expectException(\TypeError::class);
new CollectionOfValidators('IAmAFail');
}
/**
* @covers ::isValid
*/
public function testIsValid() {
$sut = new CollectionOfValidators(new InstanceofValidator(\stdClass::class));
$this->assertTrue($sut->isValid([new \stdClass(), new \stdClass()]));
$this->assertFalse($sut->isValid([new \stdClass(), 'fail']));
$this->assertSame('"fail" does not extend or implement "stdClass".', $sut->getErrors()[0]);
$this->assertFalse($sut->isValid(new \stdClass()));
$this->assertSame('Collection of validators only applies on data arrays.', $sut->getErrors()[0]);
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Shaper\Tests\Validator;
use PHPUnit\Framework\TestCase;
use Shaper\Validator\InstanceofValidator;
/**
* @package Shaper
*
* @coversDefaultClass \Shaper\Validator\InstanceofValidator
*/
class InstanceofValidatorTest extends TestCase {
/**
* @covers ::__construct
*/
public function test__construct() {
$sut = new InstanceofValidator(TestCase::class);
$this->assertInstanceOf(InstanceofValidator::class, $sut);
}
/**
* @covers ::__construct
*/
public function test__constructError() {
$this->expectException(\TypeError::class);
new InstanceofValidator('IAmAFail');
}
/**
* @covers ::isValid
*/
public function testIsValid() {
$sut = new InstanceofValidator(\stdClass::class);
$this->assertTrue($sut->isValid(new \stdClass()));
$this->assertFalse($sut->isValid(new \Exception()));
$this->assertSame('"Exception" does not extend or implement "stdClass".', $sut->getErrors()[0]);
$this->assertFalse($sut->isValid('42'));
$this->assertSame('"42" does not extend or implement "stdClass".', $sut->getErrors()[0]);
}
}

View File

@@ -0,0 +1,95 @@
<?php
namespace Shaper\Tests\Validator;
use JsonSchema\Validator;
use PHPUnit\Framework\TestCase;
use Shaper\Validator\JsonSchemaValidator;
/**
* @package Shaper
*
* @coversDefaultClass \Shaper\Validator\JsonSchemaValidator
*/
class JsonSchemaValidatorTest extends TestCase {
/**
* @covers ::__construct
* @covers ::setValidator
*/
public function test__construct() {
$sut = new JsonSchemaValidator(NULL, new Validator());
$this->assertInstanceOf(JsonSchemaValidator::class, $sut);
}
/**
* @covers ::toJSON
*/
public function testToJSON() {
$sut = new JsonSchemaValidator(['type' => 'string'], new Validator());
$this->assertEquals('{"type":"string"}', $sut->toJSON());
}
/**
* @covers ::isValid
* @covers ::setValidator
*/
public function testIsValid() {
$sut = new JsonSchemaValidator(['type' => 'number']);
$sut->setValidator(new Validator());
$this->assertTrue($sut->isValid(42));
$this->assertFalse($sut->isValid('not true'));
$this->assertSame('String value found, but a number is required', $sut->getErrors()[0]['message']);
}
/**
* @covers ::isValid
*/
public function testIsValidError() {
$sut = new JsonSchemaValidator(['type' => 'number']);
$this->expectException(\InvalidArgumentException::class);
$this->assertTrue($sut->isValid(NULL));
}
/**
* @covers ::__sleep
* @covers ::__wakeup
*/
public function testSerialize() {
$sut = new JsonSchemaValidator(['type' => 'number'], new Validator());
$serialized = serialize($sut);
$sut = unserialize($serialized);
$this->assertInstanceOf(JsonSchemaValidator::class, $sut);
}
/**
* @covers ::getErrors
*/
public function testGetErrors() {
$sut = new JsonSchemaValidator(['type' => 'number']);
$sut->setValidator(new Validator());
$sut->isValid(NULL);
$this->assertEquals([[
'property' => '',
'pointer' => '',
'message' => 'NULL value found, but a number is required',
'constraint' => 'type',
'context' => 1,
]], $sut->getErrors());
$sut = new JsonSchemaValidator(['type' => 'number']);
$this->assertEquals([], $sut->getErrors());
}
/**
* @covers ::resetErrors
*/
public function testResetErrors() {
$sut = new JsonSchemaValidator(['type' => 'number']);
$sut->setValidator(new Validator());
$sut->isValid(NULL);
$this->assertNotEmpty($sut->getErrors()[0]);
$sut->resetErrors();
$this->assertEmpty($sut->getErrors());
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Shaper\Tests\Validator;
use Shaper\Validator\RegexpValidator;
use PHPUnit\Framework\TestCase;
/**
* @package Shaper
*
* @coversDefaultClass \Shaper\Validator\RegexpValidator
*/
class RegexpValidatorTest extends TestCase {
/**
* @covers ::isValid
* @covers ::__construct
* @dataProvider providerIdValid
*/
public function testIsValid($data, $expected) {
$sut = new RegexpValidator('^d.*e$');
$this->assertSame($expected, $sut->isValid($data));
if (!$expected) {
$message = sprintf(
'String "%s" does not match regular expression /^d.*e$/ as expected.',
$data
);
$this->assertSame($message, $sut->getErrors()[0]);
}
}
/**
* Data provider for testIsValid.
*
* @return array
*/
public function providerIdValid() {
return [
['de', TRUE],
['d123/\/@e', TRUE],
['42', FALSE],
['d123@', FALSE],
];
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Shaper\Tests\Validator;
use PHPUnit\Framework\TestCase;
use Shaper\Validator\ValidateableBase;
class FakeValidateable extends ValidateableBase {
public function isValid($data) {
return FALSE;
}
public function setErrors($value) {
$this->errors = $value;
}
}
/**
* @package Shaper
*
* @coversDefaultClass \Shaper\Validator\ValidateableBase
*/
class ValidateableBaseTest extends TestCase {
/**
* @covers ::getErrors
*/
public function testGetErrors() {
$sut = new FakeValidateable();
$sut->setErrors('foo');
$this->assertSame('foo', $sut->getErrors());
}
/**
* @covers ::resetErrors
*/
public function testResetErrors() {
$sut = new FakeValidateable();
$sut->setErrors('foo');
$sut->resetErrors();
$this->assertEquals([], $sut->getErrors());
}
}