Hi, I’ve been struggling with this for over 2 days at this point and I think that asking for help is the best way to try solving my issue(s).
I can access all these pages from my browser and from Postman, as they are coming from an Ubuntu/Apache local server. My app.php returns an instance of $app and my index.php runs it, as most examples also do.
My PHPUnit is correctly configured (with phpunit.xml and tests/bootstrap.php), and is able to run the simple examples tutorials recommend for checking if it works. However, as soon as I try to use PHPUnit with my application, I end up with 404’s on every page. I might not be pointing at the right pages, but I’ve tested many different options with no luck.
I have exhausted my resources at this point, having searched and found many different examples (such as “Slim-Test-Helper”, “Slim-Integration-Testing-Example”, Akrabat’s various resources, and several other websites). What should I do?
My site uses the RKA\Session middleware to handle logging in, but I haven’t explicitly configured anything to interface with it in the tests, could that be part of the issue?
Thank you in advance to anyone that can help with this, I truly appreciate it.
PHPUnit returns the following message:
There was 1 failure: 1) Demo\Test\DemoTest::testCheckBase Failed asserting that 404 matches expected 200. /var/www/demo/api/mir/tests/BaseTestCase.php:102 /var/www/demo/api/mir/tests/integration/DemoTest.php:11 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
The content of DemoTest is as follows, where the entity path would at least return an authorization failure instead of a server failure if it worked:
<?php namespace Demo\Test; class DemoTest extends BaseTestCase { public function testCheckBase() { $this->request('GET', '/entities'); $this->assertResponseStatus(200); } }
and the content of BaseTestCase is the following:
<?php namespace Demo\Test; use Slim\App; use Slim\Http\Environment; use Slim\Http\Headers; use Slim\Http\Request; use Slim\Http\RequestBody; use Slim\Http\Response; use Slim\Http\Uri; abstract class BaseTestCase extends \PHPUnit\Framework\TestCase { /** @var Response */ private $response; /** @var \Slim\App */ private $app; /** Sets the Slim app definition for each test */ protected function setUp() { $this->app = require __DIR__.'/../public/app.php'; // print_r($this->app); // die(); } /** Destroys local content of global vars */ protected function tearDown() { unset($this->app); unset($this->response); } /** Calls the slim app to handle the request * * @param $method * @param $url * @param $reqParams * * @return $reponse */ protected function request($method, $url, array $reqParams = []) { $request = $this->prepRequest($method, $url, $reqParams); $response = new Response(); $app = $this->app; $this->response = $app($request, $response); print_r($request); // die(); } /** Returns JSON decoded response body as array */ protected function responseData() { return json_decode((string) $this->response->getBody(), true); } /** Returns request object * * @param $method * @param $url * @param $reqParams[] * * @return $request */ private function prepRequest($method, $url, array $reqParams) { $env = Environment::mock([ 'REQUEST_URI' => $url, 'REQUEST_METHOD' => $method ]); $parts = explode('?', $url); if(isset($parts[1])) { $env['QUERY_STRING'] = $parts[1]; } $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new RequestBody(); $body->write(json_encode($reqParams)); $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); return $request->withHeader('Content-Type', 'application/json'); } /** ----------------------- * Custom assertion helpers * ------------------------ */ protected function assertResponseStatus($expectedStatus) { $this->assertEquals($expectedStatus, $this->response->getStatusCode()); } protected function assertResponseContentType($expectedContentType) { $this->assertContains($expectedContentType, $this->response->getHeader('Content-Type')); } }