Skip to content

Instantly share code, notes, and snippets.

@BackEndTea
Last active April 15, 2021 14:13
Show Gist options
  • Save BackEndTea/45a77fd509fa37755138c5e7460c9738 to your computer and use it in GitHub Desktop.
Save BackEndTea/45a77fd509fa37755138c5e7460c9738 to your computer and use it in GitHub Desktop.
<?php
// Entity
/**
* @ORM\Repository(DoctrinePricesRepository::class)
*/
class Price {
public function __construct(
/**
* @ORM\Column(type="integer)
*/
public int $value
) {}
}
// REpository
interface PricesRepository
{
public function findOneByThing(int $value): ?Price;
}
// configured as PricesRepository in `services.yaml`
class DoctrinePricesRepository extends ServiceEntityRepository implements PricesRepository
{
public function __construct(Manager $manager) {
parent::__construct($manager, Price::class);
}
public function findOneByThing(int $value): ?Price
{
return $this->entityManager->findOneBy(['value' => $value]);
}
}
// Only used in tests
class MemoryPricesRepository implements PricesRepository
{
public function __construct(private array $prices) {}
public function findOneByThing(int $value): ?Price
{
foreach($this->prices as $price) {
if($price->value === $value) {
return $price;
}
}
return null;
}
}
class PricesControllerTest extends KerneltestCase
{
public function testGetFilteredPricesByPage(): void
{
$repository = new MemoryPricesRepository([new Price(1), new Price(2)]);
$data = $repository->findOneByThing(1);
// Price 1
dd($data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment