<commit_before><?php

namespace MamuzBlogTest\Controller;

use MamuzBlog\Controller\QueryController;
use MamuzBlogTest\Bootstrap;
use Zend\Console\Request as ConsoleRequest;
use Zend\Http\Headers;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;

class QueryControllerTest extends \PHPUnit_Framework_TestCase
{
    /** @var \Zend\Mvc\Controller\AbstractActionController */
    protected $fixture;

    /** @var Request */
    protected $request;

    /** @var Response */
    protected $response;

    /** @var RouteMatch */
    protected $routeMatch;

    /** @var MvcEvent */
    protected $event;

    /** @var Headers */
    protected $xhrHeaders;

    /** @var \MamuzBlog\Feature\QueryInterface | \Mockery\MockInterface */
    protected $queryInterface;

    /** @var \MamuzBlog\Crypt\AdapterInterface | \Mockery\MockInterface */
    protected $cryptEngine;

    /** @var \Zend\Mvc\Controller\Plugin\Params | \Mockery\MockInterface */
    protected $params;

    protected function setUp()
    {
        $this->cryptEngine = \Mockery::mock('MamuzBlog\Crypt\AdapterInterface');
        $this->queryInterface = \Mockery::mock('MamuzBlog\Feature\QueryInterface');

        $this->xhrHeaders = new Headers;
        $this->xhrHeaders->addHeaderLine('X_REQUESTED_WITH', 'XMLHttpRequest');

        $this->fixture = new QueryController($this->queryInterface, $this->cryptEngine);
        $this->request = new Request();
        $this->routeMatch = new RouteMatch(array('controller' => 'index'));
        $this->event = new MvcEvent();
        $router = HttpRouter::factory(array());

        $this->params = \Mockery::mock('Zend\Mvc\Controller\Plugin\Params');
        $this->params->shouldReceive('__invoke')->andReturn($this->params);
        $pluginManager = \Mockery::mock('Zend\Mvc\Controller\PluginManager')->shouldIgnoreMissing();
        $pluginManager->shouldReceive('get')->with('params', null)->andReturn($this->params);

        $this->fixture->setPluginManager($pluginManager);
        $this->event->setRouter($router);
        $this->event->setRouteMatch($this->routeMatch);
        $this->fixture->setEvent($this->event);
    }

    public function testExtendingZendActionController()
    {
        $this->assertInstanceOf('Zend\Mvc\Controller\AbstractActionController', $this->fixture);
    }

    public function testActivePostsWithoutTagCanBeAccessed()
    {
        $page = 'foo';
        $params = array(2, 1);
        $posts = array(1, 2);

        $this->params->shouldReceive('fromRoute')->with('page', 1)->andReturn($page);
        $this->params->shouldReceive('fromRoute')->with('tag')->andReturn(null);
        $this->params->shouldReceive('fromRoute')->andReturn($params);

        $this->queryInterface->shouldReceive('setCurrentPage')->with($page);
        $this->queryInterface->shouldReceive('findActivePosts')->andReturn($posts);

        $this->routeMatch->setParam('action', 'activePosts');

        // HTTP
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($params, $viewVariables['routeParams']);
        $this->assertSame($posts, $viewVariables['collection']);
        $this->assertFalse($viewVariables['isTerminal']);

        // XHR
        $this->request->setHeaders($this->xhrHeaders);
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($params, $viewVariables['routeParams']);
        $this->assertSame($posts, $viewVariables['collection']);
        $this->assertTrue($viewVariables['isTerminal']);

        // CLI
        $this->request = new ConsoleRequest;
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($params, $viewVariables['routeParams']);
        $this->assertSame($posts, $viewVariables['collection']);
        $this->assertTrue($viewVariables['isTerminal']);
    }

    public function testActivePostsWithTagCanBeAccessed()
    {
        $page = 'foo';
        $tag = 'bar';
        $params = array(2, 1);
        $posts = array(1, 2);

        $this->params->shouldReceive('fromRoute')->with('page', 1)->andReturn($page);
        $this->params->shouldReceive('fromRoute')->with('tag')->andReturn($tag);
        $this->params->shouldReceive('fromRoute')->andReturn($params);

        $this->queryInterface->shouldReceive('setCurrentPage')->with($page);
        $this->queryInterface->shouldReceive('findActivePostsByTag')->with($tag)->andReturn($posts);
        $this->routeMatch->setParam('action', 'activePosts');

        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($params, $viewVariables['routeParams']);
        $this->assertSame($posts, $viewVariables['collection']);
        $this->assertFalse($viewVariables['isTerminal']);
    }

    public function testActivePostWithPostCanBeAccessed()
    {
        $id = 'foo';
        $post = 'bar';

        $this->params->shouldReceive('fromRoute')->with('id')->andReturn($id);
        $this->cryptEngine->shouldReceive('decrypt')->with($id)->andReturn($id);
        $this->queryInterface->shouldReceive('findActivePostById')->with($id)->andReturn($post);

        $this->routeMatch->setParam('action', 'activePost');

        // HTTP
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($post, $viewVariables['post']);
        $this->assertFalse($viewVariables['isTerminal']);

        // XHR
        $this->request->setHeaders($this->xhrHeaders);
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($post, $viewVariables['post']);
        $this->assertTrue($viewVariables['isTerminal']);

        // CLI
        $this->request = new ConsoleRequest;
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($post, $viewVariables['post']);
        $this->assertTrue($viewVariables['isTerminal']);
    }

    public function testActivePostWithoutPostCanBeAccessed()
    {
        $id = 'foo';

        $this->params->shouldReceive('fromRoute')->with('id')->andReturn($id);
        $this->cryptEngine->shouldReceive('decrypt')->with($id)->andReturn($id);
        $this->queryInterface->shouldReceive('findActivePostById')->with($id)->andReturn(null);

        $this->routeMatch->setParam('action', 'activePost');
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertNull($result);
        $this->assertEquals(404, $response->getStatusCode());
    }

    public function testActivePostWithNullDecryptionCanBeAccessed()
    {
        $id = 'foo';

        $this->params->shouldReceive('fromRoute')->with('id')->andReturn($id);
        $this->cryptEngine->shouldReceive('decrypt')->with($id)->andReturn(null);

        $this->routeMatch->setParam('action', 'activePost');
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertNull($result);
        $this->assertEquals(404, $response->getStatusCode());
    }
}
<commit_msg>repair ut bootstrap<commit_after><?php

namespace MamuzBlogTest\Controller;

use MamuzBlog\Controller\QueryController;
use MamuzBlogTest\Bootstrap;
use Zend\Console\Request as ConsoleRequest;
use Zend\Http\Headers;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;

class QueryControllerTest extends \PHPUnit_Framework_TestCase
{
    /** @var \Zend\Mvc\Controller\AbstractActionController */
    protected $fixture;

    /** @var Request */
    protected $request;

    /** @var Response */
    protected $response;

    /** @var RouteMatch */
    protected $routeMatch;

    /** @var MvcEvent */
    protected $event;

    /** @var Headers */
    protected $xhrHeaders;

    /** @var \MamuzBlog\Feature\QueryInterface | \Mockery\MockInterface */
    protected $queryInterface;

    /** @var \MamuzBlog\Crypt\AdapterInterface | \Mockery\MockInterface */
    protected $cryptEngine;

    /** @var \Zend\Mvc\Controller\Plugin\Params | \Mockery\MockInterface */
    protected $params;

    protected function setUp()
    {
        $this->cryptEngine = \Mockery::mock('MamuzBlog\Crypt\AdapterInterface');
        $this->queryInterface = \Mockery::mock('MamuzBlog\Feature\QueryInterface');

        $this->xhrHeaders = new Headers;
        $this->xhrHeaders->addHeaderLine('X_REQUESTED_WITH', 'XMLHttpRequest');

        $this->fixture = new QueryController($this->queryInterface, $this->cryptEngine);
        $this->request = new Request();
        $this->routeMatch = new RouteMatch(array('controller' => 'index'));
        $this->event = new MvcEvent();
        $router = HttpRouter::factory();

        $this->params = \Mockery::mock('Zend\Mvc\Controller\Plugin\Params');
        $this->params->shouldReceive('__invoke')->andReturn($this->params);
        $pluginManager = \Mockery::mock('Zend\Mvc\Controller\PluginManager')->shouldIgnoreMissing();
        $pluginManager->shouldReceive('get')->with('params', null)->andReturn($this->params);

        $this->fixture->setPluginManager($pluginManager);
        $this->event->setRouter($router);
        $this->event->setRouteMatch($this->routeMatch);
        $this->fixture->setEvent($this->event);
    }

    public function testExtendingZendActionController()
    {
        $this->assertInstanceOf('Zend\Mvc\Controller\AbstractActionController', $this->fixture);
    }

    public function testActivePostsWithoutTagCanBeAccessed()
    {
        $page = 'foo';
        $params = array(2, 1);
        $posts = array(1, 2);

        $this->params->shouldReceive('fromRoute')->with('page', 1)->andReturn($page);
        $this->params->shouldReceive('fromRoute')->with('tag')->andReturn(null);
        $this->params->shouldReceive('fromRoute')->andReturn($params);

        $this->queryInterface->shouldReceive('setCurrentPage')->with($page);
        $this->queryInterface->shouldReceive('findActivePosts')->andReturn($posts);

        $this->routeMatch->setParam('action', 'activePosts');

        // HTTP
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($params, $viewVariables['routeParams']);
        $this->assertSame($posts, $viewVariables['collection']);
        $this->assertFalse($viewVariables['isTerminal']);

        // XHR
        $this->request->setHeaders($this->xhrHeaders);
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($params, $viewVariables['routeParams']);
        $this->assertSame($posts, $viewVariables['collection']);
        $this->assertTrue($viewVariables['isTerminal']);

        // CLI
        $this->request = new ConsoleRequest;
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($params, $viewVariables['routeParams']);
        $this->assertSame($posts, $viewVariables['collection']);
        $this->assertTrue($viewVariables['isTerminal']);
    }

    public function testActivePostsWithTagCanBeAccessed()
    {
        $page = 'foo';
        $tag = 'bar';
        $params = array(2, 1);
        $posts = array(1, 2);

        $this->params->shouldReceive('fromRoute')->with('page', 1)->andReturn($page);
        $this->params->shouldReceive('fromRoute')->with('tag')->andReturn($tag);
        $this->params->shouldReceive('fromRoute')->andReturn($params);

        $this->queryInterface->shouldReceive('setCurrentPage')->with($page);
        $this->queryInterface->shouldReceive('findActivePostsByTag')->with($tag)->andReturn($posts);
        $this->routeMatch->setParam('action', 'activePosts');

        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($params, $viewVariables['routeParams']);
        $this->assertSame($posts, $viewVariables['collection']);
        $this->assertFalse($viewVariables['isTerminal']);
    }

    public function testActivePostWithPostCanBeAccessed()
    {
        $id = 'foo';
        $post = 'bar';

        $this->params->shouldReceive('fromRoute')->with('id')->andReturn($id);
        $this->cryptEngine->shouldReceive('decrypt')->with($id)->andReturn($id);
        $this->queryInterface->shouldReceive('findActivePostById')->with($id)->andReturn($post);

        $this->routeMatch->setParam('action', 'activePost');

        // HTTP
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($post, $viewVariables['post']);
        $this->assertFalse($viewVariables['isTerminal']);

        // XHR
        $this->request->setHeaders($this->xhrHeaders);
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($post, $viewVariables['post']);
        $this->assertTrue($viewVariables['isTerminal']);

        // CLI
        $this->request = new ConsoleRequest;
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
        $this->assertEquals(200, $response->getStatusCode());

        $viewVariables = $result->getVariables();
        $this->assertSame($post, $viewVariables['post']);
        $this->assertTrue($viewVariables['isTerminal']);
    }

    public function testActivePostWithoutPostCanBeAccessed()
    {
        $id = 'foo';

        $this->params->shouldReceive('fromRoute')->with('id')->andReturn($id);
        $this->cryptEngine->shouldReceive('decrypt')->with($id)->andReturn($id);
        $this->queryInterface->shouldReceive('findActivePostById')->with($id)->andReturn(null);

        $this->routeMatch->setParam('action', 'activePost');
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertNull($result);
        $this->assertEquals(404, $response->getStatusCode());
    }

    public function testActivePostWithNullDecryptionCanBeAccessed()
    {
        $id = 'foo';

        $this->params->shouldReceive('fromRoute')->with('id')->andReturn($id);
        $this->cryptEngine->shouldReceive('decrypt')->with($id)->andReturn(null);

        $this->routeMatch->setParam('action', 'activePost');
        $result = $this->fixture->dispatch($this->request);
        $response = $this->fixture->getResponse();

        $this->assertNull($result);
        $this->assertEquals(404, $response->getStatusCode());
    }
}

{'fraction_non_alphanumeric': 0.17937370455436671, 'fraction_numerical': 0.0041454260265531345, 'mean_word_length': 3.6069677419354838, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 435, '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': '1884709', 'n_tokens_mistral': 5133, 'n_tokens_neox': 4923, 'n_words': 630}