Skip to content

Instantly share code, notes, and snippets.

@yourwebmaker
Created October 28, 2020 12:49
Show Gist options
  • Save yourwebmaker/74df7bd92917eb207699f82ab483cc4b to your computer and use it in GitHub Desktop.
Save yourwebmaker/74df7bd92917eb207699f82ab483cc4b to your computer and use it in GitHub Desktop.
<?php
class FooEntity
{
public string $entityId;
public DateTime $date;
public function __construct(string $entityId, DateTime $date)
{
$this->entityId = $entityId;
$this->date = $date;
}
}
interface FooRepository
{
public function doSomething(FooEntity $entity): void;
public function get(string $fooId) : FooEntity;
}
class FooClient
{
private FooRepository $repository;
public function __construct(FooRepository $repository)
{
$this->repository = $repository;
}
public function heyJoe(): void
{
$entity = new FooEntity('entityId-123', DateTime::createFromFormat('d/m/Y', '10/04/1987'));
$this->repository->doSomething($entity);
}
}
class FooClientTest
{
public function test_repository_calls_correctly() : void
{
$repository = new class implements FooRepository
{
/** @var array<FooEntity> */
private array $storage = [];
public function doSomething(FooEntity $entity): void
{
$this->storage[$entity->entityId] = $entity;
}
public function get(string $fooId) : FooEntity
{
return $this->storage[$fooId];
}
};
$client = new FooClient($repository);
$client->heyJoe();
self::assertEquals('some value', $repository->get('someId'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment