Skip to content

Commit 62e4115

Browse files
author
codeliner
committed
More tests and bugfixes
1 parent a0118d4 commit 62e4115

19 files changed

+718
-21
lines changed

.php_cs.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"php":"7.4.1","version":"2.16.1:v2.16.1#c8afb599858876e95e8ebfcd97812d383fa23f02","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"src\/DataConverter.php":3585776980,"src\/ImmutableRecordDataConverter.php":2264693083,"src\/ImmutableRecord.php":2694122341,"src\/ImmutableRecordLogic.php":3915115104}}

.travis.yml

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

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
"@test"
4444
],
4545
"docheader": "vendor/bin/docheader check src/ tests/",
46-
"cs": "php-cs-fixer fix -v --diff --dry-run",
47-
"cs-fix": "php-cs-fixer fix -v --diff",
46+
"cs": "php-cs-fixer fix src -v --diff --dry-run",
47+
"cs-fix": "php-cs-fixer fix src -v --diff",
4848
"test": "vendor/bin/phpunit"
4949
}
5050
}

src/DataConverter.php

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

src/ImmutableRecord.php

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

src/ImmutableRecordDataConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of event-engine/php-data.
4-
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
4+
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
55
*
66
* For the full copyright and license information, please view the LICENSE
77
* file that was distributed with this source code.
@@ -37,7 +37,7 @@ public function canConvertTypeToData(string $type): bool
3737
{
3838
$class = $this->getClassOfType($type);
3939

40-
if(!class_exists($class)) {
40+
if (!class_exists($class)) {
4141
return false;
4242
}
4343

src/ImmutableRecordLogic.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of event-engine/php-data.
4-
* (c) 2018-2019 prooph software GmbH <contact@prooph.de>
4+
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
55
*
66
* For the full copyright and license information, please view the LICENSE
77
* file that was distributed with this source code.
@@ -100,24 +100,24 @@ public function toArray(): array
100100
case ImmutableRecord::PHP_TYPE_BOOL:
101101
case ImmutableRecord::PHP_TYPE_ARRAY:
102102
if (\array_key_exists($key, $arrayPropItemTypeMap) && ! self::isScalarType($arrayPropItemTypeMap[$key])) {
103-
if ($isNullable && $this->{$key}() === null) {
103+
if ($isNullable && $this->{$key} === null) {
104104
$nativeData[$key] = null;
105105
continue 2;
106106
}
107107

108108
$nativeData[$key] = \array_map(function ($item) use ($key, &$arrayPropItemTypeMap) {
109109
return $this->voTypeToNative($item, $key, $arrayPropItemTypeMap[$key]);
110-
}, $this->{$key}());
110+
}, $this->{$key});
111111
} else {
112-
$nativeData[$key] = $this->{$key}();
112+
$nativeData[$key] = $this->{$key};
113113
}
114114
break;
115115
default:
116-
if ($isNullable && $this->{$key}() === null) {
116+
if ($isNullable && (! isset($this->{$key}))) {
117117
$nativeData[$key] = null;
118118
continue 2;
119119
}
120-
$nativeData[$key] = $this->voTypeToNative($this->{$key}(), $key, $type);
120+
$nativeData[$key] = $this->voTypeToNative($this->{$key}, $key, $type);
121121
}
122122
}
123123

@@ -126,7 +126,7 @@ public function toArray(): array
126126

127127
public function equals(ImmutableRecord $other): bool
128128
{
129-
if(get_class($this) !== get_class($other)) {
129+
if (get_class($this) !== get_class($other)) {
130130
return false;
131131
}
132132

@@ -191,7 +191,7 @@ private function setNativeData(array $nativeData): void
191191
private function assertAllNotNull(): void
192192
{
193193
foreach (self::$__propTypeMap as $key => [$type, $isNative, $isNullable]) {
194-
if (null === $this->{$key} && ! $isNullable) {
194+
if (! isset($this->{$key}) && ! $isNullable) {
195195
throw new \InvalidArgumentException(\sprintf(
196196
'Missing record data for key %s of record %s.',
197197
$key,

tests/ImmutableRecordLogicTest.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
<?php
2-
2+
/**
3+
* This file is part of event-engine/php-data.
4+
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
39
declare(strict_types=1);
410

511
namespace EventEngineTest\Data;
612

13+
use EventEngineTest\Data\Stub\ImmutableItem;
714
use EventEngineTest\Data\Stub\ImmutableRecordWithNoTypes;
815
use EventEngineTest\Data\Stub\ImmutableRecordWithTypedGetters;
916
use EventEngineTest\Data\Stub\TypeHintedImmutableRecord;
17+
use EventEngineTest\Data\Stub\TypeHintedImmutableRecordWithValueObjects;
18+
use EventEngineTest\Data\Stub\ValueObject\Access;
19+
use EventEngineTest\Data\Stub\ValueObject\ItemList;
20+
use EventEngineTest\Data\Stub\ValueObject\Name;
21+
use EventEngineTest\Data\Stub\ValueObject\Percentage;
22+
use EventEngineTest\Data\Stub\ValueObject\Type;
23+
use EventEngineTest\Data\Stub\ValueObject\Version;
1024
use PHPUnit\Framework\TestCase;
1125

1226
final class ImmutableRecordLogicTest extends TestCase
@@ -20,6 +34,8 @@ protected function setUp()
2034
$this->data = [
2135
'name' => 'test',
2236
'version' => 1,
37+
'itemList' => [['name' => 'one']],
38+
'access' => true,
2339
];
2440
}
2541

@@ -31,6 +47,7 @@ public function it_detects_type_hinted_properties()
3147
$typeHinted = TypeHintedImmutableRecord::fromArray($this->data);
3248

3349
$this->data['type'] = null;
50+
$this->data['percentage'] = 0.5;
3451

3552
$this->assertEquals(
3653
$this->data,
@@ -46,13 +63,53 @@ public function it_detects_coupled_getters_for_properties()
4663
$typedGetters = ImmutableRecordWithTypedGetters::fromArray($this->data);
4764

4865
$this->data['type'] = null;
66+
$this->data['percentage'] = 0.5;
4967

5068
$this->assertEquals(
5169
$this->data,
5270
$typedGetters->toArray()
5371
);
5472
}
5573

74+
/**
75+
* @test
76+
*/
77+
public function it_can_handle_value_objects()
78+
{
79+
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromArray($this->data);
80+
81+
$this->data['type'] = null;
82+
$this->data['percentage'] = 0.5;
83+
84+
$this->assertEquals(
85+
$this->data,
86+
$valueObjects->toArray()
87+
);
88+
}
89+
90+
/**
91+
* @test
92+
*/
93+
public function it_takes_value_object_as_initialization_params()
94+
{
95+
$valueObjects = TypeHintedImmutableRecordWithValueObjects::fromRecordData([
96+
'name' => Name::fromString($this->data['name']),
97+
'type' => Type::fromString('value_object'),
98+
'version' => Version::fromInt($this->data['version']),
99+
'access' => Access::fromBool($this->data['access']),
100+
'percentage' => Percentage::fromFloat(0.9),
101+
'itemList' => ItemList::fromItems(ImmutableItem::fromRecordData(['name' => 'one'])),
102+
]);
103+
104+
$this->data['type'] = 'value_object';
105+
$this->data['percentage'] = 0.9;
106+
107+
$this->assertEquals(
108+
$this->data,
109+
$valueObjects->toArray()
110+
);
111+
}
112+
56113
/**
57114
* @test
58115
*/

tests/Stub/ImmutableItem.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* This file is part of event-engine/php-data.
4+
* (c) 2018-2020 prooph software GmbH <contact@prooph.de>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace EventEngineTest\Data\Stub;
12+
13+
use EventEngine\Data\ImmutableRecord;
14+
use EventEngine\Data\ImmutableRecordLogic;
15+
16+
final class ImmutableItem implements ImmutableRecord
17+
{
18+
use ImmutableRecordLogic;
19+
20+
private $name;
21+
22+
public function name(): string
23+
{
24+
return $this->name;
25+
}
26+
}

tests/Stub/ImmutableRecordWithNoTypes.php

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

511
namespace EventEngineTest\Data\Stub;
@@ -14,6 +20,9 @@ final class ImmutableRecordWithNoTypes implements ImmutableRecord
1420
private $name;
1521
private $type;
1622
private $version;
23+
private $itemList;
24+
private $access;
25+
private $percentage;
1726

1827
public function name()
1928
{
@@ -29,4 +38,19 @@ public function version()
2938
{
3039
return $this->version;
3140
}
41+
42+
public function itemList()
43+
{
44+
return $this->itemList;
45+
}
46+
47+
public function access()
48+
{
49+
return $this->access;
50+
}
51+
52+
public function percentage()
53+
{
54+
return $this->percentage;
55+
}
3256
}

0 commit comments

Comments
 (0)