Skip to content

Commit

Permalink
#79: improve test structure, implement acceptance testing block
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Oct 29, 2018
1 parent a084014 commit c69f796
Show file tree
Hide file tree
Showing 63 changed files with 820 additions and 624 deletions.
23 changes: 16 additions & 7 deletions .travis.yml
@@ -1,14 +1,18 @@
sudo: false
sudo: required
language: php
addons:
chrome: stable
mariadb: '10.1'
hosts:
- pimcore-test.dev

env:
global:
- PIMCORE_ENVIRONMENT=test
- DACHCOM_BUNDLE_TEST=1
- PIMCORE_TEST_URL=http://pimcore-test.dev
- PIMCORE_TEST_URL=http://localhost
- CHROME_DRIVER_VERSION=2.41
- SELENIUM_VERSION=3.6.0
- WEBDRIVER_HOST=localhost
- WEBDRIVER_URL="http://localhost:8080/"
- PIMCORE_TEST_DB_DSN="mysql://root@localhost/dachcom_bundle_test"
- SYMFONY_DEPRECATIONS_HELPER=weak
matrix:
Expand All @@ -27,14 +31,19 @@ matrix:

cache:
directories:
- ~/.composer/cache/files
- vendor
- $HOME/.composer/cache

install:
- tests/etc/travis/install

after_failure:
- cd $TRAVIS_BUILD_DIR
- cat ./lib/Toolbox/tests/_output/*

script:
# we moved etc dir during install to .
- '$HOME/chromedriver --url-base=/wd/hub &'
- 'php ${TRAVIS_BUILD_DIR}/bin/console server:start 127.0.0.1:8080'
- 'php ${TRAVIS_BUILD_DIR}/bin/console server:status'
- etc/travis/script

notifications:
Expand Down
8 changes: 7 additions & 1 deletion src/ToolboxBundle/Manager/ConfigManager.php
Expand Up @@ -41,7 +41,6 @@ class ConfigManager implements ConfigManagerInterface
*/
private $areaNamespace = null;


/**
* ConfigManager constructor.
*
Expand All @@ -54,6 +53,7 @@ public function __construct(ContextResolverInterface $contextResolver)

/**
* @param array $config
*
* @throws \Exception
*/
public function setConfig($config = [])
Expand All @@ -74,6 +74,7 @@ public function setAreaNameSpace($namespace = ConfigManagerInterface::AREABRICK_

/**
* @param $section
*
* @return mixed
* @throws \Exception
*/
Expand Down Expand Up @@ -110,6 +111,7 @@ public function getCurrentContextSettings()

/**
* @param string $areaName
*
* @return mixed
* @throws \Exception
*/
Expand All @@ -123,6 +125,7 @@ public function getAreaConfig($areaName = '')
/**
* @param string $areaName
* @param string $configElementName
*
* @return mixed
* @throws \Exception
*/
Expand All @@ -135,6 +138,7 @@ public function getAreaElementConfig($areaName = '', $configElementName = '')

/**
* @param string $areaName
*
* @return mixed
* @throws \Exception
*/
Expand All @@ -147,6 +151,7 @@ public function getAreaParameterConfig($areaName = '')

/**
* @param string $thumbnailName
*
* @return mixed
* @throws \Exception
*/
Expand Down Expand Up @@ -204,6 +209,7 @@ private function ensureConfigNamespace()

/**
* @param $currentContextId
*
* @return array
* @throws \Exception
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/_support/AcceptanceTester.php
@@ -0,0 +1,13 @@
<?php

namespace DachcomBundle\Test;

/**
* Class AcceptanceTester
*
* @package DachcomBundle\Test
*/
class AcceptanceTester extends \Codeception\Actor
{
use _generated\AcceptanceTesterActions;
}
10 changes: 8 additions & 2 deletions tests/_support/App/TestAppKernel.php
Expand Up @@ -30,6 +30,10 @@ public function registerContainerConfiguration(LoaderInterface $loader)
*/
public function registerBundlesToCollection(\Pimcore\HttpKernel\BundleCollection\BundleCollection $collection)
{
if (class_exists('\\AppBundle\\AppBundle')) {
$collection->addBundle(new \AppBundle\AppBundle());
}

$bundleClass = getenv('DACHCOM_BUNDLE_CLASS');
$collection->addBundle(new $bundleClass());
}
Expand All @@ -39,8 +43,10 @@ public function registerBundlesToCollection(\Pimcore\HttpKernel\BundleCollection
*/
protected function build(ContainerBuilder $container)
{
$container->addCompilerPass(new \DachcomBundle\Test\DependencyInjection\MakeServicesPublicPass(), \Symfony\Component\DependencyInjection\Compiler\PassConfig::TYPE_BEFORE_OPTIMIZATION, -100000);
$container->addCompilerPass(new \DachcomBundle\Test\DependencyInjection\MonologChannelLoggerPass(), \Symfony\Component\DependencyInjection\Compiler\PassConfig::TYPE_BEFORE_OPTIMIZATION, 1);
$container->addCompilerPass(new \DachcomBundle\Test\DependencyInjection\MakeServicesPublicPass(),
\Symfony\Component\DependencyInjection\Compiler\PassConfig::TYPE_BEFORE_OPTIMIZATION, -100000);
$container->addCompilerPass(new \DachcomBundle\Test\DependencyInjection\MonologChannelLoggerPass(),
\Symfony\Component\DependencyInjection\Compiler\PassConfig::TYPE_BEFORE_OPTIMIZATION, 1);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/_support/DependencyInjection/MakeServicesPublicPass.php
Expand Up @@ -7,6 +7,9 @@

class MakeServicesPublicPass implements CompilerPassInterface
{
/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
$prefix = getenv('DACHCOM_BUNDLE_NAME');
Expand Down
130 changes: 130 additions & 0 deletions tests/_support/Helper/Browser/PhpBrowser.php
@@ -0,0 +1,130 @@
<?php

namespace DachcomBundle\Test\Helper\Browser;

use Codeception\Module;
use Codeception\Lib;
use Codeception\Exception\ModuleException;
use DachcomBundle\Test\Helper\PimcoreCore;
use DachcomBundle\Test\Helper\PimcoreUser;
use Pimcore\Model\User;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\BrowserKit\Cookie;

class PhpBrowser extends Module implements Lib\Interfaces\DependsOnModule
{
const PIMCORE_ADMIN_CSRF_TOKEN_NAME = 'MOCK_CSRF_TOKEN';

/**
* @var Cookie
*/
protected $sessionSnapShot;

/**
* @var PimcoreCore
*/
protected $pimcoreCore;

/**
* @return array|mixed
*/
public function _depends()
{
return [
'Codeception\Module\Symfony' => 'PhpBrowser needs the pimcore core framework to work.'
];
}

/**
* @param PimcoreCore $pimcoreCore
*/
public function _inject($pimcoreCore)
{
$this->pimcoreCore = $pimcoreCore;
}

/**
* @inheritDoc
*/
public function _initialize()
{
$this->sessionSnapShot = [];

parent::_initialize();
}

/**
* Actor Function to see a page with enabled edit-mode
*
* @param string $page
*/
public function amOnPageInEditMode(string $page)
{
$this->pimcoreCore->amOnPage(sprintf('%s?pimcore_editmode=true', $page));
}

/**
* Actor Function to login into Pimcore Backend
*
* @param $username
*/
public function amLoggedInAs($username)
{
$firewallName = 'admin';

try {
/** @var PimcoreUser $userModule */
$userModule = $this->getModule('\\' . PimcoreUser::class);
} catch (ModuleException $pimcoreModule) {
$this->debug('[PIMCORE BUNDLE MODULE] could not load pimcore user module');
return;
}

$pimcoreUser = $userModule->getUser($username);

if (!$pimcoreUser instanceof User) {
$this->debug(sprintf('[PIMCORE BUNDLE MODULE] could not fetch user %s.', $username));
return;
}

/** @var Session $session */
$session = $this->pimcoreCore->getContainer()->get('session');

$this->pimcoreCore->client->getCookieJar()->clear();

$user = new \Pimcore\Bundle\AdminBundle\Security\User\User($pimcoreUser);
$token = new UsernamePasswordToken($user, null, $firewallName, $pimcoreUser->getRoles());
$this->pimcoreCore->getContainer()->get('security.token_storage')->setToken($token);

\Pimcore\Tool\Session::useSession(function (AttributeBagInterface $adminSession) use ($pimcoreUser) {
\Pimcore\Tool\Session::regenerateId();
$adminSession->set('user', $pimcoreUser);
$adminSession->set('csrfToken', self::PIMCORE_ADMIN_CSRF_TOKEN_NAME);
});

// allow re-usage of session in same cest.
if (!empty($this->sessionSnapShot)) {
$cookie = $this->sessionSnapShot;
} else {
$cookie = new Cookie($session->getName(), $session->getId());
$this->sessionSnapShot = $cookie;
}

$this->pimcoreCore->client->getCookieJar()->set($cookie);

}

/**
* Actor Function to send tokenized ajax request in backend
*
* @param string $url
* @param array $params
*/
public function sendTokenAjaxPostRequest(string $url, array $params = [])
{
$params['csrfToken'] = self::PIMCORE_ADMIN_CSRF_TOKEN_NAME;
$this->pimcoreCore->sendAjaxPostRequest($url, $params);
}
}
18 changes: 18 additions & 0 deletions tests/_support/Helper/Browser/WebDriver.php
@@ -0,0 +1,18 @@
<?php

namespace DachcomBundle\Test\Helper\Browser;

use Codeception\Module;

class WebDriver extends Module\WebDriver
{
/**
* Actor Function to see a page with enabled edit-mode
*
* @param string $page
*/
public function amOnPageInEditMode(string $page)
{
$this->amOnPage(sprintf('%s?pimcore_editmode=true', $page));
}
}
8 changes: 0 additions & 8 deletions tests/_support/Helper/PimcoreBackend.php
Expand Up @@ -9,14 +9,6 @@

class PimcoreBackend extends Module
{
/**
* @inheritDoc
*/
public function _initialize()
{
parent::_initialize();
}

/**
* @param TestInterface $test
*/
Expand Down

0 comments on commit c69f796

Please sign in to comment.