-
Notifications
You must be signed in to change notification settings - Fork 23
Add new assertions #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
TavoNiievez
wants to merge
1
commit into
Codeception:main
Choose a base branch
from
TavoNiievez:new_asserts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add new assertions #218
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,14 +4,23 @@ | |
|
|
||
| namespace Codeception\Module\Symfony; | ||
|
|
||
| use Doctrine\DBAL\Connection; | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| use Doctrine\ORM\EntityRepository; | ||
| use Doctrine\ORM\Tools\SchemaValidator; | ||
| use Doctrine\Persistence\ManagerRegistry; | ||
| use PHPUnit\Framework\Assert; | ||
| use Throwable; | ||
|
|
||
| use function implode; | ||
| use function interface_exists; | ||
| use function is_dir; | ||
| use function is_object; | ||
| use function is_subclass_of; | ||
| use function is_writable; | ||
| use function json_encode; | ||
| use function sprintf; | ||
| use function trim; | ||
|
|
||
| trait DoctrineAssertionsTrait | ||
| { | ||
|
|
@@ -97,4 +106,203 @@ public function seeNumRecords(int $expectedNum, string $className, array $criter | |
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that Doctrine can connect to a database. | ||
| * | ||
| * The argument is treated as a connection name first. | ||
| * If no connection with that name exists, the method falls back to | ||
| * resolving it as an entity manager name and uses that manager's connection. | ||
| * | ||
| * ```php | ||
| * <?php | ||
| * $I->seeDoctrineDatabaseIsUp(); | ||
| * $I->seeDoctrineDatabaseIsUp('custom'); | ||
| * ``` | ||
| * | ||
| * @param non-empty-string $connectionName The name of the Doctrine connection to check. | ||
| */ | ||
| public function seeDoctrineDatabaseIsUp(string $connectionName = 'default'): void | ||
| { | ||
| try { | ||
| $connection = $this->resolveDoctrineConnection($connectionName); | ||
|
|
||
| $connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL()); | ||
| } catch (Throwable $e) { | ||
| Assert::fail(sprintf('Doctrine connection/entity manager "%s" failed: %s', $connectionName, $e->getMessage())); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that the Doctrine mapping is valid and the DB schema is in sync for one entity manager. | ||
| * Programmatic equivalent of `bin/console doctrine:schema:validate`. | ||
| * | ||
| * ```php | ||
| * <?php | ||
| * $I->seeDoctrineSchemaIsValid(); | ||
| * $I->seeDoctrineSchemaIsValid('custom'); | ||
| * ``` | ||
| * | ||
| * @param non-empty-string $entityManagerName | ||
| */ | ||
| public function seeDoctrineSchemaIsValid(string $entityManagerName = 'default'): void | ||
| { | ||
| try { | ||
| $em = $this->resolveEntityManager($entityManagerName); | ||
| $validator = new SchemaValidator($em); | ||
| $errors = $validator->validateMapping(); | ||
| $errorMessages = []; | ||
| foreach ($errors as $className => $classErrors) { | ||
| $errorMessages[] = sprintf(' - %s: %s', $className, implode('; ', $classErrors)); | ||
| } | ||
| $this->assertEmpty( | ||
| $errors, | ||
| sprintf( | ||
| "The Doctrine mapping is invalid for the '%s' entity manager:\n%s", | ||
| $entityManagerName, | ||
| implode("\n", $errorMessages) | ||
| ) | ||
| ); | ||
|
|
||
| if (!$validator->schemaInSyncWithMetadata()) { | ||
| Assert::fail( | ||
| sprintf( | ||
| 'The database schema is not in sync with the current mapping for the "%s" entity manager. Generate and run a new migration.', | ||
| $entityManagerName | ||
| ) | ||
| ); | ||
| } | ||
| } catch (Throwable $e) { | ||
| Assert::fail( | ||
| sprintf('Could not validate Doctrine schema for the "%s" entity manager: %s', $entityManagerName, $e->getMessage()) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that Doctrine proxy directory is writable for a given entity manager. | ||
| * | ||
| * This assertion is only meaningful when Doctrine's legacy proxy system is in use. | ||
| * If no proxy dir is configured (for example with native lazy objects), | ||
| * the assertion is skipped. | ||
| * | ||
| * ```php | ||
| * <?php | ||
| * $I->seeDoctrineProxyDirIsWritable(); | ||
| * $I->seeDoctrineProxyDirIsWritable('custom'); | ||
| * ``` | ||
| */ | ||
| public function seeDoctrineProxyDirIsWritable(string $entityManagerName = 'default'): void | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you aware that Doctrine is moving away from their own proxy system, in favor of the new native PHP lazy objects? So (as far as I understand it), the proxy dir soon won't be needed anymore. |
||
| { | ||
| $em = $this->resolveEntityManager($entityManagerName); | ||
| $proxyDir = $em->getConfiguration()->getProxyDir(); | ||
|
|
||
| if ($proxyDir === null || trim($proxyDir) === '') { | ||
| Assert::markTestSkipped( | ||
| sprintf( | ||
| 'Doctrine proxy dir is not configured for EM "%s". This can be expected when native lazy objects are used.', | ||
| $entityManagerName | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| $this->assertTrue(is_dir($proxyDir), sprintf('Doctrine proxy dir does not exist: %s', $proxyDir)); | ||
| $this->assertTrue(is_writable($proxyDir), sprintf('Doctrine proxy dir is not writable: %s', $proxyDir)); | ||
| } | ||
|
|
||
| private function resolveDoctrineConnection(string $connectionOrEntityManagerName): Connection | ||
| { | ||
| $doctrine = $this->resolveManagerRegistry(); | ||
| if ($doctrine instanceof ManagerRegistry) { | ||
| try { | ||
| $connection = $doctrine->getConnection($connectionOrEntityManagerName); | ||
| if ($connection instanceof Connection) { | ||
| return $connection; | ||
| } | ||
|
|
||
| Assert::fail( | ||
| sprintf( | ||
| 'Doctrine connection "%s" is not an instance of %s.', | ||
| $connectionOrEntityManagerName, | ||
| Connection::class | ||
| ) | ||
| ); | ||
| } catch (Throwable) { | ||
| $manager = $doctrine->getManager($connectionOrEntityManagerName); | ||
| if ($manager instanceof EntityManagerInterface) { | ||
| return $manager->getConnection(); | ||
| } | ||
|
|
||
| Assert::fail( | ||
| sprintf( | ||
| 'Doctrine manager "%s" is not an instance of %s.', | ||
| $connectionOrEntityManagerName, | ||
| EntityManagerInterface::class | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if ($connectionOrEntityManagerName !== 'default') { | ||
| Assert::fail( | ||
| sprintf( | ||
| 'Cannot resolve Doctrine connection/entity manager "%s" without the "doctrine" ManagerRegistry service.', | ||
| $connectionOrEntityManagerName | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return $this->_getEntityManager()->getConnection(); | ||
| } | ||
|
|
||
| private function resolveEntityManager(string $entityManagerName): EntityManagerInterface | ||
| { | ||
| $doctrine = $this->resolveManagerRegistry(); | ||
| if ($doctrine instanceof ManagerRegistry) { | ||
| $manager = $doctrine->getManager($entityManagerName); | ||
| if ($manager instanceof EntityManagerInterface) { | ||
| return $manager; | ||
| } | ||
|
|
||
| Assert::fail( | ||
| sprintf( | ||
| 'Doctrine manager "%s" is not an instance of %s.', | ||
| $entityManagerName, | ||
| EntityManagerInterface::class | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| if ($entityManagerName !== 'default') { | ||
| Assert::fail( | ||
| sprintf( | ||
| 'Cannot resolve Doctrine entity manager "%s" without the "doctrine" ManagerRegistry service.', | ||
| $entityManagerName | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return $this->_getEntityManager(); | ||
| } | ||
|
|
||
| private function resolveManagerRegistry(): ?ManagerRegistry | ||
| { | ||
| $container = $this->_getContainer(); | ||
|
|
||
| if ($container->has('doctrine')) { | ||
| $service = $container->get('doctrine'); | ||
| if ($service instanceof ManagerRegistry) { | ||
| return $service; | ||
| } | ||
| } | ||
|
|
||
| if ($container->has(ManagerRegistry::class)) { | ||
| $service = $container->get(ManagerRegistry::class); | ||
| if ($service instanceof ManagerRegistry) { | ||
| return $service; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean the name of the entity manager (in case you have multiple)?