Skip to content

Commit ac06e78

Browse files
committed
Backport tests and changes from master
1 parent 7fbb831 commit ac06e78

20 files changed

+1189
-23
lines changed

.coveralls.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# for php-coveralls
2+
service_name: travis-ci
3+
coverage_clover: build/logs/clover.xml

.travis.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
language: php
2+
3+
matrix:
4+
fast_finish: true
5+
include:
6+
- php: 7.2
7+
env:
8+
- DEPENDENCIES=""
9+
- php: 7.3
10+
env:
11+
- DEPENDENCIES=""
12+
- EXECUTE_CS_CHECK=true
13+
- php: 7.4
14+
env:
15+
- DEPENDENCIES=""
16+
- TEST_COVERAGE=true
17+
18+
cache:
19+
directories:
20+
- $HOME/.composer/cache
21+
- $HOME/.php-cs-fixer
22+
- $HOME/.local
23+
24+
before_script:
25+
- mkdir -p "$HOME/.php-cs-fixer"
26+
- phpenv config-rm xdebug.ini
27+
- composer self-update
28+
- composer update --prefer-source $DEPENDENCIES
29+
30+
script:
31+
- if [[ $TEST_COVERAGE == 'true' ]]; then php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-text --coverage-clover ./build/logs/clover.xml; else ./vendor/bin/phpunit; fi
32+
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/php-cs-fixer fix src -v --diff --dry-run; fi
33+
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/docheader check examples/ src/ tests/; fi
34+
35+
after_success:
36+
- if [[ $TEST_COVERAGE == 'true' ]]; then php ./vendor/bin/php-coveralls -v; fi
37+
38+
notifications:
39+
webhooks:
40+
urls:
41+
- https://webhooks.gitter.im/e/61c75218816eebde4486
42+
on_success: change # options: [always|never|change] default: always
43+
on_failure: always # options: [always|never|change] default: always
44+
on_start: never # options: [always|never|change] default: always

src/DataConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2+
23
/**
34
* This file is part of event-engine/php-data.
4-
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
5+
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
56
*
67
* For the full copyright and license information, please view the LICENSE
78
* file that was distributed with this source code.

src/ImmutableRecord.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2+
23
/**
34
* This file is part of event-engine/php-data.
4-
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
5+
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
56
*
67
* For the full copyright and license information, please view the LICENSE
78
* file that was distributed with this source code.

src/ImmutableRecordDataConverter.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2+
23
/**
34
* This file is part of event-engine/php-data.
4-
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
5+
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
56
*
67
* For the full copyright and license information, please view the LICENSE
78
* file that was distributed with this source code.
@@ -26,7 +27,7 @@ public function convertDataToArray(string $type, $data): array
2627
return $data;
2728
}
2829

29-
if ($data instanceof ImmutableRecord || is_callable([$data, 'toArray'])) {
30+
if ($data instanceof ImmutableRecord || \is_callable([$data, 'toArray'])) {
3031
return $data->toArray();
3132
}
3233

@@ -37,11 +38,11 @@ public function canConvertTypeToData(string $type): bool
3738
{
3839
$class = $this->getClassOfType($type);
3940

40-
if(!class_exists($class)) {
41+
if (! \class_exists($class)) {
4142
return false;
4243
}
4344

44-
return is_callable([$class, 'fromArray']);
45+
return \is_callable([$class, 'fromArray']);
4546
}
4647

4748
public function convertArrayToData(string $type, array $data)

src/ImmutableRecordLogic.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private function init(): void
3636
* @param array $recordData
3737
* @return self
3838
*/
39-
public static function fromRecordData(array $recordData)
39+
public static function fromRecordData(array $recordData): self
4040
{
4141
return new self($recordData);
4242
}
@@ -45,7 +45,7 @@ public static function fromRecordData(array $recordData)
4545
* @param array $nativeData
4646
* @return self
4747
*/
48-
public static function fromArray(array $nativeData)
48+
public static function fromArray(array $nativeData): self
4949
{
5050
return new self(null, $nativeData);
5151
}
@@ -73,7 +73,7 @@ private function __construct(array $recordData = null, array $nativeData = null)
7373
* @param array $recordData
7474
* @return self
7575
*/
76-
public function with(array $recordData)
76+
public function with(array $recordData): self
7777
{
7878
$copy = clone $this;
7979
$copy->setRecordData($recordData);
@@ -94,24 +94,24 @@ public function toArray(): array
9494
case ImmutableRecord::PHP_TYPE_BOOL:
9595
case ImmutableRecord::PHP_TYPE_ARRAY:
9696
if (\array_key_exists($key, $arrayPropItemTypeMap) && ! self::isScalarType($arrayPropItemTypeMap[$key])) {
97-
if ($isNullable && $this->{$key}() === null) {
97+
if ($isNullable && $this->{$key} === null) {
9898
$nativeData[$key] = null;
9999
continue 2;
100100
}
101101

102102
$nativeData[$key] = \array_map(function ($item) use ($key, &$arrayPropItemTypeMap) {
103103
return $this->voTypeToNative($item, $key, $arrayPropItemTypeMap[$key]);
104-
}, $this->{$key}());
104+
}, $this->{$key});
105105
} else {
106-
$nativeData[$key] = $this->{$key}();
106+
$nativeData[$key] = $this->{$key};
107107
}
108108
break;
109109
default:
110-
if ($isNullable && $this->{$key}() === null) {
110+
if ($isNullable && (! isset($this->{$key}))) {
111111
$nativeData[$key] = null;
112112
continue 2;
113113
}
114-
$nativeData[$key] = $this->voTypeToNative($this->{$key}(), $key, $type);
114+
$nativeData[$key] = $this->voTypeToNative($this->{$key}, $key, $type);
115115
}
116116
}
117117

@@ -120,22 +120,22 @@ public function toArray(): array
120120

121121
public function equals(ImmutableRecord $other): bool
122122
{
123-
if(get_class($this) !== get_class($other)) {
123+
if (get_class($this) !== get_class($other)) {
124124
return false;
125125
}
126126

127127
return $this->toArray() === $other->toArray();
128128
}
129129

130-
private function setRecordData(array $recordData)
130+
private function setRecordData(array $recordData): void
131131
{
132132
foreach ($recordData as $key => $value) {
133133
$this->assertType($key, $value);
134134
$this->{$key} = $value;
135135
}
136136
}
137137

138-
private function setNativeData(array $nativeData)
138+
private function setNativeData(array $nativeData): void
139139
{
140140
$recordData = [];
141141
$arrayPropItemTypeMap = self::getArrayPropItemTypeMapFromMethodOrCache();
@@ -182,10 +182,10 @@ private function setNativeData(array $nativeData)
182182
$this->setRecordData($recordData);
183183
}
184184

185-
private function assertAllNotNull()
185+
private function assertAllNotNull(): void
186186
{
187187
foreach (self::$__propTypeMap as $key => [$type, $isNative, $isNullable]) {
188-
if (null === $this->{$key} && ! $isNullable) {
188+
if (! isset($this->{$key}) && ! $isNullable) {
189189
throw new \InvalidArgumentException(\sprintf(
190190
'Missing record data for key %s of record %s.',
191191
$key,
@@ -195,7 +195,7 @@ private function assertAllNotNull()
195195
}
196196
}
197197

198-
private function assertType(string $key, $value)
198+
private function assertType(string $key, $value): void
199199
{
200200
if (! isset(self::$__propTypeMap[$key])) {
201201
throw new \InvalidArgumentException(\sprintf(
@@ -264,7 +264,7 @@ private function isType(string $type, string $key, $value): bool
264264
}
265265
}
266266

267-
private static function buildPropTypeMap()
267+
private static function buildPropTypeMap(): array
268268
{
269269
$refObj = new \ReflectionClass(__CLASS__);
270270

@@ -384,12 +384,12 @@ private static function getArrayPropItemTypeMapFromMethodOrCache(): array
384384
}
385385

386386
/**
387-
* @var array
387+
* @var array|null
388388
*/
389389
private static $__propTypeMap;
390390

391391
/**
392-
* @var array
392+
* @var array|null
393393
*/
394394
private static $__arrayPropItemTypeMap;
395395
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
/**
4+
* This file is part of event-engine/php-data.
5+
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace EventEngineTest\Data;
14+
15+
use EventEngine\Data\ImmutableRecordDataConverter;
16+
use EventEngineTest\Data\Stub\ImmutableItem;
17+
use EventEngineTest\Data\Stub\TypeHintedImmutableRecordWithValueObjects;
18+
use PHPUnit\Framework\TestCase;
19+
use stdClass;
20+
21+
final class ImmutableRecordDataConverterTest extends TestCase
22+
{
23+
private $data = [];
24+
25+
protected function setUp()
26+
{
27+
parent::setUp();
28+
29+
$this->data = [
30+
'name' => 'test',
31+
'version' => 1,
32+
'itemList' => [['name' => 'one']],
33+
'access' => true,
34+
];
35+
}
36+
37+
/**
38+
* @test
39+
*/
40+
public function it_converts_immutable_record_to_array()
41+
{
42+
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
43+
44+
$dataConverter = new ImmutableRecordDataConverter();
45+
46+
$this->data['type'] = null;
47+
$this->data['percentage'] = 0.5;
48+
49+
$this->assertEquals(
50+
$this->data,
51+
$dataConverter->convertDataToArray(
52+
TypeHintedImmutableRecordWithValueObjects::class,
53+
$valueObjects
54+
)
55+
);
56+
}
57+
58+
/**
59+
* @test
60+
*/
61+
public function it_returns_array_if_passed_as_input()
62+
{
63+
$input = ['a' => 'test'];
64+
65+
$dataConverter = new ImmutableRecordDataConverter();
66+
67+
$output = $dataConverter->convertDataToArray('data', $input);
68+
69+
$this->assertEquals($input, $output);
70+
}
71+
72+
/**
73+
* @test
74+
*/
75+
public function it_returns_false_if_unknown_class_is_passed()
76+
{
77+
$dataConverter = new ImmutableRecordDataConverter();
78+
79+
$this->assertFalse($dataConverter->canConvertTypeToData(ImmutableItem::class . 'Unknown'));
80+
}
81+
82+
/**
83+
* @test
84+
*/
85+
public function it_converts_stdClass_to_array()
86+
{
87+
$obj = new stdClass();
88+
89+
$obj->test = 'This is a test';
90+
$obj->msg = 'With a message';
91+
92+
$this->assertEquals([
93+
'test' => 'This is a test',
94+
'msg' => 'With a message',
95+
],
96+
(new ImmutableRecordDataConverter())->convertDataToArray(stdClass::class, $obj)
97+
);
98+
}
99+
100+
/**
101+
* @test
102+
*/
103+
public function it_converts_array_to_immutable_record()
104+
{
105+
$dataConverter = new ImmutableRecordDataConverter();
106+
107+
$this->assertTrue($dataConverter->canConvertTypeToData(TypeHintedImmutableRecordWithValueObjects::class));
108+
109+
$valueObjects = $dataConverter->convertArrayToData(
110+
TypeHintedImmutableRecordWithValueObjects::class,
111+
$this->data
112+
);
113+
114+
$this->data['type'] = null;
115+
$this->data['percentage'] = 0.5;
116+
117+
$this->assertEquals(
118+
$this->data,
119+
$valueObjects->toArray()
120+
);
121+
}
122+
}

0 commit comments

Comments
 (0)