Skip to content

Commit

Permalink
Fix Cake5 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloelcolombiano committed Oct 9, 2023
1 parent 17b96b6 commit b1483d8
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
run : vendor/bin/phpunit tests/TestCase/Command/BakeFixtureFactoryCommandTest.php

- name: Run phpstan
run: composer phpstan
run: composer stan

- name: Run phpcs
run: composer cs-check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests_composer2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
- name: Install dependencies
run: |
if [[ ${{ matrix.composer-type }} == 'lowest' ]]; then
composer self-update --1 && composer update --prefer-dist --no-progress --no-suggest --prefer-stable --prefer-lowest
composer update --prefer-dist --no-progress --no-suggest --prefer-stable --prefer-lowest
elif [[ ${{ matrix.composer-type }} == 'stable' ]]; then
composer update --prefer-dist --no-progress --no-suggest --prefer-stable
else
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"php": ">=8.1",
"cakephp/orm": "^5.0",
"fakerphp/faker": "^1.15",
"vierge-noire/cakephp-test-suite-light": "dev-next"
"vierge-noire/cakephp-test-suite-light": "^3.0"
},
"require-dev": {
"cakephp/bake": "^3.0.0",
Expand Down Expand Up @@ -48,7 +48,7 @@
"mysql": "bash run_tests.sh Mysql",
"pgsql": "bash run_tests.sh Postgres",
"sqlite": "bash run_tests.sh Sqlite",
"phpstan": "vendor/bin/phpstan analyse --memory-limit=-1",
"stan": "vendor/bin/phpstan analyse --memory-limit=-1",
"psalm": "vendor/bin/psalm",
"cs-check": "vendor/bin/phpcs --colors -p -s --extensions=php src/ tests/TestApp/tests/Factory tests/TestApp/plugins/TestPlugin/tests/Factory",
"cs-fix": "vendor/bin/phpcbf --colors -p -s --extensions=php src/ tests/TestApp/tests/Factory"
Expand Down
17 changes: 12 additions & 5 deletions src/Factory/DataCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,16 @@ public function compileEntity(
if (is_string($injectedData)) {
$injectedData = $this->setDisplayFieldToInjectedString($injectedData);
}
if ($injectedData instanceof EntityInterface) {
$isEntityInjected = $injectedData instanceof EntityInterface;
if ($isEntityInjected) {
/** @var \Cake\Datasource\EntityInterface $entity */
$entity = $injectedData;
} else {
$entity = $this->getEntityFromDefaultTemplate();
$this->mergeWithInjectedData($entity, $injectedData);
}

$this->mergeWithPatchedData($entity)->mergeWithAssociatedData($entity);
$this->mergeWithPatchedData($entity)->mergeWithAssociatedData($entity, $isEntityInjected);

if ($this->isInPersistMode() && !empty($this->getModifiedUniqueFields())) {
$entity->set(self::MODIFIED_UNIQUE_PROPERTIES, $this->getModifiedUniqueFields());
Expand Down Expand Up @@ -380,12 +382,17 @@ private function mergeWithPatchedData(EntityInterface $entity): self
* Merge with the data from the associations
*
* @param \Cake\Datasource\EntityInterface $entity Entity produced by the factory.
* @param bool $isEntityInjected Whether \Cake\Datasource\EntityInterface is injected or not.
* @return self
*/
private function mergeWithAssociatedData(EntityInterface $entity): self
private function mergeWithAssociatedData(EntityInterface $entity, bool $isEntityInjected): self
{
// Overwrite the default associations if these are found in the associations
$associatedData = array_merge($this->dataFromDefaultAssociations, $this->dataFromAssociations);
if ($isEntityInjected) {
$associatedData = $this->dataFromAssociations;
} else {
// Overwrite the default associations if these are found in the associations
$associatedData = array_merge($this->dataFromDefaultAssociations, $this->dataFromAssociations);
}

foreach ($associatedData as $propertyName => $data) {
$association = $this->getAssociationByPropertyName($propertyName);
Expand Down
15 changes: 10 additions & 5 deletions src/Factory/FactoryAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace CakephpFixtureFactories\Factory;

use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Utility\Inflector;
use CakephpFixtureFactories\Error\FactoryNotFoundException;
use function Cake\Core\namespaceSplit;
Expand All @@ -26,18 +27,22 @@ trait FactoryAwareTrait
*
* Additionnal arguments are passed *as is* to `BaseFactory::make`
*
* @param string $name Factory or model name
* @param mixed ...$arguments Additional arguments for `BaseFactory::make`
* @param string $name Factory or model name
* @param \Cake\Datasource\EntityInterface|callable|array|string|int|null $makeParameter Injected data
* @param int $times Number of entities created
* @return \CakephpFixtureFactories\Factory\BaseFactory
* @throws \CakephpFixtureFactories\Error\FactoryNotFoundException if the factory could not be found
* @see \CakephpFixtureFactories\Factory\BaseFactory::make
*/
public function getFactory(string $name, mixed ...$arguments): BaseFactory
{
public function getFactory(
string $name,
array|callable|int|EntityInterface|string|null $makeParameter = [],
int $times = 1
): BaseFactory {
$factoryClassName = $this->getFactoryClassName($name);

if (class_exists($factoryClassName)) {
return $factoryClassName::make(...$arguments);
return $factoryClassName::make($makeParameter, $times);
}

throw new FactoryNotFoundException("Unable to locate factory class $factoryClassName");
Expand Down
6 changes: 3 additions & 3 deletions templates/bake/fixture_factory.twig
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class {{ factory }} extends CakephpBaseFactory
{% for association, associationData in toOne %}
/**
* @param array|callable|null|int $parameter
* @param array|callable|null|int|\Cake\Datasource\EntityInterface|string $parameter
* @return {{ factory }}
*/
public function with{{ association }}($parameter = null): {{ factory }}
Expand All @@ -64,7 +64,7 @@ class {{ factory }} extends CakephpBaseFactory
{% for association, associationData in oneToMany %}
/**
* @param array|callable|null|int $parameter
* @param array|callable|null|int|\Cake\Datasource\EntityInterface|string $parameter
* @param int $n
* @return {{ factory }}
*/
Expand All @@ -79,7 +79,7 @@ class {{ factory }} extends CakephpBaseFactory
{% for association, associationData in manyToMany %}
/**
* @param array|callable|null|int $parameter
* @param array|callable|null|int|\Cake\Datasource\EntityInterface|string $parameter
* @param int $n
* @return {{ factory }}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

class InitialMigration extends AbstractMigration
{
public $autoId = false;

public function up()
{
$this->table('authors')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

class TableWithoutModelMigration extends AbstractMigration
{
public $autoId = false;

public function up()
{
$this->table('table_without_model')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

class CreateCustomers extends AbstractMigration
{
public $autoId = false;

/**
* Change Method.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

class CreateBills extends AbstractMigration
{
public $autoId = false;

/**
* Change Method.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase/Factory/BaseFactoryMakeWithEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,12 @@ public function testWithEntitiesAndTimes()
$this->assertSame($n * $m, count($authors));
$this->assertSame($n, AuthorFactory::count());
}

public function testMakeEntityWithoutDefaultAssociations()
{
$article1 = ArticleFactory::make()->persist();
$this->assertSame(ArticleFactory::DEFAULT_NUMBER_OF_AUTHORS, count($article1->authors));
$article2 = ArticleFactory::make($article1)->persist();
$this->assertSame(ArticleFactory::DEFAULT_NUMBER_OF_AUTHORS, count($article1->authors));
}
}

0 comments on commit b1483d8

Please sign in to comment.