-
Notifications
You must be signed in to change notification settings - Fork 2
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
Konstantin Wagner
committed
Jan 31, 2018
1 parent
b540121
commit 1809170
Showing
9 changed files
with
647 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) 2018 Heimrich & Hannot GmbH | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace HeimrichHannot\QuizBundle\Test\Choice; | ||
|
||
use Contao\Config; | ||
use Contao\ContentModel; | ||
use Contao\ManagerBundle\HttpKernel\ContaoKernel; | ||
use Contao\Model; | ||
use Contao\System; | ||
use Contao\TestCase\ContaoTestCase; | ||
use Doctrine\DBAL\Connection; | ||
use HeimrichHannot\QuizBundle\Manager\ModelManager; | ||
use HeimrichHannot\QuizBundle\Model\QuizAnswerModel; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
use Symfony\Component\Routing\RouterInterface; | ||
|
||
class ModelManagerTest extends ContaoTestCase | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if (!defined('TL_ROOT')) { | ||
\define('TL_ROOT', $this->getFixturesDir()); | ||
} | ||
|
||
$router = $this->createRouterMock(); | ||
$requestStack = $this->createRequestStackMock(); | ||
$framework = $this->mockContaoFramework($this->createMockAdapter()); | ||
|
||
$database = $this->createMock(Connection::class); | ||
$container = $this->mockContainer(); | ||
$container->set('kernel', $this->createMock(ContaoKernel::class)); | ||
$container->setParameter('secret', Config::class); | ||
$container->set('request_stack', $requestStack); | ||
$container->set('router', $router); | ||
$container->set('session', new Session()); | ||
$container->set('contao.framework', $framework); | ||
$container->set('database_connection', $database); | ||
System::setContainer($container); | ||
} | ||
|
||
public function testContentElementByModel() | ||
{ | ||
$contentModel = $this->mockClassWithProperties(ContentModel::class, ['id' => 1]); | ||
|
||
$contentAdapter = $this->mockAdapter(['findPublishedByPidAndTable', 'countPublishedByPidAndTable']); | ||
$contentAdapter->method('findPublishedByPidAndTable')->willReturn($contentModel); | ||
$contentAdapter->method('countPublishedByPidAndTable')->willReturn(1); | ||
|
||
$framework = $this->mockContaoFramework([ContentModel::class => $contentAdapter]); | ||
|
||
$manager = new ModelManager($framework); | ||
|
||
$mockModel = $this->mockClassWithProperties(QuizAnswerModel::class, ['id' => 1, 'hasContentElement' => null, 'contentElement' => '']); | ||
|
||
$mockModel = $manager->getContentElementByModel($mockModel, 'tl_test'); | ||
|
||
$this->assertInstanceOf(QuizAnswerModel::class, $mockModel); | ||
$this->assertSame('', $mockModel->contentElement); | ||
$this->assertNull($mockModel->hasContentElement); | ||
} | ||
|
||
public function createRouterMock() | ||
{ | ||
$router = $this->createMock(RouterInterface::class); | ||
$router->method('generate')->with('contao_backend', $this->anything())->will($this->returnCallback(function ($route, $params = []) { | ||
$url = '/contao'; | ||
if (!empty($params)) { | ||
$count = 0; | ||
foreach ($params as $key => $value) { | ||
$url .= (0 === $count ? '?' : '&'); | ||
$url .= $key.'='.$value; | ||
++$count; | ||
} | ||
} | ||
|
||
return $url; | ||
})); | ||
|
||
return $router; | ||
} | ||
|
||
public function createRequestStackMock() | ||
{ | ||
$requestStack = new RequestStack(); | ||
$request = new \Symfony\Component\HttpFoundation\Request(); | ||
$request->attributes->set('_contao_referer_id', 'foobar'); | ||
$requestStack->push($request); | ||
|
||
return $requestStack; | ||
} | ||
|
||
public function createMockAdapter() | ||
{ | ||
$modelAdapter = $this->mockAdapter(['__construct']); | ||
|
||
return [Model::class => $modelAdapter]; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getFixturesDir(): string | ||
{ | ||
return __DIR__.DIRECTORY_SEPARATOR.'Fixtures'; | ||
} | ||
} |
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,110 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) 2018 Heimrich & Hannot GmbH | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace HeimrichHannot\QuizBundle\Test\Manager; | ||
|
||
use Contao\ManagerBundle\HttpKernel\ContaoKernel; | ||
use Contao\System; | ||
use Contao\TestCase\ContaoTestCase; | ||
use HeimrichHannot\QuizBundle\Manager\QuizAnswerManager; | ||
use HeimrichHannot\QuizBundle\Model\QuizAnswerModel; | ||
|
||
class QuizAnswerManagerTest extends ContaoTestCase | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if (!defined('TL_ROOT')) { | ||
\define('TL_ROOT', $this->getFixturesDir()); | ||
} | ||
|
||
$container = $this->mockContainer(); | ||
$container->set('kernel', $this->createMock(ContaoKernel::class)); | ||
System::setContainer($container); | ||
} | ||
|
||
public function testFindOnePublishedByPid() | ||
{ | ||
$quizAnswerModel = $this->mockClassWithProperties(QuizAnswerModel::class, ['id' => 1]); | ||
|
||
$quizAnswerAdapter = $this->mockAdapter(['findOnePublishedByPid', 'getTable', 'findOneBy']); | ||
$quizAnswerAdapter->method('findOnePublishedByPid')->willReturn($quizAnswerModel); | ||
$quizAnswerAdapter->method('getTable')->willReturn('tl_quiz_answer'); | ||
$quizAnswerAdapter->method('findOneBy')->willReturn($quizAnswerModel); | ||
|
||
$framework = $this->mockContaoFramework([QuizAnswerModel::class => $quizAnswerAdapter]); | ||
|
||
$manager = new QuizAnswerManager($framework); | ||
|
||
$result = $manager->findOnePublishedByPid(1); | ||
|
||
$this->assertInstanceOf(QuizAnswerModel::class, $result); | ||
} | ||
|
||
public function testFindPublishedByPid() | ||
{ | ||
$quizAnswerModel = $this->mockClassWithProperties(QuizAnswerModel::class, ['id' => 1]); | ||
|
||
$quizAnswerAdapter = $this->mockAdapter(['findPublishedByPid', 'getTable', 'findBy']); | ||
$quizAnswerAdapter->method('findPublishedByPid')->willReturn($quizAnswerModel); | ||
$quizAnswerAdapter->method('getTable')->willReturn('tl_quiz_answer'); | ||
$quizAnswerAdapter->method('findBy')->willReturn($quizAnswerModel); | ||
|
||
$framework = $this->mockContaoFramework([QuizAnswerModel::class => $quizAnswerAdapter]); | ||
|
||
$manager = new QuizAnswerManager($framework); | ||
|
||
$result = $manager->findPublishedByPid(1); | ||
|
||
$this->assertInstanceOf(QuizAnswerModel::class, $result); | ||
} | ||
|
||
public function testFindBy() | ||
{ | ||
$quizAnswerModel = $this->mockClassWithProperties(QuizAnswerModel::class, ['id' => 1]); | ||
|
||
$quizAnswerAdapter = $this->mockAdapter(['findBy']); | ||
$quizAnswerAdapter->method('findBy')->willReturn($quizAnswerModel); | ||
|
||
$framework = $this->mockContaoFramework([QuizAnswerModel::class => $quizAnswerAdapter]); | ||
|
||
$manager = new QuizAnswerManager($framework); | ||
|
||
$result = $manager->findBy('id', 1); | ||
|
||
$this->assertInstanceOf(QuizAnswerModel::class, $result); | ||
} | ||
|
||
public function testFindOneBy() | ||
{ | ||
$quizAnswerModel = $this->mockClassWithProperties(QuizAnswerModel::class, ['id' => 1]); | ||
|
||
$quizAnswerAdapter = $this->mockAdapter(['findOneBy']); | ||
$quizAnswerAdapter->method('findOneBy')->willReturn($quizAnswerModel); | ||
|
||
$framework = $this->mockContaoFramework([QuizAnswerModel::class => $quizAnswerAdapter]); | ||
|
||
$manager = new QuizAnswerManager($framework); | ||
|
||
$result = $manager->findOneBy('id', 1); | ||
|
||
$this->assertInstanceOf(QuizAnswerModel::class, $result); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getFixturesDir(): string | ||
{ | ||
return __DIR__.DIRECTORY_SEPARATOR.'Fixtures'; | ||
} | ||
} |
Oops, something went wrong.