<commit_before><?php

namespace PHPOrchestra\ModelBundle\Test\EventListener;

use Phake;
use PHPOrchestra\ModelBundle\EventListener\DefaultThemeListener;
use PHPOrchestra\ModelBundle\Document\Theme;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Query\Builder;

/**
 * Class DefaultThemeListenerTest
 */
class DefaultThemeListenerTest extends \PHPUnit_Framework_TestCase
{
    protected $listener;
    protected $lifecycleEventArgs;
    protected $postFlushEventArgs;

    /**
     * setUp
     */
    public function setUp()
    {
        $this->lifecycleEventArgs = Phake::mock('Doctrine\ODM\MongoDB\Event\LifecycleEventArgs');
        $this->postFlushEventArgs = Phake::mock('Doctrine\ODM\MongoDB\Event\PostFlushEventArgs');
        $this->listener = new DefaultThemeListener();
    }

    /**
     * Test if method is present
     */
    public function testCallable()
    {
        $this->assertTrue(is_callable(array(
            $this->listener,
            'preUpdate'
        )));
        $this->assertTrue(is_callable(array(
            $this->listener,
            'prePersist'
        )));
        $this->assertTrue(is_callable(array(
            $this->listener,
            'postFlush'
        )));
    }

    /**
     *
     * @param Theme $theme
     * @param array  $documents
     *
     * @dataProvider provideTheme
     */
    public function testpreUpdate(Theme $theme, $documents)
    {
        $this->makeTest('preUpdate', $theme, $documents);
    }

    /**
     *
     * @param Theme $theme
     * @param array  $documents
     *
     * @dataProvider provideTheme
     */
    public function testprePersist(Theme $theme, $documents)
    {
        $this->makeTest('prePersist', $theme, $documents);
    }

    /**
     *
     * @param Theme $theme
     * @param array  $documents
     *
     */
    protected function makeTest($method, Theme $theme, $documents)
    {
        $documentManager = Phake::mock('Doctrine\ODM\MongoDB\DocumentManager');
        $themeRepository = Phake::mock('PHPOrchestra\ModelBundle\Repository\ThemeRepository');

        Phake::when($themeRepository)->findAll()->thenReturn($documents);
        Phake::when($documentManager)->getRepository('PHPOrchestraModelBundle:Theme')->thenReturn($themeRepository);

        Phake::when($this->lifecycleEventArgs)->getDocument()->thenReturn($theme);
        Phake::when($this->lifecycleEventArgs)->getDocumentManager()->thenReturn($documentManager);


        $this->listener->$method($this->lifecycleEventArgs);

        foreach ($documents as $document) {
            Phake::verify($document)->setDefault(false);
        }
    }

    /**
     *
     * @return array
     */
    public function provideTheme()
    {
        $theme = Phake::mock('PHPOrchestra\ModelBundle\Document\Theme');
        Phake::when($theme)->isDefault()->thenReturn(true);
        Phake::when($theme)->getId()->thenReturn('fakeThemeId0');

        $document = Phake::mock('PHPOrchestra\ModelBundle\Document\Theme');
        Phake::when($document)->getId()->thenReturn('fakeThemeId1');

        return array(
            array(
                $theme, array($document)
            )
        );
    }
}
<commit_msg>Add postFlush to test<commit_after><?php

namespace PHPOrchestra\ModelBundle\Test\EventListener;

use Phake;
use PHPOrchestra\ModelBundle\EventListener\DefaultThemeListener;
use PHPOrchestra\ModelBundle\Model\ThemeInterface;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Query\Builder;

/**
 * Class DefaultThemeListenerTest
 */
class DefaultThemeListenerTest extends \PHPUnit_Framework_TestCase
{
    protected $listener;
    protected $lifecycleEventArgs;
    protected $postFlushEventArgs;
    protected $documentManager;
    protected $themeRepository;

    /**
     * setUp
     */
    public function setUp()
    {
        $this->lifecycleEventArgs = Phake::mock('Doctrine\ODM\MongoDB\Event\LifecycleEventArgs');
        $this->postFlushEventArgs = Phake::mock('Doctrine\ODM\MongoDB\Event\PostFlushEventArgs');
        $this->documentManager = Phake::mock('Doctrine\ODM\MongoDB\DocumentManager');
        $this->themeRepository = Phake::mock('PHPOrchestra\ModelBundle\Repository\ThemeRepository');
        $this->listener = new DefaultThemeListener();
    }

    /**
     * Test if method is present
     */
    public function testCallable()
    {
        $this->assertTrue(is_callable(array(
            $this->listener,
            'preUpdate'
        )));
        $this->assertTrue(is_callable(array(
            $this->listener,
            'prePersist'
        )));
        $this->assertTrue(is_callable(array(
            $this->listener,
            'postFlush'
        )));
    }

    /**
     *
     * @param ThemeInterface $theme
     * @param array  $documents
     *
     * @dataProvider provideTheme
     */
    public function testpreUpdate(ThemeInterface $theme, $documents)
    {
        $this->makeTest('preUpdate', $theme, $documents);
    }

    /**
     *
     * @param ThemeInterface $theme
     * @param array  $documents
     *
     * @dataProvider provideTheme
     */
    public function testprePersist(ThemeInterface $theme, $documents)
    {
        $this->makeTest('prePersist', $theme, $documents);
    }

    /**
     *
     * @param ThemeInterface $theme
     * @param array  $documents
     *
     * @dataProvider provideTheme
     */
    public function testpostFlush(ThemeInterface $theme, $documents)
    {
        $this->loadConfig($theme, $documents);
        $this->listener->prePersist($this->lifecycleEventArgs);
        $this->listener->postFlush($this->postFlushEventArgs);

        foreach ($documents as $document) {
            Phake::verify($this->documentManager)->persist($document);
        }
        Phake::verify($this->documentManager)->flush();

    }

    /**
     *
     * @param ThemeInterface $theme
     * @param array  $documents
     *
     */
    protected function makeTest($method, ThemeInterface $theme, $documents)
    {
        $this->loadConfig($theme, $documents);
        $this->listener->$method($this->lifecycleEventArgs);

        foreach ($documents as $document) {
            Phake::verify($document)->setDefault(false);
        }
    }

    /**
     *
     * @param ThemeInterface $theme
     * @param array  $documents
     *
     */
    protected function loadConfig(ThemeInterface $theme, $documents){
        $this->documentManager = Phake::mock('Doctrine\ODM\MongoDB\DocumentManager');
        $this->themeRepository = Phake::mock('PHPOrchestra\ModelBundle\Repository\ThemeRepository');

        Phake::when($this->themeRepository)->findAll()->thenReturn($documents);
        Phake::when($this->documentManager)->getRepository('PHPOrchestraModelBundle:Theme')->thenReturn($this->themeRepository);

        Phake::when($this->lifecycleEventArgs)->getDocument()->thenReturn($theme);
        Phake::when($this->lifecycleEventArgs)->getDocumentManager()->thenReturn($this->documentManager);
        Phake::when($this->postFlushEventArgs)->getDocumentManager()->thenReturn($this->documentManager);
    }

    /**
     *
     * @return array
     */
    public function provideTheme()
    {
        $theme = Phake::mock('PHPOrchestra\ModelBundle\Model\ThemeInterface');
        Phake::when($theme)->isDefault()->thenReturn(true);
        Phake::when($theme)->getId()->thenReturn('fakeThemeId0');

        $document = Phake::mock('PHPOrchestra\ModelBundle\Model\ThemeInterface');
        Phake::when($document)->getId()->thenReturn('fakeThemeId1');

        return array(
            array(
                $theme, array($document)
            )
        );
    }
}

{'fraction_non_alphanumeric': 0.13976531942633638, 'fraction_numerical': 0.0005215123859191656, 'mean_word_length': 2.816417910447761, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 89, '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': '3953597', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2140, 'n_words': 304}