<commit_before><?php

namespace OpenOrchestra\BackofficeBundle\Tests\Validator\Constraints;

use Phake;

use OpenOrchestra\BackofficeBundle\Validator\Constraints\BlockNodePatternValidator;
use OpenOrchestra\ModelInterface\Model\NodeInterface;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Class BlockNodePatternValidatorTest
 */
class BlockNodePatternValidatorTest extends \PHPUnit_Framework_TestCase
{
    protected $validator;
    protected $constraint;
    protected $context;
    protected $constraintViolationBuilder;
    protected $flashBag;
    protected $session;
    protected $templating;
    protected $translator;

    /**
     * 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->constraintViolationBuilder = Phake::mock('Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface');
        $this->generateFormManager = Phake::mock('OpenOrchestra\BackofficeBundle\StrategyManager\GenerateFormManager');
        $this->flashBag = Phake::mock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
        $this->session = Phake::mock('Symfony\Component\HttpFoundation\Session\Session');
        $this->templating = Phake::mock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
        $this->translator = Phake::mock('Symfony\Component\Translation\TranslatorInterface');

        Phake::when($this->context)->buildViolation(Phake::anyParameters())->thenReturn($this->constraintViolationBuilder);
        Phake::when($this->constraintViolationBuilder)->atPath(Phake::anyParameters())->thenReturn($this->constraintViolationBuilder);
        Phake::when($this->session)->getFlashBag()->thenReturn($this->flashBag);

        $this->validator = new BlockNodePatternValidator($this->generateFormManager, $this->session, $this->templating, $this->translator);
        $this->validator->initialize($this->context);
    }

    /**
     * Test instance
     */
    public function testClass()
    {
        $this->assertInstanceOf('Symfony\Component\Validator\ConstraintValidator', $this->validator);
    }

    /**
     * @param NodeInterface $node
     * @param array         $parameter
     * @param int           $violationTimes
     * @param int           $flashBagTimes
     *
     * @dataProvider provideCountAndViolation
     */
    public function testAddViolationOrNot(NodeInterface $node, array $parameter, $violationTimes, $flashBagTimes)
    {
        $blocks = $node->getBlocks();
        foreach ($blocks as $key => $block) {
            Phake::when($this->generateFormManager)->getRequiredUriParameter($block)->thenReturn($parameter[$key]);
        }

        $this->validator->validate($node, $this->constraint);

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

        Phake::verify($this->flashBag, Phake::times($flashBagTimes))->add(Phake::anyParameters());
    }

    /**
     * @return array
     */
    public function provideCountAndViolation()
    {
        $fakeId = 'fakeId';

        $block = Phake::mock('OpenOrchestra\ModelInterface\Model\BlockInterface');
        Phake::when($block)->getLabel()->thenReturn('fakeLabel');

        $status = Phake::mock('OpenOrchestra\ModelInterface\Model\StatusInterface');
        Phake::when($status)->isPublished()->thenReturn(true);

        $area0 = Phake::mock('OpenOrchestra\ModelInterface\Model\AreaContainerInterface');
        Phake::when($area0)->getAreas()->thenReturn(array());
        Phake::when($area0)->getBlocks()->thenReturn(array(array('nodeId' => 0, 'blockId' => 0)));
        $areas0 = new ArrayCollection();
        $areas0->add($area0);

        $area1 = Phake::mock('OpenOrchestra\ModelInterface\Model\AreaContainerInterface');
        Phake::when($area1)->getAreas()->thenReturn(array());
        Phake::when($area1)->getBlocks()->thenReturn(array(array('nodeId' => 0, 'blockId' => 0), array('nodeId' => 0, 'blockId' => 1)));
        $areas1 = new ArrayCollection();
        $areas1->add($area1);
<commit_msg>complete test<commit_after><?php

namespace OpenOrchestra\BackofficeBundle\Tests\Validator\Constraints;

use Phake;

use OpenOrchestra\BackofficeBundle\Validator\Constraints\BlockNodePatternValidator;
use OpenOrchestra\ModelInterface\Model\NodeInterface;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Class BlockNodePatternValidatorTest
 */
class BlockNodePatternValidatorTest extends \PHPUnit_Framework_TestCase
{
    protected $validator;
    protected $constraint;
    protected $context;
    protected $constraintViolationBuilder;
    protected $flashBag;
    protected $session;
    protected $templating;
    protected $translator;

    /**
     * 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->constraintViolationBuilder = Phake::mock('Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface');
        $this->generateFormManager = Phake::mock('OpenOrchestra\BackofficeBundle\StrategyManager\GenerateFormManager');
        $this->flashBag = Phake::mock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
        $this->session = Phake::mock('Symfony\Component\HttpFoundation\Session\Session');
        $this->templating = Phake::mock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
        $this->translator = Phake::mock('Symfony\Component\Translation\TranslatorInterface');

        Phake::when($this->context)->buildViolation(Phake::anyParameters())->thenReturn($this->constraintViolationBuilder);
        Phake::when($this->constraintViolationBuilder)->atPath(Phake::anyParameters())->thenReturn($this->constraintViolationBuilder);
        Phake::when($this->session)->getFlashBag()->thenReturn($this->flashBag);

        $this->validator = new BlockNodePatternValidator($this->generateFormManager, $this->session, $this->templating, $this->translator);
        $this->validator->initialize($this->context);
    }

    /**
     * Test instance
     */
    public function testClass()
    {
        $this->assertInstanceOf('Symfony\Component\Validator\ConstraintValidator', $this->validator);
    }

    /**
     * @param NodeInterface $node
     * @param array         $parameter
     * @param int           $violationTimes
     * @param int           $flashBagTimes
     *
     * @dataProvider provideCountAndViolation
     */
    public function testAddViolationOrNot(NodeInterface $node, array $parameter, $violationTimes, $flashBagTimes)
    {
        $blocks = $node->getBlocks();
        foreach ($blocks as $key => $block) {
            Phake::when($this->generateFormManager)->getRequiredUriParameter($block)->thenReturn($parameter[$key]);
        }

        $this->validator->validate($node, $this->constraint);

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

        Phake::verify($this->flashBag, Phake::times($flashBagTimes))->add(Phake::anyParameters());
    }

    /**
     * @return array
     */
    public function provideCountAndViolation()
    {
        $fakeId = 'fakeId';

        $block = Phake::mock('OpenOrchestra\ModelInterface\Model\BlockInterface');
        Phake::when($block)->getLabel()->thenReturn('fakeLabel');

        $status = Phake::mock('OpenOrchestra\ModelInterface\Model\StatusInterface');
        Phake::when($status)->isPublished()->thenReturn(true);

        $area0 = Phake::mock('OpenOrchestra\ModelInterface\Model\AreaContainerInterface');
        Phake::when($area0)->getAreas()->thenReturn(array());
        Phake::when($area0)->getBlocks()->thenReturn(array(array('nodeId' => 0, 'blockId' => 0), array('nodeId' => 1, 'blockId' => 0)));
        $areas0 = new ArrayCollection();
        $areas0->add($area0);

        $area1 = Phake::mock('OpenOrchestra\ModelInterface\Model\AreaContainerInterface');
        Phake::when($area1)->getAreas()->thenReturn(array());
        Phake::when($area1)->getBlocks()->thenReturn(array(array('nodeId' => 0, 'blockId' => 0), array('nodeId' => 0, 'blockId' => 1)));
        $areas1 = new ArrayCollection();
        $areas1->add($area1);

{'fraction_non_alphanumeric': 0.15217903415783274, 'fraction_numerical': 0.00447585394581861, 'mean_word_length': 4.133615477629988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2018403', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2356, 'n_words': 301}