Skip to content

Commit

Permalink
Use SQL typecasting for Array persistence (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 11, 2020
1 parent f501663 commit febfc7c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"homepage": "https://mvorisek.cz/"
}
],
"version": "2.3.5",
"version": "2.3.6",
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
Expand Down
14 changes: 14 additions & 0 deletions src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ private function addIdToLoadRow(Model $model, array &$row, $id): void
}
}

public function typecastSaveRow(Model $model, array $row): array
{
$sqlPersistence = (new \ReflectionClass(Sql::class))->newInstanceWithoutConstructor();

return $sqlPersistence->typecastSaveRow($model, $row);
}

public function typecastLoadRow(Model $model, array $row): array
{
$sqlPersistence = (new \ReflectionClass(Sql::class))->newInstanceWithoutConstructor();

return $sqlPersistence->typecastLoadRow($model, $row);
}

/**
* {@inheritdoc}
*/
Expand Down
25 changes: 20 additions & 5 deletions tests/ContainsManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,32 @@ public function testNestedContainsMany()

// let's test how it all looks in persistence without typecasting
$exp_lines = $i->setOrder('id')->export(null, null, false)[0]['lines'];
$formatDtForCompareFunc = function (\DateTimeInterface $dt): string {
$dt = (clone $dt)->setTimeZone(new \DateTimeZone('UTC')); // @phpstan-ignore-line

return $dt->format('Y-m-d H:i:s.u');
};
$this->assertSame(
json_encode([
'1' => [
'id' => 1, 'vat_rate_id' => '1', 'price' => '10', 'qty' => '2', 'add_date' => (new \DateTime('2019-06-01'))->format('Y-m-d\TH:i:sP'), 'discounts' => json_encode([
'1' => ['id' => 1, 'percent' => '5', 'valid_till' => (new \DateTime('2019-07-15'))->format('Y-m-d\TH:i:sP')],
'2' => ['id' => 2, 'percent' => '10', 'valid_till' => (new \DateTime('2019-07-30'))->format('Y-m-d\TH:i:sP')],
'id' => 1,
'vat_rate_id' => 1,
'price' => 10,
'qty' => 2,
'add_date' => $formatDtForCompareFunc(new \DateTime('2019-06-01')),
'discounts' => json_encode([
'1' => ['id' => 1, 'percent' => 5, 'valid_till' => $formatDtForCompareFunc(new \DateTime('2019-07-15'))],
'2' => ['id' => 2, 'percent' => 10, 'valid_till' => $formatDtForCompareFunc(new \DateTime('2019-07-30'))],
]),
],
'2' => [
'id' => 2, 'vat_rate_id' => '2', 'price' => '15', 'qty' => '5', 'add_date' => (new \DateTime('2019-07-01'))->format('Y-m-d\TH:i:sP'), 'discounts' => json_encode([
'1' => ['id' => 1, 'percent' => '20', 'valid_till' => (new \DateTime('2019-12-31'))->format('Y-m-d\TH:i:sP')],
'id' => 2,
'vat_rate_id' => 2,
'price' => 15,
'qty' => 5,
'add_date' => $formatDtForCompareFunc(new \DateTime('2019-07-01')),
'discounts' => json_encode([
'1' => ['id' => 1, 'percent' => 20, 'valid_till' => $formatDtForCompareFunc(new \DateTime('2019-12-31'))],
]),
],
]),
Expand Down
22 changes: 19 additions & 3 deletions tests/ContainsOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function testContainsOne()
$this->assertFalse($a->loaded());

// now store some address
$a->setMulti($row = ['id' => 1, 'country_id' => 1, 'address' => 'foo', 'built_date' => new \DateTime('2019-01-01 UTC'), 'tags' => ['foo', 'bar'], 'door_code' => null]);
$a->setMulti($row = ['id' => 1, 'country_id' => 1, 'address' => 'foo', 'built_date' => new \DateTime('2019-01-01'), 'tags' => ['foo', 'bar'], 'door_code' => null]);
$a->save();

// now reload invoice and see if it is saved
Expand All @@ -156,7 +156,7 @@ public function testContainsOne()

// now add nested containsOne - DoorCode
$c = $i->ref('addr')->ref('door_code');
$c->setMulti($row = ['id' => 1, 'code' => 'ABC', 'valid_till' => new \DateTime('2019-07-01 UTC')]);
$c->setMulti($row = ['id' => 1, 'code' => 'ABC', 'valid_till' => new \DateTime('2019-07-01')]);
$c->save();
$this->assertEquals($row, $i->ref('addr')->ref('door_code')->get());

Expand All @@ -174,8 +174,24 @@ public function testContainsOne()

// let's test how it all looks in persistence without typecasting
$exp_addr = $i->setOrder('id')->export(null, null, false)[0]['addr'];
$formatDtForCompareFunc = function (\DateTimeInterface $dt): string {
$dt = (clone $dt)->setTimeZone(new \DateTimeZone('UTC')); // @phpstan-ignore-line

return $dt->format('Y-m-d H:i:s.u');
};
$this->assertSame(
'{"id":1,"country_id":"2","address":"bar","built_date":"2019-01-01T00:00:00+00:00","tags":"[\"foo\",\"bar\"]","door_code":"{\"id\":1,\"code\":\"DEF\",\"valid_till\":\"2019-07-01T00:00:00+00:00\"}"}',
json_encode([
'id' => 1,
'country_id' => 2,
'address' => 'bar',
'built_date' => $formatDtForCompareFunc(new \DateTime('2019-01-01')),
'tags' => json_encode(['foo', 'bar']),
'door_code' => json_encode([
'id' => 1,
'code' => 'DEF',
'valid_till' => $formatDtForCompareFunc(new \DateTime('2019-07-01')),
]),
]),
$exp_addr
);

Expand Down
14 changes: 7 additions & 7 deletions tests/PersistentArrayOfStringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ public function testTypecasting()
'id' => 1,
'string' => 'TwoLines',
'text' => "Two\nLines",
'integer' => '123',
'money' => '123.45',
'float' => '123.456789',
'boolean' => '1',
'boolean_enum' => '0',
'integer' => 123,
'money' => 123.45,
'float' => 123.456789,
'boolean' => 1,
'boolean_enum' => 'N',
'date' => '2019-01-20',
'datetime' => '2019-01-20T12:23:34+00:00',
'time' => '12:23:34',
'datetime' => '2019-01-20 12:23:34.000000',
'time' => '12:23:34.000000',
'array' => '{"foo":"bar","int":123,"rows":["a","b"]}',
'object' => '{"foo":"bar","int":123,"rows":["a","b"]}',
]], $data);
Expand Down

0 comments on commit febfc7c

Please sign in to comment.