/**
  * @test
  */
 public function findUserByUsername()
 {
     $username = "******";
     $user = new User();
     $user->setUsername($username);
     $repoMock = $this->getMock("RepositoryMock", array("findOneBy"));
     $repoMock->expects($this->once())->method("findOneBy")->with($this->equalTo(["username" => $username]))->will($this->returnValue($user));
     $emMock = $this->getEntityManagerMock(array("getRepository"));
     $emMock->expects($this->once())->method("getRepository")->with($this->equalTo(EntityConstant::USER))->will($this->returnValue($repoMock));
     $passwordEncoderMock = $this->getPasswordEncoderMock();
     /** @var \Doctrine\ORM\EntityManager $emMock */
     /** @var \Kumatch\BBSAPI\Utility\PasswordEncoder $passwordEncoderMock */
     $useCase = new UserRegistration($emMock, $passwordEncoderMock);
     $this->assertEquals($user, $useCase->findByUsername($username));
 }
 /**
  * @test
  */
 public function succeedCreation()
 {
     $title = "foo";
     $thread = new Thread();
     $thread->setTitle($title);
     $user = new User();
     $user->setUsername("*****@*****.**");
     $threadRepo = $this->getMock("RepositoryMock", array("add"));
     $threadRepo->expects($this->once())->method("add")->with($this->logicalAnd($this->isInstanceOf('Kumatch\\BBSAPI\\Entity\\Thread'), $this->attributeEqualTo("title", $title), $this->attributeEqualTo("user", $user), $this->attributeEqualTo("lastPostedAt", null)));
     $em = $this->getEntityManagerMock(array("getRepository", "flush"));
     $em->expects($this->once())->method("getRepository")->with($this->equalTo(EntityConstant::THREAD))->will($this->returnValue($threadRepo));
     $em->expects($this->once())->method("flush");
     /** @var EntityManager $em */
     $useCase = new ThreadManagement($em);
     $thread = $useCase->create($thread, $user);
     $this->assertInstanceOf('Kumatch\\BBSAPI\\Entity\\Thread', $thread);
     $this->assertEquals($title, $thread->getTitle());
     $this->assertEquals($user, $thread->getUser());
     $this->assertNull($thread->getLastPostedAt());
 }
 /**
  * @test
  */
 public function failIfPasswordIsInvalid()
 {
     $username = "******";
     $password = "******";
     $encodedPassword = "******";
     $token = "oHyu823kfhdGFpVzLf/PiCoVbo5IqwuQSN0YeiJj";
     $now = 1234567890;
     $user = new User();
     $user->setUsername($username)->setEncodedPassword($encodedPassword);
     $userRepo = $this->getMock("RepositoryMock", array("findOneBy"));
     $userRepo->expects($this->once())->method("findOneBy")->with($this->equalTo(array("username" => $username)))->will($this->returnValue($user));
     $accessTokenRepo = $this->getMock("RepositoryMock", array("add"));
     $accessTokenRepo->expects($this->never())->method("add");
     $em = $this->getEntityManagerMock(array("getRepository", "flush"));
     $em->expects($this->at(0))->method("getRepository")->with($this->equalTo(EntityConstant::USER))->will($this->returnValue($userRepo));
     $em->expects($this->at(1))->method("getRepository")->with($this->equalTo(EntityConstant::ACCESS_TOKEN))->will($this->returnValue($accessTokenRepo));
     $em->expects($this->never())->method("flush");
     $passwordEncoder = $this->getPasswordEncoderMock(array("encode"));
     $passwordEncoder->expects($this->once())->method("encode")->with($this->equalTo($password))->will($this->returnValue("invalid_password_string"));
     $tokenGenerator = $this->getTokenGeneratorMock(array("generate"));
     $tokenGenerator->expects($this->never())->method("generate");
     /** @var \Doctrine\ORM\EntityManager $em */
     /** @var \Kumatch\BBSAPI\Utility\PasswordEncoder $passwordEncoder */
     /** @var \Kumatch\BBSAPI\Utility\TokenGenerator $tokenGenerator */
     $useCase = new UserAuthentication($em, $passwordEncoder, $tokenGenerator);
     /** @var UserAuthentication $useCase */
     $this->assertFalse($useCase->invoke($username, $password));
 }