Skip to content

Commit

Permalink
[feature] Allow to use foundry without Doctrine (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Nov 10, 2022
1 parent 7598467 commit 3588274
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 81 deletions.
22 changes: 0 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ jobs:
- name: 'Test: MySQL'
run: vendor/bin/simple-phpunit -v
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7

- name: 'Test: MySQL, FoundryBundle'
Expand All @@ -87,7 +86,6 @@ jobs:
- name: 'Test: MySQL, DAMABundle'
run: vendor/bin/simple-phpunit -v --configuration phpunit-dama-doctrine.xml.dist
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7

- name: 'Test: MySQL, FoundryBundle, DAMABundle'
Expand All @@ -99,21 +97,18 @@ jobs:
- name: 'Test: MySQL, DoctrineMigrationsBundle'
run: vendor/bin/simple-phpunit -v
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
USE_MIGRATIONS: 1
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7

- name: 'Test: MySQL, DoctrineMigrationsBundle, DAMABundle'
run: vendor/bin/simple-phpunit -v --configuration phpunit-dama-doctrine.xml.dist
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
USE_MIGRATIONS: 1
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7

- name: 'Test: PostgreSQL'
run: vendor/bin/simple-phpunit -v
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: postgresql://postgres:1234@127.0.0.1:5432/zenstruck_foundry?charset=utf8

- name: 'Test: PostgreSQL, FoundryBundle'
Expand All @@ -136,7 +131,6 @@ jobs:
- name: 'Test: SQLite'
run: vendor/bin/simple-phpunit -v
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: sqlite:///%kernel.cache_dir%/app.db

- name: 'Test: SQLite, FoundryBundle'
Expand All @@ -148,20 +142,17 @@ jobs:
- name: 'Test: SQLite, DAMABundle'
run: vendor/bin/simple-phpunit -v --configuration phpunit-dama-doctrine.xml.dist
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: sqlite:///%kernel.cache_dir%/app.db

- name: 'Test: SQLite, FoundryBundle, DAMABundle'
run: vendor/bin/simple-phpunit -v --configuration phpunit-dama-doctrine.xml.dist
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
# USE_FOUNDRY_BUNDLE: 1
DATABASE_URL: sqlite:///%kernel.cache_dir%/app.db

- name: 'Test: MySQL, Mongo'
run: vendor/bin/simple-phpunit -v
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7
MONGO_URL: mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb

Expand All @@ -175,14 +166,12 @@ jobs:
- name: 'Test: MySQL, Mongo, DAMABundle'
run: vendor/bin/simple-phpunit -v --configuration phpunit-dama-doctrine.xml.dist
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7
MONGO_URL: mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb

- name: 'Test: Mongo standalone'
run: vendor/bin/simple-phpunit -v
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
MONGO_URL: mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb

code-coverage:
Expand Down Expand Up @@ -231,7 +220,6 @@ jobs:
- name: 'Coverage: MySQL'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=mysql.clover
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7

- name: 'Coverage: MySQL, FoundryBundle'
Expand All @@ -243,7 +231,6 @@ jobs:
- name: 'Coverage: MySQL, DAMABundle'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=mysql-dama.clover --configuration phpunit-dama-doctrine.xml.dist
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7

- name: 'Coverage: MySQL, FoundryBundle, DAMABundle'
Expand All @@ -255,21 +242,18 @@ jobs:
- name: 'Test: MySQL, DoctrineMigrationsBundle'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=mysql-migrations.clover
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
USE_MIGRATIONS: 1
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7

- name: 'Test: MySQL, DoctrineMigrationsBundle, DAMABundle'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=mysql-migrations-dama.clover --configuration phpunit-dama-doctrine.xml.dist
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
USE_MIGRATIONS: 1
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7

- name: 'Coverage: PostgreSQL'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=postgres.clover
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: postgresql://postgres:1234@127.0.0.1:5432/zenstruck_foundry?charset=utf8

- name: 'Coverage: PostgreSQL, FoundryBundle'
Expand All @@ -292,7 +276,6 @@ jobs:
- name: 'Coverage: SQLite'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=sqlite.clover
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: sqlite:///%kernel.cache_dir%/app.db

- name: 'Coverage: SQLite, FoundryBundle'
Expand All @@ -304,20 +287,17 @@ jobs:
- name: 'Coverage: SQLite, DAMABundle'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=sqlite-dama.clover --configuration phpunit-dama-doctrine.xml.dist
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: sqlite:///%kernel.cache_dir%/app.db

- name: 'Coverage: SQLite, FoundryBundle, DAMABundle'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=sqlite-foundry-dama.clover --configuration phpunit-dama-doctrine.xml.dist
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
# USE_FOUNDRY_BUNDLE: 1
DATABASE_URL: sqlite:///%kernel.cache_dir%/app.db

- name: 'Test: MySQL, Mongo'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=mysql-mongo.clover
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7
MONGO_URL: mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb

Expand All @@ -331,14 +311,12 @@ jobs:
- name: 'Test: MySQL, Mongo, DAMABundle'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=mysql-mongo-dama.clover --configuration phpunit-dama-doctrine.xml.dist
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
DATABASE_URL: mysql://root:1234@127.0.0.1:3306/zenstruck_foundry?serverVersion=5.7
MONGO_URL: mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb

- name: 'Test: Mongo standalone'
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover=mongo.clover
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0&max[self]=99999
MONGO_URL: mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb

- name: Publish coverage report to Codecov
Expand Down
49 changes: 16 additions & 33 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1609,9 +1609,7 @@ Foundry can be used in standard PHPUnit unit tests (TestCase's that just extend
``Symfony\Bundle\FrameworkBundle\Test\KernelTestCase``). These tests still require using the ``Factories`` trait to boot
Foundry but will not have doctrine available. Factories created in these tests will not be persisted (calling
``->withoutPersisting()`` is not necessary). Because the bundle is not available in these tests,
any bundle configuration you have will not be picked up. You will need to add
`Test-Only Configuration`_. Unfortunately, this may mean duplicating your bundle configuration
here.
any bundle configuration you have will not be picked up.

.. code-block:: php
Expand All @@ -1631,52 +1629,37 @@ here.
}
}
.. note::

`Factories as Services`_ and `Stories as Services`_ with required
constructor arguments are not usable in non-Kernel tests. The container is not available to resolve their dependencies.
The easiest work-around is to make the test an instance of ``Symfony\Bundle\FrameworkBundle\Test\KernelTestCase`` so the
container is available.

Test-Only Configuration
~~~~~~~~~~~~~~~~~~~~~~~

Foundry can be configured statically, with pure PHP, in your ``tests/bootstrap.php``. This is useful if you have a mix
of Kernel and `non-Kernel tests`_ or if `Using Without the Bundle`_:
You will need to configure manually Foundry. Unfortunately, this may mean duplicating your bundle configuration here.

.. code-block:: php
// tests/bootstrap.php
// ...
// configure a default instantiator
Zenstruck\Foundry\Test\TestState::setInstantiator(
(new Zenstruck\Foundry\Instantiator())
Zenstruck\Foundry\Test\TestState::configure(
instantiator: (new Zenstruck\Foundry\Instantiator())
->withoutConstructor()
->allowExtraAttributes()
->alwaysForceProperties()
->alwaysForceProperties(),
faker: Faker\Factory::create('fr_FR')
);
// configure a custom faker
Zenstruck\Foundry\Test\TestState::setFaker(Faker\Factory::create('fr_FR'));
.. note::

// enable auto-refreshing "globally"
Zenstruck\Foundry\Test\TestState::enableDefaultProxyAutoRefresh();
`Factories as Services`_ and `Stories as Services`_ with required
constructor arguments are not usable in non-Kernel tests. The container is not available to resolve their dependencies.
The easiest work-around is to make the test an instance of ``Symfony\Bundle\FrameworkBundle\Test\KernelTestCase`` so the
container is available.

// disable auto-refreshing "globally"
Zenstruck\Foundry\Test\TestState::disableDefaultProxyAutoRefresh();
Using in Unit Tests
~~~~~~~~~~~~~~~~~~~

.. note::
When using foundry in unit tests, by using ``PHPUnit\Framework\TestCase``, Foundry simply creates the object instancies
but does not try to persist them (this is also true for any object not managed by Doctrine).

If using `bundle configuration`_ as well, *test-only configuration* will override the
bundle configuration.
You can still configure Foundry statically:

Using without the Bundle
~~~~~~~~~~~~~~~~~~~~~~~~

The provided bundle is not strictly required to use Foundry for tests. You can have all your factories, stories, and
configuration live in your ``tests/`` directory. You can configure foundry with
`Test-Only Configuration`_.

.. _stories:

Expand Down
2 changes: 1 addition & 1 deletion phpunit-dama-doctrine.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<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]=0&amp;max[direct]=0"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=9999&amp;max[direct]=0"/>
<env name="SHELL_VERBOSITY" value="0"/>
</php>

Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<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"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=9999&amp;max[direct]=0"/>
<env name="SHELL_VERBOSITY" value="0"/>
</php>

Expand Down
4 changes: 0 additions & 4 deletions src/Bundle/DependencyInjection/ChainManagerRegistryPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public function process(ContainerBuilder $container): void
$managerRegistries[] = new Reference('doctrine_mongodb');
}

if (0 === \count($managerRegistries)) {
throw new \LogicException('Neither doctrine/orm nor mongodb-odm are present.');
}

$container->getDefinition(ChainManagerRegistry::class)
->setArgument('$managerRegistries', $managerRegistries)
;
Expand Down
4 changes: 0 additions & 4 deletions src/ChainManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ final class ChainManagerRegistry implements ManagerRegistry
/** @param list<ManagerRegistry> $managerRegistries */
public function __construct(array $managerRegistries)
{
if (0 === \count($managerRegistries)) {
throw new \InvalidArgumentException('no manager registry provided');
}

$this->managerRegistries = $managerRegistries;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Test/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait Factories
public static function _setUpFactories(): void
{
if (!\is_subclass_of(static::class, KernelTestCase::class)) {
TestState::bootFoundry();
TestState::bootFoundryForUnitTest();

return;
}
Expand Down
Loading

0 comments on commit 3588274

Please sign in to comment.