Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: split Factory class (#452) #462

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ jobs:

- name: 'Test'
run: |
# we need phpstan to be installed in order to run tests on phpstan extension
composer bin phpstan install

if [ "${{ matrix.use-dama }}" == "1" ]; then
CONFIGURATION="--configuration phpunit-dama-doctrine.xml.dist"
fi
Expand Down Expand Up @@ -144,7 +147,11 @@ jobs:
dependency-versions: "highest"

- name: 'Coverage'
run: vendor/bin/simple-phpunit -v --configuration phpunit-dama-doctrine.xml.dist --coverage-text --coverage-clover=foundry.clover
run: |
# we need phpstan to be installed in order to run tests on phpstan extension
composer bin phpstan install

vendor/bin/simple-phpunit -v --configuration phpunit-dama-doctrine.xml.dist --coverage-text --coverage-clover=foundry.clover
env:
USE_ORM: 1
USE_ODM: 1
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ help:
validate: sca psalm test database-validate-mapping ### Run sca, full test suite and validate migrations

.PHONY: test
test: vendor ### Run PHPUnit tests suite
test: vendor $(PHPSTAN_BIN) ### Run PHPUnit tests suite
@$(MAKE) --no-print-directory docker-start-if-not-running
@${DC_EXEC} -e USE_ORM=${USE_ORM} -e USE_ODM=${USE_ODM} ${PHP} vendor/bin/simple-phpunit --configuration ${PHPUNIT_CONFIG_FILE} $(ARGS)

Expand Down Expand Up @@ -111,7 +111,7 @@ database-generate-migration: database-drop-schema ### Generate new migration bas
.PHONY: database-validate-mapping
database-validate-mapping: database-drop-schema ### Validate mapping in Zenstruck\Foundry\Tests\Fixtures\Entity
@${DOCKER_PHP} vendor/bin/doctrine-migrations migrations:migrate --no-interaction --allow-no-migration
@${DOCKER_PHP} bin/doctrine orm:validate-schema
@${DOCKER_PHP} bin/doctrine orm:validate-schema -v

.PHONY: database-drop-schema
database-drop-schema: vendor ### Drop database schema
Expand Down Expand Up @@ -150,7 +150,7 @@ docker-start: ### Start containers

.PHONY: docker-stop
docker-stop: ### Stop containers
@rm $(DOCKER_PHP_CONTAINER_FLAG)
@rm $(DOCKER_PHP_CONTAINER_FLAG) || true
@$(DOCKER_COMPOSE) stop

.PHONY: docker-purge
Expand Down
31 changes: 17 additions & 14 deletions bin/tools/phpstan/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"zenstruck/callback": "^1.1"
},
"require-dev": {
"ext-simplexml": "*",
"bamarni/composer-bin-plugin": "^1.4",
"dama/doctrine-test-bundle": "^7.0",
"doctrine/doctrine-bundle": "^2.5",
Expand Down Expand Up @@ -63,6 +64,14 @@
"target-directory": "bin/tools",
"bin-links": true,
"forward-command": false
},
"phpstan": {
"includes": [
"phpstan-foundry.neon"
]
},
"psalm": {
"pluginClass": "Zenstruck\\Foundry\\Psalm\\FoundryPlugin"
}
},
"minimum-stability": "dev",
Expand Down
9 changes: 3 additions & 6 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,17 @@ you can also add *states*:
public function published(): self
{
// call setPublishedAt() and pass a random DateTime
return $this->addState(['published_at' => self::faker()->dateTime()]);
return $this->withAttributes(['published_at' => self::faker()->dateTime()]);
}

public function unpublished(): self
{
return $this->addState(['published_at' => null]);
return $this->withAttributes(['published_at' => null]);
}

public function withViewCount(int $count = null): self
{
return $this->addState(function () use ($count) {
return $this->withAttributes(function () use ($count) {
return ['view_count' => $count ?? self::faker()->numberBetween(0, 10000)];
});
}
Expand All @@ -396,9 +396,6 @@ You can use states to make your tests very explicit to improve readability:
->create()
;

// states that don't require arguments can be added as strings to PostFactory::new()
$post = PostFactory::new('published', 'withViewCount')->create();

Attributes
~~~~~~~~~~

Expand Down
30 changes: 25 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
parameters:
ignoreErrors:
-
message: "#^Method Zenstruck\\\\Foundry\\\\AnonymousFactory\\:\\:create\\(\\) should return Zenstruck\\\\Foundry\\\\Proxy\\<TModel of object\\> but returns \\(TModel of object\\)\\|Zenstruck\\\\Foundry\\\\Proxy\\<TModel of object\\>\\.$#"
count: 1
path: src/AnonymousFactory.php

-
message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, string\\|null given\\.$#"
count: 1
path: src/Bundle/DependencyInjection/GlobalStatePass.php

-
message: "#^Parameter \\#1 \\$class of method Doctrine\\\\Persistence\\\\ManagerRegistry\\:\\:getManagerForClass\\(\\) expects class\\-string, string given\\.$#"
message: "#^Constructor of class Zenstruck\\\\Foundry\\\\Factory has an unused parameter \\$class\\.$#"
count: 1
path: src/Configuration.php
path: src/Factory.php

-
message: "#^Constructor of class Zenstruck\\\\Foundry\\\\Factory has an unused parameter \\$defaultAttributes\\.$#"
count: 1
path: src/Factory.php

-
message: "#^Parameter \\#1 \\$min of function random_int expects int, int\\|null given\\.$#"
Expand All @@ -20,6 +30,11 @@ parameters:
count: 1
path: src/FactoryCollection.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\ModelFactory\\:\\:__callStatic\\(\\) should return array\\<int, Zenstruck\\\\Foundry\\\\Proxy\\<TModel of object\\>\\> but returns array\\<int, TModel of object\\>\\.$#"
count: 1
path: src/ModelFactory.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\RepositoryProxy\\:\\:proxyResult\\(\\) should return array\\<int, Zenstruck\\\\Foundry\\\\Proxy\\<TProxiedObject of object\\>\\>\\|Zenstruck\\\\Foundry\\\\Proxy\\<TProxiedObject of object\\> but returns \\(TProxiedObject of object\\)\\|null\\.$#"
count: 1
Expand Down Expand Up @@ -51,11 +66,16 @@ parameters:
path: src/Test/ORMDatabaseResetter.php

-
message: "#^Parameter \\#1 \\$configuration of static method Zenstruck\\\\Foundry\\\\Factory\\<object\\>\\:\\:boot\\(\\) expects Zenstruck\\\\Foundry\\\\Configuration, object\\|null given\\.$#"
message: "#^Parameter \\#1 \\$factoryManager of static method Zenstruck\\\\Foundry\\\\BaseFactory\\<mixed\\>\\:\\:boot\\(\\) expects Zenstruck\\\\Foundry\\\\FactoryManager, object\\|null given\\.$#"
count: 1
path: src/ZenstruckFoundryBundle.php

-
message: "#^Function Zenstruck\\\\Foundry\\\\anonymous\\(\\) should return Zenstruck\\\\Foundry\\\\Factory\\<TObject of object\\> but returns class@anonymous/src/functions\\.php\\:45\\.$#"
message: "#^Parameter \\#1 \\$persistenceManager of static method Zenstruck\\\\Foundry\\\\Persistence\\\\PersistentObjectFactory\\<object\\>\\:\\:bootPersistentObjectFactory\\(\\) expects Zenstruck\\\\Foundry\\\\Persistence\\\\PersistenceManager, object\\|null given\\.$#"
count: 1
path: src/functions.php
path: src/ZenstruckFoundryBundle.php

-
message: "#^Parameter \\#2 \\$configuration of static method Zenstruck\\\\Foundry\\\\BaseFactory\\<mixed\\>\\:\\:boot\\(\\) expects Zenstruck\\\\Foundry\\\\Configuration, object\\|null given\\.$#"
count: 1
path: src/ZenstruckFoundryBundle.php
25 changes: 25 additions & 0 deletions phpstan-foundry.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
services:
-
class: Zenstruck\Foundry\PhpStan\FactoryMethodsTypeResolver
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: Zenstruck\Foundry\PhpStan\FactoryCollectionMethodsTypeResolver
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: Zenstruck\Foundry\PhpStan\FactoryStaticMethodsTypeResolver
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
-
class: Zenstruck\Foundry\PhpStan\FactoryRepositoryMethodTypeResolver
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
-
class: Zenstruck\Foundry\PhpStan\AnonymousFactoryTypeResolver
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
-
class: Zenstruck\Foundry\PhpStan\AnonymousHelpersTypeResolver
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
16 changes: 8 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
includes:
- phpstan-baseline.neon
- phpstan-foundry.neon

parameters:
inferPrivatePropertyTypeFromConstructor: true
Expand All @@ -9,9 +10,11 @@ parameters:
paths:
- ./src

# let's analyse factories generated with maker
- ./tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_phpstan.php
- ./tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_phpstan.php
# let's analyse a factory generated with maker
- ./tests/Fixtures/Maker/expected/can_create_factory.php

# and a factory used in tests
- ./tests/Fixtures/Factories/PostFactory.php
level: 8
bootstrapFiles:
- ./vendor/autoload.php
Expand All @@ -33,8 +36,5 @@ parameters:
- var_dump
excludePaths:
- ./src/Bundle/Resources

ignoreErrors:
-
message: '#Unsafe usage of new static\(\).*#'
path: ./src/ModelFactory.php
- ./src/PhpStan
- ./src/Psalm
6 changes: 5 additions & 1 deletion phpunit-dama-doctrine.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
<ini name="error_reporting" value="-1"/>
<env name="KERNEL_CLASS" value="Zenstruck\Foundry\Tests\Fixtures\Kernel"/>
<env name="USE_DAMA_DOCTRINE_TEST_BUNDLE" value="1"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=9999&amp;max[direct]=0&amp;quiet[]=indirect&amp;quiet[]=other"/>
<!--
max[self]=1 represents "The Zenstruck\Foundry\BaseFactory::new() method is considered final" deprecation.
It could be removed in 2.0
-->
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=1&amp;max[direct]=0&amp;quiet[]=indirect&amp;quiet[]=other"/>
<env name="SHELL_VERBOSITY" value="0"/>
</php>

Expand Down
6 changes: 5 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
<php>
<ini name="error_reporting" value="-1"/>
<env name="KERNEL_CLASS" value="Zenstruck\Foundry\Tests\Fixtures\Kernel"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0&amp;quiet[]=indirect&amp;quiet[]=other"/>
<!--
max[self]=1 represents "The Zenstruck\Foundry\BaseFactory::new() method is considered final" deprecation.
It could be removed in 2.0
-->
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=1&amp;max[direct]=0&amp;quiet[]=indirect&amp;quiet[]=other"/>
<env name="SHELL_VERBOSITY" value="0"/>
</php>

Expand Down
9 changes: 7 additions & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
xsi:schemaLocation="https://getpsalm.org/schema/config bin/tools/psalm/vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
autoloader="vendor/autoload.php"
>
<projectFiles>
<file name="tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_psalm.php" />
<file name="tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_psalm.php" />
<file name="tests/Fixtures/Maker/expected/can_create_factory.php"/>
<file name="tests/Fixtures/Psalm/test-types-with-persistence.php"/>
<file name="tests/Fixtures/Psalm/test-types-without-persistence.php"/>
</projectFiles>
<plugins>
<pluginClass class="Zenstruck\Foundry\Psalm\FoundryPlugin"/>
</plugins>
</psalm>
Loading