array( "base_url" => "https://jsonplaceholder.cypress.io/", "get" => array( "route" => "posts/1", "expected_return" => array( "userId" => 1, "id" => 1, "title" => "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body" => "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" ), "expected_code" => 200, ), "post" => array( "route" => "posts", "datas" => array( "title" => 'foo', "body" => 'bar', "userId" => 20 ), "expected_return" => array( "id" => 101, "title" => 'foo', "body" => 'bar', "userId" => 20 ), "expected_code" => 201, ), "put" => array( "route" => "posts/2", "datas" => array( "id" => 2, "title" => 'foo', "body" => 'bar', "userId" => 20 ), "expected_return" => array( "id" => 2, "title" => 'foo', "body" => 'bar', "userId" => 20 ), "expected_code" => 200, ), "delete" => array( "route" => "posts/1", "expected_return" => array(), "expected_code" => 200, ), ), ); /** * Méthode lancée en début de traitement * * @return void */ public function common_setUp() { parent::common_setUp(); // $this->datas_resource = "jsonplaceholder"; // if ($this->inst_om_rest_client === null) { $this->inst_om_rest_client = new om_rest_client(null); } // Permet d'avoir un message d'erreur cURL lors d'un retour 404 curl_setopt( $this->inst_om_rest_client->getCurl(), CURLOPT_FAILONERROR, true ); } /** * Test de la méthode GET. * * @return void */ public function test_get_method() { $response = $this->inst_om_rest_client->execute( "GET", "", array(), sprintf( '%1$s%2$s', $this->datas[$this->datas_resource]["base_url"], $this->datas[$this->datas_resource]["get"]["route"] ), array() ); // Vérification que le code retour est correct. Si ce n'est pas le cas, le // message d'erreur cURL est affiché dans la console $this->assertEquals( $this->datas[$this->datas_resource]["get"]["expected_code"], $this->inst_om_rest_client->getResponseCode(), $this->inst_om_rest_client->getErrorMessage() ); $this->assertEquals( $this->datas[$this->datas_resource]["get"]["expected_return"], $response ); } /** * Test de la méthode POST. * * @return void */ public function test_post_method() { $response = $this->inst_om_rest_client->execute( "POST", "application/json; charset=UTF-8", json_encode($this->datas[$this->datas_resource]["post"]["datas"]), sprintf( '%1$s%2$s', $this->datas[$this->datas_resource]["base_url"], $this->datas[$this->datas_resource]["post"]["route"] ), array( "Accept: application/json", "Accept-charset: UTF-8" ) ); // Vérification que le code retour est correct. Si ce n'est pas le cas, le // message d'erreur cURL est affiché dans la console $this->assertEquals( $this->datas[$this->datas_resource]["post"]["expected_code"], $this->inst_om_rest_client->getResponseCode(), $this->inst_om_rest_client->getErrorMessage() ); $this->assertEquals( $this->datas[$this->datas_resource]["post"]["expected_return"], $response ); } /** * Test de la méthode PUT. * * @return void */ public function test_put_method() { $response = $this->inst_om_rest_client->execute( "PUT", "application/json; charset=UTF-8", json_encode($this->datas[$this->datas_resource]["put"]["datas"]), sprintf( '%1$s%2$s', $this->datas[$this->datas_resource]["base_url"], $this->datas[$this->datas_resource]["put"]["route"] ), array( "Accept: application/json", "Accept-charset: UTF-8" ) ); // Vérification que le code retour est correct. Si ce n'est pas le cas, le // message d'erreur cURL est affiché dans la console $this->assertEquals( $this->datas[$this->datas_resource]["put"]["expected_code"], $this->inst_om_rest_client->getResponseCode(), $this->inst_om_rest_client->getErrorMessage() ); $this->assertEquals( $this->datas[$this->datas_resource]["put"]["expected_return"], $response ); } /** * Test de la méthode DELETE. * * @return void */ public function test_delete_method() { $response = $this->inst_om_rest_client->execute( "DELETE", "", array(), sprintf( '%1$s%2$s', $this->datas[$this->datas_resource]["base_url"], $this->datas[$this->datas_resource]["delete"]["route"] ), array() ); // Vérification que le code retour est correct. Si ce n'est pas le cas, le // message d'erreur cURL est affiché dans la console $this->assertEquals( $this->datas[$this->datas_resource]["delete"]["expected_code"], $this->inst_om_rest_client->getResponseCode(), $this->inst_om_rest_client->getErrorMessage() ); $this->assertEquals( $this->datas[$this->datas_resource]["delete"]["expected_return"], $response ); } }