Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/vendor/
/composer.lock
/framework-tests
/.php-cs-fixer.cache
/.php-cs-fixer.cache
4 changes: 3 additions & 1 deletion src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Codeception\Module\Symfony\DataCollectorName;
use Codeception\Module\Symfony\DoctrineAssertionsTrait;
use Codeception\Module\Symfony\DomCrawlerAssertionsTrait;
use Codeception\Module\Symfony\EnvironmentAssertionsTrait;
use Codeception\Module\Symfony\EventsAssertionsTrait;
use Codeception\Module\Symfony\FormAssertionsTrait;
use Codeception\Module\Symfony\HttpClientAssertionsTrait;
Expand Down Expand Up @@ -62,10 +63,10 @@
use function class_exists;
use function codecept_root_dir;
use function count;
use function extension_loaded;
use function file_exists;
use function implode;
use function in_array;
use function extension_loaded;
use function ini_get;
use function ini_set;
use function is_object;
Expand Down Expand Up @@ -150,6 +151,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
use ConsoleAssertionsTrait;
use DoctrineAssertionsTrait;
use DomCrawlerAssertionsTrait;
use EnvironmentAssertionsTrait;
use EventsAssertionsTrait;
use FormAssertionsTrait;
use HttpClientAssertionsTrait;
Expand Down
104 changes: 104 additions & 0 deletions src/Codeception/Module/Symfony/DoctrineAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Copy link
Member

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)?

*/
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
Copy link
Member

Choose a reason for hiding this comment

The 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));
}
}
Loading