<commit_before><?php namespace MaartenStaa\Routing;

/**
 * Copyright (c) 2015 by <NAME>.
 *
 * Some rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *
 *     * The names of the contributors may not be used to endorse or
 *       promote products derived from this software without specific
 *       prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

use Illuminate\Cache\CacheManager;
use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;

class IntegrationTest extends \PHPUnit_Framework_TestCase
{
    /**
     * The Illuminate application instance.
     *
     * @var \Illuminate\Foundation\Application
     */
    protected $app;

    /**
     * Setup the test environment.
     *
     * @return void
     */
    public function setUp()
    {
        if ($this->app === null) {
            $this->refreshApplication();
        }
    }

    /**
     * Refresh the application instance.
     *
     * @return void
     */
    protected function refreshApplication()
    {
        $this->app = $this->createApplication();

        $this->app['env'] = 'testing';

        $loader = $this->getMockBuilder('Illuminate\Config\LoaderInterface')
            ->setMethods(array('load', 'exists', 'getNamespaces', 'cascadePackage'))
            ->getMockForAbstractClass();

        $loader->method('load')
            ->will($this->returnValue(array()));

        $loader->method('exists')
            ->will($this->returnValue(true));

        $loader->method('getNamespaces')
            ->will($this->returnValue(array()));

        $loader->method('cascadePackage')
            ->will($this->returnValue(array()));

        $this->app['config'] = new Repository($loader, $this->app['env']);

        $this->app['cache'] = new CacheManager($this->app);
        $this->app['cache']->setDefaultDriver('array');

        $this->app->boot();
    }

    /**
     * Creates the application.
     *
     * @return \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    protected function createApplication()
    {
        return new Application;
    }

    /**
     * Create a router.
     *
     * @return Router
     */
    protected function getRouter()
    {
        return new Router($this->app['events'], $this->app);
    }

    public function testCacheRoutes()
    {
        $router = $this->getRouter();

        $key = $router->cache(
            __FILE__,
            function () use ($router) {
                $router->get('/', 'HomeController@actionIndex');
            }
        );

        $this->assertTrue($this->app->cache->has($key), 'Routes must be in cache');
        $this->assertEquals(1, $router->getRoutes()->count(), 'Routes must be in collection');

        $cachedRoutes = unserialize($this->app->cache->get($key));
        $this->assertArrayHasKey('routes', $cachedRoutes);
        $this->assertArrayHasKey('GET', $cachedRoutes['routes']);
        $this->assertCount(1, $cachedRoutes['routes']['GET']);

        // Next request should not call the callback.
        $router = $this->getRouter();
        $router->cache(
            __FILE__,
            function () use ($router) {
                throw new Exception('This should not be called');
            }
        );
        $this->assertEquals(1, $router->getRoutes()->count(), 'Routes must be obtained from cache');
    }

    public function testAllMethodsWorks()
    {
        $methods = array('get', 'post', 'put', 'patch', 'delete');

        $router = $this->getRouter();

        $key = $router->cache(
            __FILE__,
            function () use ($router, $methods) {
                foreach ($methods as $method) {
                    $router->$method('/', 'HomeController@action' . ucfirst($method));
                }
            }
        );

        $this->assertTrue($this->app->cache->has($key), 'Routes must be in cache');
        $this->assertEquals(count($methods), $router->getRoutes()->count(), 'Routes must be in collection');

        $cachedRoutes = unserialize($this->app->cache->get($key));
        $this->assertArrayHasKey('routes', $cachedRoutes);

        foreach ($methods as $method) {
            $this->assertArrayHasKey(strtoupper($method), $cachedRoutes['routes']);
            $this->assertCount(1, $cachedRoutes['routes'][strtoupper($method)]);
        }
    }
}<commit_msg>Add integration test for controller routing<commit_after><?php namespace MaartenStaa\Routing;

/**
 * Copyright (c) 2015 by <NAME>.
 *
 * Some rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *
 *     * The names of the contributors may not be used to endorse or
 *       promote products derived from this software without specific
 *       prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

use Illuminate\Cache\CacheManager;
use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;

class IntegrationTest extends \PHPUnit_Framework_TestCase
{
    /**
     * The Illuminate application instance.
     *
     * @var \Illuminate\Foundation\Application
     */
    protected $app;

    /**
     * Setup the test environment.
     *
     * @return void
     */
    public function setUp()
    {
        if ($this->app === null) {
            $this->refreshApplication();
        }
    }

    /**
     * Refresh the application instance.
     *
     * @return void
     */
    protected function refreshApplication()
    {
        $this->app = $this->createApplication();

        $this->app['env'] = 'testing';

        $loader = $this->getMockBuilder('Illuminate\Config\LoaderInterface')
            ->setMethods(array('load', 'exists', 'getNamespaces', 'cascadePackage'))
            ->getMockForAbstractClass();

        $loader->method('load')
            ->will($this->returnValue(array()));

        $loader->method('exists')
            ->will($this->returnValue(true));

        $loader->method('getNamespaces')
            ->will($this->returnValue(array()));

        $loader->method('cascadePackage')
            ->will($this->returnValue(array()));

        $this->app['config'] = new Repository($loader, $this->app['env']);

        $this->app['cache'] = new CacheManager($this->app);
        $this->app['cache']->setDefaultDriver('array');

        $this->app->boot();
    }

    /**
     * Creates the application.
     *
     * @return \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    protected function createApplication()
    {
        return new Application;
    }

    /**
     * Create a router.
     *
     * @return Router
     */
    protected function getRouter()
    {
        return new Router($this->app['events'], $this->app);
    }

    public function testCacheRoutes()
    {
        $router = $this->getRouter();

        $key = $router->cache(
            __FILE__,
            function () use ($router) {
                $router->get('/', 'HomeController@actionIndex');
            }
        );

        $this->assertTrue($this->app->cache->has($key), 'Routes must be in cache');
        $this->assertEquals(1, $router->getRoutes()->count(), 'Routes must be in collection');

        $cachedRoutes = unserialize($this->app->cache->get($key));
        $this->assertArrayHasKey('routes', $cachedRoutes);
        $this->assertArrayHasKey('GET', $cachedRoutes['routes']);
        $this->assertCount(1, $cachedRoutes['routes']['GET']);

        // Next request should not call the callback.
        $router = $this->getRouter();
        $router->cache(
            __FILE__,
            function () use ($router) {
                throw new Exception('This should not be called');
            }
        );
        $this->assertEquals(1, $router->getRoutes()->count(), 'Routes must be obtained from cache');
    }

    public function testAllMethodsWorks()
    {
        $methods = array('get', 'post', 'put', 'patch', 'delete');

        $router = $this->getRouter();

        $key = $router->cache(
            __FILE__,
            function () use ($router, $methods) {
                foreach ($methods as $method) {
                    $router->$method('/', 'HomeController@action' . ucfirst($method));
                }
            }
        );

        $this->assertTrue($this->app->cache->has($key), 'Routes must be in cache');
        $this->assertEquals(count($methods), $router->getRoutes()->count(), 'Routes must be in collection');

        $cachedRoutes = unserialize($this->app->cache->get($key));
        $this->assertArrayHasKey('routes', $cachedRoutes);

        foreach ($methods as $method) {
            $this->assertArrayHasKey(strtoupper($method), $cachedRoutes['routes']);
            $this->assertCount(1, $cachedRoutes['routes'][strtoupper($method)]);
        }

        // Next request should not call the callback.
        $router = $this->getRouter();
        $router->cache(
            __FILE__,
            function () use ($router) {
                throw new Exception('This should not be called');
            }
        );
        $this->assertEquals(count($methods), $router->getRoutes()->count(), 'Routes must be obtained from cache');
    }

    public function testControllerRouting()
    {
        $router = $this->getRouter();

        $controllerName = str_shuffle('abcdefghijklmnopqrstuvwxyz');

        // Create a controller class.
        eval('class ' . $controllerName . ' { public function getHomePage() {} }');

        $key = $router->cache(
            __FILE__,
            function () use ($router, $controllerName) {
                $router->controller('/', $controllerName);
            }
        );

        $this->assertTrue($this->app->cache->has($key), 'Routes must be in cache');
        // 2 because controller adds missingMethod
        $this->assertEquals(2, $router->getRoutes()->count(), 'Routes must be in collection');

        // Next request should not call the callback.
        $router = $this->getRouter();
        $router->cache(
            __FILE__,
            function () use ($router) {
                throw new Exception('This should not be called');
            }
        );
        $this->assertEquals(2, $router->getRoutes()->count(), 'Routes must be obtained from cache');
    }
}
{'fraction_non_alphanumeric': 0.13426727530300644, 'fraction_numerical': 0.0014953565244766253, 'mean_word_length': 2.514103982300885, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6711045', 'n_tokens_mistral': 3403, 'n_tokens_neox': 3269, 'n_words': 948}