forked from zenstruck/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataProviderWithInMemoryTest.php
More file actions
128 lines (105 loc) · 4.39 KB
/
DataProviderWithInMemoryTest.php
File metadata and controls
128 lines (105 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
declare(strict_types=1);
/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zenstruck\Foundry\Tests\Integration\DataProvider;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RequiresMethod;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Foundry\Attribute\ResetDatabase;
use Zenstruck\Foundry\InMemory\AsInMemoryTest;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
use Zenstruck\Foundry\Persistence\ProxyGenerator;
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
use Zenstruck\Foundry\Tests\Fixture\Entity\Contact;
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory;
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ProxyContactFactory;
use Zenstruck\Foundry\Tests\Fixture\InMemory\InMemoryContactRepository;
use Zenstruck\Foundry\Tests\Fixture\TestKernel;
use Zenstruck\Foundry\Tests\Integration\RequiresORM;
/**
* @author Nicolas PHILIPPE <nikophil@gmail.com>
* @requires PHPUnit >=11.4
*/
#[RequiresPhpunit('>=11.4')]
#[RequiresPhpunitExtension(FoundryExtension::class)]
#[ResetDatabase]
final class DataProviderWithInMemoryTest extends KernelTestCase
{
use RequiresORM; // needed to use the entity manager
private InMemoryContactRepository $contactRepository;
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
$this->contactRepository = self::getContainer()->get(InMemoryContactRepository::class); // @phpstan-ignore assign.propertyType
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class); // @phpstan-ignore assign.propertyType
}
/**
* @param PersistentObjectFactory<Contact> $factory
*/
#[Test]
#[DataProvider('provideContactFactory')]
#[AsInMemoryTest]
#[IgnoreDeprecations]
public function it_can_create_in_memory_factory_in_data_provider(PersistentObjectFactory $factory): void
{
if ('1' !== ($_ENV['USE_FOUNDRY_PHPUNIT_EXTENSION'] ?? null)) {
self::markTestSkipped('Needs Foundry PHPUnit extension.');
}
$contact = $factory->create();
if (TestKernel::canUseLegacyProxy()) {
self::assertSame([ProxyGenerator::unwrap($contact)], $this->contactRepository->_all());
} else {
self::assertSame([$contact], $this->contactRepository->_all());
}
self::assertSame(0, $this->entityManager->getRepository(Contact::class)->count([]));
}
public static function provideContactFactory(): iterable
{
yield [ContactFactory::new()];
if (TestKernel::canUseLegacyProxy()) {
yield [ProxyContactFactory::new()]; // @phpstan-ignore argument.type
}
}
#[Test]
#[DataProvider('provideContact')]
#[AsInMemoryTest]
#[RequiresPhp('^8.4')]
public function it_can_create_in_memory_objects_in_data_provider(?Contact $contact = null): void
{
self::assertInstanceOf(Contact::class, $contact);
self::assertSame([$contact], $this->contactRepository->_all());
self::assertSame(0, $this->entityManager->getRepository(Contact::class)->count());
}
public static function provideContact(): iterable
{
yield [ContactFactory::createOne()];
}
#[Test]
#[DataProvider('provideContactWithLegacyProxy')]
#[AsInMemoryTest]
#[RequiresMethod(\Symfony\Component\VarExporter\LazyProxyTrait::class, 'createLazyProxy')]
#[IgnoreDeprecations]
public function it_can_create_in_memory_objects_in_data_provider_with_legacy_proxy(?Contact $contact = null): void
{
self::assertInstanceOf(Contact::class, $contact);
self::assertSame([ProxyGenerator::unwrap($contact)], $this->contactRepository->_all());
self::assertSame(0, $this->entityManager->getRepository(Contact::class)->count());
}
public static function provideContactWithLegacyProxy(): iterable
{
yield [ProxyContactFactory::createOne()];
}
}