Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from cakephp/split-helper
Browse files Browse the repository at this point in the history
Split helper
  • Loading branch information
jadb committed Feb 16, 2015
2 parents 04619c6 + b40740f commit 7b6379f
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 119 deletions.
1 change: 1 addition & 0 deletions src/Command/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ protected function createUnitSuite($actor = 'Unit')
'namespace' => 'App\Test\Unit',
'modules' => [
'enabled' => [
'Cake\Codeception\Framework',
'Asserts',
'App\TestSuite\Codeception\\' . $actor . 'Helper'
]
Expand Down
134 changes: 134 additions & 0 deletions src/Framework.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
namespace Cake\Codeception;

use Cake\Codeception\Helper\ConfigTrait;
use Cake\Codeception\Helper\DbTrait;
use Cake\Core\Configure;
use Cake\Event\EventManager;
use Cake\TestSuite\Fixture\FixtureManager;
use Codeception\TestCase;

class Framework extends \Codeception\Lib\Framework
{

use ConfigTrait;
use DbTrait;

/**
* Module's default configuration.
*
* @var array
*/
public $config = [
'autoFixtures' => true,
'dropTables' => false,
];

/**
* The class responsible for managing the creation, loading and removing of fixtures
*
* @var \Cake\TestSuite\Fixture\FixtureManager
*/
protected $fixtureManager = null;

/**
* Configure values to restore at end of test.
*
* @var array
*/
protected $configure = [];

protected $testCase = null;

// @codingStandardsIgnoreStart
public function _before(TestCase $test) // @codingStandardsIgnoreEnd
{
if (method_exists($test, 'getTestClass')) {
$this->testCase = $test->getTestClass();
} else {
$this->testCase = $test;
}

if (!isset($this->testCase->autoFixtures)) {
$this->testCase->autoFixtures = $this->config['autoFixtures'];
}

if (!isset($this->testCase->dropTables)) {
$this->testCase->dropTables = $this->config['dropTables'];
}

EventManager::instance(new EventManager());
$this->fixtureManager = new FixtureManager();

if ($this->testCase->autoFixtures) {
if (!isset($this->testCase->fixtures)) {
$this->testCase->fixtures = [];
}
$this->loadFixtures($this->testCase->fixtures);
}

$this->snapshotApplication();
}

// @codingStandardsIgnoreStart
public function _after(TestCase $test) // @codingStandardsIgnoreEnd
{
$this->resetApplication();
$this->fixtureManager->unload($this->testCase);
if ($this->testCase->dropTables) {
$this->fixtureManager->shutDown();
}
}

/**
* Chooses which fixtures to load for a given test
*
* @return void
*/
public function loadFixtures($fixtures)
{
if (func_num_args() > 1) {
$fixtures = func_get_args();
}
$this->testCase->fixtures = $fixtures;
$this->fixtureManager->fixturize($this->testCase);
$this->fixtureManager->load($this->testCase);
}

/**
* Asserts the expected CakePHP version.
*
* @param string $ver Expected version.
* @param string $operator Comparison to run, defaults to greater or equal.
* @
*/
public function expectedCakePHPVersion($ver, $operator = 'ge')
{
$this->assertTrue(version_compare($ver, Configure::version(), $operator));
}

/**
* Resets the application's configuration.
*
* @return void
*/
protected function resetApplication()
{
if (!empty($this->configure)) {
Configure::clear();
Configure::write($this->configure);
}
}

/**
* Snapshots the application's configuration.
*
* @return void
*/
protected function snapshotApplication()
{
if (empty($this->configure)) {
$this->configure = Configure::read();
}
}
}
122 changes: 3 additions & 119 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,30 @@
namespace Cake\Codeception;

use Cake\Codeception\Helper\AuthTrait;
use Cake\Codeception\Helper\ConfigTrait;
use Cake\Codeception\Helper\DbTrait;
use Cake\Codeception\Helper\DispatcherTrait;
use Cake\Codeception\Helper\RouterTrait;
use Cake\Codeception\Helper\SessionTrait;
use Cake\Codeception\Helper\ViewTrait;
use Cake\Core\Configure;
use Cake\Event\EventManager;
use Cake\Routing\Router;
use Cake\TestSuite\Fixture\FixtureManager;
use Codeception\Lib\Framework;
use Codeception\TestCase;

class Helper extends Framework
{

use AuthTrait;
use ConfigTrait;
use DbTrait;
use DispatcherTrait;
use RouterTrait;
use SessionTrait;
use ViewTrait;

/**
* Module's default configuration.
*
* @var array
*/
public $config = [
'autoFixtures' => true,
'dropTables' => false,
];

/**
* The class responsible for managing the creation, loading and removing of fixtures
*
* @var \Cake\TestSuite\Fixture\FixtureManager
*/
protected $fixtureManager = null;

/**
* Configure values to restore at end of test.
*
* @var array
*/
protected $configure = [];

protected $testCase = null;

// @codingStandardsIgnoreStart
public function _before(TestCase $test)
// @codingStandardsIgnoreStart
public function _before(TestCase $test) // @codingStandardsIgnoreEnd
{
if (method_exists($test, 'getTestClass')) {
$this->testCase = $test->getTestClass();
} else {
$this->testCase = $test;
}

if (!isset($this->testCase->autoFixtures)) {
$this->testCase->autoFixtures = $this->config['autoFixtures'];
}

if (!isset($this->testCase->dropTables)) {
$this->testCase->dropTables = $this->config['dropTables'];
}

EventManager::instance(new EventManager());
$this->fixtureManager = new FixtureManager();

if ($this->testCase->autoFixtures) {
if (!isset($this->testCase->fixtures)) {
$this->testCase->fixtures = [];
}
$this->loadFixtures($this->testCase->fixtures);
}

parent::_before($test);
$this->client = $this->getConnectorInstance();

$this->snapshotApplication();
$this->reloadRoutes();
}

public function _after(TestCase $test)
{
$this->resetApplication();
$this->fixtureManager->unload($this->testCase);
if ($this->testCase->dropTables) {
$this->fixtureManager->shutDown();
}
}
// @codingStandardsIgnoreEnd

public function loadFixtures($fixtures)
{
if (func_num_args() > 1) {
$fixtures = func_get_args();
}
$this->testCase->fixtures = $fixtures;
$this->fixtureManager->fixturize($this->testCase);
$this->fixtureManager->load($this->testCase);
}

/**
* Asserts the expected CakePHP version.
*
* @param string $ver Expected version.
* @param string $operator Comparison to run, defaults to greater or equal.
* @
*/
public function expectedCakePHPVersion($ver, $operator = 'ge')
{
$this->assertTrue(version_compare($ver, Configure::version(), $operator));
}

/**
* Returns one of the instantiated services
*
Expand All @@ -139,31 +48,6 @@ protected function reloadRoutes()
Router::reload();
}

/**
* Resets the application's configuration.
*
* @return void
*/
protected function resetApplication()
{
if (!empty($this->configure)) {
Configure::clear();
Configure::write($this->configure);
}
}

/**
* Snapshots the application's configuration.
*
* @return void
*/
protected function snapshotApplication()
{
if (empty($this->configure)) {
$this->configure = Configure::read();
}
}

/**
* Instantiate a connector.
*
Expand Down

0 comments on commit 7b6379f

Please sign in to comment.