Created
December 12, 2016 23:08
-
-
Save shimabox/7154db0e5cadf91f6bab20874934b8e3 to your computer and use it in GitHub Desktop.
header関数を使うメソッドのテストコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* メッセージ(json)を返すAPIと仮定 | |
* | |
* あくまでもサンプルなので例外処理は入れていません | |
*/ | |
class Sample | |
{ | |
/** | |
* @var array | |
*/ | |
protected $messages = array( | |
1 => "hoge", | |
2 => "piyo" | |
); | |
/** | |
* @param int $id | |
*/ | |
public function getMessage($id) | |
{ | |
$message = array("message" => $this->messages[$id]); | |
header("Content-Type: application/json; charset=utf-8"); | |
echo json_encode($message); | |
} | |
} | |
/** | |
* jsonを返すAPIのテストコード | |
* | |
* @group Sample | |
*/ | |
class SampleTest extends \PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @test | |
* @runInSeparateProcess | |
*/ | |
public function it_can_output_a_message() | |
{ | |
$expected = json_encode(array("message" => "hoge")); | |
$sample = new Sample(); | |
ob_start(); | |
$sample->getMessage(1); | |
$actual = ob_get_contents(); | |
ob_end_clean(); | |
$this->assertSame($expected, $actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment