<commit_before><?php
/**
 * Date: 15.11.14
 * Time: 23:26
 * Devs: [
 *   ]
 */

namespace Ojs\Common\Tests;


ini_set('session.save_handler', 'files');
ini_set('session.save_path', '/tmp');
session_start();

use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;

abstract class BaseTestCase extends WebTestCase
{
    /** @var Application */
    protected $app;

    /** @var  Client */
    protected $client;

    /** @var  EntityManager */
    protected $em;

    /** @var  Router */
    protected $router;


    public function setUp()
    {
        $this->client = $this->createClient();

        $this->app = new Application($this->client->getKernel());
        $this->app->setAutoExit(false);


        $this->em = static::$kernel->getContainer()
            ->get('doctrine')
            ->getManager();

        $this->router = static::$kernel->getContainer()->get('router');
    }

    public function command($command, $opts = [])
    {
        $opts['-e'] = 'test';
        $opts['-q'] = null;
        $opts['command'] = $command;
        return $this->app->run(new ArrayInput($opts));
    }


    /**
     *
     * @param string $username
     * @param array $role
     */
    protected function logIn($username = null, $role = null)
    {
        $session = $this->client->getContainer()->get('session');
        $firewall = 'main';
        $user = $this->em->getRepository('OjsUserBundle:User')
            ->findOneByUsername($username ? $username : 'admin');
        if (!$user) {
            $_role = $this->em->getRepository('OjsUserBundle:Role')
                ->findOneByRole($role[0]);
            $user = $this->em->getRepository('OjsUserBundle:User')
                ->findOneByRole($_role);
        }
        if (!($user instanceof UserInterface))
            throw new \Exception("User not find. " . get_class($user));

        $token = new UsernamePasswordToken($user, null, $firewall, $role ? $role : array('ROLE_SUPER_ADMIN'));
        $session->set('_security_' . $firewall, serialize($token));
        $session->save();
        $cookie = new Cookie($session->getName(), $session->getId());
        $this->client->getCookieJar()->set($cookie);
    }

    protected function apiRequest($url, $type = 'GET', $data = [])
    {
        $this->client->request(
            $type,
            $url . '?apikey=<KEY>ZQ==',
            $data,
            [],
            ['HTTP_ACCEPT' => 'application/json']
        );
        /** @var Response $response */
        $response = $this->client->getResponse();
        return $response;
    }


    protected function getInputStream($input)
    {
        $stream = fopen('php://memory', 'r+', false);
        fputs($stream, $input);
        rewind($stream);

        return $stream;
    }

    protected function isAccessible($params, $data = [],$type='GET',$redirectOnSuccess=false)
    {
        $this->client->request(
            $type,
            call_user_func_array([$this->router, 'generate'], $params),
            $data
        );
        /** @var Response $response */
        $response = $this->client->getResponse();
        if($response->isServerError())
            throw new \Exception($response->getContent());
        if($redirectOnSuccess)
            return $response->isRedirection();
        return $response->isSuccessful();
    }
} <commit_msg>Basetest case bug.<commit_after><?php
/**
 * Date: 15.11.14
 * Time: 23:26
 * Devs: [
 *   ]
 */

namespace Ojs\Common\Tests;


ini_set('session.save_handler', 'files');
ini_set('session.save_path', '/tmp');
session_start();

use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;

abstract class BaseTestCase extends WebTestCase
{
    /** @var Application */
    protected $app;

    /** @var  Client */
    protected $client;

    /** @var  EntityManager */
    protected $em;

    /** @var  Router */
    protected $router;


    public function setUp()
    {
        $this->client = $this->createClient();

        $this->app = new Application($this->client->getKernel());
        $this->app->setAutoExit(false);


        $this->em = static::$kernel->getContainer()
            ->get('doctrine')
            ->getManager();

        $this->router = static::$kernel->getContainer()->get('router');
    }

    public function command($command, $opts = [])
    {
        $opts['-e'] = 'test';
        $opts['-q'] = null;
        $opts['command'] = $command;
        return $this->app->run(new ArrayInput($opts));
    }


    /**
     *
     * @param string $username
     * @param array $role
     */
    protected function logIn($username = null, $role = null)
    {
        $session = $this->client->getContainer()->get('session');
        $firewall = 'main';
        $user = $this->em->getRepository('OjsUserBundle:User')
            ->findOneByUsername($username ? $username : 'admin');
        if (!$user) {
            $_role = $this->em->getRepository('OjsUserBundle:Role')
                ->findOneByRole($role[0]);
            $user = $this->em->getRepository('OjsUserBundle:User')
                ->findOneByRole($_role);
        }
        if (!($user instanceof UserInterface))
            throw new \Exception("User not find. " . get_class($user));

        $token = new UsernamePasswordToken($user, null, $firewall, $role ? $role : array('ROLE_SUPER_ADMIN'));
        $session->set('_security_' . $firewall, serialize($token));
        $session->save();
        $cookie = new Cookie($session->getName(), $session->getId());
        $this->client->getCookieJar()->set($cookie);
    }

    protected function apiRequest($url, $type = 'GET', $data = [])
    {
        $this->client->request(
            $type,
            $url . '?apikey=MW<KEY>ZQ==',
            $data,
            [],
            ['HTTP_ACCEPT' => 'application/json']
        );
        /** @var Response $response */
        $response = $this->client->getResponse();
        return $response;
    }


    protected function getInputStream($input)
    {
        $stream = fopen('php://memory', 'r+', false);
        fputs($stream, $input);
        rewind($stream);

        return $stream;
    }

    protected function isAccessible($params, $data = [],$type='GET',$redirectOnSuccess=false)
    {
        $this->client->request(
            $type,
            call_user_func_array([$this->router, 'generate'], $params),
            $data
        );
        /** @var Response $response */
        $response = $this->client->getResponse();
        if($response->isServerError())
            throw new \Exception($response->getStatusCode());
        if($redirectOnSuccess)
            return $response->isRedirection();
        return $response->isSuccessful();
    }
} 
{'fraction_non_alphanumeric': 0.16776489193463362, 'fraction_numerical': 0.0028993147074327886, 'mean_word_length': 2.5529026217228465, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 103, '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': '1994315', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2185, 'n_words': 392}