cakephp3で自作のコンポーネントをphpunitを使って単体テストをすることになり試行錯誤している中でpostパラメータが必要でどう言うふうにpostパラメータを設定すればよいかわからず困っています。
状況としては以下です。
・windows環境でxampp使用
・PHPは7.1.9
・cakephpのバージョンは3.5.1
・phpunitは6.4.3をインストール済み
・コンポーネントのテストをしたい
・テストしたいコンポーネントにはpostパラメータを参照する必要があるがpostパラメータを持ってこれていない
・phpunitに関する知識が0なので公式ページがなかなか理解できない。
公式ページはこちらを参照しています。
テストは以下のように書きました。
hogeコンポーネントのfugafugaメソッドがfalseを返すかをテストしたいのですがそのfugafugaメソッドはpostパラメータを必要とします。
php
1<?php 2namespace App\Test\TestCase\Controller\Component; 3 4use App\Controller\Component\hogeComponent; 5use Cake\Controller\Controller; 6use Cake\Controller\ComponentRegistry; 7use Cake\Event\Event; 8use Cake\Http\ServerRequest; 9use Cake\Http\Response; 10use Cake\TestSuite\IntegrationTestCase; 11 12class hogeComponentTest extends IntegrationTestCase 13{ 14 private $component = null; 15 public function setUp() 16 { 17 parent::setUp(); 18 $request = new ServerRequest(); 19 $response = new Response(); 20 $this->controller = $this->getMockBuilder('Cake\Controller\Controller') 21 ->setConstructorArgs([$request, $response]) 22 ->setMethods(null) 23 ->getMock(); 24 $registry = new ComponentRegistry($this->controller); 25 $this->component = new hogeComponent($registry); 26 } 27 /** 28 * Test index method 29 * 30 * @return void 31 */ 32 public function testIndex() 33 { 34 $data = ['id' => 1]; 35 $this->post('/hogehoge', $data); 36 $result = $this->component->fugafuga(); 37 $this->assertEquals(false, $result); 38 } 39}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。