Posted at

PHPStormでCakePHP3環境のPhpUnitとFabricate設定

More than 1 year has passed since last update.


PhpUnit設定


設定

①composerにphpunitとfabricate追加して設置

②メニューからFile->Setting->Languages&Frameworks->Php (InterPreter追加)

Interpreterの...ボタンをクリック

+ボタンしてremote..を選択、自分の環境に合わせて設定後「OK」クリック



③PHPUnit設定 



- Remote Interpreter追加



-Path to ScriptとDefault configuration file指定



④実行


Fabricate設定

以下の情報参照

https://github.com/sizuhiko/cakephp-fabricate-adaptor


  • テストのためのUsersテーブル


users.sql

CREATE TABLE `users` (

`id` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


  • test\bootstrap.phpにFabricateAdaptor設定追加


bootstrap.php

use Fabricate\Fabricate;

use CakeFabricate\Adaptor\CakeFabricateAdaptor;

Fabricate::config(function($config) {
$config->adaptor = new CakeFabricateAdaptor();
});


①Bakeからモデル追加

②UsersTableTest.phpにFabricateソース追加


UsersTableTest.php

<?php

namespace App\Test\TestCase\Model\Table;

use App\Model\Table\UsersTable;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Fabricate\Fabricate;      //追加

/**
* App\Model\Table\UsersTable Test Case
*/

class UsersTableTest extends TestCase
{

/**
* Test subject
*
* @var \App\Model\Table\UsersTable
*/

public $Users;

/**
* Fixtures
*
* @var array
*/

public $fixtures = [
'app.users'
];

/**
* setUp method
*
* @return void
*/

public function setUp()
{
parent::setUp();
$config = TableRegistry::exists('Users') ? [] : ['className' => 'App\Model\Table\UsersTable'];
$this->Users = TableRegistry::get('Users', $config);

//ユーザーテーブル追加
Fabricate::create('Users', ["id" => 1, "name" => "name1"]);
Fabricate::create('Users', ["id" => 2, "name" => "name2"]);
Fabricate::create('Users', ["id" => 3, "name" => "name3"]);

}

public function testデータ確認() {
     //Fabricateから追加した10件のrandomデータが追加されたのか確認
$data = $this->Users->find('all');

$this->assertEquals(3, $data->count());

foreach ($data as $row) {
echo ' ID:' . $row->id . ' NAME:' . $row->name . '\n';
}
}
}


④データ確認