<commit_before><?php

namespace PlaygroundCoreTest\Mapper;

use PlaygroundCoreTest\Bootstrap;
use \PlaygroundCore\Entity\Formgen as FormgenEntity;

class FormgenTest extends \PHPUnit_Framework_TestCase
{
    protected $traceError = true;

    protected $formgenMapper;

    public function setUp()
    {
        $this->sm = Bootstrap::getServiceManager();
        $this->em = $this->sm->get('doctrine.entitymanager.orm_default');
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
        $classes = $this->em->getMetadataFactory()->getAllMetadata();
        $tool->dropSchema($classes);
        $tool->createSchema($classes);
        parent::setUp();
    }

    public function testInsert()
    {   
        $this->em->transactional(function($em) {
            $formgen = new FormgenEntity();
            $formgen->setTitle('Titre de test');
            $formgen->setDescription('Description de test');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });

        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 1);

        $this->em->transactional(function($em) {
            $formgen = new formgenEntity();
            $formgen->setTitle('Titre de test 2');
            $formgen->setDescription('Description de test 2');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });
        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 2);
    }

    public function testFindBy(){

        $this->em->transactional(function($em) {
            $formgen = new FormgenEntity();
            $formgen->setTitle('Titre de test');
            $formgen->setDescription('Description de test');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });

        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 1);

        $this->em->transactional(function($em) {
            $formgen = new formgenEntity();
            $formgen->setTitle('Titre de test 2');
            $formgen->setDescription('Description de test 2');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });
        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 2);

        $formgens = $this->getFormgenMapper()->findBy(array('description'=>'Description de test 2'));
        $this->assertEquals(count($formgens), 1);
    }

    public function testFindById()
    {
        $this->em->transactional(function($em) {
            $formgen = new FormgenEntity();
            $formgen->setTitle('Titre de test');
            $formgen->setDescription('Description de test');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });
        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 1);

        $this->em->transactional(function($em) {
            $formgen = new formgenEntity();
            $formgen->setTitle('Titre de test 2');
            $formgen->setDescription('Description de test 2');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });
        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 2);
        $formgen = $formgens[0];

        $formgens = $this->getFormgenMapper()->findById($formgen->getId());
        $this->assertEquals(count($formgens), 1); 
    }


    public function testUpdate()
    {
        $this->em->transactional(function($em) {
            $formgen = new FormgenEntity();
            $formgen->setTitle('Titre de test');
            $formgen->setDescription('Description de test');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });

        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 1);



        $this->em->transactional(function($em) {
            $formgen2 = new formgenEntity();
            $formgen2->setTitle('Titre de test 2');
            $formgen2->setDescription('Description de test 2');
            $formgen2->setFormjsonified('[{"dujsondetest2"}]');
            $formgen2->setFormtemplate('<li>test2</li>');
            $this->getFormgenMapper()->insert($formgen2);
        });
        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 2);
        $formgen = $formgens[0];


        $formgen->setTitle('Titre de test 3');
        $formgen = $this->getFormgenMapper()->update($formgen);

        $formgens = $this->getFormgenMapper()->findBy(array('title'=>'Titre de test 3'));
        $this->assertEquals(count($formgens), 1);


    }

    public function testRemove()
    {
        $this->em->transactional(function($em) {
            $formgen = new FormgenEntity();
            $formgen->setTitle('Titre de test');
            $formgen->setDescription('Description de test');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });

        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 1);

        $this->em->transactional(function($em) {
            $formgen = new formgenEntity();
            $formgen->setTitle('Titre de test 2');
            $formgen->setDescription('Description de test 2');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });

        $this->em->flush();
        $this->em->clear();<commit_msg>fixin php 5.3 closure bug<commit_after><?php

namespace PlaygroundCoreTest\Mapper;

use PlaygroundCoreTest\Bootstrap;
use \PlaygroundCore\Entity\Formgen as FormgenEntity;

class FormgenTest extends \PHPUnit_Framework_TestCase
{
    protected $traceError = true;

    protected $formgenMapper;

    public function setUp()
    {
        $this->sm = Bootstrap::getServiceManager();
        $this->em = $this->sm->get('doctrine.entitymanager.orm_default');
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
        $classes = $this->em->getMetadataFactory()->getAllMetadata();
        $tool->dropSchema($classes);
        $tool->createSchema($classes);
        parent::setUp();
    }

    public function testInsert()
    {   
        // It has to work with 5.3.x and closure don't support direct $this referencing
        $self = $this;
        $this->em->transactional(function($em) use ($self) {
            $formgen = new FormgenEntity();
            $formgen->setTitle('Titre de test');
            $formgen->setDescription('Description de test');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $self->getFormgenMapper()->insert($formgen);
        });

        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 1);

        $this->em->transactional(function($em) use ($self){
            $formgen = new formgenEntity();
            $formgen->setTitle('Titre de test 2');
            $formgen->setDescription('Description de test 2');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $self->getFormgenMapper()->insert($formgen);
        });
        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 2);
    }

    public function testFindBy(){

        $self = $this;
        $this->em->transactional(function($em) use ($self) {
            $formgen = new FormgenEntity();
            $formgen->setTitle('Titre de test');
            $formgen->setDescription('Description de test');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $self->getFormgenMapper()->insert($formgen);
        });

        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 1);

        $this->em->transactional(function($em) use ($self) {
            $formgen = new formgenEntity();
            $formgen->setTitle('Titre de test 2');
            $formgen->setDescription('Description de test 2');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $self->getFormgenMapper()->insert($formgen);
        });
        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 2);

        $formgens = $this->getFormgenMapper()->findBy(array('description'=>'Description de test 2'));
        $this->assertEquals(count($formgens), 1);
    }

    public function testFindById()
    {
        $self = $this;
        $this->em->transactional(function($em) use ($self) {
            $formgen = new FormgenEntity();
            $formgen->setTitle('Titre de test');
            $formgen->setDescription('Description de test');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $self->getFormgenMapper()->insert($formgen);
        });
        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 1);

        $self = $this;
        $this->em->transactional(function($em) use ($self) {
            $formgen = new formgenEntity();
            $formgen->setTitle('Titre de test 2');
            $formgen->setDescription('Description de test 2');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $self->getFormgenMapper()->insert($formgen);
        });
        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 2);
        $formgen = $formgens[0];

        $formgens = $this->getFormgenMapper()->findById($formgen->getId());
        $this->assertEquals(count($formgens), 1); 
    }


    public function testUpdate()
    {
        $self = $this;
        $this->em->transactional(function($em) use ($self) {
            $formgen = new FormgenEntity();
            $formgen->setTitle('Titre de test');
            $formgen->setDescription('Description de test');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $self->getFormgenMapper()->insert($formgen);
        });

        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 1);

        $this->em->transactional(function($em) use ($self) {
            $formgen2 = new formgenEntity();
            $formgen2->setTitle('Titre de test 2');
            $formgen2->setDescription('Description de test 2');
            $formgen2->setFormjsonified('[{"dujsondetest2"}]');
            $formgen2->setFormtemplate('<li>test2</li>');
            $self->getFormgenMapper()->insert($formgen2);
        });
        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 2);
        $formgen = $formgens[0];


        $formgen->setTitle('Titre de test 3');
        $formgen = $this->getFormgenMapper()->update($formgen);

        $formgens = $this->getFormgenMapper()->findBy(array('title'=>'Titre de test 3'));
        $this->assertEquals(count($formgens), 1);


    }

    public function testRemove()
    {
        $self = $this;
        $this->em->transactional(function($em) {
            $formgen = new FormgenEntity();
            $formgen->setTitle('Titre de test');
            $formgen->setDescription('Description de test');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });

        $this->em->flush();
        $this->em->clear();

        $formgens = $this->getFormgenMapper()->findAll();
        $this->assertEquals(count($formgens), 1);

        $this->em->transactional(function($em) {
            $formgen = new formgenEntity();
            $formgen->setTitle('Titre de test 2');
            $formgen->setDescription('Description de test 2');
            $formgen->setFormjsonified('[{"dujsondetest"}]');
            $formgen->setFormtemplate('<li>test</li>');
            $this->getFormgenMapper()->insert($formgen);
        });

        $this->em->flush();
        $this->em->clear();
{'fraction_non_alphanumeric': 0.1870327674142388, 'fraction_numerical': 0.005170125061133236, 'mean_word_length': 2.5509799057305877, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 387, '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': '7241869', 'n_tokens_mistral': 4262, 'n_tokens_neox': 4138, 'n_words': 588}