Skip to content

Instantly share code, notes, and snippets.

@teklakct
Created April 23, 2019 10:57
Show Gist options
  • Save teklakct/a55410305dd367f48c8e2495f5f71664 to your computer and use it in GitHub Desktop.
Save teklakct/a55410305dd367f48c8e2495f5f71664 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace My\Infrastructure\DoctrineAdapter;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use My\Domain\FinalGreetingRepository;
use My\Domain\Model\FinalGreeting;
use My\Domain\Exception\FinalGreetingNotFound;
calss DoctrineFinalGreetingRepository implements FinalGreetingRepository
{
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/** @inheritDoc */
public function get(FinalGreetingId $greetingId): FinalGreeting
{
$greeting = $this->getDoctrineRepo()->find($greetingId->toString());
if (!$greeting) {
throw new FinalGreetingNotFound();
}
return $greeting;
}
/** @inheritDoc */
public function findAll(): array
{
return $this->getDoctrineRepo()->findAll();
}
private function getDoctrineRepo(): ObjectRepository
{
return $this->entityManager->getRepository(Authorization::class);
}
}
<?php
class ExampleOtherClass {
private $greetingRepository;
public function __construct(FinalGreetingRepository $greetingRepository)
{
$this->greetingRepository = $greetingRepository;
}
public function doSth(): FinalGreeting
{
return $this->greetingRepository->find(FinalGreetingId::fromString('hello'));
}
}
<?php
declare(strict_types=1);
namespace My\Domain;
use My\Domain\Model\FinalGreeting;
use My\Domain\Exception\FinalGreetingNotFound;
interface FinalGreetingRepository
{
/** @throws FinalGreetingNotFound */
public function get(FinalGreetingId $greetingId): FinalGreeting;
/** @return FinalGreeting[] */
public function findAll(): array;
}
<?php
public function it_should_be_some_fancy_test_name(Greeter $greeter)
{
// Given
$finalGreeting = new FinalGreeting();
$greeter->makeGreeting->willReturn($finalGreeting);
// When
$this->makeGreeting
// Then
->shouldBeEqualTo('hello world');
}
<?php
public function it_should_be_some_fancy_test_name(FinalGreetingRepository $greetingRepository)
{
// Given
$finalGreeting = new FinalGreeting();
// When
$greetingRepository->find(FinalGreetingId::fromString('hello'))->willReturn($finalGreeting);
// Then
$greet = $this->doSth();
$greet->getGreeting()->shouldBe('hello');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment