public function testJSONResponse() { function assertion($key, $value) { $test = new Test(); $test->server('json_response', 'POST', array('key' => $key, 'value' => $value)); $response = $test->curl->response; PHPUnit_Framework_Assert::assertNotNull($response); PHPUnit_Framework_Assert::assertNull($response->null); PHPUnit_Framework_Assert::assertTrue($response->true); PHPUnit_Framework_Assert::assertFalse($response->false); PHPUnit_Framework_Assert::assertTrue(is_int($response->integer)); PHPUnit_Framework_Assert::assertTrue(is_float($response->float)); PHPUnit_Framework_Assert::assertEmpty($response->empty); PHPUnit_Framework_Assert::assertTrue(is_string($response->string)); } assertion('Content-Type', 'application/json; charset=utf-8'); assertion('content-type', 'application/json; charset=utf-8'); assertion('Content-Type', 'application/json'); assertion('content-type', 'application/json'); assertion('CONTENT-TYPE', 'application/json'); assertion('CONTENT-TYPE', 'APPLICATION/JSON'); }
<?php function assertion($code, $reason, $statusReason) { header("HTTP/1.1 {$code} {$statusReason}"); die($reason); } mysql_connect("localhost", "root", "") or assertion(503, "Failed to connect to MySQL.", "Server Error"); mysql_select_db("db_stackexchange") or assertion(503, "Failed to load database.", "Server Error"); $id = $_GET['q_id']; $query = "delete from questions where question_id={$id}"; $hasil = mysql_query($query); $query = "delete from answers where question_id={$id}"; $hasil = mysql_query($query);
<?php function assertion($code, $reason, $statusReason) { header("HTTP/1.1 {$code} {$statusReason}"); die($reason); } mysql_connect("localhost", "root", "") or assertion(503, "Failed to connect to MySQL.", "Server Error"); mysql_select_db("db_stackexchange") or assertion(503, "Failed to load database.", "Server Error"); function sign($n) { return ($n > 0) - ($n < 0); } $id = intval($_POST['id']); $type = $_POST['isQuestion'] ? "question" : "answer"; $crement = intval($_POST['n']); if (sign($crement) == 1) { $query = "update {$type}s set n_upvote=n_upvote+{$crement} where {$type}_id={$id}"; } else { if (sign($crement) == -1) { $crement = abs($crement); $query = "update {$type}s set n_downvote=n_downvote+{$crement} where {$type}_id={$id}"; } } mysql_query($query) or assertion(503, "Failed to update database. " . mysql_error(), "Server Error"); $hasil = mysql_query("select n_upvote,n_downvote from {$type}s where {$type}_id={$id} limit 1"); $votes = mysql_fetch_row($hasil); echo $votes[0] - $votes[1];
public function testJSONResponse() { function assertion($key, $value) { $test = new Test(); $test->server('json_response', 'POST', array('key' => $key, 'value' => $value)); $response = $test->curl->response; PHPUnit_Framework_Assert::assertNotNull($response); PHPUnit_Framework_Assert::assertNull($response->null); PHPUnit_Framework_Assert::assertTrue($response->true); PHPUnit_Framework_Assert::assertFalse($response->false); PHPUnit_Framework_Assert::assertTrue(is_int($response->integer)); PHPUnit_Framework_Assert::assertTrue(is_float($response->float)); PHPUnit_Framework_Assert::assertEmpty($response->empty); PHPUnit_Framework_Assert::assertTrue(is_string($response->string)); PHPUnit_Framework_Assert::assertEquals(json_encode(array('null' => null, 'true' => true, 'false' => false, 'integer' => 1, 'float' => 3.14, 'empty' => '', 'string' => 'string')), $test->curl->raw_response); } assertion('Content-Type', 'APPLICATION/JSON'); assertion('Content-Type', 'APPLICATION/JSON; CHARSET=UTF-8'); assertion('Content-Type', 'APPLICATION/JSON;CHARSET=UTF-8'); assertion('Content-Type', 'application/json'); assertion('Content-Type', 'application/json; charset=utf-8'); assertion('Content-Type', 'application/json;charset=UTF-8'); assertion('content-type', 'APPLICATION/JSON'); assertion('content-type', 'APPLICATION/JSON; CHARSET=UTF-8'); assertion('content-type', 'APPLICATION/JSON;CHARSET=UTF-8'); assertion('content-type', 'application/json'); assertion('content-type', 'application/json; charset=utf-8'); assertion('content-type', 'application/json;charset=UTF-8'); assertion('CONTENT-TYPE', 'APPLICATION/JSON'); assertion('CONTENT-TYPE', 'APPLICATION/JSON; CHARSET=UTF-8'); assertion('CONTENT-TYPE', 'APPLICATION/JSON;CHARSET=UTF-8'); assertion('CONTENT-TYPE', 'application/json'); assertion('CONTENT-TYPE', 'application/json; charset=utf-8'); assertion('CONTENT-TYPE', 'application/json;charset=UTF-8'); }
function _testExpected($test_name) { assertion(strtolower($test_name) == strtolower($this->_expected_names[$this->_count]), $this->_count . ": Expected [" . $this->_expected_names[$this->_count] . "] got [{$test_name}]"); $this->_count++; }