Skip to content

Instantly share code, notes, and snippets.

@lightsuner
Last active August 29, 2015 13:57
Show Gist options
  • Save lightsuner/9757298 to your computer and use it in GitHub Desktop.
Save lightsuner/9757298 to your computer and use it in GitHub Desktop.
<?php
namespace CoreBundle\Service\Expert;
use Codeception\Util\Stub;
use Domain\CoreBundle\Service\Expert\ExpertManager;
use Codeception\Configuration as Configuration;
use Codeception\Exception\Module as ModuleException;
use Domain\CoreBundle\Entity\User;
/**
* @group db
*/
class ExpertManagerTest extends \Codeception\TestCase\Test
{
/**
* @var \CodeGuy
*/
protected $codeGuy;
/**
* @var \Domain\CoreBundle\Service\Expert\ExpertManager
*/
protected $expertManager;
private $serviceContainer;
protected $em;
protected $userManager;
/**
* @var array
*/
protected $candidateUsers = [];
protected $linkedinJSONResponse;
public function testSentExpertRequest()
{
$requestRep = $this->em->getRepository('CoreBundle:ExpertRequest');
$beforeRequestsCount = $requestRep->countOfActiveRequest();
foreach ($this->candidateUsers as $user) {
$this->expertManager
->setUser($user)
->sentActiveRequest()
;
}
$requestsCount = $requestRep->countOfActiveRequest() - $beforeRequestsCount;
$candidatesCount = count($this->candidateUsers);
$this->assertEquals($requestsCount, $candidatesCount);
}
public function testRequestApprove()
{
$i = 0;
foreach ($this->candidateUsers as $user) {
$this->expertManager
->setUser($user)
->sentActiveRequest()
;
$request = $this->expertManager->getActiveRequest();
$this->assertInstanceOf('Domain\CoreBundle\Entity\ExpertRequest', $request);
$this->expertManager
->setActiveRequest($request)
->approve();
;
$this->assertEquals(1, $request->getStatus());
$user = $request->getUser();
$this->assertFalse($user->hasRole(User::ROLE_CANDIDATE));
$this->assertTrue($user->hasRole(User::ROLE_EXPERT));
$expert = $user->getExpert();
$this->assertInstanceOf('Domain\CoreBundle\Entity\Expert', $expert);
$this->assertEquals(\Domain\CoreBundle\Entity\Expert::STATUS_ACTIVE, $expert->getActive());
}
}
protected function _before()
{
// accessing container
$this->serviceContainer = $this->getModule('Symfony2')->container;
$this->serviceContainer->enterScope('request');
$this->em = $this->serviceContainer->get('doctrine.orm.entity_manager');
$this->userManager = $this->serviceContainer->get('fos_user.user_manager');
$this->generateSchema();
//subs
//$entityManager = Stub::makeEmpty('Doctrine\Common\Persistence\ObjectManager', []);
$eventListener = Stub::makeEmpty('Domain\CoreBundle\Service\Event\PrimitiveEventListener', []);
$iDownloader = Stub::makeEmpty('Ikantam\FilesBundle\Service\Downloader', []);
$iUploadedFilesManager = Stub::makeEmpty('Ikantam\FilesBundle\Service\FilesManager\Manager', []);
$imageUploadController = Stub::makeEmpty('Domain\CoreBundle\Controller\ImageUploadController', []);
$this->linkedinJSONResponse = $this->loadJsonLinkedinResponce();
$linkedinAPI = $this->getMockOfLinkedInAPI();
$this->expertManager = new ExpertManager(
$this->em,
$linkedinAPI,
$eventListener,
$iDownloader,
$iUploadedFilesManager,
$imageUploadController
);
$this->generateUsers();
}
/**
* Generate db schema
*/
protected function generateSchema()
{
$metadatas = $this->getMetadatas();
if (!empty($metadatas)) {
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
$tool->dropSchema($metadatas);
$tool->createSchema($metadatas);
}
}
/**
* @return array
*/
protected function getMetadatas()
{
return $this->em->getMetadataFactory()->getAllMetadata();
}
/**
* Generate users
*/
protected function generateUsers()
{
$faker = \Faker\Factory::create();
for ($i=0; $i < 5; $i++){
$this->generateUser($faker);
}
}
/**
* Generate test user
*
* @param $faker
*/
protected function generateUser($faker)
{
$user = $this->userManager->createUser();
$password = '11111111';
$user->setEmail($faker->unique()->email)
->setUsername($faker->name)
->setPlainPassword($password)
->setFirstName($faker->firstName)
->setLastName($faker->lastName)
->setEnabled(true)
->setTerms(true)
->addRole(User::ROLE_CANDIDATE)
;
$this->userManager->updateUser($user, true);
$this->candidateUsers[$user->getId()] = $user;
}
/**
* Return linkedinAPI mock
*
* @return \Mockery\MockInterface|\Yay_MockObject
*/
protected function getMockOfLinkedInAPI()
{
$linkedinDataMick = $this->getLinkedInDataMock();
$linkedinAPI = \Mockery::mock('Domain\CoreBundle\Service\Socializer\Linkedin\Linkedin');
$linkedinAPI
->shouldReceive('getAll')
->andReturn($linkedinDataMick)
;
$linkedinAPI->shouldReceive('setUser');
return $linkedinAPI;
}
/**
* Return sdtClass object of linkedin response
*
* @return mixed
*/
protected function getLinkedInDataMock()
{
return json_decode($this->linkedinJSONResponse);
}
protected function loadJsonLinkedinResponce()
{
$filePath = 'tests/_data/linkedin/AllUserData.json';
if (!file_exists(Configuration::projectDir() . $filePath)) {
throw new ModuleConfigException(
__CLASS__,
"\nFile with dump doesn't exist.
Please, check path for json file: " . $filePath
);
}
return file_get_contents(Configuration::projectDir() . $filePath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment