Skip to content

Instantly share code, notes, and snippets.

@shimabox
Created December 12, 2016 23:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save shimabox/7154db0e5cadf91f6bab20874934b8e3 to your computer and use it in GitHub Desktop.
header関数を使うメソッドのテストコード
<?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