Skip to content

Commit 1e515ac

Browse files
committed
Add new assertions
1 parent a516dc6 commit 1e515ac

File tree

6 files changed

+602
-2
lines changed

6 files changed

+602
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/vendor/
33
/composer.lock
44
/framework-tests
5-
/.php-cs-fixer.cache
5+
/.php-cs-fixer.cache

src/Codeception/Module/Symfony.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Codeception\Module\Symfony\DataCollectorName;
1616
use Codeception\Module\Symfony\DoctrineAssertionsTrait;
1717
use Codeception\Module\Symfony\DomCrawlerAssertionsTrait;
18+
use Codeception\Module\Symfony\EnvironmentAssertionsTrait;
1819
use Codeception\Module\Symfony\EventsAssertionsTrait;
1920
use Codeception\Module\Symfony\FormAssertionsTrait;
2021
use Codeception\Module\Symfony\HttpClientAssertionsTrait;
@@ -62,10 +63,10 @@
6263
use function class_exists;
6364
use function codecept_root_dir;
6465
use function count;
66+
use function extension_loaded;
6567
use function file_exists;
6668
use function implode;
6769
use function in_array;
68-
use function extension_loaded;
6970
use function ini_get;
7071
use function ini_set;
7172
use function is_object;
@@ -150,6 +151,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
150151
use ConsoleAssertionsTrait;
151152
use DoctrineAssertionsTrait;
152153
use DomCrawlerAssertionsTrait;
154+
use EnvironmentAssertionsTrait;
153155
use EventsAssertionsTrait;
154156
use FormAssertionsTrait;
155157
use HttpClientAssertionsTrait;

src/Codeception/Module/Symfony/DoctrineAssertionsTrait.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44

55
namespace Codeception\Module\Symfony;
66

7+
use Doctrine\DBAL\Connection;
8+
use Doctrine\ORM\EntityManagerInterface;
79
use Doctrine\ORM\EntityRepository;
10+
use Doctrine\ORM\Tools\SchemaValidator;
11+
use Doctrine\Persistence\ManagerRegistry;
812
use PHPUnit\Framework\Assert;
13+
use Throwable;
914

15+
use function implode;
1016
use function interface_exists;
17+
use function is_dir;
1118
use function is_object;
19+
use function is_string;
1220
use function is_subclass_of;
21+
use function is_writable;
1322
use function json_encode;
1423
use function sprintf;
1524

@@ -107,4 +116,99 @@ public function seeNumRecords(int $expectedNum, string $className, array $criter
107116
)
108117
);
109118
}
119+
120+
/**
121+
* Asserts that Doctrine can connect to a database.
122+
*
123+
* ```php
124+
* <?php
125+
* $I->seeDoctrineDatabaseIsUp();
126+
* $I->seeDoctrineDatabaseIsUp('custom');
127+
* ```
128+
*
129+
* @param non-empty-string $connectionName The name of the Doctrine connection to check.
130+
*/
131+
public function seeDoctrineDatabaseIsUp(string $connectionName = 'default'): void
132+
{
133+
try {
134+
/** @var ManagerRegistry $doctrine */
135+
$doctrine = $this->grabService('doctrine');
136+
/** @var Connection $connection */
137+
$connection = $doctrine->getConnection($connectionName);
138+
$connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL());
139+
} catch (Throwable $e) {
140+
Assert::fail(sprintf('Doctrine connection "%s" failed: %s', $connectionName, $e->getMessage()));
141+
}
142+
}
143+
144+
/**
145+
* Asserts that the Doctrine mapping is valid and the DB schema is in sync for one entity manager.
146+
* Programmatic equivalent of `bin/console doctrine:schema:validate`.
147+
*
148+
* ```php
149+
* <?php
150+
* $I->seeDoctrineSchemaIsValid();
151+
* $I->seeDoctrineSchemaIsValid('custom');
152+
* ```
153+
*
154+
* @param non-empty-string $entityManagerName
155+
*/
156+
public function seeDoctrineSchemaIsValid(string $entityManagerName = 'default'): void
157+
{
158+
try {
159+
/** @var ManagerRegistry $doctrine */
160+
$doctrine = $this->grabService('doctrine');
161+
/** @var EntityManagerInterface $em */
162+
$em = $doctrine->getManager($entityManagerName);
163+
$validator = new SchemaValidator($em);
164+
$errors = $validator->validateMapping();
165+
$errorMessages = [];
166+
foreach ($errors as $className => $classErrors) {
167+
$errorMessages[] = sprintf(' - %s: %s', $className, implode('; ', $classErrors));
168+
}
169+
$this->assertEmpty(
170+
$errors,
171+
sprintf(
172+
"The Doctrine mapping is invalid for the '%s' entity manager:\n%s",
173+
$entityManagerName,
174+
implode("\n", $errorMessages)
175+
)
176+
);
177+
178+
if (!$validator->schemaInSyncWithMetadata()) {
179+
Assert::fail(
180+
sprintf(
181+
'The database schema is not in sync with the current mapping for the "%s" entity manager. Generate and run a new migration.',
182+
$entityManagerName
183+
)
184+
);
185+
}
186+
} catch (Throwable $e) {
187+
Assert::fail(
188+
sprintf('Could not validate Doctrine schema for the "%s" entity manager: %s', $entityManagerName, $e->getMessage())
189+
);
190+
}
191+
}
192+
193+
/**
194+
* Asserts that Doctrine proxy directory is writable for a given entity manager.
195+
*
196+
* ```php
197+
* <?php
198+
* $I->seeDoctrineProxyDirIsWritable();
199+
* $I->seeDoctrineProxyDirIsWritable('custom');
200+
* ```
201+
*/
202+
public function seeDoctrineProxyDirIsWritable(string $entityManagerName = 'default'): void
203+
{
204+
/** @var ManagerRegistry $doctrine */
205+
$doctrine = $this->grabService('doctrine');
206+
/** @var EntityManagerInterface $em */
207+
$em = $doctrine->getManager($entityManagerName);
208+
$proxyDir = $em->getConfiguration()->getProxyDir();
209+
210+
$this->assertIsString($proxyDir, sprintf('Doctrine proxy dir is not configured for EM "%s".', $entityManagerName));
211+
$this->assertTrue(is_dir($proxyDir), sprintf('Doctrine proxy dir does not exist: %s', $proxyDir));
212+
$this->assertTrue(is_writable($proxyDir), sprintf('Doctrine proxy dir is not writable: %s', $proxyDir));
213+
}
110214
}

0 commit comments

Comments
 (0)