Skip to content

Commit

Permalink
[feat] Allow to use foundry without Doctrine
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Nov 10, 2022
1 parent 7598467 commit 045e14f
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 77 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
39 changes: 10 additions & 29 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1638,46 +1638,27 @@ here.
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
~~~~~~~~~~~~~~~~~~~~~~~
Using in Unit Tests
~~~~~~~~~~~~~~~~~~~

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).

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 can still configure Foundry statically:

.. 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'));
// enable auto-refreshing "globally"
Zenstruck\Foundry\Test\TestState::enableDefaultProxyAutoRefresh();
// disable auto-refreshing "globally"
Zenstruck\Foundry\Test\TestState::disableDefaultProxyAutoRefresh();
.. note::

If using `bundle configuration`_ as well, *test-only configuration* will override the
bundle configuration.

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:

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::bootFoundryWithoutBundle();

return;
}
Expand Down
45 changes: 39 additions & 6 deletions src/Test/TestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Zenstruck\Foundry\ChainManagerRegistry;
use Zenstruck\Foundry\Configuration;
use Zenstruck\Foundry\Factory;
use Zenstruck\Foundry\Instantiator;
use Zenstruck\Foundry\StoryManager;

/**
Expand All @@ -28,23 +29,43 @@ final class TestState
/** @var callable[] */
private static $globalStates = [];

/**
* @deprecated Use TestState::configure()
*/
public static function setInstantiator(callable $instantiator): void
{
trigger_deprecation('zenstruck\foundry', '1.23', 'Usage of TestState::setInstantiator() is deprecated. Please use TestState::configure().');

self::$instantiator = $instantiator;
}

/**
* @deprecated Use TestState::configure()
*/
public static function setFaker(Faker\Generator $faker): void
{
trigger_deprecation('zenstruck\foundry', '1.23', 'Usage of TestState::setFaker() is deprecated. Please use TestState::configure().');

self::$faker = $faker;
}

/**
* @deprecated Use bundle configuration
*/
public static function enableDefaultProxyAutoRefresh(): void
{
trigger_deprecation('zenstruck\foundry', '1.23', 'Usage of TestState::enableDefaultProxyAutoRefresh() is deprecated. Please use bundle configuration under "auto_refresh_proxies" key.');

self::$defaultProxyAutoRefresh = true;
}

/**
* @deprecated Use bundle configuration
*/
public static function disableDefaultProxyAutoRefresh(): void
{
trigger_deprecation('zenstruck\foundry', '1.23', 'Usage of TestState::disableDefaultProxyAutoRefresh() is deprecated. Please use bundle configuration under "auto_refresh_proxies" key.');

self::$defaultProxyAutoRefresh = false;
}

Expand Down Expand Up @@ -73,9 +94,9 @@ public static function addGlobalState(callable $callback): void
self::$globalStates[] = $callback;
}

public static function bootFoundry(?Configuration $configuration = null): void
public static function bootFoundryWithoutBundle(): void
{
$configuration = $configuration ?? new Configuration([], [], 'schema', []);
$configuration = new Configuration([], [], 'schema', []);

if (self::$instantiator) {
$configuration->setInstantiator(self::$instantiator);
Expand All @@ -91,9 +112,17 @@ public static function bootFoundry(?Configuration $configuration = null): void
$configuration->disableDefaultProxyAutoRefresh();
}

self::bootFoundry($configuration);
}

public static function bootFoundry(Configuration $configuration): void
{
Factory::boot($configuration);
}

/**
* @internal
*/
public static function shutdownFoundry(): void
{
Factory::shutdown();
Expand Down Expand Up @@ -123,6 +152,8 @@ public static function bootFromContainer(ContainerInterface $container): void
return;
}

trigger_deprecation('zenstruck\foundry', '1.23', 'Usage of foundry without the bundle is deprecated and will not be possible anymore in 2.0.');

$configuration = new Configuration([], [], 'schema', []);

try {
Expand Down Expand Up @@ -150,10 +181,6 @@ public static function initializeChainManagerRegistry(ContainerInterface $contai
$managerRegistries[] = $container->get('doctrine_mongodb');
}

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

return new ChainManagerRegistry($managerRegistries);
}

Expand All @@ -176,4 +203,10 @@ public static function flushGlobalState(?GlobalStateRegistry $globalStateRegistr

StoryManager::setGlobalState();
}

public static function configure(?Instantiator $instantiator = null, ?Faker\Generator $faker = null): void
{
self::$instantiator = $instantiator;
self::$faker = $faker;
}
}
Loading

0 comments on commit 045e14f

Please sign in to comment.