Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLEANUP] Add an Environment class #148

Merged
merged 1 commit into from
Jul 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 14 additions & 55 deletions Classes/Core/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
* This class bootstraps the phpList core system.
*
* Include it from the entry point and call Bootstrap::getInstance() to get an instance,
* and $bootstrap->setApplicationContext($context) if you would like to run the application in
* the development or testing context. (For the production context, the setApplicationContext call
* is not needed).
* and $bootstrap->setEnvironment($environment) if you would like to run the application in
* the development or testing environment. (For the production environment,
* the setEnvironment call is not needed).
*
* After that, call $bootstrap->configure() and $bootstrap->dispatch().
*
Expand All @@ -26,41 +26,6 @@
*/
class Bootstrap
{
/**
* application context for running a live site
*
* @var string
*/
const APPLICATION_CONTEXT_PRODUCTION = 'prod';

/**
* application context for developing locally
*
* @var string
*/
const APPLICATION_CONTEXT_DEVELOPMENT = 'dev';

/**
* application context for running automated tests
*
* @var string
*/
const APPLICATION_CONTEXT_TESTING = 'test';

/**
* @var string
*/
const DEFAULT_APPLICATION_CONTEXT = self::APPLICATION_CONTEXT_PRODUCTION;

/**
* @var string[]
*/
private static $validApplicationContexts = [
self::APPLICATION_CONTEXT_PRODUCTION,
self::APPLICATION_CONTEXT_DEVELOPMENT,
self::APPLICATION_CONTEXT_TESTING,
];

/**
* @var Bootstrap|null
*/
Expand All @@ -74,7 +39,7 @@ class Bootstrap
/**
* @var string
*/
private $applicationContext = self::DEFAULT_APPLICATION_CONTEXT;
private $environment = Environment::DEFAULT_ENVIRONMENT;

/**
* @var EntityManager
Expand Down Expand Up @@ -135,56 +100,50 @@ public static function purgeInstance()
}

/**
* @param string $context must be one of the APPLICATION_CONTEXT_* constants
* @param string $environment must be one of the Environment::* constants
*
* @return Bootstrap fluent interface
*
* @throws \UnexpectedValueException
*/
public function setApplicationContext(string $context): Bootstrap
public function setEnvironment(string $environment): Bootstrap
{
if (!in_array($context, self::$validApplicationContexts, true)) {
throw new \UnexpectedValueException(
'$context must be one of "Production", "Development", or "Testing", but actually is: ' . $context,
1499112172108
);
}

$this->applicationContext = $context;
Environment::validateEnvironment($environment);
$this->environment = $environment;

return $this;
}

/**
* @return string
*/
public function getApplicationContext(): string
public function getEnvironment(): string
{
return $this->applicationContext;
return $this->environment;
}

/**
* @return bool
*/
private function isDoctrineOrmDevelopmentModeEnabled(): bool
{
return $this->applicationContext !== self::APPLICATION_CONTEXT_PRODUCTION;
return $this->environment !== Environment::PRODUCTION;
}

/**
* @return bool
*/
private function isSymfonyDebugModeEnabled(): bool
{
return $this->applicationContext !== self::APPLICATION_CONTEXT_PRODUCTION;
return $this->environment !== Environment::PRODUCTION;
}

/**
* @return bool
*/
private function isDebugEnabled(): bool
{
return $this->applicationContext !== self::APPLICATION_CONTEXT_PRODUCTION;
return $this->environment !== Environment::PRODUCTION;
}

/**
Expand Down Expand Up @@ -308,7 +267,7 @@ private function configureDoctrineOrm(): Bootstrap
private function configureApplicationKernel(): Bootstrap
{
$this->applicationKernel = new ApplicationKernel(
$this->getApplicationContext(),
$this->getEnvironment(),
$this->isSymfonyDebugModeEnabled()
);

Expand Down
70 changes: 70 additions & 0 deletions Classes/Core/Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);

namespace PhpList\PhpList4\Core;

/**
* This class provides methods and constants for the application environment/context.
*
* @author Oliver Klee <[email protected]>
*/
final class Environment
{
/**
* environment for running a live site
*
* @var string
*/
const PRODUCTION = 'prod';

/**
* environment for developing locally
*
* @var string
*/
const DEVELOPMENT = 'dev';

/**
* environment for running automated tests
*
* @var string
*/
const TESTING = 'test';

/**
* @var string
*/
const DEFAULT_ENVIRONMENT = self::PRODUCTION;

/**
* @var string[]
*/
private static $validEnvironments = [self::PRODUCTION, self::DEVELOPMENT, self::TESTING];

/**
* Private constructor to avoid instantiation.
*/
private function __construct()
{
}

/**
* Validates that $environment is a valid environment, and throws an exception otherwise.
*
* @param string $environment must be one of the environment constants
*
* @return void
*
* @throws \UnexpectedValueException
*/
public static function validateEnvironment(string $environment)
{
if (!in_array($environment, self::$validEnvironments, true)) {
$environmentsText = '"' . implode('", ', self::$validEnvironments) . '"';
throw new \UnexpectedValueException(
'$environment must be one of ' . $environmentsText . ', but actually was: "' . $environment . '"',
1499112172108
);
}
}
}
2 changes: 1 addition & 1 deletion Configuration/PHPMD/rules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<rule ref="rulesets/cleancode.xml/StaticAccess">
<properties>
<property name="exceptions"
value="\Doctrine\ORM\EntityManager,\Doctrine\ORM\Tools\Setup,\Symfony\Component\Debug\Debug"/>
value="\Doctrine\ORM\EntityManager,\Doctrine\ORM\Tools\Setup,\Symfony\Component\Debug\Debug,PhpList\PhpList4\Core\Environment"/>
</properties>
</rule>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace PhpList\PhpList4\Tests\Integration\ApplicationBundle\Controller;

use PhpList\PhpList4\Core\Bootstrap;
use PhpList\PhpList4\Core\Environment;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

Expand All @@ -21,9 +22,9 @@ class DefaultControllerTest extends WebTestCase

protected function setUp()
{
Bootstrap::getInstance()->setApplicationContext(Bootstrap::APPLICATION_CONTEXT_TESTING)->configure();
Bootstrap::getInstance()->setEnvironment(Environment::TESTING)->configure();

$this->client = self::createClient(['environment' => Bootstrap::APPLICATION_CONTEXT_TESTING]);
$this->client = self::createClient(['environment' => Environment::TESTING]);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions Tests/Integration/Core/ApplicationKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use PhpList\PhpList4\Core\ApplicationKernel;
use PhpList\PhpList4\Core\Bootstrap;
use PhpList\PhpList4\Core\Environment;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -21,7 +22,7 @@ class ApplicationKernelTest extends TestCase

protected function setUp()
{
$this->subject = new ApplicationKernel(Bootstrap::APPLICATION_CONTEXT_TESTING, true);
$this->subject = new ApplicationKernel(Environment::TESTING, true);
}

protected function tearDown()
Expand Down Expand Up @@ -67,7 +68,7 @@ private function getApplicationRoot(): string
public function getCacheDirReturnsEnvironmentSpecificVarCacheDirectoryInApplicationRoot()
{
self::assertSame(
$this->getApplicationRoot() . '/var/cache/' . Bootstrap::APPLICATION_CONTEXT_TESTING,
$this->getApplicationRoot() . '/var/cache/' . Environment::TESTING,
$this->subject->getCacheDir()
);
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/Integration/Core/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace PhpList\PhpList4\Tests\Integration\Core;

use PhpList\PhpList4\Core\Bootstrap;
use PhpList\PhpList4\Core\Environment;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -21,7 +22,7 @@ class BootstrapTest extends TestCase
protected function setUp()
{
$this->subject = Bootstrap::getInstance();
$this->subject->setApplicationContext(Bootstrap::APPLICATION_CONTEXT_TESTING);
$this->subject->setEnvironment(Environment::TESTING);
}

protected function tearDown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Doctrine\ORM\EntityManagerInterface;
use PhpList\PhpList4\Core\Bootstrap;
use PhpList\PhpList4\Core\Environment;
use PhpList\PhpList4\Domain\Model\Interfaces\Identity;
use PHPUnit\DbUnit\Database\Connection;
use PHPUnit\DbUnit\DataSet\CsvDataSet;
Expand Down Expand Up @@ -50,8 +51,7 @@ abstract class AbstractRepositoryTest extends TestCase
protected function setUp()
{
$this->initializeDatabaseTester();
$this->bootstrap = Bootstrap::getInstance()->setApplicationContext(Bootstrap::APPLICATION_CONTEXT_TESTING)
->configure();
$this->bootstrap = Bootstrap::getInstance()->setEnvironment(Environment::TESTING)->configure();
$this->entityManager = $this->bootstrap->getEntityManager();
self::assertTrue($this->entityManager->isOpen());
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/Unit/Core/ApplicationKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use PhpList\PhpList4\Core\ApplicationKernel;
use PhpList\PhpList4\Core\Bootstrap;
use PhpList\PhpList4\Core\Environment;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Kernel;

Expand All @@ -22,7 +23,7 @@ class ApplicationKernelTest extends TestCase

protected function setUp()
{
$this->subject = new ApplicationKernel(Bootstrap::APPLICATION_CONTEXT_TESTING, true);
$this->subject = new ApplicationKernel(Environment::TESTING, true);
}

protected function tearDown()
Expand Down
Loading