Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
veolys-nanne committed Mar 16, 2016
1 parent 90a0b0d commit a2f55c7
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Backoffice/Tests/Validator/Constraints/RoleStatusesTest.php
@@ -0,0 +1,58 @@
<?php

namespace OpenOrchestra\Backoffice\Tests\Validator\Constraints;

use OpenOrchestra\Backoffice\Validator\Constraints\RoleStatuses;
use OpenOrchestra\BaseBundle\Tests\AbstractTest\AbstractBaseTestCase;
use Symfony\Component\Validator\Constraint;

/**
* Class RoleStatusesTest
*/
class RoleStatusesTest extends AbstractBaseTestCase
{
/**
* @var RoleStatuses
*/
protected $constraint;

/**
* Set up the test
*/
public function setUp()
{
$this->constraint = new RoleStatuses();
}

/**
* Test instance
*/
public function testInstance()
{
$this->assertInstanceOf('Symfony\Component\Validator\Constraint', $this->constraint);
}

/**
* Test target
*/
public function testTarget()
{
$this->assertSame(Constraint::CLASS_CONSTRAINT, $this->constraint->getTargets());
}

/**
* Test messages
*/
public function testMessages()
{
$this->assertSame('open_orchestra_backoffice_validators.role.duplicate_statuses', $this->constraint->message);
}

/**
* Test validate by
*/
public function testValidateBy()
{
$this->assertSame('role_statuses', $this->constraint->validatedBy());
}
}
@@ -0,0 +1,85 @@
<?php

namespace OpenOrchestra\Backoffice\Tests\Validator\Constraints;

use OpenOrchestra\Backoffice\Validator\Constraints\RoleStatusesValidator;
use OpenOrchestra\BaseBundle\Tests\AbstractTest\AbstractBaseTestCase;
use Phake;

/**
* Class RoleStatusesValidatorTest
*/
class RoleStatusesValidatorTest extends AbstractBaseTestCase
{
/**
* @var RoleStatusesValidator
*/
protected $validator;

protected $constraint;
protected $context;
protected $roleId = 'fakeRoleId';

/**
* Set up the test
*/
public function setUp()
{
$this->constraint = Phake::mock('Symfony\Component\Validator\Constraint');
$this->context = Phake::mock('Symfony\Component\Validator\Context\ExecutionContextInterface');
$this->role = Phake::mock('OpenOrchestra\ModelInterface\Model\RoleInterface');
$constraintViolationBuilder = Phake::mock('Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface');

Phake::when($this->context)->buildViolation(Phake::anyParameters())->thenReturn($constraintViolationBuilder);
Phake::when($this->role)->getId()->thenReturn($this->roleId);
Phake::when($this->role)->getFromStatus()->thenReturn(Phake::mock('OpenOrchestra\ModelInterface\Model\StatusInterface'));
Phake::when($this->role)->getToStatus()->thenReturn(Phake::mock('OpenOrchestra\ModelInterface\Model\StatusInterface'));
Phake::when($constraintViolationBuilder)->atPath(Phake::anyParameters())->thenReturn($constraintViolationBuilder);
}

/**
* Test instance
*/
public function testClass()
{
$roleRepository = Phake::mock('OpenOrchestra\ModelInterface\Repository\RoleRepositoryInterface');
$validator = new RoleStatusesValidator($roleRepository);
$this->assertInstanceOf('Symfony\Component\Validator\ConstraintValidator', $validator);
}

/**
* @param string|null $roleId
* @param integer $violationTimes
*
* @dataProvider provideAreaIdAndViolation
*/
public function testValidate($roleId, $violationTimes)
{
$roleRepository = Phake::mock('OpenOrchestra\ModelInterface\Repository\RoleRepositoryInterface');
if (!is_null($roleId)) {
$role = Phake::mock('OpenOrchestra\ModelInterface\Model\RoleInterface');
Phake::when($role)->getId()->thenReturn($roleId);
Phake::when($roleRepository)->findOneByFromStatusAndToStatus(Phake::anyParameters())->thenReturn($role);
} else {
Phake::when($roleRepository)->findOneByFromStatusAndToStatus(Phake::anyParameters())->thenReturn(null);
}

$validator = new RoleStatusesValidator($roleRepository);
$validator->initialize($this->context);
$validator->validate($this->role, $this->constraint);

Phake::verify($this->context, Phake::times($violationTimes))->buildViolation(Phake::anyParameters());
}

/**
* @return array
*/
public function provideAreaIdAndViolation()
{
return array(
array(null, 0),
array($this->roleId, 0),
array('fakeAnotherRoleId', 1),
);
}
}

0 comments on commit a2f55c7

Please sign in to comment.