-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix deprecation messages not showing (#180)
- Loading branch information
1 parent
19369f5
commit 4aa7ae4
Showing
8 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |