Skip to content

Commit

Permalink
fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico Kaltofen committed Mar 21, 2018
1 parent 7b838f0 commit bf9b9a2
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions tests/Manager/ReaderManagerTest.php
Expand Up @@ -11,6 +11,7 @@
use Contao\Config;
use Contao\CoreBundle\Exception\RedirectResponseException;
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\Database;
use Contao\DataContainer;
use Contao\FilesModel;
Expand All @@ -30,11 +31,14 @@
use HeimrichHannot\ReaderBundle\Registry\ReaderConfigRegistry;
use HeimrichHannot\ReaderBundle\Tests\TestCaseEnvironment;
use HeimrichHannot\Request\Request;
use HeimrichHannot\UtilsBundle\Classes\ClassUtil;
use HeimrichHannot\UtilsBundle\Container\ContainerUtil;
use HeimrichHannot\UtilsBundle\Form\FormUtil;
use HeimrichHannot\UtilsBundle\Image\ImageUtil;
use HeimrichHannot\UtilsBundle\Model\ModelUtil;
use HeimrichHannot\UtilsBundle\Url\UrlUtil;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;

Expand Down Expand Up @@ -315,6 +319,7 @@ function ($values, $id = null) {
$container->set('huh.utils.container', $this->containerUtil);
$container->set('huh.utils.image', $this->imageUtil);
$container->set('huh.utils.model', $this->modelUtil);
$container->set('huh.utils.class', new ClassUtil());
$container->set('database_connection', $this->createMock(Connection::class));
$container->set('request_stack', $this->createRequestStackMock());
$container->set('router', $this->createRouterMock());
Expand All @@ -324,6 +329,11 @@ function ($values, $id = null) {
$dbalAdapter->method('getParams')->willReturn([]);
$container->set('doctrine.dbal.default_connection', $dbalAdapter);

$requestStack = new RequestStack();
$requestStack->push(new \Symfony\Component\HttpFoundation\Request());

$container->set('huh.request', new \HeimrichHannot\RequestBundle\Component\HttpFoundation\Request($this->mockContaoFramework(), $requestStack, $this->mockScopeMatcher()));

$container->set('contao.framework', $this->mockContaoFramework());

System::setContainer($container);
Expand Down Expand Up @@ -456,11 +466,17 @@ public function testRetrieveItem()

$item = new DefaultItem($this->manager, $this->johnDoeModel->row());

$this->assertSame($item->getRaw(), $this->manager->retrieveItem()->getRaw());
$data = json_decode(json_encode($item));
$managerData = json_decode(json_encode($this->manager->retrieveItem()));

$this->assertSame($data->raw, $managerData->raw);

Request::setGet('auto_item', '1');

$this->assertSame($item->getRaw(), $this->manager->retrieveItem()->getRaw());
$data = json_decode(json_encode($item));
$managerData = json_decode(json_encode($this->manager->retrieveItem()));

$this->assertSame($data->raw, $managerData->raw);

// unpublished
Request::setGet('auto_item', '2');
Expand All @@ -485,7 +501,11 @@ public function testRetrieveItem()
]
);

$this->assertSame($item->getRaw(), $this->manager->retrieveItem()->getRaw());
$data = json_decode(json_encode($item));

$managerData = json_decode(json_encode($this->manager->retrieveItem()));

$this->assertSame($data->raw, $managerData->raw);
}

public function testTriggerOnLoadCallbacks()
Expand Down Expand Up @@ -780,16 +800,18 @@ public function testPrepareItem()

Config::set('dateFormat', 'd.m.Y');

$data = json_decode(json_encode($johnDoeItem));

$this->assertSame(
[
'raw' => [
'raw' => (object) [
'id' => '1',
'firstname' => 'John',
'lastname' => 'DoeModified',
'someDate' => 1520004293,
'published' => '1',
],
'formatted' => [
'formatted' => (object) [
'id' => '1',
'firstname' => 'John',
'lastname' => 'DoeModified',
Expand All @@ -798,12 +820,25 @@ public function testPrepareItem()
],
],
[
'raw' => $johnDoeItem->getRaw(),
'formatted' => $johnDoeItem->getFormatted(),
'raw' => $data->raw,
'formatted' => $data->formatted,
]
);
}

/**
* Mocks a request scope matcher.
*
* @return ScopeMatcher
*/
protected function mockScopeMatcher(): ScopeMatcher
{
return new ScopeMatcher(
new RequestMatcher(null, null, null, null, ['_scope' => 'backend']),
new RequestMatcher(null, null, null, null, ['_scope' => 'frontend'])
);
}

protected static function getMethod($class, $name)
{
$class = new \ReflectionClass($class);
Expand Down

0 comments on commit bf9b9a2

Please sign in to comment.