Skip to content

Instantly share code, notes, and snippets.

@bubbobne
Created June 29, 2017 14:30
Show Gist options
  • Save bubbobne/cf576e5729a4ba9b633d02b1735dfea9 to your computer and use it in GitHub Desktop.
Save bubbobne/cf576e5729a4ba9b633d02b1735dfea9 to your computer and use it in GitHub Desktop.
slim phpunit test
<?php
use myAppNameSpace\App;
use Slim\Http\Environment;
use Slim\Http\Request;
/**
* Description of ExampeSlimTest
*
* @author daniele andreis
*/
class ExampleSlimTest extends PHPUnit_Framework_TestCase {
protected $app;
public function setUp() {
$this->app = (new App())->get();
}
public function testApiRootGet() {
$env = Environment::mock([
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/'
]);
$req = Request::createFromEnvironment($env);
$this->app->getContainer()['request'] = $req;
$response = $this->app->run(true);
$this->assertSame($response->getStatusCode(), 200);
$this->assertSame((string) $response->getBody(), "my API description");
}
public function testBasicAuth() {
$env = Environment::mock([
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/myEndPoint',
]);
$_SERVER['PHP_AUTH_USER']='myUser';
$_SERVER['PHP_AUTH_PW']='myPWD';
$req = Request::createFromEnvironment($env);
$this->app->getContainer()['request'] = $req;
$response = $this->app->run(true);
$this->assertSame($response->getStatusCode(), 200);
$this->assertTrue(is_array(json_decode((string) $response->getBody())), "The respose not contains an array");
}
public function testFailedBasicAuth() {
$env = Environment::mock([
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/myEndPoint',
]);
$req = Request::createFromEnvironment($env);
$this->app->getContainer()['request'] = $req;
$response = $this->app->run(true);
$this->assertSame($response->getStatusCode(), 401);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment