Write contract tests against an abstract base class that all interface implementations must satisfy.
Contract Tests
abstract class RepositoryContractTest extends TestCase {
abstract protected function createRepository(): UserRepositoryInterface;
protected UserRepositoryInterface $repo;
protected function setUp(): void {
$this->repo = $this->createRepository();
}
public function testFindByIdReturnsUser(): void {
$created = $this->repo->create(["name"=>"Alice","email"=>"a@b.com"]);
$found = $this->repo->findById($created->id);
$this->assertEquals($created->email, $found->email);
}
}
class SqliteUserRepositoryTest extends RepositoryContractTest {
protected function createRepository(): UserRepositoryInterface {
return new SqliteUserRepository(new PDO("sqlite::memory:"));
}
}