Skip to content

Commit

Permalink
feat: make compatible with PHP8 (#14)
Browse files Browse the repository at this point in the history
* feat: make compatible with PHP8
  • Loading branch information
e0ipso committed May 19, 2021
1 parent b339ff8 commit 7d73018
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
/vendor/
/composer.lock
/build
.phpunit.result.cache
6 changes: 3 additions & 3 deletions .travis.yml
@@ -1,12 +1,12 @@
language: php
php:
- 7.1
- 7.2
- 7.3
- 7.4
- 8.0

before_script:
- mkdir -p build/logs
- composer install --no-interaction
- echo "xdebug.mode = coverage" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

script:
- php vendor/bin/phpunit
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -20,8 +20,8 @@
"justinrainbow/json-schema": "^5.2"
},
"require-dev": {
"phpunit/phpunit": "^7.0.2",
"php-coveralls/php-coveralls": "^2.0",
"phpunit/phpcov": "^5.0"
"phpunit/phpunit": "^9.5",
"phpunit/phpcov": "^8.2",
"php-coveralls/php-coveralls": "^2.4"
}
}
64 changes: 9 additions & 55 deletions tests/src/DataAdaptor/DataAdaptorBaseTest.php
Expand Up @@ -2,50 +2,8 @@

namespace Shaper\Tests\DataAdaptor;

use Shaper\DataAdaptor\DataAdaptorBase;
use PHPUnit\Framework\TestCase;
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();
}
}

class DataAdaptorFake2 extends DataAdaptorFake {
public function conformsToExpectedInputShape($data, Context $context = NULL) {
return FALSE;
}
}

class DataAdaptorFake3 extends DataAdaptorFake {
public function conformsToInternalShape($data, Context $context = NULL) {
return FALSE;
}
}

class DataAdaptorFake4 extends DataAdaptorFake {
public function conformsToOutputShape($data, Context $context = NULL) {
return FALSE;
}
}

/**
* @package Shaper
Expand All @@ -54,19 +12,13 @@ public function conformsToOutputShape($data, Context $context = NULL) {
*/
class DataAdaptorBaseTest extends TestCase {

/**
* @var \Shaper\Tests\DataAdaptor\DataAdaptorFake
*/
protected $sut;

/**
* @var \Shaper\Util\Context
*/
protected $context;

protected function setUp() {
protected function setUp(): void {
parent::setUp();
$this->sut = new DataAdaptorFake();
$this->context = new Context();
$this->context['key'] = 'lorem';
}
Expand All @@ -77,31 +29,32 @@ protected function setUp() {
* @covers ::conformsToInternalShape
*/
public function testtransform() {
$sut = new DataAdaptorFake();
$data = new \stdClass();
$data->lorem = 'caramba!';
$actual = $this->sut->transform($data, $this->context);
$actual = $sut->transform($data, $this->context);
$this->assertSame('caramba!', $actual);
}

/**
* @covers ::transform
* @expectedException \TypeError
*/
public function testtransformErrorInput() {
$sut = new DataAdaptorFake2();
$data = new \stdClass();
$data->lorem = 'caramba!';
$this->expectException(\TypeError::class);
$sut->transform($data, $this->context);
}

/**
* @covers ::transform
* @expectedException \TypeError
*/
public function testtransformErrorOutput() {
$sut = new DataAdaptorFake3();
$data = new \stdClass();
$data->lorem = 'caramba!';
$this->expectException(\TypeError::class);
$sut->transform($data);
}

Expand All @@ -111,7 +64,8 @@ public function testtransformErrorOutput() {
* @covers ::conformsToOutputShape
*/
public function testundoTransform() {
$actual = $this->sut->undoTransform('caramba!', $this->context);
$sut = new DataAdaptorFake();
$actual = $sut->undoTransform('caramba!', $this->context);
$expected = new \stdClass();
$expected->lorem = 'caramba!';
$expected->bar = 'default';
Expand All @@ -120,25 +74,25 @@ public function testundoTransform() {

/**
* @covers ::undoTransform
* @expectedException \TypeError
*/
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
* @expectedException \TypeError
*/
public function testundoTransformErrorOutput() {
$sut = new DataAdaptorFake4();
$expected = new \stdClass();
$expected->lorem = 'caramba!';
$expected->bar = 'default';
$this->expectException(\TypeError::class);
$sut->undoTransform('caramba!');
}

Expand Down
29 changes: 29 additions & 0 deletions tests/src/DataAdaptor/DataAdaptorFake.php
@@ -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();
}
}
13 changes: 13 additions & 0 deletions tests/src/DataAdaptor/DataAdaptorFake2.php
@@ -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;
}
}
13 changes: 13 additions & 0 deletions tests/src/DataAdaptor/DataAdaptorFake3.php
@@ -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;
}
}
13 changes: 13 additions & 0 deletions tests/src/DataAdaptor/DataAdaptorFake4.php
@@ -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;
}
}
51 changes: 9 additions & 42 deletions tests/src/Transformation/TransformationBaseTest.php
Expand Up @@ -9,77 +9,44 @@
use Shaper\Validator\AcceptValidator;
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;
}

}

class TransformationFail extends TransformationFake {
protected function doTransform($data, Context $context) {
return 'bar';
}
}

/**
* @package Shaper
*
* @coversDefaultClass \Shaper\Transformation\TransformationBase
*/
class TransformationBaseTest extends TestCase {

/**
* @var \Shaper\Tests\Transformation\TransformationFake
*/
protected $sut;

/**
* @var \Shaper\Util\Context
*/
protected $context;

protected function setUp() {
parent::setUp();
$this->sut = new TransformationFake();
$this->context = new Context();
}

/**
* @covers ::transform
* @covers ::getInputValidator
* @covers ::getOutputValidator
* @covers ::conformsToExpectedInputShape
* @covers ::conformsToOutputShape
* @expectedException \TypeError
*/
public function testTransform() {
$actual = $this->sut->transform('foo');
$sut = new TransformationFake();
$actual = $sut->transform('foo');
$this->assertSame(42, $actual);
$this->sut->transform([], $this->context);
$this->expectException(\TypeError::class);
$sut->transform([], new Context());
}

/**
* @covers ::transform
* @expectedException \TypeError
*/
public function testTransformBeforeError() {
$this->sut->transform([], $this->context);
$sut = new TransformationFake();
$this->expectException(\TypeError::class);
$sut->transform([], new Context());
}

/**
* @covers ::transform
* @expectedException \TypeError
*/
public function testTransformAfterError() {
$sut = new TransformationFail();
$sut->transform('foo', $this->context);
$this->expectException(\TypeError::class);
$sut->transform('foo', new Context());
}

}
11 changes: 11 additions & 0 deletions tests/src/Transformation/TransformationFail.php
@@ -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';
}
}
21 changes: 21 additions & 0 deletions tests/src/Transformation/TransformationFake.php
@@ -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;
}

}
2 changes: 1 addition & 1 deletion tests/src/Transformation/TransformationsQueueTest.php
Expand Up @@ -40,7 +40,7 @@ class TransformationsQueueTest extends TestCase {
*/
protected $context;

protected function setUp()/* The :void return type declaration that should be here would cause a BC issue */ {
protected function setUp(): void {
parent::setUp();
$this->sut = new TransformationsQueue();
$this->sut->push(new TransformationFake());
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Validator/CollectionOfValidatorsTest.php
Expand Up @@ -23,9 +23,9 @@ public function test__construct() {

/**
* @covers ::__construct
* @expectedException \TypeError
*/
public function test__constructError() {
$this->expectException(\TypeError::class);
new CollectionOfValidators('IAmAFail');
}

Expand Down

0 comments on commit 7d73018

Please sign in to comment.