Writing unit test in slim framework

Writing Unit test for Slim Framework

It’s not so easy to write Test Cases for slim framework. But I tried by autoloading SlimEnvironment class by requiring applications global autoload script.

And after that I just created a Slim mock environment to handle the test case environment.

require_once __DIR__ . '/../vendor/autoload.php';
use Slim\Environment;
class RoutesTest extends PHPUnit_Framework_TestCase
    public function request($method, $path, $options = array())
        // Capture STDOUT
        // Prepare a mock environment
            'REQUEST_METHOD' => $method,
            'PATH_INFO' => $path,
            'SERVER_NAME' => 'slim-test.dev',
        ), $options));
        $app = new \Slim\Slim();
        $this->app = $app;
        $this->request = $app->request();
        $this->response = $app->response();
        // Return STDOUT
        return ob_get_clean();
    public function get($path, $options = array())
        $this->request('GET', $path, $options);
    public function testIndex()
        $this->assertEquals('200', $this->response->status());

If you have any further idea about to please share with me. Thanks in advance!

Shaharia is a professional software engineer with more than 10 years of experience in the relevant fields. Digital ad certified, cloud platform architect, Big data enthusiasts, tech early adopters.

Site Footer