<?php

namespace Ingot\Tests;

use Ingot\Tests\Fixtures\app\Application;
use Symfony\Component\HttpFoundation\Request;
use function Ingot\debug_enabled;

class ApplicationTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @test
     */
    function it_loads_application_with_env_and_pco_file()
    {
        $app = $this->createApplication();
        $app->boot();

        $this->assertTrue($app['dev']);
        $this->assertEquals('dev', $app['env']);
        $this->assertEquals('/some/path', $app['pco_expanded']);
    }

    /**
     * @test
     */
    public function it_overwrites_remote_addr_with_x_real_ip()
    {

        $app = $this->createApplication();

        $request = Request::create('/');
        $request->server->set('REMOTE_ADDR', 'not-my-ip');
        $request->headers->set('X_REAL_IP', 'my-ip');

        $this->createApplication()->handle($request);

        $this->assertEquals('my-ip', $request->server->get('REMOTE_ADDR'));
        $this->assertEquals('my-ip', $request->getClientIp());
    }

    /**
     * @test
     */
    function it_guesses_if_debug_is_enabled()
    {
        $this->assertTrue(debug_enabled('dev'));
        $this->assertTrue(debug_enabled('test'));
        $this->assertFalse(debug_enabled('stage'));
        $this->assertFalse(debug_enabled('prod'));
    }

    private function createApplication()
    {
        $app = new Application(__DIR__ . '/Fixtures/app', 'dev', true);

        return $app;
    }
}
