Skip to content

Commit

Permalink
Merge pull request #74 from vierge-noire/cake3_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 f841515 + 03a136e commit f8b4bf7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 9 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 self
*/
protected function setDefaultTemplate()
protected function setDefaultTemplate(): void
{
$this->setDefaultData(function(Generator $faker) {
return [
Expand Down Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions src/Factory/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()
{
Expand All @@ -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");
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;
use Cake\ORM\Association\BelongsTo;
Expand Down Expand Up @@ -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) {
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 @@ -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);
Expand Down

0 comments on commit f8b4bf7

Please sign in to comment.