baserCMS クラスリファレンス

BcTestCase extends TestCase
in package
uses IntegrationTestTrait, BcContainerTrait, TruncateDirtyTables

Class BcTestCase

テストクラスでは setUp() で各サービス/テーブルをプロパティに代入する慣習があり、 PHP 8.2 の動的プロパティ非推奨を回避するため、基底クラスに付与する。 この属性は子クラスにも継承されるため、全テストクラスに適用される。

Attributes
#[AllowDynamicProperties]

Table of Contents

Constants

EVENT_LAYER_CONTROLLER  : string = 'Controller'
イベントレイヤー entryEventToMock() の引数として利用
EVENT_LAYER_HELPER  : mixed = 'Helper'
EVENT_LAYER_MODEL  : mixed = 'Model'
EVENT_LAYER_VIEW  : mixed = 'View'

Properties

$Application  : Application
$BaserCore  : Plugin

Methods

attachEvent()  : mixed
イベントを設定する
classMethod()  : mixed
クラスメソッド名を取得する
dropTable()  : mixed
テーブルを削除する
getRequest()  : ServerRequest
Request を取得する
getService()  : array<string|int, mixed>|mixed|object
Get Service
hasService()  : bool
Has Service
resetEvent()  : mixed
イベントをリセットする
setUp()  : void
Set Up
setUploadFileToRequest()  : mixed
アップロードするファイルをリクエストに設定する IntegrationTestTrait を使ったテストで利用する
tearDown()  : void
Tear Down
tearDownAfterClass()  : void
tear down after class テスト時に生成されたログや一時ファイルに書き込み権限を与える ブラウザでアクセスした際にエラーとなるため
truncateTable()  : void
テーブルを空にする
apiLoginAdmin()  : array<string|int, mixed>
Api Login
entryEventToMock()  : EventListenerInterface|MockObject
モックにコントローラーのイベントを登録する
execPrivateMethod()  : mixed
private・protectedメソッドを実行する
getPrivateProperty()  : mixed
private・protectedプロパティの値を取得する
getUser()  : mixed
サンプル用のユーザーを取得する
loadTestAppPlugins()  : void
テスト用アプリのプラグイン読み込み・bootstrap・ルート接続を行う
loginAdmin()  : ServerRequest
管理画面にログインする

Constants

EVENT_LAYER_CONTROLLER

イベントレイヤー entryEventToMock() の引数として利用

public string EVENT_LAYER_CONTROLLER = 'Controller'

EVENT_LAYER_HELPER

public mixed EVENT_LAYER_HELPER = 'Helper'

EVENT_LAYER_MODEL

public mixed EVENT_LAYER_MODEL = 'Model'

EVENT_LAYER_VIEW

public mixed EVENT_LAYER_VIEW = 'View'

Properties

Methods

attachEvent()

イベントを設定する

public attachEvent(mixed $events) : mixed
Parameters
$events : mixed
Tags
checked
noTodo
unitTest

classMethod()

クラスメソッド名を取得する

public classMethod() : mixed
Tags
checked
noTodo
unitTest

テストができないのでスキップ

dropTable()

テーブルを削除する

public dropTable(string $tableName) : mixed
Parameters
$tableName : string
Tags
checked
noTodo
unitTest

getRequest()

Request を取得する

public getRequest([string $url = '/' ][, mixed $data = [] ][, mixed $method = 'GET' ][, mixed $config = [] ]) : ServerRequest
Parameters
$url : string = '/'
$data : mixed = []
$method : mixed = 'GET'
$config : mixed = []
Tags
checked
unitTest
noTodo
Return values
ServerRequest

getService()

Get Service

public getService(mixed $service) : array<string|int, mixed>|mixed|object
Parameters
$service : mixed
Tags
checked
noTodo
unitTest
Return values
array<string|int, mixed>|mixed|object

hasService()

Has Service

public hasService(mixed $service) : bool
Parameters
$service : mixed
Tags
checked
noTodo
unitTest
Return values
bool

resetEvent()

イベントをリセットする

public resetEvent() : mixed
Tags
checked
noTodo
unitTest

setUp()

Set Up

public setUp() : void
Tags
checked
noTodo
unitTest

setUploadFileToRequest()

アップロードするファイルをリクエストに設定する IntegrationTestTrait を使ったテストで利用する

public setUploadFileToRequest(string $name, string $path[, string $fileName = '' ][, int $error = UPLOAD_ERR_OK ]) : mixed
Parameters
$name : string
$path : string
$fileName : string = ''
$error : int = UPLOAD_ERR_OK
Tags
checked
noTodo
unitTest

tearDown()

Tear Down

public tearDown() : void
Tags
checked
noTodo
unitTest

tearDownAfterClass()

tear down after class テスト時に生成されたログや一時ファイルに書き込み権限を与える ブラウザでアクセスした際にエラーとなるため

public static tearDownAfterClass() : void
Tags
checked
unitTest
noTodo

truncateTable()

テーブルを空にする

public static truncateTable(string $tableName) : void
Parameters
$tableName : string
Tags
noTodo
checked
unitTest

BcDatabaseService::truncate のラッパーメソッドのためスキップ

apiLoginAdmin()

Api Login

protected apiLoginAdmin([int $id = 1 ]) : array<string|int, mixed>
Parameters
$id : int = 1
Tags
checked
unitTest
noTodo
Return values
array<string|int, mixed>

entryEventToMock()

モックにコントローラーのイベントを登録する

protected entryEventToMock(mixed $layer, mixed $eventName, mixed $callback) : EventListenerInterface|MockObject
Parameters
$layer : mixed
$eventName : mixed
$callback : mixed
Tags
checked
unitTest
noTodo
Return values
EventListenerInterface|MockObject

execPrivateMethod()

private・protectedメソッドを実行する

protected execPrivateMethod(object $class, string $method[, array<string|int, mixed> $args = [] ]) : mixed
Parameters
$class : object

対象クラス

$method : string

対象メソッド

$args : array<string|int, mixed> = []

対象メソッドに必要な引数

Tags
checked
unitTest
noTodo
Return values
mixed —

$value

getPrivateProperty()

private・protectedプロパティの値を取得する

protected getPrivateProperty(object $class, string $property) : mixed
Parameters
$class : object
$property : string
Tags
throws
ReflectionException
checked
noTodo
unitTest

getUser()

サンプル用のユーザーを取得する

protected getUser([mixed $id = 1 ]) : mixed
Parameters
$id : mixed = 1
Tags
checked
unitTest
noTodo

loadTestAppPlugins()

テスト用アプリのプラグイン読み込み・bootstrap・ルート接続を行う

protected loadTestAppPlugins() : void

【各 app へのプラグイン読み込み】 CakePHP 5.2 では Application を new するたびにグローバルの Plugin コレクションがリセットされ、 テスト用DB(plugins テーブルが空)からはコンテンツプラグインが読み込まれない。 tests/bootstrap.php で退避したプラグイン一覧(BcApp.testAppPluginsToLoad)を、次の 2 つの app に読み込ませる。

  • $this->appPluginsToLoad: 統合テストがリクエストごとに生成する app(IntegrationTestTrait::createApp())向け。 これが無いと $this->get()/$this->post() の app にプラグインが無く、ルートが欠落して 404 等になる。
  • $this->Application: setUp 自身が bootstrap する app 向け。これが無いと Plugin::isLoaded('BcXxx') が false となり、 ヘルパテスト等のテンプレート解決(pluginSplit)が失敗する。

【スナップショット bootstrap】 CakePHP 5.2 では BaseApplication::pluginBootstrap() が PluginCollection の live なイテレータを走査するが、 BaserCorePlugin::bootstrap() がイテレーション中に addPlugin()/remove()(テーマ追加・DebugKit 削除等)で コレクションを変更するため、直後に並ぶプラグイン(例: BcBlog)の bootstrap() がスキップされ setting.php が読み込まれない。そこで live イテレーションを使わず、未 bootstrap が無くなるまで スナップショット単位で bootstrap して取りこぼしを防ぐ。

loginAdmin()

管理画面にログインする

protected loginAdmin(ServerRequest $request[, mixed $id = 1 ]) : ServerRequest
Parameters
$request : ServerRequest
$id : mixed = 1
Tags
checked
unitTest
noTodo
Return values
ServerRequest
On this page

Search results