Skip to content

Commit 66094e0

Browse files
committed
Update PHP minimum version to 8.0 and dependencies to latest compatible version
1 parent 86809e9 commit 66094e0

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/Repository.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,24 @@ public function delete(Entity $entity, bool $force = false): bool
471471
return true;
472472
}
473473

474+
/**
475+
* {@inheritedoc}
476+
*/
477+
public function exists(array $conditions): bool
478+
{
479+
return $this->findBy($conditions) !== null;
480+
}
481+
482+
/**
483+
* {@inheritedoc}
484+
*/
485+
public function existIgnore(array $conditions, mixed $value, string $field = 'id'): bool
486+
{
487+
$o = $this->findBy($conditions);
488+
489+
return $o !== null && $o->{$field} != $value; // don't use ===
490+
}
491+
474492
/**
475493
* Set the filters
476494
* @param EntityQuery<TEntity> $query

src/RepositoryInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,20 @@ public function findAll(mixed ...$ids): array;
171171
* @return TEntity[]
172172
*/
173173
public function findAllBy(array $conditions): array;
174+
175+
/**
176+
* Whether the entity with given fields data exist
177+
* @param array<string, mixed> $conditions
178+
* @return bool
179+
*/
180+
public function exists(array $conditions): bool;
181+
182+
/**
183+
* Whether the entity with given fields data exist only if not equal to given value
184+
* @param array<string, mixed> $conditions
185+
* @param mixed $value
186+
* @param string $field
187+
* @return bool
188+
*/
189+
public function existIgnore(array $conditions, mixed $value, string $field = 'id'): bool;
174190
}

tests/RepositoryTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,76 @@ public function testFindUsingLimit(): void
318318
$this->assertEquals(0, $this->getPropertyValue(Repository::class, $e, 'limit'));
319319
}
320320

321+
public function testExists(): void
322+
{
323+
$entity = $this->getEntityInstance();
324+
325+
$ws = $this->getMockInstance(WhereStatement::class);
326+
$where = $this->getMockInstance(Where::class, [
327+
'is' => $ws,
328+
]);
329+
330+
$eq = $this->getMockBuilder(EntityQuery::class)
331+
->disableOriginalConstructor()
332+
->getMock();
333+
334+
$eq->expects($this->exactly(1))
335+
->method('get')
336+
->will($this->returnValue($entity));
337+
338+
$eq->expects($this->exactly(1))
339+
->method('where')
340+
->will($this->returnValue($where));
341+
342+
$entityClass = get_class($entity);
343+
$manager = $this->getEntityManager([
344+
'query' => $eq
345+
], []);
346+
347+
$manager->expects($this->exactly(1))
348+
->method('query')
349+
->with($entityClass);
350+
351+
$e = new Repository($manager, $entityClass);
352+
$res = $e->exists(['id' => 1]);
353+
$this->assertTrue($res);
354+
}
355+
356+
public function testExistIgnore(): void
357+
{
358+
$entity = $this->getEntityInstance(['id' => 3]);
359+
360+
$ws = $this->getMockInstance(WhereStatement::class);
361+
$where = $this->getMockInstance(Where::class, [
362+
'is' => $ws,
363+
]);
364+
365+
$eq = $this->getMockBuilder(EntityQuery::class)
366+
->disableOriginalConstructor()
367+
->getMock();
368+
369+
$eq->expects($this->exactly(1))
370+
->method('get')
371+
->will($this->returnValue($entity));
372+
373+
$eq->expects($this->exactly(1))
374+
->method('where')
375+
->will($this->returnValue($where));
376+
377+
$entityClass = get_class($entity);
378+
$manager = $this->getEntityManager([
379+
'query' => $eq
380+
], []);
381+
382+
$manager->expects($this->exactly(1))
383+
->method('query')
384+
->with($entityClass);
385+
386+
$e = new Repository($manager, $entityClass);
387+
$res = $e->existIgnore(['id' => 1], 1);
388+
$this->assertTrue($res);
389+
}
390+
321391
public function testFindBy(): void
322392
{
323393
$ws = $this->getMockInstance(WhereStatement::class);

0 commit comments

Comments
 (0)