# InitDbFixture

> InitDbFixture represents the initial state needed for DB-related tests.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [test](/docs/api/yii/test/index.md) › InitDbFixture

| class | `yii\test\InitDbFixture` |
| --- | --- |
| Extends | [`DbFixture`](/docs/api/yii/test/dbfixture/index.md) » [`Fixture`](/docs/api/yii/test/fixture/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/test/InitDbFixture.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/test/InitDbFixture.php#L30) |

InitDbFixture represents the initial state needed for DB-related tests.

Its main task is to toggle integrity check of the database during data loading.
This is needed by other DB-related fixtures (e.g. [`ActiveFixture`](/docs/api/yii/test/activefixture/index.md)) so that they can populate
data into the database without triggering integrity check errors.

Besides, DbFixture also attempts to load an [initialization script](/docs/api/yii/test/initdbfixture/index.md#property-initScript) if it exists.

You should normally use InitDbFixture to prepare a skeleton test database.
Other DB fixtures will then add specific tables and data to this database.

For more details and usage information on InitDbFixture, see the [guide article on fixtures](/docs/guide/test-fixtures/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-db"></span>[`$db`](/docs/api/yii/test/dbfixture/index.md#property-db) | [`yii\db\Connection`](/docs/api/yii/db/connection/index.md)`\|array\|string` | The DB connection object or the application component ID of the DB connection. Default: `'db'`. | [`DbFixture`](/docs/api/yii/test/dbfixture/index.md) |
| <span id="property-depends"></span>[`$depends`](/docs/api/yii/test/fixture/index.md#property-depends) | `array` | The fixtures that this fixture depends on. Default: `[]`. | [`Fixture`](/docs/api/yii/test/fixture/index.md) |
| [`$initScript`](#property-initScript) | `string` | The init script file that should be executed when loading this fixture. Default: `'@app/tests/fixtures/initdb.php'`. | `InitDbFixture` |
| [`$schemas`](#property-schemas) | `array` | List of database schemas that the test tables may reside in. Default: `['']`. | `InitDbFixture` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`afterLoad()`](#method-afterLoad) | This method is called AFTER all fixture data have been loaded for the current test. | `InitDbFixture` |
| [`afterUnload()`](#method-afterUnload) | This method is called AFTER all fixture data have been unloaded for the current test. | `InitDbFixture` |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`beforeLoad()`](#method-beforeLoad) | This method is called BEFORE any fixture data is loaded for the current test. | `InitDbFixture` |
| [`beforeUnload()`](#method-beforeUnload) | This method is called BEFORE any fixture data is unloaded for the current test. | `InitDbFixture` |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`checkIntegrity()`](#method-checkIntegrity) | Toggles the DB integrity check. | `InitDbFixture` |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/test/dbfixture/index.md#method-init) | Initializes the object. | [`DbFixture`](/docs/api/yii/test/dbfixture/index.md) |
| [`load()`](#method-load) | Loads the fixture. | `InitDbFixture` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-unload"></span>[`unload()`](/docs/api/yii/test/fixture/index.md#method-unload) | Unloads the fixture. | [`Fixture`](/docs/api/yii/test/fixture/index.md) |

## Property details

### $initScript <span id="property-initScript"></span>

```php
public string $initScript = '@app/tests/fixtures/initdb.php'
```

The init script file that should be executed when loading this fixture.

This should be either a file path or [path alias](/docs/guide/concept-aliases/index.md). Note that if the file does not exist,
no error will be raised.

### $schemas <span id="property-schemas"></span>

```php
public array $schemas = ['']
```

List of database schemas that the test tables may reside in.

Defaults to
`['']`, meaning using the default schema (an empty string refers to the
default schema). This property is mainly used when turning on and off integrity checks
so that fixture data can be populated into the database without causing problem.

## Method details

### afterLoad() <span id="method-afterLoad"></span>

```php
public function afterLoad(): mixed
```

This method is called AFTER all fixture data have been loaded for the current test.

**Returns**

`mixed`

### afterUnload() <span id="method-afterUnload"></span>

```php
public function afterUnload(): mixed
```

This method is called AFTER all fixture data have been unloaded for the current test.

**Returns**

`mixed`

### beforeLoad() <span id="method-beforeLoad"></span>

```php
public function beforeLoad(): mixed
```

This method is called BEFORE any fixture data is loaded for the current test.

**Returns**

`mixed`

### beforeUnload() <span id="method-beforeUnload"></span>

```php
public function beforeUnload(): mixed
```

This method is called BEFORE any fixture data is unloaded for the current test.

**Returns**

`mixed`

### checkIntegrity() <span id="method-checkIntegrity"></span>

```php
public function checkIntegrity(bool $check): mixed
```

Toggles the DB integrity check.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$check` | `bool` | Whether to turn on or off the integrity check. |

**Returns**

`mixed`

### load() <span id="method-load"></span>

```php
public function load(): mixed
```

Loads the fixture.

This method is called before performing every test method.
You should override this method with concrete implementation about how to set up the fixture.

**Returns**

`mixed`