|
| 1 | +<?php |
| 2 | +namespace PluginTestSupport\Test\TestCase; |
| 3 | + |
| 4 | +use Cake\TestSuite\TestCase; |
| 5 | +use Cake\Datasource\ConnectionManager; |
| 6 | +use Cake\TestSuite\Fixture\FixtureManager; |
| 7 | +use Cake\Core\Configure; |
| 8 | +use Cake\Cache\Cache; |
| 9 | + |
| 10 | +/** |
| 11 | + * AppTestCase |
| 12 | + * テストのベース設定 |
| 13 | + */ |
| 14 | +class AppTestCase extends TestCase |
| 15 | +{ |
| 16 | + static protected $_connectionInfo = [ |
| 17 | + 'className' => 'Cake\Database\Connection', |
| 18 | + 'driver' => 'Cake\Database\Driver\Postgres', |
| 19 | + 'persistent' => false, |
| 20 | + 'host' => 'localhost', |
| 21 | + //'port' => 'nonstandard_port_number', |
| 22 | + 'username' => 'postgres', |
| 23 | + 'password' => '', |
| 24 | + 'database' => 'cakephp_test', |
| 25 | + 'encoding' => 'utf8', |
| 26 | + 'timezone' => 'Asia/Tokyo', |
| 27 | + 'cacheMetadata' => false, |
| 28 | + 'quoteIdentifiers' => false, |
| 29 | + ]; |
| 30 | + |
| 31 | + static protected $_appInfo = 'App'; |
| 32 | + |
| 33 | + public static function setUpBeforeClass(){ |
| 34 | + parent::setUpBeforeClass(); |
| 35 | + //データベース接続設定 |
| 36 | + $connectionInfo = self::$_connectionInfo; |
| 37 | + |
| 38 | + //cacheの設定はしていないためcacheを無効にする |
| 39 | + Cache::disable(); |
| 40 | + |
| 41 | + //defaultは使わないがいないとエラーになるのでテストと同じものを設定する |
| 42 | + Configure::write('Datasources.default',$connectionInfo); |
| 43 | + Configure::write('Datasources.test',$connectionInfo); |
| 44 | + ConnectionManager::config(Configure::consume('Datasources')); |
| 45 | + |
| 46 | + //APPが定義されていないとエラーになるため。(逆に何か設定してあればとりあえず動くみたい?) |
| 47 | + if (!defined('APP')){ |
| 48 | + define('APP', self::$_appInfo); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * setUp method |
| 54 | + * |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + public function setUp() |
| 58 | + { |
| 59 | + parent::setUp(); |
| 60 | + //fixtureManagerを呼び出し、fixtureを実行する |
| 61 | + $this->fixtureManager = new FixtureManager(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * tearDown method |
| 66 | + * |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + public function tearDown() |
| 70 | + { |
| 71 | + //fixtureの終了 |
| 72 | + $this->fixtureManager->shutdown(); |
| 73 | + unset($this->fixtureManager); |
| 74 | + parent::tearDown(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * tearDownsetConnectionInfo method |
| 79 | + * |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + protected function setConnectionInfo($info){ |
| 83 | + //接続情報を変更する |
| 84 | + self::$_connectionInfo = $info; |
| 85 | + } |
| 86 | + |
| 87 | + protected function setAppInfo($info){ |
| 88 | + //APP情報を変更する |
| 89 | + self::$_appInfo = $info; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments