Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Dec 5, 2020
1 parent a5c2326 commit 826748d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core-bundle/src/HttpKernel/ModelArgumentResolver.php
Expand Up @@ -82,7 +82,8 @@ private function fetchModel(Request $request, ArgumentMetadata $argument): ?Mode

// Special handling for pageModel that could be globally registered
if (
isset($GLOBALS['objPage'])
\is_a($type, PageModel::class, true)
&& isset($GLOBALS['objPage'])
&& $GLOBALS['objPage'] instanceof PageModel
&& (int) $GLOBALS['objPage']->id === (int) $value
) {
Expand Down
38 changes: 38 additions & 0 deletions core-bundle/tests/HttpKernel/ModelArgumentResolverTest.php
Expand Up @@ -19,9 +19,17 @@
use Contao\System;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Contao\CoreBundle\Framework\ContaoFramework;

class ModelArgumentResolverTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

unset($GLOBALS);
}

/**
* @dataProvider getArguments
*/
Expand Down Expand Up @@ -159,4 +167,34 @@ public function testChecksIfTheModelExistsIfTheArgumentIsNotNullable(): void

$this->assertFalse($resolver->supports($request, $argument));
}

public function testReturnsTheGlobalPageModel(): void
{
$framework = $this->createMock(ContaoFramework::class);
$framework
->expects($this->once())
->method('initialize')
;
$framework
->expects($this->never())
->method('getAdapter')
;

$pageModel = $this->mockClassWithProperties(PageModel::class, ['id' => 42]);
$GLOBALS['objPage'] = $pageModel;

$request = Request::create('/foobar');
$request->attributes->set('pageModel', 42);
$request->attributes->set('_scope', ContaoCoreBundle::SCOPE_FRONTEND);

$argument = new ArgumentMetadata('pageModel', PageModel::class, false, false, '', true);
$resolver = new ModelArgumentResolver($framework, $this->mockScopeMatcher());

$this->assertTrue($resolver->supports($request, $argument));

foreach ($resolver->resolve($request, $argument) as $model) {
$this->assertSame($pageModel, $model);
}
}

}

0 comments on commit 826748d

Please sign in to comment.