# Codeception Test Suite Configuration
class_name: UnitTester
modules:
enabled:
- Asserts
- Yii2:
part: [orm, email, fixtures]
- Db
populate: true
cleanup: true
reconnect: true
config:
Db:
dsn: 'pgsql:host=localhost;dbname=mybase'
user: 'myuser'
password: 'correctPassword'
<?php
namespace tests\unit\models;
use app\models\Types;
class TypesTest extends \Codeception\Test\Unit
{
/**
* @var \UnitTester
*/
protected $tester;
protected function _before()
{
}
protected function _after()
{
}
// tests
public function testEnterType()
{
$types = new Types();
// Пустое значение недопустимо
$types->name = NULL;
$this->assertFalse($types->validate(['name']));
// Больше 100 символов недопустимо
$types->name = '**** aaaaabbbbbcccccdddddeeeeefffffggggghhhhhiiiiijjjjjkkkkklllllmmmmmnnnnnooooopppppqqqqqrrrrrsssss ****';
$this->assertFalse($types->validate(['name']));
$validName = '--TEST TYPE--';
// Допустимая комбинация
$types->name = $validName;
$this->assertTrue($types->validate(['name']));
// Сохранение данных в базу
$this->assertTrue($types->validate());
$this->assertTrue($types->save());
$this->assertGreaterThan(0, $types->id);
$this->tester->seeInDatabase('types', ['name' => $validName ]);
}
}
Codeception PHP Testing Framework v4.1.6
Powered by PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
Running with seed:
Unit Tests (1) -----------------------------
✖ TypesTest: Enter type (0.03s)
---------------------------------------------
Time: 90 ms, Memory: 12.00 MB
There was 1 failure:
---------
1) TypesTest: Enter type
Test tests/unit/models/TypesTest.php:testEnterType
No matching records found for criteria {"name":"--TEST TYPE--"} in table types
Failed asserting that 0 is greater than 0.
#1 Codeception\Module\Db->seeInDatabase
#2 /var/www/basic/tests/_support/_generated/UnitTesterActions.php:1397
#3 /var/www/basic/tests/unit/models/TypesTest.php:48
FAILURES!
Tests: 1, Assertions: 7, Failures: 1.
$count = count(Types::find()->where([ 'name' => $validName ])->all());
$this->assertGreaterThan(0, $count);
$this->assertEquals(1, $count);
$this->tester->seeInDatabase('types', [ 'name' => $validName ]);
$this->tester->haveInDatabase('types', [ 'name' => $validName ]);