diff --git a/composer.json b/composer.json index 57a382bc..16a92ec8 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ }, "require-dev": { "cakephp/bake": "^1.5", + "cakephp/migrations": "^2.3", "josegonzalez/dotenv": "dev-master", "phpstan/phpstan": "0.12.x-dev", "phpunit/phpunit": "^6.1", diff --git a/docs/factories.md b/docs/factories.md index f1460f3e..1bdae76c 100644 --- a/docs/factories.md +++ b/docs/factories.md @@ -37,7 +37,7 @@ class ArticleFactory extends BaseFactory * You may use methods of the factory here * @return self */ - protected function setDefaultTemplate() + protected function setDefaultTemplate(): void { $this->setDefaultData(function(Generator $faker) { return [ @@ -67,6 +67,14 @@ class ArticleFactory extends BaseFactory ``` You may add any methods related to your business model, such as `setJobTitle` to help you build efficient and reusable factories. +### Required fields + +If a field is required in the database, it will have to be populated in the `setDefaultTemplate` method. You may simply set it to a fixed value, for example 1. + +### Locale + +The factories will generate data in the locale of your application, if the latter is supported by faker. + ### Validation / Behaviors With the aim of persisting data in the database as straighforwardly as possible, all behaviors (except Timestamp) and all validations are deactivated when creating CakePHP entities and persisting them to the database. Validation may be reactivated / customized by overwriting diff --git a/src/Factory/BaseFactory.php b/src/Factory/BaseFactory.php index 12b819e8..c47c4870 100644 --- a/src/Factory/BaseFactory.php +++ b/src/Factory/BaseFactory.php @@ -14,11 +14,10 @@ namespace CakephpFixtureFactories\Factory; use Cake\Datasource\EntityInterface; -use Cake\Datasource\ResultSetInterface; +use Cake\I18n\I18n; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use CakephpFixtureFactories\Error\PersistenceException; -use Exception; use Faker\Factory; use Faker\Generator; use InvalidArgumentException; @@ -195,12 +194,21 @@ private static function makeFromCallable(callable $fn): BaseFactory } /** - * @return Generator + * Faker's local is set as the I18n local. + * If not supported by Faker, take faker's default. + * + * @return \Faker\Generator */ public function getFaker(): Generator { if (is_null(self::$faker)) { - $faker = Factory::create(); + try { + $fakerLocale = I18n::getLocale(); + $faker = Factory::create($fakerLocale); + } catch (\Throwable $e) { + $fakerLocale = Factory::DEFAULT_LOCALE; + $faker = Factory::create($fakerLocale); + } $faker->seed(1234); self::$faker = $faker; } @@ -293,8 +301,8 @@ public function getRootTableRegistry(): Table } /** - * @return array|EntityInterface|EntityInterface[]|ResultSetInterface|false|null - * @throws Exception + * @return array|\Cake\Datasource\EntityInterface|\Cake\Datasource\EntityInterface[]|false|null + * @throws \CakephpFixtureFactories\Error\PersistenceException if the entity/entities could not be saved. */ public function persist() { @@ -308,7 +316,7 @@ public function persist() } else { return $this->persistMany($entities); } - } catch (Exception $exception) { + } catch (\Throwable $exception) { $factory = get_class($this); $message = $exception->getMessage(); throw new PersistenceException("Error in Factory $factory.\n Message: $message \n"); diff --git a/src/Factory/DataCompiler.php b/src/Factory/DataCompiler.php index 23e5147c..a48db9ea 100644 --- a/src/Factory/DataCompiler.php +++ b/src/Factory/DataCompiler.php @@ -14,6 +14,7 @@ namespace CakephpFixtureFactories\Factory; +use Cake\Database\Driver\Postgres; use Cake\Datasource\EntityInterface; use Cake\ORM\Association; use Cake\ORM\Association\BelongsTo; @@ -476,7 +477,7 @@ public function setPrimaryKeyOffset($primaryKeyOffset) */ private function updatePostgresSequence(array $primaryKeys) { - if (Util::isRunningOnPostgresql($this->getFactory())) { + if ($this->getFactory()->getRootTableRegistry()->getConnection()->config()['driver'] === Postgres::class) { $tableName = $this->getFactory()->getRootTableRegistry()->getTable(); foreach ($primaryKeys as $pk => $offset) { diff --git a/tests/TestCase/Factory/BaseFactoryAssociationsTest.php b/tests/TestCase/Factory/BaseFactoryAssociationsTest.php index 27ba342f..440c5042 100644 --- a/tests/TestCase/Factory/BaseFactoryAssociationsTest.php +++ b/tests/TestCase/Factory/BaseFactoryAssociationsTest.php @@ -14,6 +14,7 @@ namespace CakephpFixtureFactories\Test\TestCase\Factory; use Cake\Core\Configure; +use Cake\Database\Driver\Postgres; use Cake\ORM\Query; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; @@ -646,7 +647,7 @@ public function testCountryWith4Cities() $this->assertSame(4, count($country->cities)); $this->assertSame(4, $this->CitiesTable->find()->count()); - if (Util::isRunningOnPostgresql(CountryFactory::make())) { + if (CountryFactory::make()->getRootTableRegistry()->getConnection()->config()['driver'] === Postgres::class) { $this->assertSame($city1, $this->CitiesTable->get(1)->name); $this->assertSame($city2, $this->CitiesTable->get(2)->name); $this->assertSame($street1, $this->AddressesTable->get(1)->street);