<commit_before><?php
/*
 * Copyright (c) Ouzo contributors, http://ouzoframework.org
 * This file is made available under the MIT License (view the LICENSE file for more information).
 */
namespace Ouzo\Tests;

use Ouzo\Config;
use Ouzo\FrontController;
use Ouzo\Injection\Injector;
use Ouzo\Injection\InjectorConfig;
use Ouzo\Utilities\Arrays;
use Ouzo\Utilities\Strings;

class ControllerTestCase extends DbTransactionalTestCase
{
    /**
     * @var InjectorConfig
     */
    protected $injectorConfig;
    /**
     * @var FrontController
     */
    protected $frontController;

    public function __construct($name = null, array $data = array(), $dataName = '')
    {
        parent::__construct($name, $data, $dataName);

        $this->injectorConfig = new InjectorConfig();
        $this->frontControllerBindings($this->injectorConfig);
        $injector = new Injector($this->injectorConfig);
        $this->frontController = $injector->getInstance('\Ouzo\FrontController');
    }

    private static function _prefixSystem()
    {
        return Config::getValue('global', 'prefix_system');
    }

    public function get($url, $data = null)
    {
        $url = $this->_appendParamsToUrl($url, $data);
        $_SERVER['REQUEST_URI'] = self::_prefixSystem() . $url;
        $_SERVER['REQUEST_METHOD'] = 'GET';
        $_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);

        $this->_initFrontController();
    }

    private function _appendParamsToUrl($url, $data)
    {
        if ($data) {
            $conjunction = $this->_urlHasParams($url) ? "&" : "?";
            return $url . $conjunction . http_build_query($data);
        }
        return $url;
    }

    private function _urlHasParams($url)
    {
        return count($this->_parseUrlParams($url)) > 0;
    }

    private function _parseUrlParams($url)
    {
        $urlComponents = parse_url($url);
        $query = Arrays::getValue($urlComponents, 'query', '');
        parse_str($query, $array);
        return $array;
    }

    private function _initFrontController()
    {
        $this->frontController->init();
    }

    public function post($url, $data)
    {
        $_SERVER['REQUEST_URI'] = self::_prefixSystem() . $url;
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $_POST = $data;
        $_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);

        $this->_initFrontController();
    }

    public function put($url, $data)
    {
        $_SERVER['REQUEST_URI'] = self::_prefixSystem() . $url;
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $_POST = array_merge($data, array('_method' => 'PUT'));
        $_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);
        $this->_initFrontController();
    }

    public function patch($url)
    {
        $_SERVER['REQUEST_URI'] = self::_prefixSystem() . $url;
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $_POST['_method'] = 'PATCH';
        $_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);
        $this->_initFrontController();
    }

    public function delete($url)
    {
        $_SERVER['REQUEST_URI'] = self::_prefixSystem() . $url;
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $_POST['_method'] = 'DELETE';
        $_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);
        $this->_initFrontController();
    }

    public function assertRedirectsTo($path)
    {
        $this->assertEquals($this->_removePrefix($path), $this->_removePrefix($this->frontController->getRedirectHandler()->getLocation()));
    }

    private function _removePrefix($string)
    {
        return Strings::removePrefix($string, self::_prefixSystem());
    }

    public function assertRenders($viewName)
    {
        $statusResponse = $this->requestContext()->getCurrentControllerObject()->getStatusResponse();
        $location = $this->requestContext()->getCurrentControllerObject()->getRedirectLocation();
        if ($statusResponse != 'show') {
            $this->fail("Expected render $viewName but was $statusResponse $location");
        }
        $this->assertEquals($viewName, $this->requestContext()->getCurrentControllerObject()->view->getViewName());
    }

    public function assertAssignsModel($variable, $modelObject)
    {
        $modelVariable = $this->requestContext()->getCurrentControllerObject()->view->$variable;
        $this->assertNotNull($modelVariable);
        Assert::thatModel($modelVariable)->hasSameAttributesAs($modelObject);
    }

    public function assertDownloadsFile($file)
    {
        $this->assertEquals($file, $this->frontController->getDownloadHandler()->getFileName());
    }

    public function assertAssignsValue($variable, $value)
    {
        $this->assertNotNull($this->requestContext()->getCurrentControllerObject()->view->$variable);
        $this->assertEquals($value, $this->requestContext()->getCurrentControllerObject()->view->$variable);
    }

    public function assertRenderedContent()
    {
        return Assert::thatString($this->getActualContent());
    }

    public function assertRenderedJsonAttributeEquals($attribute, $equals)
    {
        $json = $this->getRenderedJsonAsArray();
        $this->assertEquals($equals, $json[$attribute]);
    }

    public function getAssigned($name)
    {
        return $this->requestContext()->getCurrentControllerObject()->view->$name;
    }

    public function getRenderedJsonAsArray()
    {
        return json_decode($this->getActualContent(), true);
    }

    public function assertResponseHeader($expected)
    {
        $actual = $this->getResponseHeaders();
        Assert::thatArray($actual)->contains($expected);
    }

    public function assertHasCookie($cookieAttributes)
    {
        $actual = $this->frontController->getCookiesSetter()->getCookies();
        Assert::thatArray($actual)->contains($cookieAttributes);
    }

    public function getResponseHeaders()
    {
        return $this->frontController->getHeaderSender()->getHeaders();<commit_msg>Used Json::decode() instead of json_decode()<commit_after><?php
/*
 * Copyright (c) Ouzo contributors, http://ouzoframework.org
 * This file is made available under the MIT License (view the LICENSE file for more information).
 */
namespace Ouzo\Tests;

use Ouzo\Config;
use Ouzo\FrontController;
use Ouzo\Injection\Injector;
use Ouzo\Injection\InjectorConfig;
use Ouzo\Utilities\Arrays;
use Ouzo\Utilities\Json;
use Ouzo\Utilities\Strings;

class ControllerTestCase extends DbTransactionalTestCase
{
    /**
     * @var InjectorConfig
     */
    protected $injectorConfig;
    /**
     * @var FrontController
     */
    protected $frontController;

    public function __construct($name = null, array $data = array(), $dataName = '')
    {
        parent::__construct($name, $data, $dataName);

        $this->injectorConfig = new InjectorConfig();
        $this->frontControllerBindings($this->injectorConfig);
        $injector = new Injector($this->injectorConfig);
        $this->frontController = $injector->getInstance('\Ouzo\FrontController');
    }

    private static function _prefixSystem()
    {
        return Config::getValue('global', 'prefix_system');
    }

    public function get($url, $data = null)
    {
        $url = $this->_appendParamsToUrl($url, $data);
        $_SERVER['REQUEST_URI'] = self::_prefixSystem() . $url;
        $_SERVER['REQUEST_METHOD'] = 'GET';
        $_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);

        $this->_initFrontController();
    }

    private function _appendParamsToUrl($url, $data)
    {
        if ($data) {
            $conjunction = $this->_urlHasParams($url) ? "&" : "?";
            return $url . $conjunction . http_build_query($data);
        }
        return $url;
    }

    private function _urlHasParams($url)
    {
        return count($this->_parseUrlParams($url)) > 0;
    }

    private function _parseUrlParams($url)
    {
        $urlComponents = parse_url($url);
        $query = Arrays::getValue($urlComponents, 'query', '');
        parse_str($query, $array);
        return $array;
    }

    private function _initFrontController()
    {
        $this->frontController->init();
    }

    public function post($url, $data)
    {
        $_SERVER['REQUEST_URI'] = self::_prefixSystem() . $url;
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $_POST = $data;
        $_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);

        $this->_initFrontController();
    }

    public function put($url, $data)
    {
        $_SERVER['REQUEST_URI'] = self::_prefixSystem() . $url;
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $_POST = array_merge($data, array('_method' => 'PUT'));
        $_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);
        $this->_initFrontController();
    }

    public function patch($url)
    {
        $_SERVER['REQUEST_URI'] = self::_prefixSystem() . $url;
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $_POST['_method'] = 'PATCH';
        $_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);
        $this->_initFrontController();
    }

    public function delete($url)
    {
        $_SERVER['REQUEST_URI'] = self::_prefixSystem() . $url;
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $_POST['_method'] = 'DELETE';
        $_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);
        $this->_initFrontController();
    }

    public function assertRedirectsTo($path)
    {
        $this->assertEquals($this->_removePrefix($path), $this->_removePrefix($this->frontController->getRedirectHandler()->getLocation()));
    }

    private function _removePrefix($string)
    {
        return Strings::removePrefix($string, self::_prefixSystem());
    }

    public function assertRenders($viewName)
    {
        $statusResponse = $this->requestContext()->getCurrentControllerObject()->getStatusResponse();
        $location = $this->requestContext()->getCurrentControllerObject()->getRedirectLocation();
        if ($statusResponse != 'show') {
            $this->fail("Expected render $viewName but was $statusResponse $location");
        }
        $this->assertEquals($viewName, $this->requestContext()->getCurrentControllerObject()->view->getViewName());
    }

    public function assertAssignsModel($variable, $modelObject)
    {
        $modelVariable = $this->requestContext()->getCurrentControllerObject()->view->$variable;
        $this->assertNotNull($modelVariable);
        Assert::thatModel($modelVariable)->hasSameAttributesAs($modelObject);
    }

    public function assertDownloadsFile($file)
    {
        $this->assertEquals($file, $this->frontController->getDownloadHandler()->getFileName());
    }

    public function assertAssignsValue($variable, $value)
    {
        $this->assertNotNull($this->requestContext()->getCurrentControllerObject()->view->$variable);
        $this->assertEquals($value, $this->requestContext()->getCurrentControllerObject()->view->$variable);
    }

    public function assertRenderedContent()
    {
        return Assert::thatString($this->getActualContent());
    }

    public function assertRenderedJsonAttributeEquals($attribute, $equals)
    {
        $json = $this->getRenderedJsonAsArray();
        $this->assertEquals($equals, $json[$attribute]);
    }

    public function getAssigned($name)
    {
        return $this->requestContext()->getCurrentControllerObject()->view->$name;
    }

    public function getRenderedJsonAsArray()
    {
        return Json::decode($this->getActualContent(), true);
    }

    public function assertResponseHeader($expected)
    {
        $actual = $this->getResponseHeaders();
        Assert::thatArray($actual)->contains($expected);
    }

    public function assertHasCookie($cookieAttributes)
    {
        $actual = $this->frontController->getCookiesSetter()->getCookies();
        Assert::thatArray($actual)->contains($cookieAttributes);
    }

    public function getResponseHeaders()
    {
        return $this->frontController->getHeaderSender()->getHeaders();
{'fraction_non_alphanumeric': 0.15223731355720357, 'fraction_numerical': 0.00016665277893508876, 'mean_word_length': 3.3188197193234976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 159, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3389139', 'n_tokens_mistral': 3297, 'n_tokens_neox': 3273, 'n_words': 553}