Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting for PUT/POST/DELETE Methods
    primarykey
    data
    text
    <p>I am having some trouble running tests for <code>PUT</code>, <code>POST</code> and <code>DELETE</code> using the unit testing framework <a href="http://phpspec.net/" rel="nofollow noreferrer">phpspec2</a>.</p> <p>This is the code that I have in my unit test:</p> <pre><code>$this-&gt;beConstructedWith(new \Tonic\Application(), new \Tonic\Request(array( 'uri' =&gt; '/', 'method' =&gt; 'PUT', 'Content-Type' =&gt; 'application/json', 'data' =&gt; '{"name":"testing", "email":"testing@@test.com", "password":"Passw0rd"}' )), array()); $expectedResult = new \stdClass(); $expectedResult-&gt;message = "An error was encountered."; $expectedResult-&gt;error[] = "The email must be valid."; $response = $this-&gt;exec(); $response-&gt;shouldReturnAnInstanceOf("Tonic\Response"); $response-&gt;contentType-&gt;shouldBe("application/json"); $response-&gt;body-&gt;shouldBe(json_encode($expectedResult)); </code></pre> <p>From looking around the <a href="http://peej.github.io/tonic/" rel="nofollow noreferrer">Tonic</a> code and the examples in the <a href="https://github.com/peej/tonic" rel="nofollow noreferrer">git repository</a> I think I have it set up correctly.</p> <p>The following is the result of running <code>bin/phpspec -v run</code> for this unit test:</p> <p><img src="https://i.stack.imgur.com/udjLv.png" alt="phpspec2 Error"></p> <p>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.</p> <p><strong>Edit</strong>: PUT Method</p> <pre><code>/** * 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-&gt;validate(); // If the data is invalid then we want to let the requester know. if (true === $error) { $this-&gt;output-&gt;message = "An error was encountered."; $this-&gt;responseCode = \Tonic\Response::NOTFOUND; } else { // Else we want to PUT the data into our table. $query = $this-&gt;db-&gt;prepare("INSERT INTO `user` (`name`, `email`, `password`, `dateOfBirth`) VALUES (:name, :email, :password, :dateOfBirth)"); $query-&gt;bindValue(":name", $this-&gt;request-&gt;data-&gt;name); $query-&gt;bindValue(":email", $this-&gt;request-&gt;data-&gt;email); $query-&gt;bindValue(":password", hash('sha256', $this-&gt;request-&gt;data-&gt;password)); $query-&gt;bindValue(":dateOfBirth", $this-&gt;request-&gt;data-&gt;dateOfBirth); $query-&gt;execute(); // Check that the new user was successfully inserted into the database. If not let the requester know what happened. if (0 === $query-&gt;rowCount()) { if ("00000" === $query-&gt;errorCode()) { $this-&gt;output-&gt;message = "No rows affected by query."; } else { $this-&gt;output-&gt;message = "There was an error running the query."; $this-&gt;output-&gt;error[] = $query-&gt;errorInfo(); } $this-&gt;responseCode = \Tonic\Response::CONFLICT; } else { // Inserted successfully. $this-&gt;output-&gt;message = "User successfully created."; $this-&gt;responseCode = \Tonic\Response::CREATED; $this-&gt;headers["Location"] = "/" . $this-&gt;request-&gt;data-&gt;email; } } return new \Tonic\Response($this-&gt;responseCode, $this-&gt;output, $this-&gt;headers); } </code></pre>
    singulars
    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.
 

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