Skip to content

Commit

Permalink
Merge pull request #17 from byjg/4.9
Browse files Browse the repository at this point in the history
Check if an observer is already added.
  • Loading branch information
byjg authored Jun 27, 2024
2 parents dd7e34e + f3845e2 commit 0adcb21
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 63 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"ByJG\\MicroOrm\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Test\\": "tests/"
}
},
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
Expand Down
38 changes: 19 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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.
-->

<!-- see http://www.phpunit.de/wiki/Documentation -->
<phpunit bootstrap="./vendor/autoload.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./vendor/autoload.php"
colors="true"
testdox="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
stopOnFailure="false">

<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
<ini name="error_reporting" value="E_ALL" />
</php>
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
<php>
<ini name="display_errors" value="On"/>
<ini name="display_startup_errors" value="On"/>
<ini name="error_reporting" value="E_ALL"/>
</php>

<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>

<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
30 changes: 30 additions & 0 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
7 changes: 7 additions & 0 deletions src/ORMSubject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ByJG\MicroOrm;

use ByJG\MicroOrm\Exception\InvalidArgumentException;

class ORMSubject
{
const EVENT_INSERT = 'insert';
Expand Down Expand Up @@ -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);
}

Expand Down
22 changes: 10 additions & 12 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
76 changes: 44 additions & 32 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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']);

Expand All @@ -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']);

Expand Down Expand Up @@ -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]);

Expand All @@ -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']);

Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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']);

Expand Down Expand Up @@ -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']);

Expand All @@ -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']);

Expand All @@ -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);
Expand All @@ -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]);


Expand Down

0 comments on commit 0adcb21

Please sign in to comment.