Skip to content

Commit

Permalink
Merge pull request #73 from vierge-noire/next
Browse files Browse the repository at this point in the history
Apply app's locale to faker
  • Loading branch information
pabloelcolombiano authored Apr 25, 2021
2 parents aff688a + 4316c7f commit 43a3e0b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 19 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion docs/factories.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
parameters:
level: 4
level: 5
bootstrapFiles:
- tests/bootstrap.php
paths:
- src/
- tests/TestCase/DocumentationExamplesTest.php
- tests/TestApp
- tests/TestApp
29 changes: 15 additions & 14 deletions src/Factory/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()
{
Expand All @@ -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");
Expand Down Expand Up @@ -363,6 +363,7 @@ private function getSaveOptions(): array
*/
protected function persistMany(array $entities)
{
/** @phpstan-ignore-next-line */
return $this->getTable()->saveManyOrFail($entities, $this->getSaveOptions());
}

Expand Down
3 changes: 2 additions & 1 deletion src/Factory/DataCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Factory/BaseFactoryAssociationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 43a3e0b

Please sign in to comment.