Skip to content

Instantly share code, notes, and snippets.

@tanakahisateru
Created April 6, 2012 14:28
Show Gist options
  • Save tanakahisateru/2320342 to your computer and use it in GitHub Desktop.
Save tanakahisateru/2320342 to your computer and use it in GitHub Desktop.
KPHPUG#3のPhakeで書き下したりコメント打ったりして勉強
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* PHP version 5.3
*
* Copyright (c) 2012 GOTO Hidenori <hidenorigoto@gmail.com>,
* 2012 KUBO Atsuhiro <kubo@iteman.jp>,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package KPHPUGQuestionnaireBundle
* @copyright 2012 GOTO Hidenori <hidenorigoto@gmail.com>
* @copyright 2012 KUBO Atsuhiro <kubo@iteman.jp>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @since File available since Release 0.1.0
*/
namespace KPHPUG\QuestionnaireBundle\Tests\Domain\Service;
use KPHPUG\QuestionnaireBundle\Domain\Entity\AnswerFactory;
use KPHPUG\QuestionnaireBundle\Domain\Entity\QuestionnaireItemFactory;
use KPHPUG\QuestionnaireBundle\Domain\Service\Answering;
/**
* @package KPHPUGQuestionnaireBundle
* @copyright 2012 GOTO Hidenori <hidenorigoto@gmail.com>
* @copyright 2012 KUBO Atsuhiro <kubo@iteman.jp>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @since Class available since Release 0.1.0
*/
class AnsweringTest extends \PHPUnit_Framework_TestCase
{
/**
* このコードでは $_xxx_ がモックで、$xxxが本物です。
*
* @test
*/
public function answerToQuestionnaire()
{
// エンティティマネージャから各リポジトリが得られるということ
$_entityManager_ = \Phake::mock('Doctrine\ORM\EntityManager');
$_answerRepository_ = \Phake::mock('KPHPUG\QuestionnaireBundle\Domain\Entity\AnswerRepository');
$_questionnaireItemRepository_ = \Phake::mock('KPHPUG\QuestionnaireBundle\Domain\Entity\QuestionnaireItemRepository');
\Phake::when($_entityManager_)->getRepository('KPHPUG\QuestionnaireBundle\Domain\Entity\Answer')
->thenReturn($_answerRepository_);
\Phake::when($_entityManager_)->getRepository('KPHPUG\QuestionnaireBundle\Domain\Entity\QuestionnaireItem')
->thenReturn($_questionnaireItemRepository_);
// アンケート項目のモック2こ準備 それぞれIDは1と2
$_questionnaireItem1_ = \Phake::mock('KPHPUG\QuestionnaireBundle\Domain\Entity\QuestionnaireItem');
$_questionnaireItem2_ = \Phake::mock('KPHPUG\QuestionnaireBundle\Domain\Entity\QuestionnaireItem');
\Phake::when($_questionnaireItem1_)->getId()->thenReturn(1);
\Phake::when($_questionnaireItem2_)->getId()->thenReturn(2);
// アンケート項目リポジトリからは2このアンケート項目がIDで取り出せる
\Phake::when($_questionnaireItemRepository_)->find(1)->thenReturn($_questionnaireItem1_);
\Phake::when($_questionnaireItemRepository_)->find(2)->thenReturn($_questionnaireItem2_);
/////////////////////////////////////////////////////
$answering = new Answering($_entityManager_); // テスト対象のサービスがあって、
$answerFactory = new AnswerFactory(); // 回答のファクトリに
$answer = $answerFactory->create(array( // アンケート項目2つ持った回答を作らせる
$_questionnaireItem1_,
$_questionnaireItem2_,
));
// サービスを使って「回答内容」を「回答する」というメインの処理がここ
$answering->answer($answer);
/////////////////////////////////////////////////////
\Phake::verify($_answerRepository_)->add($answer); // 回答リポジトリには回答が追加されたはず
\Phake::verify($_questionnaireItemRepository_)->find(1); // アンケート項目リポジトリでID=1のものが参照されたはず
\Phake::verify($_questionnaireItemRepository_)->find(2); // アンケート項目リポジトリでID=2のものが参照されたはず
\Phake::verify($_entityManager_)->flush(); // エンティティマネージャはフラッシュされたはず
}
}
/*
* Local Variables:
* mode: php
* coding: utf-8
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* indent-tabs-mode: nil
* End:
*/
@tanakahisateru
Copy link
Author

なんとかテストまでやって、オブジェクト指向設計→テスト記述で層の責務の確認→DIコンテナ的な実例の実装→グリーン・コミット→次の設計 というふうに、ぐるぐる回るのをイテレーションできたら、オブジェクトを刻むことがテストのスコープを狭くして品質担保につながるんだってわかりやすいんだろうなと思いましたが、まあ ... そんなの1日ではとても足りないですよね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment