Skip to content

Commit

Permalink
entity: fixed primary proxy for non-array single-column-PK & more con…
Browse files Browse the repository at this point in the history
…version for primary proxy
  • Loading branch information
hrach committed Dec 24, 2018
1 parent 9f0fd42 commit e07a3e2
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/Entity/AbstractEntity.php
Expand Up @@ -360,10 +360,16 @@ private function setterPrimaryProxy($value, PropertyMetadata $metadata)
return $value;
}

$value = (array) $value;
if (count($keys) === 1) {
$value = [$value];
} elseif (!is_array($value)) {
$class = get_class($this);
throw new InvalidArgumentException("Value for $class::\$id has to be passed as array.");
}

if (count($keys) !== count($value)) {
$class = get_class($this);
throw new InvalidStateException("Value for $class::\$id has insufficient number of parameters.");
throw new InvalidArgumentException("Value for $class::\$id has insufficient number of parameters.");
}

foreach ($keys as $key) {
Expand Down
2 changes: 0 additions & 2 deletions src/Repository/Repository.php
Expand Up @@ -148,8 +148,6 @@ public function getById($id): ?IEntity
return null;
} elseif ($id instanceof IEntity) {
$id = $id->getValue('id');
} elseif (!(is_scalar($id) || is_array($id))) {
throw new InvalidArgumentException('Primary key value has to be a scalar.');
}

$entity = $this->identityMap->getById($id);
Expand Down
19 changes: 19 additions & 0 deletions tests/cases/integration/Entity/entity.compositePK.phpt
Expand Up @@ -76,12 +76,31 @@ class EntityCompositePKTest extends DataTestCase
Assert::null($tagFollower);
}


public function testGetByIdWronglyUsedWithIndexedKeys()
{
Assert::exception(function () {
$this->orm->tagFollowers->getById(['author' => 1, 'tag' => 3]);
}, InvalidArgumentException::class, 'Composite primary value has to be passed as a list, without array keys.');
}


public function testSetIdOnlyPartially()
{
Assert::exception(function () {
$userStat = new UserStat();
$userStat->id = 3;
}, InvalidArgumentException::class, 'Value for NextrasTests\Orm\UserStat::$id has to be passed as array.');
}


public function testSetIdWithInsufficientParameters()
{
Assert::exception(function () {
$userStat = new UserStat();
$userStat->id = [1];
}, InvalidArgumentException::class, 'Value for NextrasTests\Orm\UserStat::$id has insufficient number of parameters.');
}
}


Expand Down
37 changes: 37 additions & 0 deletions tests/cases/integration/Entity/entity.pk.phpt
@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);

/**
* @testCase
* @dataProvider ../../../sections.ini
*/

namespace NextrasTests\Orm\Integration\Entity;

use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Log;
use Tester\Assert;


$dic = require_once __DIR__ . '/../../../bootstrap.php';


class EntityPkTest extends DataTestCase
{
public function testDateTimeWithProxyPk()
{
$log = new Log();
$log->id = $datetime = new \DateTimeImmutable('tomorrow');
$log->count = 3;
$this->orm->persistAndFlush($log);

$log->count = 5;
$this->orm->persistAndFlush($log);

$entry = $this->orm->logs->getById($datetime);
Assert::true($entry !== null);
}
}


$test = new EntityPkTest($dic);
$test->run();
6 changes: 6 additions & 0 deletions tests/db/mssql-init.sql
Expand Up @@ -136,3 +136,9 @@ CREATE TABLE photos (

ALTER TABLE photo_albums
ADD CONSTRAINT photo_albums_preview_id FOREIGN KEY (preview_id) REFERENCES photos (id) ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE TABLE logs (
date datetimeoffset NOT NULL,
count int NOT NULL,
PRIMARY KEY (date)
);
6 changes: 6 additions & 0 deletions tests/db/mysql-init.sql
Expand Up @@ -135,3 +135,9 @@ CREATE TABLE photos (

ALTER TABLE photo_albums
ADD CONSTRAINT photo_albums_preview_id FOREIGN KEY (preview_id) REFERENCES photos (id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE logs (
date TIMESTAMP NOT NULL,
count INT NOT NULL,
PRIMARY KEY (date)
);
6 changes: 6 additions & 0 deletions tests/db/pgsql-init.sql
Expand Up @@ -142,3 +142,9 @@ CREATE TABLE "photos" (

ALTER TABLE "photo_albums"
ADD CONSTRAINT "photo_albums_preview_id" FOREIGN KEY ("preview_id") REFERENCES "photos" ("id") ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE "logs" (
"date" TIMESTAMPTZ NOT NULL,
"count" int NOT NULL,
PRIMARY KEY ("date")
);
1 change: 1 addition & 0 deletions tests/inc/model/Model.php
Expand Up @@ -12,6 +12,7 @@
* @property-read BookCollectionsRepository $bookColletions
* @property-read ContentsRepository $contents
* @property-read EansRepository $eans
* @property-read LogsRepository $logs
* @property-read PhotoAlbumsRepository $photoAlbums
* @property-read PhotosRepository $photos
* @property-read PublishersRepository $publishers
Expand Down
15 changes: 15 additions & 0 deletions tests/inc/model/log/Log.php
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;

use Nextras\Orm\Entity\Entity;


/**
* @property \DateTimeImmutable $id {primary-proxy}
* @property \DateTimeImmutable $date {primary}
* @property int $count
*/
final class Log extends Entity
{
}
10 changes: 10 additions & 0 deletions tests/inc/model/log/LogsMapper.php
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;

use Nextras\Orm\Mapper\Mapper;


final class LogsMapper extends Mapper
{
}
14 changes: 14 additions & 0 deletions tests/inc/model/log/LogsRepository.php
@@ -0,0 +1,14 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;

use Nextras\Orm\Repository\Repository;


final class LogsRepository extends Repository
{
static function getEntityClassNames(): array
{
return [Log::class];
}
}

0 comments on commit e07a3e2

Please sign in to comment.