forked from contao/contao
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
916 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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
130 changes: 130 additions & 0 deletions
130
core-bundle/tests/Routing/Content/ArticleUrlResolverTest.php
This file contains 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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace Contao\CoreBundle\Tests\Routing\Content; | ||
|
||
use Contao\ArticleModel; | ||
use Contao\CoreBundle\Routing\Content\ArticleUrlResolver; | ||
use Contao\CoreBundle\Routing\Content\ContentRoute; | ||
use Contao\CoreBundle\Tests\TestCase; | ||
use Contao\PageModel; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
|
||
class ArticleUrlResolverTest extends TestCase | ||
{ | ||
/** | ||
* @var ArticleUrlResolver | ||
*/ | ||
private $resolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->resolver = new ArticleUrlResolver(); | ||
} | ||
|
||
public function testSupportsArticles(): void | ||
{ | ||
$this->assertTrue($this->resolver->supportsContent($this->mockArticle())); | ||
$this->assertFalse($this->resolver->supportsContent($this->mockClassWithProperties(PageModel::class))); | ||
} | ||
|
||
public function testCreatesParameterdContentRouteForArticle(): void | ||
{ | ||
$page = $this->mockPage(); | ||
$article = $this->mockArticle(['alias' => 'foobar']); | ||
|
||
$article | ||
->expects($this->once()) | ||
->method('getRelated') | ||
->with('pid') | ||
->willReturn($page) | ||
; | ||
|
||
/** @var ContentRoute $route */ | ||
$route = $this->resolver->resolveContent($article); | ||
|
||
$this->assertInstanceOf(ContentRoute::class, $route); | ||
$this->assertSame($page, $route->getPage()); | ||
$this->assertSame('/foo/bar{parameters}.baz', $route->getPath()); | ||
$this->assertSame('/articles/foobar', $route->getDefault('parameters')); | ||
} | ||
|
||
|
||
|
||
public function testCreatesParameterdContentRouteWithIdIfArticleHasNoAlias(): void | ||
{ | ||
$page = $this->mockPage(); | ||
$article = $this->mockArticle(['id' => 17, 'alias' => '']); | ||
|
||
$article | ||
->expects($this->once()) | ||
->method('getRelated') | ||
->with('pid') | ||
->willReturn($page) | ||
; | ||
|
||
/** @var ContentRoute $route */ | ||
$route = $this->resolver->resolveContent($article); | ||
|
||
$this->assertInstanceOf(ContentRoute::class, $route); | ||
$this->assertSame($page, $route->getPage()); | ||
$this->assertSame('/foo/bar{parameters}.baz', $route->getPath()); | ||
$this->assertSame('/articles/17', $route->getDefault('parameters')); | ||
} | ||
|
||
public function testThrowsExceptionIfPageIsNotFound(): void | ||
{ | ||
$article = $this->mockArticle(); | ||
|
||
$article | ||
->expects($this->once()) | ||
->method('getRelated') | ||
->with('pid') | ||
->willReturn(null) | ||
; | ||
|
||
$this->expectException(RouteNotFoundException::class); | ||
|
||
$this->resolver->resolveContent($article); | ||
} | ||
|
||
/** | ||
* @return ArticleModel&MockObject $article | ||
*/ | ||
private function mockArticle(array $parameters = []): ArticleModel | ||
{ | ||
return $this->mockClassWithProperties( | ||
ArticleModel::class, | ||
array_merge( | ||
[ | ||
'id' => 5, | ||
'pid' => 1, | ||
'alias' => 'foo', | ||
], | ||
$parameters | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @return PageModel&MockObject $page | ||
*/ | ||
private function mockPage(array $properties = []): PageModel | ||
{ | ||
return $this->mockClassWithProperties( | ||
PageModel::class, | ||
array_merge( | ||
[ | ||
'id' => 17, | ||
'alias' => 'bar', | ||
'domain' => 'www.example.com', | ||
'rootLanguage' => 'xy', | ||
'rootUseSSL' => true, | ||
'urlPrefix' => 'foo', | ||
'urlSuffix' => '.baz', | ||
], | ||
$properties | ||
) | ||
); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
namespace Contao\CoreBundle\Tests\Routing\Content; | ||
|
||
use Contao\CoreBundle\Routing\Content\ContentRoute; | ||
use Contao\CoreBundle\Tests\TestCase; | ||
use Contao\PageModel; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
class ContentRouteTest extends TestCase | ||
{ | ||
public function testReturnsThePageModel() | ||
{ | ||
$page = $this->mockPage(); | ||
|
||
$route = new ContentRoute($page); | ||
|
||
$this->assertSame($page, $route->getPage()); | ||
} | ||
|
||
public function testRoutePathMergesPageAliasWithUrlPrefixAndSuffix(): void | ||
{ | ||
$route = new ContentRoute($this->mockPage()); | ||
|
||
$this->assertSame('/foo/bar.baz', $route->getPath()); | ||
|
||
$route->setUrlPrefix('prefix'); | ||
$route->setUrlSuffix('.suffix'); | ||
|
||
$this->assertSame('/prefix/bar.suffix', $route->getPath()); | ||
|
||
$route->setPath('/path/{pattern}'); | ||
|
||
$this->assertSame('/prefix/path/{pattern}.suffix', $route->getPath()); | ||
} | ||
|
||
public function testReturnsTheUrlPrefix(): void | ||
{ | ||
$route = new ContentRoute($this->mockPage()); | ||
|
||
$this->assertSame('foo', $route->getUrlPrefix()); | ||
|
||
$route->setUrlPrefix('prefix'); | ||
|
||
$this->assertSame('prefix', $route->getUrlPrefix()); | ||
} | ||
|
||
public function testReturnsTheUrlSuffix(): void | ||
{ | ||
$route = new ContentRoute($this->mockPage()); | ||
|
||
$this->assertSame('.baz', $route->getUrlSuffix()); | ||
|
||
$route->setUrlSuffix('suffix'); | ||
|
||
$this->assertSame('suffix', $route->getUrlSuffix()); | ||
} | ||
|
||
public function testReturnsTheContent(): void | ||
{ | ||
$content = (object) ['foo' => 'bar']; | ||
|
||
$route = new ContentRoute($this->mockPage(), $content); | ||
|
||
$this->assertSame($content, $route->getContent()); | ||
|
||
$route->setContent('foo'); | ||
$this->assertSame('foo', $route->getContent()); | ||
} | ||
|
||
public function testAddsPageLanguageAsLocaleToRouteDefaults(): void | ||
{ | ||
$route = new ContentRoute($this->mockPage()); | ||
|
||
$this->assertSame('xy', $route->getDefault('_locale')); | ||
|
||
$route = new ContentRoute($this->mockPage(['rootLanguage' => 'en'])); | ||
|
||
$this->assertSame('en', $route->getDefault('_locale')); | ||
} | ||
|
||
public function testSetsPageDomainAsRouteHost(): void | ||
{ | ||
$route = new ContentRoute($this->mockPage()); | ||
$this->assertSame('www.example.com', $route->getHost()); | ||
} | ||
|
||
public function testSetsProtocolIfRootPageUsesSSL(): void | ||
{ | ||
$route = new ContentRoute($this->mockPage(['rootUseSSL' => false])); | ||
$this->assertEmpty($route->getSchemes()); | ||
|
||
$route = new ContentRoute($this->mockPage(['rootUseSSL' => true])); | ||
$this->assertSame(['https'], $route->getSchemes()); | ||
} | ||
|
||
public function testCreatePageWithParametersAndRequiresItemIfConfigured(): void | ||
{ | ||
$route = ContentRoute::createWithParameters($this->mockPage(['requireItem' => false]), '/items/news'); | ||
|
||
$this->assertSame('/foo/bar{parameters}.baz', $route->getPath()); | ||
$this->assertSame('/items/news', $route->getDefault('parameters')); | ||
$this->assertSame('(/.+)?', $route->getRequirement('parameters')); | ||
|
||
$route = ContentRoute::createWithParameters($this->mockPage(['requireItem' => true]), '/items/news'); | ||
|
||
$this->assertSame('/foo/bar{parameters}.baz', $route->getPath()); | ||
$this->assertSame('/items/news', $route->getDefault('parameters')); | ||
$this->assertSame('/.+', $route->getRequirement('parameters')); | ||
} | ||
|
||
/** | ||
* @return PageModel&MockObject $page | ||
*/ | ||
private function mockPage(array $properties = []): PageModel | ||
{ | ||
return $this->mockClassWithProperties( | ||
PageModel::class, | ||
array_merge( | ||
[ | ||
'id' => 17, | ||
'alias' => 'bar', | ||
'domain' => 'www.example.com', | ||
'rootLanguage' => 'xy', | ||
'rootUseSSL' => true, | ||
'urlPrefix' => 'foo', | ||
'urlSuffix' => '.baz', | ||
], | ||
$properties | ||
) | ||
); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Contao\CoreBundle\Tests\Routing\Content; | ||
|
||
use Contao\ArticleModel; | ||
use Contao\CoreBundle\Routing\Content\ContentRoute; | ||
use Contao\CoreBundle\Routing\Content\PageUrlResolver; | ||
use Contao\CoreBundle\Tests\TestCase; | ||
use Contao\PageModel; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
class PageUrlResolverTest extends TestCase | ||
{ | ||
/** | ||
* @var PageUrlResolver | ||
*/ | ||
private $resolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->resolver = new PageUrlResolver(); | ||
} | ||
|
||
public function testSupportsPages(): void | ||
{ | ||
$this->assertTrue($this->resolver->supportsContent($this->mockPage())); | ||
$this->assertFalse($this->resolver->supportsContent($this->mockClassWithProperties(ArticleModel::class))); | ||
} | ||
|
||
public function testCreatesParameterdContentRoute(): void | ||
{ | ||
$page = $this->mockPage(); | ||
|
||
/** @var ContentRoute $route */ | ||
$route = $this->resolver->resolveContent($page); | ||
|
||
$this->assertInstanceOf(ContentRoute::class, $route); | ||
$this->assertSame($page, $route->getPage()); | ||
$this->assertSame('/foo/bar{parameters}.baz', $route->getPath()); | ||
$this->assertSame('', $route->getDefault('parameters')); | ||
} | ||
|
||
/** | ||
* @return PageModel&MockObject $page | ||
*/ | ||
private function mockPage(array $properties = []): PageModel | ||
{ | ||
return $this->mockClassWithProperties( | ||
PageModel::class, | ||
array_merge( | ||
[ | ||
'id' => 17, | ||
'alias' => 'bar', | ||
'domain' => 'www.example.com', | ||
'rootLanguage' => 'xy', | ||
'rootUseSSL' => true, | ||
'urlPrefix' => 'foo', | ||
'urlSuffix' => '.baz', | ||
], | ||
$properties | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.