diff --git a/composer.json b/composer.json index 9f0c502b..5ab7e222 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "require-dev": { "cakephp/bake": "^2.0", "cakephp/cakephp-codesniffer": "^4.0", + "cakephp/migrations": "^3.0", "josegonzalez/dotenv": "dev-master", "phpstan/phpstan": "^0.12.48@dev", "phpunit/phpunit": "^8.0", diff --git a/docs/factories.md b/docs/factories.md index 8aa2e4c5..ad990564 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 void */ - protected function setDefaultTemplate() + protected function setDefaultTemplate(): void { $this->setDefaultData(function(Generator $faker) { return [ @@ -71,6 +71,10 @@ You may add any methods related to your business model, such as `setJobTitle` to 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 straightforwardly as possible, all validations and rules diff --git a/phpstan.neon b/phpstan.neon index e860386e..a68e79d4 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,8 +1,8 @@ parameters: - level: 4 + level: 5 bootstrapFiles: - tests/bootstrap.php paths: - src/ - tests/TestCase/DocumentationExamplesTest.php - - tests/TestApp \ No newline at end of file + - tests/TestApp diff --git a/src/Factory/BaseFactory.php b/src/Factory/BaseFactory.php index 01761f7a..f10d0e42 100644 --- a/src/Factory/BaseFactory.php +++ b/src/Factory/BaseFactory.php @@ -13,12 +13,11 @@ */ namespace CakephpFixtureFactories\Factory; -use Cake\Database\Driver\Postgres; use Cake\Datasource\EntityInterface; +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; @@ -118,7 +117,7 @@ abstract protected function getRootTableRegistryName(): string; abstract protected function setDefaultTemplate(): void; /** - * @param array|callable|null|int|\Cake\Datasource\EntityInterface|\Cake\Datasource\EntityInterface[] $makeParameter Injected data + * @param array|callable|null|int|\Cake\Datasource\EntityInterface $makeParameter Injected data * @param int $times Number of entities created * @return static */ @@ -159,14 +158,6 @@ protected function setUp(BaseFactory $factory, int $times): void $factory->getDataCompiler()->collectAssociationsFromDefaultTemplate(); } - /** - * @return bool - */ - public function isRunningOnPostgresql(): bool - { - return $this->getRootTableRegistry()->getConnection()->config()['driver'] === Postgres::class; - } - /** * Method to apply all model event listeners, both in the * related TableRegistry as well as in the Behaviors @@ -211,12 +202,21 @@ private static function makeFromCallable(callable $fn): BaseFactory } /** + * 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; } @@ -314,7 +314,7 @@ public function getRootTableRegistry(): Table /** * @return array|\Cake\Datasource\EntityInterface|\Cake\Datasource\EntityInterface[]|false|null - * @throws \Exception + * @throws \CakephpFixtureFactories\Error\PersistenceException if the entity/entities could not be saved. */ public function persist() { @@ -328,7 +328,7 @@ public function persist() } else { return $this->persistMany($entities); } - } catch (Exception $exception) { + } catch (\Throwable $exception) { $factory = static::class; $message = $exception->getMessage(); throw new PersistenceException("Error in Factory $factory.\n Message: $message \n"); @@ -363,6 +363,7 @@ private function getSaveOptions(): array */ protected function persistMany(array $entities) { + /** @phpstan-ignore-next-line */ return $this->getTable()->saveManyOrFail($entities, $this->getSaveOptions()); } diff --git a/src/Factory/DataCompiler.php b/src/Factory/DataCompiler.php index e04bf381..57b9017c 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\BelongsTo; use Cake\ORM\Association\HasOne; @@ -492,7 +493,7 @@ public function setPrimaryKeyOffset($primaryKeyOffset): void */ private function updatePostgresSequence(array $primaryKeys): void { - if ($this->getFactory()->isRunningOnPostgresql()) { + 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 22e7c3dd..45947c10 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; @@ -647,7 +648,7 @@ public function testCountryWith4Cities() $this->assertSame(4, count($country->cities)); $this->assertSame(4, $this->CitiesTable->find()->count()); - if (CountryFactory::make()->isRunningOnPostgresql()) { + 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);