-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
ORMPurgerExcludeTest.php
97 lines (76 loc) · 2.69 KB
/
ORMPurgerExcludeTest.php
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
<?php
declare(strict_types=1);
namespace Doctrine\Tests\Common\DataFixtures;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\Common\DataFixtures\TestPurgeEntity\ExcludedEntity;
use Doctrine\Tests\Common\DataFixtures\TestPurgeEntity\IncludedEntity;
use function count;
use function preg_match;
class ORMPurgerExcludeTest extends BaseTestCase
{
public const TEST_ENTITY_INCLUDED = IncludedEntity::class;
public const TEST_ENTITY_EXCLUDED = ExcludedEntity::class;
/**
* Loads test data
*/
protected function loadTestData(): EntityManager
{
$em = $this->getMockSqliteEntityManager('TestPurgeEntity');
$schemaTool = new SchemaTool($em);
$schemaTool->dropDatabase();
$schemaTool->createSchema([
$em->getClassMetadata(self::TEST_ENTITY_INCLUDED),
$em->getClassMetadata(self::TEST_ENTITY_EXCLUDED),
]);
$entity = new ExcludedEntity();
$entity->setId(1);
$em->persist($entity);
$entity = new IncludedEntity();
$entity->setId(1);
$em->persist($entity);
$em->flush();
return $em;
}
/**
* Execute test purge
*
* @param string[] $list
*/
private function executeTestPurge(array $list, ?callable $filter): void
{
$em = $this->loadTestData();
$excludedRepository = $em->getRepository(self::TEST_ENTITY_EXCLUDED);
$includedRepository = $em->getRepository(self::TEST_ENTITY_INCLUDED);
$excluded = $excludedRepository->findAll();
$included = $includedRepository->findAll();
$this->assertGreaterThan(0, count($included));
$this->assertGreaterThan(0, count($excluded));
$connection = $em->getConnection();
$configuration = $connection->getConfiguration();
if ($filter !== null) {
$configuration->setSchemaAssetsFilter($filter);
}
$purger = new ORMPurger($em, $list);
$purger->purge();
$excluded = $excludedRepository->findAll();
$included = $includedRepository->findAll();
$this->assertCount(0, $included);
$this->assertGreaterThan(0, count($excluded));
}
/**
* Test for purge exclusion usig explicit exclution list.
*/
public function testPurgeExcludeUsingList(): void
{
$this->executeTestPurge(['ExcludedEntity'], null);
}
public function testPurgeExcludeUsingFilterCallable(): void
{
$this->executeTestPurge(
[],
static fn (string $table): bool => (bool) preg_match('~^(?!ExcludedEntity)~', $table),
);
}
}