What format do I pass data to slim post unit test?


I’m trying to create a unit test for a post route that I know is working. What I’m struggling with is how to pass data to Slim’s runApp method, which is a method in the testing class BaseTestClass. Here is the code for my test:

public function testCreateUser()
    $username = 'test-user';
    $email = 'test-email';
    $data = ['username' => $username, 'email' => $email];
    $response = $this->runApp('POST', '/api/user', $data);

    $this->assertEquals(200, $response->getStatusCode());
    $this->assertContains('id', (string)$response->getBody());

The test returns a 500 error so it fails the first assertion. The route worked when a request is sent from the angular code below:

service.createUser = function(user) {

return $http.post('api/user', user).success(function(data) {


where user is a js object: var user = {username: username, email: email}. Many thanks for your help…

Hi all,

I’ve continued trying to debug this issue with limited success. I did get some more insight into what was causing the 500 error: Call to a member function prepare() on null. File: /Applications/MAMP/htdocs/chatClient/src/routes.php. Line: 121.

This is line 121-- $stmt = $this->db->prepare($sql); So, the db is null while the test is running. I’m not sure why since the route works fine under normal circumstances. Any help would be GREATLY appreciated. Thanks!!

In case it’s helpful, this is the code around Line 121–

$app->get('/api/getmessages/{user_id}', function ($request, $response, $args) {
try {
$sql = "SELECT author, message, date, user_id FROM messages
      WHERE user_id = :user_id ORDER BY date DESC";
$stmt = $this->db->prepare($sql);
$user_id = $args['user_id'];
$stmt->bindParam(':user_id', $user_id);
$result = array();
while($row = $stmt->fetchObject()) {
    $result[] = $row;
$newResponse = $response->withJson($result);
return $newResponse;
} catch (PDOException $pdoException) {
  // Do something with your error message, for now you could just:
   echo $pdoException->getMessage();