diff --git a/composer.json b/composer.json index 1ad61bf..be68fa4 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,11 @@ "ByJG\\MicroOrm\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Test\\": "tests/" + } + }, "prefer-stable": true, "minimum-stability": "dev", "require": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d5df793..13600e9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -4,33 +4,33 @@ To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> - - - - - - - - + stopOnFailure="false" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> - - - ./src - - + + + + + - - - ./tests/ - - + + + ./src + + + + + ./tests/ + + diff --git a/src/Mapper.php b/src/Mapper.php index 0099cf7..6826779 100644 --- a/src/Mapper.php +++ b/src/Mapper.php @@ -143,9 +143,39 @@ public function getEntity(array $fieldValues = []) } return $instance; + } + + public function getQuery(): Query + { + return Query::getInstance() + ->table($this->table); + } + public function getQueryBasic(): QueryBasic + { + return QueryBasic::getInstance() + ->table($this->table); + } + + public function getDeleteQuery(): DeleteQuery + { + return DeleteQuery::getInstance() + ->table($this->table); } + public function getInsertQuery(): InsertQuery + { + return InsertQuery::getInstance() + ->table($this->table); + } + + public function getUpdateQuery(): UpdateQuery + { + return UpdateQuery::getInstance() + ->table($this->table); + } + + /** * @return string */ diff --git a/src/ORMSubject.php b/src/ORMSubject.php index 8057cc9..960f265 100644 --- a/src/ORMSubject.php +++ b/src/ORMSubject.php @@ -2,6 +2,8 @@ namespace ByJG\MicroOrm; +use ByJG\MicroOrm\Exception\InvalidArgumentException; + class ORMSubject { const EVENT_INSERT = 'insert'; @@ -34,6 +36,11 @@ public function addObserver(ObserverProcessorInterface $observerProcessor, Repos if (!isset($this->observers[$observerProcessor->getObserverdTable()])) { $this->observers[$observerProcessor->getObserverdTable()] = []; } + foreach ($this->observers[$observerProcessor->getObserverdTable()] as $observer) { + if (get_class($observer->getObserverdProcessor()) === get_class($observerProcessor) && get_class($observer->getRepository()) === get_class($observer_in)) { + throw new InvalidArgumentException("Observer already exists"); + } + } $this->observers[$observerProcessor->getObserverdTable()][] = new ObserverProcessorInternal($observerProcessor, $observer_in); } diff --git a/src/Repository.php b/src/Repository.php index ec0e942..771dc5b 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -133,23 +133,24 @@ public function get($pkId) } /** - * @param array $pkId - * @return mixed|null - * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException + * @param array|int $pkId + * @return bool + * @throws RepositoryReadOnlyException + * @throws Exception\InvalidArgumentException */ public function delete($pkId) { [$filterList, $filterKeys] = $this->getPkFilter($pkId); - $updatable = DeleteQuery::getInstance() - ->table($this->mapper->getTable()) + $updatable = $this->getMapper()->getDeleteQuery() ->where($filterList, $filterKeys); return $this->deleteByQuery($updatable); } /** - * @param UpdateBuilderInterface $updatable + * @param DeleteQuery $updatable * @return bool + * @throws Exception\InvalidArgumentException * @throws RepositoryReadOnlyException */ public function deleteByQuery(DeleteQuery $updatable) @@ -174,8 +175,7 @@ public function deleteByQuery(DeleteQuery $updatable) */ public function getByFilter($filter, array $params, $forUpdate = false) { - $query = new Query(); - $query->table($this->mapper->getTable()) + $query = $this->getMapper()->getQuery() ->where($filter, $params); if ($forUpdate) { @@ -315,14 +315,12 @@ public function save($instance, UpdateConstraint $updateConstraint = null) if ($isInsert) { $closure = $this->beforeInsert; $array = $closure($array); - $updatable = InsertQuery::getInstance() - ->table($this->mapper->getTable()) + $updatable = $this->getMapper()->getInsertQuery() ->fields(array_keys($array)); } else { $closure = $this->beforeUpdate; $array = $closure($array); - $updatable = UpdateQuery::getInstance() - ->table($this->mapper->getTable()); + $updatable = $this->getMapper()->getUpdateQuery(); foreach ($array as $field => $value) { $updatable->set($field, $value); } diff --git a/tests/RepositoryTest.php b/tests/RepositoryTest.php index 446023e..2af7e49 100644 --- a/tests/RepositoryTest.php +++ b/tests/RepositoryTest.php @@ -6,6 +6,7 @@ use ByJG\AnyDataset\Db\Factory; use ByJG\MicroOrm\DeleteQuery; use ByJG\MicroOrm\Exception\AllowOnlyNewValuesConstraintException; +use ByJG\MicroOrm\Exception\InvalidArgumentException; use ByJG\MicroOrm\Exception\RepositoryReadOnlyException; use ByJG\MicroOrm\FieldMapping; use ByJG\MicroOrm\Literal; @@ -407,8 +408,7 @@ public function testDeleteLiteral() public function testDelete2() { - $query = DeleteQuery::getInstance() - ->table($this->userMapper->getTable()) + $query = $this->userMapper->getDeleteQuery() ->where('name like :name', ['name'=>'Jane%']); $this->repository->deleteByQuery($query); @@ -424,8 +424,7 @@ public function testDelete2() public function testGetByQueryNone() { - $query = new Query(); - $query->table($this->infoMapper->getTable()) + $query = $this->infoMapper->getQuery() ->where('iduser = :id', ['id'=>1000]) ->orderBy(['property']); @@ -437,8 +436,7 @@ public function testGetByQueryNone() public function testGetByQueryOne() { - $query = new Query(); - $query->table($this->infoMapper->getTable()) + $query = $this->infoMapper->getQuery() ->where('iduser = :id', ['id'=>3]) ->orderBy(['property']); @@ -504,8 +502,7 @@ public function testFilterInTwo() */ public function testGetScalar() { - $query = new Query(); - $query->table($this->infoMapper->getTable()) + $query = $this->infoMapper->getQuery() ->fields(['property']) ->where('iduser = :id', ['id'=>3]); @@ -517,8 +514,7 @@ public function testGetScalar() public function testGetByQueryMoreThanOne() { - $query = new Query(); - $query->table($this->infoMapper->getTable()) + $query = $this->infoMapper->getQuery() ->where('iduser = :id', ['id'=>1]) ->orderBy(['property']); @@ -536,8 +532,7 @@ public function testGetByQueryMoreThanOne() public function testJoin() { - $query = new Query(); - $query->table($this->userMapper->getTable()) + $query = $this->userMapper->getQuery() ->fields([ 'users.id', 'users.name', @@ -570,8 +565,7 @@ public function testJoin() public function testLeftJoin() { - $query = new Query(); - $query->table($this->userMapper->getTable()) + $query = $this->userMapper->getQuery() ->fields([ 'users.id', 'users.name', @@ -596,8 +590,7 @@ public function testLeftJoin() public function testTop() { - $query = Query::getInstance() - ->table($this->userMapper->getTable()) + $query = $this->userMapper->getQuery() ->top(1); $result = $this->repository->getByQuery($query); @@ -611,8 +604,7 @@ public function testTop() public function testLimit() { - $query = Query::getInstance() - ->table($this->userMapper->getTable()) + $query = $this->userMapper->getQuery() ->limit(1, 1); $result = $this->repository->getByQuery($query); @@ -626,12 +618,11 @@ public function testLimit() public function testQueryRaw() { - $query = Query::getInstance() + $query = $this->userMapper->getQuery() ->fields([ "name", "julianday('2020-06-28') - julianday(createdate) as days" ]) - ->table($this->userMapper->getTable()) ->limit(1, 1); $result = $this->repository->getByQuery($query); @@ -699,8 +690,7 @@ public function getObserverdTable(): string // This update has an observer, and you change the `test` variable - $query = new Query(); - $query->table($this->infoMapper->getTable()) + $query = $this->infoMapper->getQuery() ->where('iduser = :id', ['id'=>3]) ->orderBy(['property']); @@ -798,12 +788,38 @@ public function getObserverdTable(): string $this->assertTrue($this->test); } + public function testAddSameObserverTwice() + { + $this->test = null; + + $class = new class($this->infoMapper->getTable(), $this->repository, $this) implements ObserverProcessorInterface { + + protected $table; + public function __construct($table, $parentRepository, $parent) + { + $this->table = $table; + } + + public function process(ObserverData $observerData) + { + } + + public function getObserverdTable(): string + { + return $this->table; + } + } ; + $this->repository->addObserver($class); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Observer already exists"); + $this->repository->addObserver($class); + } public function testConstraintDifferentValues() { // This update has an observer, and you change the `test` variable - $query = new Query(); - $query->table($this->infoMapper->getTable()) + $query = $this->infoMapper->getQuery() ->where('iduser = :id', ['id'=>3]) ->orderBy(['property']); @@ -826,8 +842,7 @@ public function testConstraintSameValues() $this->expectExceptionMessage("You are not updating the property 'value'"); // This update has an observer, and you change the `test` variable - $query = new Query(); - $query->table($this->infoMapper->getTable()) + $query = $this->infoMapper->getQuery() ->where('iduser = :id', ['id'=>3]) ->orderBy(['property']); @@ -845,8 +860,7 @@ public function testConstraintSameValues() public function testQueryBasic() { - $query = new QueryBasic(); - $query->table($this->infoMapper->getTable()) + $query = $this->infoMapper->getQueryBasic() ->where('iduser = :id', ['id'=>3]); $infoRepository = new Repository($this->dbDriver, $this->infoMapper); @@ -861,12 +875,10 @@ public function testQueryBasic() public function testUnion() { - $query = new QueryBasic(); - $query->table($this->infoMapper->getTable()) + $query = $this->infoMapper->getQueryBasic() ->where('id = :id1', ['id1'=>3]); - $query2 = new QueryBasic(); - $query2->table($this->infoMapper->getTable()) + $query2 = $this->infoMapper->getQueryBasic() ->where('id = :id2', ['id2'=>1]);