there4 / slim-test-helpers Goto Github PK
View Code? Open in Web Editor NEWIntegration testing helpers for the Slim Framework
License: MIT License
Integration testing helpers for the Slim Framework
License: MIT License
I'm using this library to do testing on my SLIM-based site. I have noticed that under some circumstances, the responses coming back from a GET request will be incorrect when run repeatedly.
For instance, the first response from this will function as expected:
$this->client->get('/topic/appetites/rss');
$body = $this->client->response->getBody();
And $body
contains the expected information.
However, if you run this:
$this->client->get('/topic/education/rss');
$body = $this->client->response->getBody();
$body
will be empty.
A similar thing happens when testing the status of responses. If the preceding responses were a 200, a subsequent response that you'd expect to be a 404 will return 200. But, if you put that 404 before the 200s, it will properly return a 404.
I think this has something to do with the output buffering (oh fun!), but I haven't been able to figure it out yet. I'm posting this in the hopes that someone else has seen the issue and has some thoughts. I'm not expecting anyone to fix this for me and I hope to be able to dig into it further in the near future.
When trying the same as the first example you have in your README.md file, calling $this->client->response->getBody()->getContent()
returns ""
in my test meaning the following does not work:
$this->client->get('/');
$this->assertEquals('Hello World', $this->client->response->getBody());
Your example code:
class VersionTest extends LocalWebTestCase
{
public function testVersion()
{
$this->client->get('/version');
$this->assertEquals(200, $this->client->response->getStatusCode());
$this->assertEquals($this->app->config('version'), $this->client->response->getBody());
}
}
We have our own version (replicated) of WebTestCase
as we have our own config and setup process.
To get it working, our test make-up is the following:
public function testHomePage()
{
$body = $this->client->get('/');
$this->assertEquals(200, $this->client->response->getStatusCode());
$this->assertContains('Hello world', $body);
$this->assertContains('<title>Welcome</title>', $body);
}
The main difference with mine is that I am assigning $body = $this->client->get('/');
as ::get
calls ::request
but ends with return (string)$this->response->getBody();
.
Am I missing something or have the docs not been updated? I can PR on the fix in the docs providing I am not missing something first.
Hi, thanks for the library.
Thoughts on implementing $_FILES superglobal testing?
Hi,
I'm trying to build a integration test suite in my project and with a simple test for a get query I get the following error
ErrorException: Undefined index: REQUEST_METHOD
/path/vendor/slim/slim/Slim/Environment.php:124
/path/vendor/slim/slim/Slim/Environment.php:76
/path/vendor/slim/slim/Slim/Slim.php:159
/path/vendor/slim/slim/Slim/Helper/Set.php:228
/path/vendor/slim/slim/Slim/Helper/Set.php:88
/path/vendor/slim/slim/Slim/Slim.php:234
/path/vendor/slim/slim/Slim/Slim.php:674
/path/vendor/slim/slim/Slim/Middleware/MethodOverride.php:78
/path/src/Middleware/ApiAuthorization.php:57
/path/vendor/slim/slim/Slim/Slim.php:1302
/path/public/index.php:80
/path/Test/BaseTestCase.php:21
/path/vendor/there4/slim-test-helpers/src/There4/Slim/Test/WebTestCase.php:19
$this->client->get('/v1/users/fake', array(), $this->getAuthHeader());
$this->assertEquals(200, $this->client->response->status());
$this->assertEquals(array(), $this->client->response->body());
I don't know if I need to do something more, this is my getSlimInstance method:
$testSettings = array(
'version' => '0.0.0',
'debug' => false,
'mode' => 'testing',
);
$settings = include __DIR__.'/../config/settings.php';
$settings = array_merge($settings, $testSettings);
$app = new \Slim\Slim($settings);
Did I forgot about something?
Thanks
Ouch, just discovered that my tests broke after upgrading from 2.1.1 to 2.1.2. Turned out to be two issues:
process()
over __invoke()
introduced in #35 caused this to happen:Slim Application Error:
Type: UnexpectedValueException
Message: Middleware must return instance of \Psr\Http\Message\ResponseInterface
File: /vagrant/vendor/slim/slim/Slim/MiddlewareAwareTrait.php
Line: 75
Trace: #0 /vagrant/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(123): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#1 /vagrant/vendor/slim/slim/Slim/App.php(370): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#2 /vagrant/vendor/there4/slim-test-helpers/src/There4/Slim/Test/WebTestClient.php(107): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#3 /vagrant/vendor/there4/slim-test-helpers/src/There4/Slim/Test/WebTestClient.php(38): There4\Slim\Test\WebTestClient->request('get', '/api/auth/login', Array, Array)
#4 /vagrant/tests/integration/AuthTest.php(7): There4\Slim\Test\WebTestClient->get('/api/auth/login')
#5 [internal function]: AuthTest->testLoginRoute()
#6 /vagrant/vendor/phpunit/phpunit/src/Framework/TestCase.php(909): ReflectionMethod->invokeArgs(Object(AuthTest), Array)
#7 /vagrant/vendor/phpunit/phpunit/src/Framework/TestCase.php(768): PHPUnit_Framework_TestCase->runTest()
#8 /vagrant/vendor/phpunit/phpunit/src/Framework/TestResult.php(612): PHPUnit_Framework_TestCase->runBare()
#9 /vagrant/vendor/phpunit/phpunit/src/Framework/TestCase.php(724): PHPUnit_Framework_TestResult->run(Object(AuthTest))
#10 /vagrant/vendor/phpunit/phpunit/src/Framework/TestSuite.php(722): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#11 /vagrant/vendor/phpunit/phpunit/src/Framework/TestSuite.php(722): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#12 /vagrant/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(440): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#13 /vagrant/vendor/phpunit/phpunit/src/TextUI/Command.php(149): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#14 /vagrant/vendor/phpunit/phpunit/src/TextUI/Command.php(100): PHPUnit_TextUI_Command->run(Array, true)
#15 /vagrant/vendor/phpunit/phpunit/phpunit(52): PHPUnit_TextUI_Command::main()
#16 {main}
View in rendered output by enabling the "displayErrorDetails" setting.
To fix this, I made a slightly more complicated mock. Would have been better if there was a way to disable all middleware. Didn't find one, but I wouldn't be surprised if it exists..
While these two issues are not likely to bite many users, I wonder if the best would be to delete the 2.1.2 release and release it as 3.0.0 instead?
When can we expect this?
So we can use slim 2.6 as fixed by a2b62fe and specify a version number for slim-test-helpers
Hi,
I'm upgrading to php 7 in my slim project and my unit testing can't work with scalar type hinting and returning types because that's only supported in phpunit 5.*
Is there a possibility to migrate and support phpunit 5.*?
Thanks
Hi,
Any chance to get a new release with the last fixes : 8558755
Thx :)
I am facing this weird problem.
I have wrote the following test.
public function testGetTaskGroup()
{
$this->client->get('/task/group');
$this->assertEquals(200, $this->client->response->status());
$results = json_decode($this->client->response->body(), true);
}
public function testCreateTaskGroupItem()
{
$data = array(
"name" => "first_task"
);
$this->client->post('/task/group', $data);
$this->assertEquals(200, $this->client->response->status());
$results = json_decode($this->client->response->body(), true);
}
The POST variables in '/task/group' endpoint is missing when I retrieve it through $app->request->post() or $app->request->params() but if I comment out testGetTaskGroup
, the variables for POST are appearing inside '/task/group' endpoint.
Any idea why this is happening? Thanks.
The current stable version of Slim is 2.6.2, but until now the maximum version supported by slim-test-helpers is 2.4.*. Will the current stable version of Slim be supported in one of the next releases?
Hi,
I would like to test an 'Internal Server Error' from a LocalWebTestCase but I do not know how to do it.
First I added this error handler in SlimApp:
// Error handler
$container['errorHandler'] = function ($c) {
return function ($request, $response, $exception) use ($c) {
$c->logger->error('Internal Server Error');
$data = array('message' => 'Internal Server Error');
return $c['response']->withJson($data, 500);
};
};
I added a specific route only for testing purpose:
// Route for testing server internal error (ie: $container['errorHandler'])
$app->get('/internalerror', function ($request, $response, $args) {
$this->logger->info("Route '/' internalerror");
throw new \Exception('Testing /internalerror.');
return $response;
});
In a test case I do:
public function testInternalError()
{
try {
$this->client->get('/internalerror');
} catch (\Exception $e) {
}
$response = $this->client->response;
$this->assertEquals(500, $response->getStatusCode());
$this->assertEquals('Internal Server Error', $response->getReasonPhrase());
$this->assertEquals('application/json;charset=utf-8', $response->getHeader('Content-Type')[0]);
$data = json_decode($response->getBody());
$this->assertEquals('Internal Server Error', $data->message);
}
But $this->client->response
return null.
My aim would be to test the message sent by the server defined in the error handler.
In a production environment, this route is working fine and I have the message 'Internal Server Error' in json.
http://localhost:8080/internalerror
{"message":"Internal Server Error"}
Thanks for your help!
Since e2674fc calling get
, post
(or any other methods) doesn't return the HTTP response body as it used to do with __call
.
This breaks some of our tests :(
PR will be sent soon.
This project was extracted from the https://github.com/there4/slim-unit-testing-example and as such, it doesn't have it's own tests yet.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.