diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 9bf04af..cae8fcc 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -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 diff --git a/.github/workflows/tests_composer2.yml b/.github/workflows/tests_composer2.yml index c7ca3f8..d89baba 100644 --- a/.github/workflows/tests_composer2.yml +++ b/.github/workflows/tests_composer2.yml @@ -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 diff --git a/composer.json b/composer.json index 973b2e2..444d302 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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" diff --git a/src/Factory/DataCompiler.php b/src/Factory/DataCompiler.php index f046562..4a8e6c1 100644 --- a/src/Factory/DataCompiler.php +++ b/src/Factory/DataCompiler.php @@ -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()); @@ -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); diff --git a/src/Factory/FactoryAwareTrait.php b/src/Factory/FactoryAwareTrait.php index ca39918..681d280 100644 --- a/src/Factory/FactoryAwareTrait.php +++ b/src/Factory/FactoryAwareTrait.php @@ -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; @@ -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"); diff --git a/templates/bake/fixture_factory.twig b/templates/bake/fixture_factory.twig index 797d60f..38e2b7f 100644 --- a/templates/bake/fixture_factory.twig +++ b/templates/bake/fixture_factory.twig @@ -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 }} @@ -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 }} */ @@ -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 }} */ diff --git a/tests/TestApp/config/Migrations/20200208100000_initial_migration.php b/tests/TestApp/config/Migrations/20200208100000_initial_migration.php index 5f76db4..68f2d09 100644 --- a/tests/TestApp/config/Migrations/20200208100000_initial_migration.php +++ b/tests/TestApp/config/Migrations/20200208100000_initial_migration.php @@ -16,8 +16,6 @@ class InitialMigration extends AbstractMigration { - public $autoId = false; - public function up() { $this->table('authors') diff --git a/tests/TestApp/config/Migrations/20211022100000_table_without_model_migration.php b/tests/TestApp/config/Migrations/20211022100000_table_without_model_migration.php index 776ba0d..f3a822e 100644 --- a/tests/TestApp/config/Migrations/20211022100000_table_without_model_migration.php +++ b/tests/TestApp/config/Migrations/20211022100000_table_without_model_migration.php @@ -16,8 +16,6 @@ class TableWithoutModelMigration extends AbstractMigration { - public $autoId = false; - public function up() { $this->table('table_without_model') diff --git a/tests/TestApp/plugins/TestPlugin/config/Migrations/20200513155719_CreateCustomers.php b/tests/TestApp/plugins/TestPlugin/config/Migrations/20200513155719_CreateCustomers.php index 83a8060..12c6031 100644 --- a/tests/TestApp/plugins/TestPlugin/config/Migrations/20200513155719_CreateCustomers.php +++ b/tests/TestApp/plugins/TestPlugin/config/Migrations/20200513155719_CreateCustomers.php @@ -16,8 +16,6 @@ class CreateCustomers extends AbstractMigration { - public $autoId = false; - /** * Change Method. * diff --git a/tests/TestApp/plugins/TestPlugin/config/Migrations/20200513155720_CreateBills.php b/tests/TestApp/plugins/TestPlugin/config/Migrations/20200513155720_CreateBills.php index d5de30f..9523dba 100644 --- a/tests/TestApp/plugins/TestPlugin/config/Migrations/20200513155720_CreateBills.php +++ b/tests/TestApp/plugins/TestPlugin/config/Migrations/20200513155720_CreateBills.php @@ -16,8 +16,6 @@ class CreateBills extends AbstractMigration { - public $autoId = false; - /** * Change Method. * diff --git a/tests/TestCase/Factory/BaseFactoryMakeWithEntityTest.php b/tests/TestCase/Factory/BaseFactoryMakeWithEntityTest.php index 2e1ff4a..ffc2ec2 100644 --- a/tests/TestCase/Factory/BaseFactoryMakeWithEntityTest.php +++ b/tests/TestCase/Factory/BaseFactoryMakeWithEntityTest.php @@ -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)); + } }