|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the zenstruck/foundry package. |
| 7 | + * |
| 8 | + * (c) Kevin Bond <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Zenstruck\Foundry\Tests\Integration\DataProvider; |
| 15 | + |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 17 | +use PHPUnit\Framework\Attributes\IgnoreDeprecations; |
| 18 | +use PHPUnit\Framework\Attributes\RequiresPhpunit; |
| 19 | +use PHPUnit\Framework\Attributes\RequiresPhpunitExtension; |
| 20 | +use PHPUnit\Framework\Attributes\Test; |
| 21 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 22 | +use Zenstruck\Foundry\Persistence\PersistentObjectFactory; |
| 23 | +use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory; |
| 24 | +use Zenstruck\Foundry\Persistence\Proxy; |
| 25 | +use Zenstruck\Foundry\PHPUnit\FoundryExtension; |
| 26 | +use Zenstruck\Foundry\Test\Factories; |
| 27 | +use Zenstruck\Foundry\Test\ResetDatabase; |
| 28 | +use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; |
| 29 | +use Zenstruck\Foundry\Tests\Fixture\Model\GenericModel; |
| 30 | + |
| 31 | +use function Zenstruck\Foundry\Persistence\unproxy; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Nicolas PHILIPPE <[email protected]> |
| 35 | + * @requires PHPUnit >=11.4 |
| 36 | + */ |
| 37 | +#[RequiresPhpunit('>=11.4')] |
| 38 | +#[RequiresPhpunitExtension(FoundryExtension::class)] |
| 39 | +#[IgnoreDeprecations] |
| 40 | +abstract class DataProviderWithPersistentFactoryInKernelTestCase extends KernelTestCase |
| 41 | +{ |
| 42 | + use Factories; |
| 43 | + use ResetDatabase; |
| 44 | + |
| 45 | + #[Test] |
| 46 | + #[DataProvider('createOneObjectInDataProvider')] |
| 47 | + public function assert_it_can_create_one_object_in_data_provider(?GenericModel $providedData): void |
| 48 | + { |
| 49 | + static::proxyFactory()::assert()->count(1); |
| 50 | + |
| 51 | + self::assertInstanceOf(Proxy::class, $providedData); |
| 52 | + self::assertNotInstanceOf(Proxy::class, unproxy($providedData)); // asserts two proxies are not nested |
| 53 | + self::assertSame('value set in data provider', $providedData->getProp1()); |
| 54 | + } |
| 55 | + |
| 56 | + public static function createOneObjectInDataProvider(): iterable |
| 57 | + { |
| 58 | + yield 'createOne()' => [ |
| 59 | + static::proxyFactory()::createOne(['prop1' => 'value set in data provider']), |
| 60 | + ]; |
| 61 | + |
| 62 | + yield 'create()' => [ |
| 63 | + static::proxyFactory()->create(['prop1' => 'value set in data provider']), |
| 64 | + ]; |
| 65 | + } |
| 66 | + |
| 67 | + #[Test] |
| 68 | + #[DataProvider('createMultipleObjectsInDataProvider')] |
| 69 | + public function assert_it_can_create_multiple_objects_in_data_provider(?array $providedData): void |
| 70 | + { |
| 71 | + self::assertIsArray($providedData); |
| 72 | + static::proxyFactory()::assert()->count(2); |
| 73 | + self::assertSame('prop 1', $providedData[0]->getProp1()); |
| 74 | + self::assertSame('prop 2', $providedData[1]->getProp1()); |
| 75 | + } |
| 76 | + |
| 77 | + public static function createMultipleObjectsInDataProvider(): iterable |
| 78 | + { |
| 79 | + yield 'createSequence()' => [ |
| 80 | + static::proxyFactory()::createSequence([ |
| 81 | + ['prop1' => 'prop 1'], |
| 82 | + ['prop1' => 'prop 2'], |
| 83 | + ]), |
| 84 | + ]; |
| 85 | + |
| 86 | + yield 'FactoryCollection::create()' => [ |
| 87 | + static::proxyFactory()->sequence([ |
| 88 | + ['prop1' => 'prop 1'], |
| 89 | + ['prop1' => 'prop 2'], |
| 90 | + ])->create(), |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + #[Test] |
| 95 | + #[DataProvider('useGetterOnProxyObjectCreatedInDataProvider')] |
| 96 | + public function assert_using_getter_proxy_object_created_in_a_data_provider_throws(?\Throwable $e): void |
| 97 | + { |
| 98 | + self::assertInstanceOf(\LogicException::class, $e); |
| 99 | + self::assertStringStartsWith('Cannot access to a persisted object from a data provider.', $e->getMessage()); |
| 100 | + } |
| 101 | + |
| 102 | + public static function useGetterOnProxyObjectCreatedInDataProvider(): iterable |
| 103 | + { |
| 104 | + try { |
| 105 | + static::proxyFactory()::createOne()->getProp1(); |
| 106 | + } catch (\Throwable $e) { |
| 107 | + } |
| 108 | + |
| 109 | + yield [$e ?? null]; |
| 110 | + } |
| 111 | + |
| 112 | + #[Test] |
| 113 | + #[DataProvider('throwsExceptionWhenCreatingObjectInDataProvider')] |
| 114 | + public function it_throws_when_creating_persisted_object_with_non_proxy_factory_in_data_provider(?\Throwable $e |
| 115 | + ): void { |
| 116 | + self::assertInstanceOf(\LogicException::class, $e); |
| 117 | + self::assertStringStartsWith( |
| 118 | + 'Cannot create object in a data provider for non-proxy factories.', |
| 119 | + $e->getMessage() |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + public static function throwsExceptionWhenCreatingObjectInDataProvider(): iterable |
| 124 | + { |
| 125 | + try { |
| 126 | + static::factory()::createOne(); |
| 127 | + } catch (\Throwable $e) { |
| 128 | + } |
| 129 | + |
| 130 | + yield [$e ?? null]; |
| 131 | + } |
| 132 | + |
| 133 | + #[Test] |
| 134 | + #[DataProvider('useGetterOnObjectCreatedInDataProvider')] |
| 135 | + public function assert_it_can_use_getter_on_non_persisted_object_created_in_data_provider( |
| 136 | + string $providedData, |
| 137 | + mixed $expectedData |
| 138 | + ): void { |
| 139 | + self::assertEquals($expectedData, unproxy($providedData)); |
| 140 | + } |
| 141 | + |
| 142 | + public static function useGetterOnObjectCreatedInDataProvider(): iterable |
| 143 | + { |
| 144 | + yield 'object factory' => [Object1Factory::createOne()->getProp1(), 'router-constructor']; |
| 145 | + yield 'persistent factory' => [static::factory()::new()->withoutPersisting()->create()->getProp1(), 'default1']; |
| 146 | + yield 'persistent factory using many' => [ |
| 147 | + static::factory()::new()->withoutPersisting()->many(1)->create()[0]->getProp1(), |
| 148 | + 'default1' |
| 149 | + ]; |
| 150 | + yield 'proxy factory' => [static::proxyFactory()::new()->withoutPersisting()->create()->getProp1(), 'default1']; |
| 151 | + yield 'proxy factory using many' => [ |
| 152 | + static::proxyFactory()::new()->withoutPersisting()->many(1)->create()[0]->getProp1(), |
| 153 | + 'default1' |
| 154 | + ]; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @return PersistentProxyObjectFactory<GenericModel> |
| 159 | + */ |
| 160 | + abstract protected static function proxyFactory(): PersistentProxyObjectFactory; |
| 161 | + |
| 162 | + /** |
| 163 | + * @return PersistentObjectFactory<GenericModel> |
| 164 | + */ |
| 165 | + abstract protected static function factory(): PersistentObjectFactory; |
| 166 | +} |
0 commit comments