hakanersu
4/19/2014 - 1:12 PM

gistfile1.php

<?php
/**
 * This is a very basic getting started example for setting up a basic project using PSR-4 and
 * using composer's autoloader to run the tests.
 * 
 */

// Create directory MyApp
// Inside MyApp/ create composer.json with the below contents

{
    "require": {
    },
    "autoload": {
        "psr-4": {
            "MyApp\\": ["src/", "tests/"]
        }
    }
}

// Create two directories for src/ and tests/

// Create directory MyApp/src/Http
// This is a very lightweight Request.php file. A class handling only Get and Post

namespace MyApp\Http;

class Request
{
    private $query;

    private $request;

    public function __construct(array $query = array(), array $request = array())
    {
        $this->query = $query;
        $this->request = $request;
    }

    public static function init()
    {
        return new self(
            $_GET,
            $_POST
        );
    }

    public function get($param, $default = null)
    {
        // order being called: GET, POST
        return $this->getQuery($param, $this->getPost($param, $default));
    }

    public function getPost($param, $default = null)
    {
        if (!isset($this->request[$param])) {
            return $default;
        }

        return $this->request[$param];
    }

    public function getQuery($param, $default = null)
    {
        if (!isset($this->query[$param])) {
            return $default;
        }

        return $this->query[$param];
    }
}

// Create directory MyApp/tests/Http
// This is the RequestTest.php file

namespace MyApp\Http;

class RequestTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var Request
     */
    protected $request;

    public function setUp()
    {
        $this->request = new Request();
    }

    public function testGet()
    {
        $request = new Request(array('q1' => 'query1'));

        $this->assertEquals('query1', $request->get('q1', false));
    }

    public function testGetKeyNotFoundReturnsDefaultNull()
    {
        $request = new Request(array('q1' => 'query1'));

        $this->assertNull($request->get('q3'));
    }

    public function testGetKeyNotFoundReturnsCustomEmptyArray()
    {
        $request = new Request(array('q1' => 'query1'));

        $this->assertEquals(array(), $request->get('q3', array()));
    }

    public function testGetPost()
    {
        $request = new Request(array(), array('p1' => 'post1'));

        $this->assertEquals('post1', $request->getPost('p1', false));


    }

    public function testGetQuery()
    {
        $request = new Request(array('q1' => 'query1'));

        $this->assertEquals('query1', $request->getQuery('q1', false));
    }

}

// Create MyApp/tests/phpunit.xml.dist so that phpunit knows where how to bootstrap the application
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
         bootstrap="../vendor/autoload.php"
        >
    <testsuites>
        <testsuite>
            <directory>.</directory>
        </testsuite>
    </testsuites>

</phpunit>


// Run composer install in the root directory of MyApp 
// If succesful, composer has generated the autoloader file, at: MyApp/vendor/autoload.php

// Now we can run phpunit in MyApp/tests just by calling phpunit