I am having some trouble running tests for PUT
, POST
and DELETE
using the unit testing framework phpspec2.
This is the code that I have in my unit test:
$this->beConstructedWith(new \Tonic\Application(), new \Tonic\Request(array(
'uri' => '/',
'method' => 'PUT',
'Content-Type' => 'application/json',
'data' => '{"name":"testing", "email":"testing@@test.com", "password":"Passw0rd"}'
)), array());
$expectedResult = new \stdClass();
$expectedResult->message = "An error was encountered.";
$expectedResult->error[] = "The email must be valid.";
$response = $this->exec();
$response->shouldReturnAnInstanceOf("Tonic\Response");
$response->contentType->shouldBe("application/json");
$response->body->shouldBe(json_encode($expectedResult));
From looking around the Tonic code and the examples in the git repository I think I have it set up correctly.
The following is the result of running bin/phpspec -v run
for this unit test:
When I run an actual request against the Tonic service it works fine. I know I must be doing something wrong in the setup of the unit test but I don't know what.
Edit: PUT Method
/**
* Add a new user to the table.
*
* @method PUT
* @accepts application/json
* @provides application/json
* @json
* @return \Tonic\Response
*/
public function add()
{
// Validate the data before we go any futher.
$error = $this->validate();
// If the data is invalid then we want to let the requester know.
if (true === $error) {
$this->output->message = "An error was encountered.";
$this->responseCode = \Tonic\Response::NOTFOUND;
} else { // Else we want to PUT the data into our table.
$query = $this->db->prepare("INSERT INTO `user` (`name`, `email`, `password`, `dateOfBirth`) VALUES (:name, :email, :password, :dateOfBirth)");
$query->bindValue(":name", $this->request->data->name);
$query->bindValue(":email", $this->request->data->email);
$query->bindValue(":password", hash('sha256', $this->request->data->password));
$query->bindValue(":dateOfBirth", $this->request->data->dateOfBirth);
$query->execute();
// Check that the new user was successfully inserted into the database. If not let the requester know what happened.
if (0 === $query->rowCount()) {
if ("00000" === $query->errorCode()) {
$this->output->message = "No rows affected by query.";
} else {
$this->output->message = "There was an error running the query.";
$this->output->error[] = $query->errorInfo();
}
$this->responseCode = \Tonic\Response::CONFLICT;
} else { // Inserted successfully.
$this->output->message = "User successfully created.";
$this->responseCode = \Tonic\Response::CREATED;
$this->headers["Location"] = "/" . $this->request->data->email;
}
}
return new \Tonic\Response($this->responseCode, $this->output, $this->headers);
}
Okay, so I figured it out. The issue was to do with the Content-Type
set in the following code:
$this->beConstructedWith(new \Tonic\Application(), new \Tonic\Request(array(
'uri' => '/',
'method' => 'PUT',
'Content-Type' => 'application/json',
'data' => '{"name":"testing", "email":"testing@@test.com", "password":"Passw0rd"}'
)), array());
It should be contentType
:
$this->beConstructedWith(new \Tonic\Application(), new \Tonic\Request(array(
'uri' => '/',
'method' => 'PUT',
'contentType' => 'application/json',
'data' => '{"name":"testing", "email":"testing@@test.com", "password":"Passw0rd"}'
)), array());