|
4 | 4 |
|
5 | 5 | namespace Codeception\Module\Symfony;
|
6 | 6 |
|
| 7 | +use Doctrine\DBAL\Connection; |
| 8 | +use Doctrine\ORM\EntityManagerInterface; |
7 | 9 | use Doctrine\ORM\EntityRepository;
|
| 10 | +use Doctrine\ORM\Tools\SchemaValidator; |
| 11 | +use Doctrine\Persistence\ManagerRegistry; |
8 | 12 | use PHPUnit\Framework\Assert;
|
| 13 | +use Throwable; |
9 | 14 |
|
| 15 | +use function implode; |
10 | 16 | use function interface_exists;
|
| 17 | +use function is_dir; |
11 | 18 | use function is_object;
|
| 19 | +use function is_string; |
12 | 20 | use function is_subclass_of;
|
| 21 | +use function is_writable; |
13 | 22 | use function json_encode;
|
14 | 23 | use function sprintf;
|
15 | 24 |
|
@@ -107,4 +116,99 @@ public function seeNumRecords(int $expectedNum, string $className, array $criter
|
107 | 116 | )
|
108 | 117 | );
|
109 | 118 | }
|
| 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 | + } |
110 | 214 | }
|
0 commit comments