-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update
ActiveRepository
to have all the necessary methods; ad…
…d examples
- Loading branch information
Showing
3 changed files
with
191 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\App\Repository; | ||
|
||
use Cycle\ActiveRecord\Repository\ActiveRepository; | ||
use Cycle\App\Entity\User; | ||
use Cycle\App\Query\UserQuery; | ||
use Cycle\Database\Injection\Fragment; | ||
use Cycle\ORM\ORMInterface; | ||
use Cycle\ORM\Select; | ||
|
||
/** | ||
* @extends ActiveRepository<User> | ||
*/ | ||
final class RepositoryWithActiveQuery extends ActiveRepository | ||
{ | ||
#[\Override] | ||
public function __construct() | ||
{ | ||
parent::__construct(User::class); | ||
} | ||
|
||
#[\Override] | ||
public function initSelect(ORMInterface $orm, string $role): Select | ||
{ | ||
return new UserQuery(); | ||
} | ||
|
||
public function withNameStartLetter(string $letter): static | ||
{ | ||
return $this->with($this->select()->where(new Fragment('SUBSTRING(name, 1, 1) = ?', $letter))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Tests\Functional\Repository; | ||
|
||
use Cycle\ActiveRecord\Facade; | ||
use Cycle\ActiveRecord\Repository\ActiveRepository; | ||
use Cycle\App\Entity\User; | ||
use Cycle\App\Repository\RepositoryWithActiveQuery; | ||
use Cycle\Database\Database; | ||
use Cycle\Database\DatabaseManager; | ||
use Cycle\Tests\Functional\DatabaseTestCase; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
final class ActiveRepositoryTest extends DatabaseTestCase | ||
{ | ||
#[Test] | ||
public function it_extends_repository_constructor(): void | ||
{ | ||
$repository = new class extends ActiveRepository { | ||
public function __construct() | ||
{ | ||
parent::__construct(User::class); | ||
} | ||
}; | ||
|
||
self::assertInstanceOf(User::class, $repository->findOne()); | ||
} | ||
|
||
#[Test] | ||
public function it_fetches_one_entity(): void | ||
{ | ||
$repository = new ActiveRepository(User::class); | ||
|
||
$user = $repository->findOne(['id' => 2]); | ||
|
||
self::assertInstanceOf(User::class, $user); | ||
self::assertSame(2, $user->id); | ||
} | ||
|
||
#[Test] | ||
public function it_fetches_one_entity_by_pk(): void | ||
{ | ||
$repository = new ActiveRepository(User::class); | ||
|
||
$user = $repository->findByPK(2); | ||
|
||
self::assertInstanceOf(User::class, $user); | ||
self::assertSame(2, $user->id); | ||
} | ||
|
||
#[Test] | ||
public function it_uses_custom_repository_with_active_query(): void | ||
{ | ||
$repository = new RepositoryWithActiveQuery(); | ||
$letter = 'J'; | ||
|
||
$user = $repository->withNameStartLetter($letter)->findOne(); | ||
|
||
self::assertInstanceOf(User::class, $user); | ||
self::assertSame($letter, $user->name[0]); | ||
} | ||
} |