-
Notifications
You must be signed in to change notification settings - Fork 24
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
/vendor/ | ||
/composer.lock | ||
/framework-tests | ||
/.php-cs-fixer.cache | ||
/.php-cs-fixer.cache |
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,12 +4,21 @@ | |
|
||
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_string; | ||
use function is_subclass_of; | ||
use function is_writable; | ||
use function json_encode; | ||
use function sprintf; | ||
|
||
|
@@ -107,4 +116,99 @@ public function seeNumRecords(int $expectedNum, string $className, array $criter | |
) | ||
); | ||
} | ||
|
||
/** | ||
* Asserts that Doctrine can connect to a database. | ||
* | ||
* ```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 { | ||
/** @var ManagerRegistry $doctrine */ | ||
$doctrine = $this->grabService('doctrine'); | ||
/** @var Connection $connection */ | ||
$connection = $doctrine->getConnection($connectionName); | ||
$connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL()); | ||
} catch (Throwable $e) { | ||
Assert::fail(sprintf('Doctrine connection "%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 { | ||
/** @var ManagerRegistry $doctrine */ | ||
$doctrine = $this->grabService('doctrine'); | ||
/** @var EntityManagerInterface $em */ | ||
$em = $doctrine->getManager($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. | ||
* | ||
* ```php | ||
* <?php | ||
* $I->seeDoctrineProxyDirIsWritable(); | ||
* $I->seeDoctrineProxyDirIsWritable('custom'); | ||
* ``` | ||
*/ | ||
public function seeDoctrineProxyDirIsWritable(string $entityManagerName = 'default'): void | ||
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. |
||
{ | ||
/** @var ManagerRegistry $doctrine */ | ||
$doctrine = $this->grabService('doctrine'); | ||
/** @var EntityManagerInterface $em */ | ||
$em = $doctrine->getManager($entityManagerName); | ||
$proxyDir = $em->getConfiguration()->getProxyDir(); | ||
|
||
$this->assertIsString($proxyDir, sprintf('Doctrine proxy dir is not configured for EM "%s".', $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)); | ||
} | ||
} |
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)?