Skip to content

Commit

Permalink
Add tests for REST controller
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Sep 1, 2015
1 parent 7b32c6f commit fda2365
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
34 changes: 34 additions & 0 deletions application/controllers/api/Rest_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

require_once APPPATH . '/libraries/REST_Controller.php';

class Rest_test extends REST_Controller
{
public function index_get()
{
$data['get'] = $this->get();
$data['query'] = $this->query();
$this->response($data, REST_Controller::HTTP_OK);
}

public function index_post()
{
$data['post'] = $this->post();
$data['query'] = $this->query();
$this->response($data, REST_Controller::HTTP_OK);
}

public function index_put()
{
$data['put'] = $this->put();
$data['query'] = $this->query();
$this->response($data, REST_Controller::HTTP_OK);
}

public function index_delete()
{
$data['delete'] = $this->delete();
$data['query'] = $this->query();
$this->response($data, REST_Controller::HTTP_OK);
}
}
94 changes: 94 additions & 0 deletions application/tests/controllers/api/Rest_test_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

class Rest_test_test extends TestCase
{
public function test_index_get()
{
set_is_cli(FALSE);
$this->warningOff();
try {
$output = $this->request(
'GET', 'api/rest_test?name=John%20O%27reilly&city=Tokyo'
);
} catch (CIPHPUnitTestExitException $e) {
$output = ob_get_clean();
}
set_is_cli(TRUE);
$this->warningOn();

$expected = <<< 'EOL'
{"get":{"name":"John O'reilly","city":"Tokyo"},"query":{"name":"John O'reilly","city":"Tokyo"}}
EOL;
$this->assertEquals($expected, $output);
$this->assertResponseCode(200);
}

public function test_index_post()
{
set_is_cli(FALSE);
$this->warningOff();
try {
$output = $this->request(
'POST', 'api/rest_test?name=John%20O%27reilly&city=Tokyo',
[
'id' => 'abc',
'password' => 'xyz'
]
);
} catch (CIPHPUnitTestExitException $e) {
$output = ob_get_clean();
}
set_is_cli(TRUE);
$this->warningOn();

$expected = <<< 'EOL'
{"post":{"id":"abc","password":"xyz"},"query":{"name":"John O'reilly","city":"Tokyo"}}
EOL;
$this->assertEquals($expected, $output);
$this->assertResponseCode(200);
}

public function test_index_put()
{
set_is_cli(FALSE);
$this->warningOff();
try {
$output = $this->request(
'PUT', 'api/rest_test?name=John%20O%27reilly&city=Tokyo',
'id=abc&password=xyz'
);
} catch (CIPHPUnitTestExitException $e) {
$output = ob_get_clean();
}
set_is_cli(TRUE);
$this->warningOn();

$expected = <<< 'EOL'
{"put":{"id":"abc","password":"xyz"},"query":{"name":"John O'reilly","city":"Tokyo"}}
EOL;
$this->assertEquals($expected, $output);
$this->assertResponseCode(200);
}

public function test_index_delete()
{
set_is_cli(FALSE);
$this->warningOff();
try {
$output = $this->request(
'DELETE', 'api/rest_test?name=John%20O%27reilly&city=Tokyo',
'id=abc&password=xyz'
);
} catch (CIPHPUnitTestExitException $e) {
$output = ob_get_clean();
}
set_is_cli(TRUE);
$this->warningOn();

$expected = <<< 'EOL'
{"delete":{"id":"abc","password":"xyz"},"query":{"name":"John O'reilly","city":"Tokyo"}}
EOL;
$this->assertEquals($expected, $output);
$this->assertResponseCode(200);
}
}

0 comments on commit fda2365

Please sign in to comment.