Created
March 18, 2014 11:29
-
-
Save xtrasmal/9618288 to your computer and use it in GitHub Desktop.
UserFactory vs MockUserFactory - log in as
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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