Skip to content

Commit

Permalink
pridane control render testy
Browse files Browse the repository at this point in the history
  • Loading branch information
Kollarovic committed May 26, 2019
1 parent cb77a47 commit 4827261
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
vendor
composer.lock
.idea
tests
31 changes: 31 additions & 0 deletions tests/Admin/AdminControlTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Kollarovic\Admin\Test;

use Tester\Assert;
use Tester\DomQuery;

require_once __DIR__ . '/../bootstrap.php';


class AdminControlTest extends TestCase
{

public function testRender()
{
$presenter = $this->createPresenter();
$control = $presenter['adminControl'];
ob_start();
$control->render();
$html = ob_get_clean();

$dom = DomQuery::fromHtml($html);
Assert::true($dom->has('html'));
Assert::true($dom->has('footer'));
Assert::contains('Mario Kollarovic', $html);
}

}


\run(new AdminControlTest());
30 changes: 30 additions & 0 deletions tests/Admin/LoginControlTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Kollarovic\Admin\Test;

use Tester\Assert;
use Tester\DomQuery;

require_once __DIR__ . '/../bootstrap.php';


class LoginControlTest extends TestCase
{

public function testRender()
{
$presenter = $this->createPresenter();
$control = $presenter['loginControl'];
ob_start();
$control->render();
$html = ob_get_clean();

$dom = DomQuery::fromHtml($html);
Assert::true($dom->has('html'));
Assert::true($dom->has('form'));
}

}


\run(new LoginControlTest());
75 changes: 75 additions & 0 deletions tests/Admin/MockPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Kollarovic\Admin\Test;

use Kollarovic\Admin\IAdminControlFactory;
use Kollarovic\Admin\ILoginControlFactory;
use Nette\Application\UI\Presenter;
use Nette\Application\UI\InvalidLinkException;
use Nette\Security\Identity;


class MockPresenter extends Presenter
{

private $currentDestination = 'Setting:web';

private $linkToUrl = [
'Homepage:default' => '/',
'//Homepage:default' => 'http://example.com/',
'Page:default' => '/page/default',
'Page:list' => '/page/list',
'this' => '/setting/web',
'page' => '/setting/page',
'Setting:default' => '/setting/default',
'Setting:base' => '/setting/base',
'Setting:advanced' => '/setting/advanced',
'Setting:web' => '/setting/web',
'Setting:mail' => '/setting/mail',
];

/** @var IAdminControlFactory @inject */
public $adminControlFactory;

/** @var ILoginControlFactory @inject */
public $loginControlFactory;


public function link($destination, $args = array())
{
if (!array_key_exists($destination, $this->linkToUrl)) {
throw new InvalidLinkException("'$destination'");
}

return $this->linkToUrl[$destination];
}


public function isLinkCurrent($destination = NULL, $args = array())
{
return ($destination == $this->currentDestination);
}


protected function createComponentAdminControl()
{
$this->user->login(new Identity(1));
$this->saveGlobalState();
$adminControl = $this->adminControlFactory->create();
$adminControl->setUserName('Mario Kollarovic')->setUserImage('images/user.png');
$adminControl->setProfileUrl('/');
$adminControl->setSkin('red');
$adminControl->onLoggedOut[] = function() {
$this->redirect('Sign:in');
};
return $adminControl;
}


protected function createComponentLoginControl()
{
$loginControl = $this->loginControlFactory->create();
return $loginControl;
}

}
16 changes: 16 additions & 0 deletions tests/Admin/MockTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Kollarovic\Admin\Test;

use Nette\Localization\ITranslator;


class MockTranslator implements ITranslator
{

function translate($message, $count = null)
{
return $message;
}

}
29 changes: 29 additions & 0 deletions tests/Admin/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?

namespace Kollarovic\Admin\Test;

use Nette\Configurator;


abstract class TestCase extends \Tester\TestCase
{

protected function createContainer()
{
$configurator = new Configurator();
$configurator->setDebugMode(false);
$configurator->setTempDirectory(TEMP_DIR);
$configurator->addConfig(__DIR__ . '/../config.neon');
return $configurator->createContainer();
}


protected function createPresenter()
{
$container = $this->createContainer();
$presenter = new MockPresenter();
$container->callInjects($presenter);
return $presenter;
}

}
2 changes: 2 additions & 0 deletions tests/Admin/webtemp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.*
18 changes: 18 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php


if (!$autoload = @include __DIR__ . '/../vendor/autoload.php') {
echo 'Install Nette Tester using `composer update --dev`';
exit(1);
}

$autoload->addPsr4('Kollarovic\Admin\Test\\', __DIR__ . '/Admin');


Tester\Environment::setup();

define('TEMP_DIR', __DIR__ . '/temp');

function run(Tester\TestCase $testCase) {
$testCase->run();
}
37 changes: 37 additions & 0 deletions tests/config.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
extensions:
navigation: Kollarovic\Navigation\DI\Extension
admin: Kollarovic\Admin\DI\Extension

navigation:
admin:
label: Homepage
link: Homepage:default
items:
page:
label: Page
link: Page:default
icon: fa-file-text-o
value: 50
setting:
label: Setting
link: Setting:default
items:
base:
label: Base
link: Setting:base
advanced:
label: Advanced
link: Setting:advanced
items:
web:
label: Web
link: Setting:web
mail:
label: Mail
link: Setting:mail

services:
translator: Kollarovic\Admin\Test\MockTranslator
nette.latteFactory:
setup:
- addFilter('translate', [@translator, 'translate'])
Empty file added tests/php.ini
Empty file.
2 changes: 2 additions & 0 deletions tests/temp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.*

0 comments on commit 4827261

Please sign in to comment.