symfony-cmf/testing
Testing utilities for Symfony CMF projects, providing helpers and base classes to simplify PHPUnit-based integration and functional tests. Speeds up setting up test environments and common CMF scenarios with reusable tooling.
Installation Add the package via Composer:
composer require symfony-cmf/testing
Ensure your project uses Symfony CMF (e.g., symfony-cmf/core-bundle).
First Use Case Extend the provided base test classes for CMF-specific testing. Example:
use Symfony\CMF\Testing\WebTestCase;
class MyRouteTest extends WebTestCase
{
public function testRouteGeneration()
{
$this->assertRouteExists('/some-route');
}
}
WebTestCase for HTTP/functional tests.assertRouteExists() to validate CMF route generation.Where to Look First
src/Resources/Test/ in the package for abstract test classes.README.md for bundle-specific helpers (e.g., RouteTestTrait, ContentTestTrait).tests/ in the package for real-world usage patterns.Functional Testing with CMF Routes
use Symfony\CMF\Testing\RouteTestTrait;
class MyControllerTest extends WebTestCase
{
use RouteTestTrait;
public function testDynamicRoute()
{
$this->assertRouteMatches(
'/blog/{slug}',
['slug' => 'test-post'],
['_controller' => 'App\Controller\BlogController::show']
);
}
}
RouteTestTrait to validate route parameters, controllers, and requirements.Content Repository Testing
use Symfony\CMF\Testing\ContentTestTrait;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
class ContentFixtureTest extends WebTestCase
{
use ContentTestTrait;
protected function setUp(): void
{
$this->loadFixtures([MyContentFixture::class]);
$this->purgeDatabase(); // Uses ORMPurger by default
}
public function testContentPersistence()
{
$content = $this->getContent('blog_post');
$this->assertEquals('Hello World', $content->getTitle());
}
}
ContentTestTrait for fixture loading, repository queries, and assertions.purgeDatabase() to customize the purger (e.g., for MongoDB).Integration with Symfony’s Test Client
public function testContentRouting()
{
$client = static::createClient();
$crawler = $client->request('GET', '/blog/test-post');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertSelectorTextContains('h1', 'Test Post');
}
createClient() with CMF-specific assertions (e.g., assertSelectorTextContains for Doctrine PHPCR-ODM or Doctrine ORM).Testing Route Providers
use Symfony\CMF\Testing\RouteProviderTestTrait;
class CustomRouteProviderTest extends WebTestCase
{
use RouteProviderTestTrait;
public function testProviderRoutes()
{
$this->assertRouteProviderGenerates(
'app.route_provider',
['_route' => 'blog_show', 'slug' => 'test']
);
}
}
RouteProviderTestTrait to test dynamic route generation from providers.loadFixtures() to pre-populate CMF content (e.g., BlogPost, Page entities).WebTestCase to modify kernel configuration for tests:
protected static function getKernelClass()
{
return TestKernel::class; // Custom kernel with test config
}
protected function getKernelParameters()
{
return array_merge(parent::getKernelParameters(), [
'kernel.test' => true,
'cmf.test_mode' => true,
]);
}
PHPCRTestTrait to manage sessions:
use Symfony\CMF\Testing\PHPCRTestTrait;
class PHPCRContentTest extends WebTestCase
{
use PHPCRTestTrait;
public function testNodePersistence()
{
$this->assertNodeExists('/site/blog');
}
}
Database State Management
purgeDatabase() or clearPHPCR() before tests:
protected function tearDown(): void
{
$this->clearPHPCR(); // For PHPCR-ODM
parent::tearDown();
}
protected function setUp(): void
{
$this->beginTransaction();
}
protected function tearDown(): void
{
$this->rollBackTransaction();
}
Route Caching
public function setUp(): void
{
$this->clearCache();
}
debug:router in tests to inspect routes:
$this->assertContains('blog_show', $this->getRouteNames());
Content Loading Order
NotFoundException errors.dependsOn:
$loader->load([AuthorFixture::class, BlogPostFixture::class]);
PHPCR Session Handling
PHPCRTestTrait to manage sessions:
public function testSessionCleanup()
{
$this->assertSessionIsOpen();
// Test logic
$this->closeSession();
}
phpunit.xml:
<php>
<env name="APP_ENV" value="test"/>
<env name="APP_DEBUG" value="1"/>
</php>
$this->log->debug('Test debug message');
$this->dump($content); // Uses Symfony\VarDumper\Cloner\VarCloner
Custom Assertions Extend base classes to add domain-specific assertions:
trait MyCustomAssertions
{
protected function assertContentHasRoute(CMFContentInterface $content, string $expectedRoute)
{
$this->assertEquals($expectedRoute, $content->getRoute());
}
}
Test Data Factories Create reusable factories for CMF entities:
class BlogPostFactory
{
public static function create(array $data = []): BlogPost
{
return (new BlogPost())
->setTitle($data['title'] ?? 'Default Title')
->setSlug($data['slug'] ?? 'default-slug');
}
}
Custom Test Traits Build traits for project-specific testing needs:
trait MenuTestTrait
{
protected function assertMenuItemExists(string $path, string $label)
{
$client = static::createClient();
$crawler = $client->request('GET', $path);
$this->assertSelectorTextContains('a', $label);
}
}
Overriding Base Classes
Subclass WebTestCase to enforce project-wide testing standards:
abstract class ProjectTestCase extends WebTestCase
{
protected function setUp(): void
{
$this->enableProfiler();
parent::setUp();
}
}
cmf_routing and cmf_core bundles are properly configured in config/packages/cmf.yaml for tests.# config/packages/test/cmf.yaml
cmf_routing:
dynamic_routes:
app.route_provider:
enabled: true
other_provider:
enabled: false
assertContentType() to validate CMF content types:
How can I help you explore Laravel packages today?