Skip to content

Commit

Permalink
update to new reconstitute signature
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Sep 27, 2019
1 parent af1aacc commit cbe2079
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 128 deletions.
24 changes: 17 additions & 7 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
# language: php

build:
dependencies:
before:
- 'composer self-update'
- 'composer update --prefer-stable --prefer-source --no-interaction --no-progress --no-suggest'
cache:
directories:
- vendor/
nodes:
analysis:
project_setup:
override: true
tests:
override: [php-scrutinizer-run]

filter:
paths: [src/*]
excluded_paths: [tests/*, vendor/*]

before_commands:
- 'composer self-update'
- 'composer update --prefer-stable --prefer-source --no-interaction --no-scripts --no-progress --no-suggest'

coding_style:
php:
upper_lower_casing:
Expand All @@ -17,8 +28,8 @@ coding_style:
true_false_null: lower
spaces:
around_operators:
concatenation: true
negation: false
concatenation: true
negation: false
other:
after_type_cast: true

Expand All @@ -34,7 +45,6 @@ tools:
enabled: true
config:
ruleset: 'unusedcode,naming,design,controversial,codesize'

php_cpd: true
php_loc: true
php_pdepend: true
Expand Down
20 changes: 11 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ env:
- COMPOSER_FLAGS="--prefer-stable --prefer-dist"

php:
- 7.2
- 7.3
- 7.4snapshot
- nightly

matrix:
Expand All @@ -22,25 +24,25 @@ matrix:
- php: 7.1
env:
- COMPOSER_FLAGS="--prefer-lowest --prefer-stable --prefer-dist"
- php: 7.2
- php: 7.1
env:
- TEST_VERSION=true
- COMPOSER_FLAGS="--prefer-stable --prefer-dist"
allow_failures:
- php: nightly

before_install:
- if [[ -z $TEST_VERSION && -f "/home/travis/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini" ]]; then phpenv config-rm xdebug.ini; fi
- composer global require hirak/prestissimo
- composer self-update --stable --no-progress
- if [[ -z $TEST_VERSION && -f "/home/travis/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini" ]]; then phpenv config-rm xdebug.ini; fi
- composer global require hirak/prestissimo
- composer self-update --stable --no-progress

install:
- travis_retry composer update $COMPOSER_FLAGS --no-interaction --no-scripts --no-progress
- if [[ $TEST_VERSION ]]; then travis_retry composer require php-coveralls/php-coveralls $COMPOSER_FLAGS --no-interaction --no-scripts --no-progress ; fi
- travis_retry composer update $COMPOSER_FLAGS --no-interaction --no-progress
- if [[ $TEST_VERSION ]]; then travis_retry composer require php-coveralls/php-coveralls $COMPOSER_FLAGS --no-interaction --no-scripts --no-progress ; fi

script:
- if [[ $TEST_VERSION ]]; then composer qa && composer report-phpunit-clover ; fi
- if [[ -z $TEST_VERSION ]]; then composer test-phpunit ; fi
- if [[ $TEST_VERSION ]]; then composer qa && composer report-phpunit-clover ; fi
- if [[ -z $TEST_VERSION ]]; then composer test-phpunit ; fi

after_script:
- if [[ $TEST_VERSION ]]; then travis_retry php vendor/bin/php-coveralls --verbose ; fi
- if [[ $TEST_VERSION ]]; then travis_retry php vendor/bin/php-coveralls --verbose ; fi
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@
"require": {
"php": "^7.1",
"ext-json": "*",
"phpgears/event": "~0.1.3"
"phpgears/event": "~0.3"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.1",
"friendsofphp/php-cs-fixer": "^2.0",
"infection/infection": "^0.9",
"phpmd/phpmd": "^2.0",
"phpstan/phpstan": "^0.10",
"phpstan/phpstan-deprecation-rules": "^0.10",
"phpstan/phpstan-strict-rules": "^0.10",
"phpunit/phpunit": "^6.0|^7.0",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "~0.11.12",
"phpstan/phpstan-deprecation-rules": "~0.11.2",
"phpstan/phpstan-strict-rules": "~0.11.1",
"phpunit/phpunit": "^7.0|^8.0",
"povils/phpmnd": "^2.0",
"roave/security-advisories": "dev-master",
"sebastian/phpcpd": "^3.0|^4.0",
"squizlabs/php_codesniffer": "^2.0",
"thecodingmachine/phpstan-strict-rules": "^0.10.1"
"sebastian/phpcpd": "^4.0",
"squizlabs/php_codesniffer": "^3.0",
"thecodingmachine/phpstan-strict-rules": "~0.11.2"
},
"suggest": {
},
Expand Down
5 changes: 0 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon

parameters:
level: max
paths:
Expand Down
4 changes: 2 additions & 2 deletions src/ReceivedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getMetadata(): array
*
* @return array<string, mixed>
*/
public function withMetadata(array $metadata)
public function withAddedMetadata(array $metadata)
{
throw new ReceivedEventException(\sprintf('Method %s should not be called ', __METHOD__));
}
Expand All @@ -116,7 +116,7 @@ public function getCreatedAt(): \DateTimeImmutable
*
* @throws ReceivedEventException
*/
public static function reconstitute(array $payload, array $attributes = []): void
public static function reconstitute(array $payload, \DateTimeImmutable $createdAt, array $attributes = []): void
{
throw new ReceivedEventException(\sprintf('Method %s should not be called ', __METHOD__));
}
Expand Down
14 changes: 8 additions & 6 deletions src/Serializer/JsonEventSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ final public function serialize(Event $event): string
[
'class' => \get_class($event),
'payload' => $event->getPayload(),
'createdAt' => $event->getCreatedAt()->format(self::DATE_RFC3339_EXTENDED),
'attributes' => $this->getSerializationAttributes($event),
],
static::JSON_ENCODE_OPTIONS
Expand Down Expand Up @@ -81,7 +82,6 @@ protected function getSerializationAttributes(Event $event): array
{
return [
'metadata' => $event->getMetadata(),
'createdAt' => $event->getCreatedAt()->format(self::DATE_RFC3339_EXTENDED),
];
}

Expand All @@ -90,7 +90,7 @@ protected function getSerializationAttributes(Event $event): array
*/
final public function fromSerialized(string $serialized): Event
{
['class' => $eventClass, 'payload' => $payload, 'attributes' => $attributes] =
['class' => $eventClass, 'payload' => $payload, 'createdAt' => $createdAt, 'attributes' => $attributes] =
$this->getEventDefinition($serialized);

if (!\class_exists($eventClass)) {
Expand All @@ -105,10 +105,12 @@ final public function fromSerialized(string $serialized): Event
));
}

$createdAt = \DateTimeImmutable::createFromFormat(self::DATE_RFC3339_EXTENDED, $createdAt);

// @codeCoverageIgnoreStart
try {
/* @var Event $eventClass */
return $eventClass::reconstitute($payload, $this->getDeserializationAttributes($attributes));
return $eventClass::reconstitute($payload, $createdAt, $this->getDeserializationAttributes($attributes));
} catch (\Exception $exception) {
throw new EventSerializationException('Error reconstituting event', 0, $exception);
}
Expand All @@ -128,10 +130,11 @@ private function getEventDefinition(string $serialized): array
{
$definition = $this->getDeserializationDefinition($serialized);

if (!isset($definition['class'], $definition['payload'], $definition['attributes'])
|| \count(\array_diff(\array_keys($definition), ['class', 'payload', 'attributes'])) !== 0
if (!isset($definition['class'], $definition['payload'], $definition['createdAt'], $definition['attributes'])
|| \count(\array_diff(\array_keys($definition), ['class', 'payload', 'createdAt', 'attributes'])) !== 0
|| !\is_string($definition['class'])
|| !\is_array($definition['payload'])
|| !\is_string($definition['createdAt'])
|| !\is_array($definition['attributes'])
) {
throw new EventSerializationException('Malformed JSON serialized event');
Expand Down Expand Up @@ -179,7 +182,6 @@ protected function getDeserializationAttributes(array $attributes): array
{
return [
'metadata' => $attributes['metadata'],
'createdAt' => \DateTimeImmutable::createFromFormat(self::DATE_RFC3339_EXTENDED, $attributes['createdAt']),
];
}
}
2 changes: 1 addition & 1 deletion tests/Async/AbstractEventQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testSerialization(): void
{
$serializer = $this->getMockBuilder(EventSerializer::class)
->getMock();
$serializer->expects($this->once())
$serializer->expects(static::once())
->method('serialize');
/* @var EventSerializer $serializer */

Expand Down
12 changes: 6 additions & 6 deletions tests/Async/AsyncEventBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public function testShouldEnqueue(): void
{
$busMock = $this->getMockBuilder(EventBus::class)
->getMock();
$busMock->expects($this->once())
$busMock->expects(static::once())
->method('dispatch');
/** @var EventBus $busMock */
$queueMock = $this->getMockBuilder(EventQueue::class)
->getMock();
$queueMock->expects($this->once())
$queueMock->expects(static::once())
->method('send');
/** @var EventQueue $queueMock */
$discriminatorMock = new class() implements EventDiscriminator {
Expand All @@ -50,12 +50,12 @@ public function testShouldNotEnqueue(): void
{
$busMock = $this->getMockBuilder(EventBus::class)
->getMock();
$busMock->expects($this->once())
$busMock->expects(static::once())
->method('dispatch');
/** @var EventBus $busMock */
$queueMock = $this->getMockBuilder(EventQueue::class)
->getMock();
$queueMock->expects($this->never())
$queueMock->expects(static::never())
->method('send');
/** @var EventQueue $queueMock */
$discriminatorMock = new class() implements EventDiscriminator {
Expand All @@ -72,12 +72,12 @@ public function testReceivedEvent(): void
{
$busMock = $this->getMockBuilder(EventBus::class)
->getMock();
$busMock->expects($this->once())
$busMock->expects(static::once())
->method('dispatch');
/** @var EventBus $busMock */
$queueMock = $this->getMockBuilder(EventQueue::class)
->getMock();
$queueMock->expects($this->never())
$queueMock->expects(static::never())
->method('send');
/** @var EventQueue $queueMock */
$discriminatorMock = new class() implements EventDiscriminator {
Expand Down
6 changes: 3 additions & 3 deletions tests/Async/Discriminator/ArrayEventDiscriminatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace Gears\Event\Async\Tests;
namespace Gears\Event\Async\Tests\Discriminator;

use Gears\Event\Async\Discriminator\ArrayEventDiscriminator;
use Gears\Event\Async\Tests\Stub\EventStub;
Expand All @@ -30,7 +30,7 @@ public function testDiscriminate(): void
/** @var \Gears\Event\Event $eventMock */
$discriminator = new ArrayEventDiscriminator([\get_class($eventMock)]);

$this->assertTrue($discriminator->shouldEnqueue($eventMock));
$this->assertFalse($discriminator->shouldEnqueue(EventStub::instance()));
static::assertTrue($discriminator->shouldEnqueue($eventMock));
static::assertFalse($discriminator->shouldEnqueue(EventStub::instance()));
}
}
6 changes: 3 additions & 3 deletions tests/Async/Discriminator/ClassEventDiscriminatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace Gears\Event\Async\Tests;
namespace Gears\Event\Async\Tests\Discriminator;

use Gears\Event\Async\Discriminator\ClassEventDiscriminator;
use Gears\Event\Async\Tests\Stub\EventStub;
Expand All @@ -30,7 +30,7 @@ public function testDiscriminate(): void
/** @var \Gears\Event\Event $eventMock */
$discriminator = new ClassEventDiscriminator(\get_class($eventMock));

$this->assertTrue($discriminator->shouldEnqueue($eventMock));
$this->assertFalse($discriminator->shouldEnqueue(EventStub::instance()));
static::assertTrue($discriminator->shouldEnqueue($eventMock));
static::assertFalse($discriminator->shouldEnqueue(EventStub::instance()));
}
}
10 changes: 5 additions & 5 deletions tests/Async/Discriminator/ParameterEventDiscriminatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

namespace Gears\Event\Async\Tests;
namespace Gears\Event\Async\Tests\Discriminator;

use Gears\Event\Async\Discriminator\ParameterEventDiscriminator;
use Gears\Event\Async\Tests\Stub\EventStub;
Expand All @@ -26,15 +26,15 @@ public function testDiscriminateParameter(): void
{
$discriminator = new ParameterEventDiscriminator('identifier');

$this->assertTrue($discriminator->shouldEnqueue(EventStub::instance(['identifier' => null])));
$this->assertFalse($discriminator->shouldEnqueue(EventStub::instance([])));
static::assertTrue($discriminator->shouldEnqueue(EventStub::instance(['identifier' => null])));
static::assertFalse($discriminator->shouldEnqueue(EventStub::instance([])));
}

public function testDiscriminateParameterValue(): void
{
$discriminator = new ParameterEventDiscriminator('identifier', '1234');

$this->assertTrue($discriminator->shouldEnqueue(EventStub::instance(['identifier' => '1234'])));
$this->assertFalse($discriminator->shouldEnqueue(EventStub::instance(['identifier' => true])));
static::assertTrue($discriminator->shouldEnqueue(EventStub::instance(['identifier' => '1234'])));
static::assertFalse($discriminator->shouldEnqueue(EventStub::instance(['identifier' => true])));
}
}
Loading

0 comments on commit cbe2079

Please sign in to comment.