- Исправлен CityFixture: namespace изменён с backend\models\City на yii_app\records\City
- Обновлены тестовые данные city.php под реальную структуру таблицы city
- Создан новый CityTest с правильными namespace и тестами валидации
- Исправлен test_db.php: переключён на PostgreSQL вместо MySQL
- Добавлены senderEmail/senderName в test.php для ContactForm
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
*/
],
],
- 'params' => $params,
+ 'params' => array_merge($params, [
+ 'senderEmail' => 'test@example.com',
+ 'senderName' => 'Test Sender',
+ 'adminEmail' => 'admin@example.com',
+ ]),
];
<?php
-$db = require __DIR__ . '/db.php';
-// test database! Important not to run tests on production or development databases
-$db['dsn'] = 'mysql:host=localhost;dbname=yii2basic_test';
+/**
+ * Конфигурация тестовой базы данных.
+ * ВАЖНО: Не запускайте тесты на продакшен или девелоп базах данных!
+ */
-return $db;
+return [
+ 'class' => yii\db\Connection::class,
+ 'dsn' => strtr('pgsql:host={host};port={port};dbname={dbname}', [
+ '{host}' => getenv('POSTGRES_HOSTNAME') ?: '127.0.0.1',
+ '{port}' => getenv('POSTGRES_PORT') ?: 5432,
+ '{dbname}' => getenv('POSTGRES_SCHEMA_TEST') ?: 'erp24_test',
+ ]),
+ 'username' => getenv('POSTGRES_USER') ?: 'bazacvetov24',
+ 'password' => getenv('POSTGRES_PASSWORD') ?: '',
+ 'schemaMap' => [
+ 'pgsql' => [
+ 'class' => 'yii\db\pgsql\Schema',
+ 'defaultSchema' => 'erp24'
+ ]
+ ],
+ 'charset' => 'utf8',
+ 'enableSchemaCache' => false,
+];
<?php
return [
- [
- 'name' => 'Город тест',
- 'code' => 'city-test',
- 'social' => 'socialsocial',
- 'phone' => '+7 (232) 4-335-647',
- 'email' => 'nicole.paucek@schultz.info',
- 'phone_support' => '8 800 770 70 22',
- 'active' => '1',
- 'created_at' => '1402312317',
- 'updated_at' => '1402312317',
+ 'city1' => [
+ 'id_region' => 1,
+ 'id_country' => 1,
+ 'oid' => 1,
+ 'city_name_ru' => 'Тестовый город',
+ 'city_name_ru_sklon' => 'Тестового города',
+ 'city_name_en' => 'Test City',
+ 'city_url' => 'test-city',
+ 'h1' => 'Тестовый город',
+ 'seo_title' => 'Тестовый город - заголовок',
+ 'seo_description_ru' => 'Описание тестового города',
+ 'seo_description_eng' => 'Test city description',
+ 'seo_content' => 'Контент для SEO',
+ 'region_name' => 'Тестовый регион',
+ 'dop' => '',
+ 'visible' => '1',
+ 'generate' => '1',
+ 'main' => '0',
+ 'naselenie' => 100000.00,
+ 'gps_center' => '55.7558,37.6173',
+ 'org_id' => 1,
+ 'work_time' => '09:00-18:00',
+ 'work_time_sklad' => '08:00-20:00',
+ ],
+ 'city2' => [
+ 'id_region' => 2,
+ 'id_country' => 1,
+ 'oid' => 2,
+ 'city_name_ru' => 'Второй город',
+ 'city_name_ru_sklon' => 'Второго города',
+ 'city_name_en' => 'Second City',
+ 'city_url' => 'second-city',
+ 'h1' => 'Второй город',
+ 'seo_title' => 'Второй город - заголовок',
+ 'seo_description_ru' => 'Описание второго города',
+ 'seo_description_eng' => 'Second city description',
+ 'seo_content' => 'Контент для SEO второго города',
+ 'region_name' => 'Второй регион',
+ 'dop' => '',
+ 'visible' => '1',
+ 'generate' => '1',
+ 'main' => '0',
+ 'naselenie' => 50000.00,
+ 'gps_center' => '59.9343,30.3351',
+ 'org_id' => 1,
+ 'work_time' => '10:00-19:00',
+ 'work_time_sklad' => '09:00-21:00',
],
];
<?php
+
namespace tests\fixtures;
use yii\test\ActiveFixture;
class CityFixture extends ActiveFixture
{
- public $modelClass = 'backend\models\City';
+ public $modelClass = 'yii_app\\records\\City';
+ public $dataFile = __DIR__ . '/../_data/city.php';
}
\ No newline at end of file
--- /dev/null
+<?php
+
+namespace tests\unit\models;
+
+use Codeception\Test\Unit;
+use tests\fixtures\CityFixture;
+use yii_app\records\City;
+
+/**
+ * City model test
+ */
+class CityTest extends Unit
+{
+ /**
+ * @var \tests\UnitTester
+ */
+ protected $tester;
+
+ /**
+ * @return array
+ */
+ public function _fixtures(): array
+ {
+ return [
+ 'city' => [
+ 'class' => CityFixture::class,
+ 'dataFile' => '@app/tests/_data/city.php',
+ ],
+ ];
+ }
+
+ /**
+ * Тест: пустые обязательные поля не проходят валидацию
+ */
+ public function testEmptyRequiredFields(): void
+ {
+ $city = new City();
+
+ $this->assertFalse($city->validate());
+ $this->assertArrayHasKey('id_region', $city->getErrors());
+ $this->assertArrayHasKey('id_country', $city->getErrors());
+ $this->assertArrayHasKey('oid', $city->getErrors());
+ }
+
+ /**
+ * Тест: валидные данные проходят валидацию
+ */
+ public function testValidData(): void
+ {
+ $city = new City();
+ $city->id_region = 1;
+ $city->id_country = 1;
+ $city->oid = 1;
+ $city->city_name_ru = 'Новый город';
+ $city->city_name_ru_sklon = 'Нового города';
+ $city->city_name_en = 'New City';
+ $city->city_url = 'new-city';
+ $city->h1 = 'Новый город';
+ $city->seo_title = 'Новый город - заголовок';
+ $city->seo_description_ru = 'Описание';
+ $city->seo_description_eng = 'Description';
+ $city->seo_content = 'Content';
+ $city->region_name = 'Регион';
+ $city->naselenie = 10000.00;
+ $city->gps_center = '55.0000,37.0000';
+ $city->org_id = 1;
+ $city->work_time = '09:00-18:00';
+ $city->work_time_sklad = '08:00-20:00';
+
+ $this->assertTrue($city->validate(), 'Валидация должна пройти с корректными данными');
+ }
+
+ /**
+ * Тест: некорректные типы данных
+ */
+ public function testInvalidIntegerFields(): void
+ {
+ $city = new City();
+ $city->id_region = 'not_integer';
+ $city->id_country = 'not_integer';
+ $city->oid = 'not_integer';
+ $city->org_id = 'not_integer';
+
+ $this->assertFalse($city->validate());
+ $this->assertArrayHasKey('id_region', $city->getErrors());
+ $this->assertArrayHasKey('id_country', $city->getErrors());
+ $this->assertArrayHasKey('oid', $city->getErrors());
+ $this->assertArrayHasKey('org_id', $city->getErrors());
+ }
+
+ /**
+ * Тест: слишком длинное название города (>255 символов)
+ */
+ public function testLongCityName(): void
+ {
+ $city = new City();
+ $city->city_name_ru = str_repeat('а', 300);
+
+ $this->assertFalse($city->validate(['city_name_ru']));
+ $this->assertArrayHasKey('city_name_ru', $city->getErrors());
+ }
+
+ /**
+ * Тест: корректная длина gps_center (max 35)
+ */
+ public function testGpsCenterLength(): void
+ {
+ $city = new City();
+ $city->gps_center = str_repeat('1', 40);
+
+ $this->assertFalse($city->validate(['gps_center']));
+ $this->assertArrayHasKey('gps_center', $city->getErrors());
+ }
+
+ /**
+ * Тест: поле main максимум 1 символ
+ */
+ public function testMainFieldLength(): void
+ {
+ $city = new City();
+ $city->main = 'abc';
+
+ $this->assertFalse($city->validate(['main']));
+ $this->assertArrayHasKey('main', $city->getErrors());
+ }
+
+ /**
+ * Тест: получение названия таблицы
+ */
+ public function testTableName(): void
+ {
+ $this->assertEquals('city', City::tableName());
+ }
+
+ /**
+ * Тест: наличие attributeLabels
+ */
+ public function testAttributeLabels(): void
+ {
+ $city = new City();
+ $labels = $city->attributeLabels();
+
+ $this->assertIsArray($labels);
+ $this->assertArrayHasKey('id_city', $labels);
+ $this->assertArrayHasKey('city_name_ru', $labels);
+ }
+}