Skip to content

Commit c350ee2

Browse files
committed
Add new assertions
1 parent e4cdf95 commit c350ee2

6 files changed

Lines changed: 599 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
/composer.lock
44
/framework-tests
55
/.php-cs-fixer.cache
6-
var/
6+
var/

src/Codeception/Module/Symfony.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Codeception\Module\Symfony\DataCollectorName;
1717
use Codeception\Module\Symfony\DoctrineAssertionsTrait;
1818
use Codeception\Module\Symfony\DomCrawlerAssertionsTrait;
19+
use Codeception\Module\Symfony\EnvironmentAssertionsTrait;
1920
use Codeception\Module\Symfony\EventsAssertionsTrait;
2021
use Codeception\Module\Symfony\FormAssertionsTrait;
2122
use Codeception\Module\Symfony\HttpClientAssertionsTrait;
@@ -143,6 +144,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
143144
use ConsoleAssertionsTrait;
144145
use DoctrineAssertionsTrait;
145146
use DomCrawlerAssertionsTrait;
147+
use EnvironmentAssertionsTrait;
146148
use EventsAssertionsTrait;
147149
use FormAssertionsTrait;
148150
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

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

0 commit comments

Comments
 (0)