LazyRouteCollectionTest.php in Drupal 10
File
core/tests/Drupal/Tests/Core/Routing/LazyRouteCollectionTest.php
View source
<?php
namespace Drupal\Tests\Core\Routing;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Routing\LazyRouteCollection;
use Drupal\Core\Routing\RouteProviderInterface;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Route;
class LazyRouteCollectionTest extends UnitTestCase {
private $routeProvider;
private $testRoutes;
protected function setUp() : void {
parent::setUp();
$this->routeProvider = $this
->createMock(RouteProviderInterface::class);
$this->testRoutes = [
'route_1' => new Route('/route-1'),
'route_2' => new Route('/route-2'),
];
}
public function testGetIterator() {
$this->routeProvider
->expects($this
->exactly(2))
->method('getRoutesByNames')
->with(NULL)
->will($this
->returnValue($this->testRoutes));
$lazyRouteCollection = new LazyRouteCollection($this->routeProvider);
$this
->assertEquals($this->testRoutes, (array) $lazyRouteCollection
->getIterator());
$this
->assertEquals($this->testRoutes, $lazyRouteCollection
->all());
}
public function testCount() {
$this->routeProvider
->method('getRoutesByNames')
->with(NULL)
->will($this
->returnValue($this->testRoutes));
$lazyRouteCollection = new LazyRouteCollection($this->routeProvider);
$this
->assertEquals(2, $lazyRouteCollection
->count());
}
public function testGetName() {
$this->routeProvider
->method('getRouteByName')
->with('route_1')
->will($this
->returnValue($this->testRoutes['route_1']));
$lazyRouteCollection = new LazyRouteCollection($this->routeProvider);
$this
->assertEquals($lazyRouteCollection
->get('route_1'), $this->testRoutes['route_1']);
$this->routeProvider
->method('getRouteByName')
->with('does_not_exist')
->will($this
->throwException(new RouteNotFoundException()));
$lazyRouteCollectionFail = new LazyRouteCollection($this->routeProvider);
$this
->assertNull($lazyRouteCollectionFail
->get('does_not_exist'));
}
}