Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is example how you may test your Slim application:</p> <p><a href="https://github.com/mac2000/SlimTestable">https://github.com/mac2000/SlimTestable</a></p> <p>Suppose we have simple application:</p> <pre><code>&lt;?php use Slim\Slim; require_once 'vendor/autoload.php'; $app = new Slim(); $app-&gt;get('/', function(){ echo 'home'; })-&gt;name('home'); $app-&gt;get('/hello/:name', function($name){ echo "hello $name"; })-&gt;name('hello'); $app-&gt;map('/login', function() use($app) { if($app-&gt;request()-&gt;params('login')) { $app-&gt;flash('success', 'Successfully logged in'); $app-&gt;redirect($app-&gt;urlFor('hello', array('name' =&gt; $app-&gt;request()-&gt;params('login')))); } else { $app-&gt;flash('error', 'Wrong login'); $app-&gt;redirect($app-&gt;urlFor('home')); } })-&gt;via('GET', 'POST'); $app-&gt;run(); </code></pre> <p>How do we test it?</p> <p>Create <code>App</code> class:</p> <pre><code>&lt;?php // src/App.php use Slim\Slim; class App extends Slim { function __construct(array $userSettings = array()) { parent::__construct($userSettings); $this-&gt;get('/', function(){ echo 'home'; })-&gt;name('home'); $this-&gt;get('/hello/:name', function($name){ echo "hello $name"; })-&gt;name('hello'); $this-&gt;map('/login', function() { if($this-&gt;request()-&gt;params('login')) { $this-&gt;flash('success', 'Successfully logged in'); $this-&gt;redirect($this-&gt;urlFor('hello', array('name' =&gt; $this-&gt;request()-&gt;params('login')))); } else { $this-&gt;flash('error', 'Wrong login'); $this-&gt;redirect($this-&gt;urlFor('home')); } })-&gt;via('GET', 'POST'); } /** * @return \Slim\Http\Response */ public function invoke() { $this-&gt;middleware[0]-&gt;call(); $this-&gt;response()-&gt;finalize(); return $this-&gt;response(); } } </code></pre> <p>Notice that we move all our routes to new class constructor, also notice new <code>invoke</code> method, which do the same as <code>run</code> method except it returns response rather than echoing it out.</p> <p>Now your <code>index.php</code> file might be like this one:</p> <pre><code>&lt;?php require_once 'vendor/autoload.php'; $app = new App(); $app-&gt;run(); </code></pre> <p>And now it is time for tests:</p> <pre><code>&lt;?php // tests/ExampleTest.php use Slim\Environment; class ExampleTest extends PHPUnit_Framework_TestCase { private $app; public function setUp() { $_SESSION = array(); $this-&gt;app = new App(); } public function testHome() { Environment::mock(array( 'PATH_INFO' =&gt; '/' )); $response = $this-&gt;app-&gt;invoke(); $this-&gt;assertContains('home', $response-&gt;getBody()); } public function testHello() { Environment::mock(array( 'PATH_INFO' =&gt; '/hello/world' )); $response = $this-&gt;app-&gt;invoke(); $this-&gt;assertTrue($response-&gt;isOk()); $this-&gt;assertContains('hello world', $response-&gt;getBody()); } public function testNotFound() { Environment::mock(array( 'PATH_INFO' =&gt; '/not-exists' )); $response = $this-&gt;app-&gt;invoke(); $this-&gt;assertTrue($response-&gt;isNotFound()); } public function testLogin() { Environment::mock(array( 'PATH_INFO' =&gt; '/login' )); $response = $this-&gt;app-&gt;invoke(); $this-&gt;assertTrue($response-&gt;isRedirect()); $this-&gt;assertEquals('Wrong login', $_SESSION['slim.flash']['error']); $this-&gt;assertEquals('/', $response-&gt;headers()-&gt;get('Location')); } public function testPostLogin() { Environment::mock(array( 'REQUEST_METHOD' =&gt; 'POST', 'PATH_INFO' =&gt; '/login', 'slim.input' =&gt; 'login=world' )); $response = $this-&gt;app-&gt;invoke(); $this-&gt;assertTrue($response-&gt;isRedirect()); $this-&gt;assertEquals('Successfully logged in', $_SESSION['slim.flash']['success']); $this-&gt;assertEquals('/hello/world', $response-&gt;headers()-&gt;get('Location')); } public function testGetLogin() { Environment::mock(array( 'PATH_INFO' =&gt; '/login', 'QUERY_STRING' =&gt; 'login=world' )); $response = $this-&gt;app-&gt;invoke(); $this-&gt;assertTrue($response-&gt;isRedirect()); $this-&gt;assertEquals('Successfully logged in', $_SESSION['slim.flash']['success']); $this-&gt;assertEquals('/hello/world', $response-&gt;headers()-&gt;get('Location')); } } </code></pre> <p>You should notice few things:</p> <p>While setting up test we are creating <code>$_SESSION</code> array for test purposes and instantiate our <code>App</code> class object.</p> <p>In tests rather than <code>run</code> we are calling <code>invoke</code> which do the same, but returns response object.</p> <p><code>Environment::mock</code> used to mock requests which are processed with our application.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload