Skip to content

Instantly share code, notes, and snippets.

@xtrasmal
Created March 18, 2014 11:29
Show Gist options
  • Save xtrasmal/9618288 to your computer and use it in GitHub Desktop.
Save xtrasmal/9618288 to your computer and use it in GitHub Desktop.
UserFactory vs MockUserFactory - log in as
<?php
interface UserBuilderInterface {
public function getUser();
}
// default implementation
class UserFactory implements UserBuilderInterface {
public function __construct(AuthenticatorServiceInterface $authenticator, UserRepository $userRepo)
{
$this->authenticator = $authenticator
$this->repo = $userRepo;
}
public function getUser()
{
$user = $this->repo->findByIdentifier($authenticator->getIdentifier());
if (empty($user)) {
throw new UserNotExistsException();
}
return $user;
}
}
// mocked user implementation
class MockUserFactory extends UserFactory {
public function getUser()
{
if ($authenticator->hasMockIdentifier()) {
$user = $this->repo->findByIdentifier($authenticator->getMockIdentifier());
if (empty($user)) {
throw new UserNotExistsException();
}
return $user;
}
return parent::getUser();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment