Skip to content

Commit

Permalink
Fix deprecation messages not showing (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloKowalczyk authored Jan 6, 2019
1 parent 19369f5 commit 4aa7ae4
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 3 deletions.
5 changes: 4 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

require_once __DIR__ . '/vendor/autoload.php';

$envFlags = new \Crunz\EnvFlags\EnvFlags();
$envFlags->disableDeprecationHandler();

if (\strpos(\getcwd(), 'tests') !== false) {
return;
}

if (!\chdir('tests')) {
throw new RuntimeException("Unable to change currenjt directory to 'tests'.");
throw new RuntimeException("Unable to change current directory to 'tests'.");
}
5 changes: 5 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@
)
;

$container
->register(\Crunz\EnvFlags\EnvFlags::class, \Crunz\EnvFlags\EnvFlags::class)
->setPublic(true)
;

foreach ($simpleServices as $id => $simpleService) {
if (!\is_string($id)) {
$id = $simpleService;
Expand Down
12 changes: 10 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Crunz;

use Crunz\EnvFlags\EnvFlags;
use Crunz\Path\Path;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -57,6 +58,7 @@ public function __construct($appName, $appVersion)
parent::__construct($appName, $appVersion);

$this->initializeContainer();
$this->registerDeprecationHandler();

foreach (self::COMMANDS as $commandClass) {
$command = $this->container
Expand All @@ -79,8 +81,6 @@ public function run(InputInterface $input = null, OutputInterface $output = null
->get(InputInterface::class);
}

$this->registerDeprecationHandler();

return parent::run($input, $output);
}

Expand Down Expand Up @@ -221,6 +221,14 @@ private function getContainerCacheDir()

private function registerDeprecationHandler()
{
/** @var EnvFlags $envFlags */
$envFlags = $this->container
->get(EnvFlags::class);

if (!$envFlags->isDeprecationHandlerEnabled()) {
return;
}

$io = $this->container
->get(SymfonyStyle::class);

Expand Down
39 changes: 39 additions & 0 deletions src/EnvFlags/EnvFlags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Crunz\EnvFlags;

use Crunz\Exception\CrunzException;

final class EnvFlags
{
const DEPRECATION_HANDLER_FLAG = 'CRUNZ_DEPRECATION_HANDLER';

/** @return bool */
public function isDeprecationHandlerEnabled()
{
$registerHandlerEnv = \getenv(self::DEPRECATION_HANDLER_FLAG, true);
$registerHandler = true;

if (false !== $registerHandlerEnv) {
$registerHandler = \filter_var($registerHandlerEnv, FILTER_VALIDATE_BOOLEAN);
}

return $registerHandler;
}

/** @throws CrunzException When enabling deprecation handler fails */
public function enableDeprecationHandler()
{
if (false === \putenv(self::DEPRECATION_HANDLER_FLAG . '=1')) {
throw new CrunzException('Enabling deprecation handler failed.');
}
}

/** @throws CrunzException When disabling deprecation handler fails */
public function disableDeprecationHandler()
{
if (false === \putenv(self::DEPRECATION_HANDLER_FLAG . '=0')) {
throw new CrunzException('Enabling deprecation handler failed.');
}
}
}
35 changes: 35 additions & 0 deletions tests/EndToEnd/DeprecationMessagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Crunz\Tests\EndToEnd;

use Crunz\Path\Path;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

final class DeprecationMessagesTest extends TestCase
{
/** @test */
public function earlyDeprecationShouldBeVisible()
{
$path = Path::create(
[
'bin',
'deprecation-application',
'crunz',
]
);
$command = PHP_BINARY . " {$path->toString()}";

if (\method_exists(Process::class, 'fromShellCommandline')) {
$process = Process::fromShellCommandline($command);
} else {
$process = new Process($command);
}

$process->start();
$process->wait();

$this->assertSame(0, $process->getExitCode());
$this->assertContains('[Deprecation] Test deprecation', $process->getOutput());
}
}
58 changes: 58 additions & 0 deletions tests/Unit/EnvFlags/EnvFlagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Crunz\Tests\Unit\EnvFlags;

use Crunz\EnvFlags\EnvFlags;
use PHPUnit\Framework\TestCase;

final class EnvFlagsTest extends TestCase
{
/**
* @test
* @dataProvider statusProvider
*/
public function deprecationHandlerStatusIsCorrect($flagValue, $expectedEnabled)
{
\putenv(EnvFlags::DEPRECATION_HANDLER_FLAG . "={$flagValue}");

$envFlags = new EnvFlags();
$this->assertSame($expectedEnabled, $envFlags->isDeprecationHandlerEnabled());
}

/** @test */
public function deprecationHandlerCanBeDisabled()
{
$envFlags = new EnvFlags();
$envFlags->disableDeprecationHandler();

$this->assertFlagValue('0');
}

/** @test */
public function deprecationHandlerCanBeEnabled()
{
$envFlags = new EnvFlags();
$envFlags->enableDeprecationHandler();

$this->assertFlagValue('1');
}

public function statusProvider()
{
yield 'true' => [
'1',
true,
];

yield 'false' => [
'0',
false,
];
}

private function assertFlagValue($expectedValue)
{
$actualValue = \getenv(EnvFlags::DEPRECATION_HANDLER_FLAG);
$this->assertSame($expectedValue, $actualValue);
}
}
13 changes: 13 additions & 0 deletions tests/bin/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$root = \dirname(\dirname(__DIR__));
$autoloaderPath = \implode(
DIRECTORY_SEPARATOR,
[
$root,
'vendor',
'autoload.php',
]
);

require_once $autoloaderPath;
15 changes: 15 additions & 0 deletions tests/bin/deprecation-application/crunz
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env php
<?php

require_once \dirname(__DIR__) . DIRECTORY_SEPARATOR . 'bootstrap.php';

// Enable Crunz deprecation handler because
// it is disabled in test env
\putenv('CRUNZ_DEPRECATION_HANDLER=1');

$application = new \Crunz\Application('Crunz', 'dev-test');

// Trigger deprecation early to test handler registration
@trigger_error('Test deprecation', E_USER_DEPRECATED);

$application->run();

0 comments on commit 4aa7ae4

Please sign in to comment.