generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract skipping or not a finder on Windows into a decorator finder (#77
- Loading branch information
Showing
13 changed files
with
361 additions
and
54 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
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,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry CPUCounter Config package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\CpuCoreCounter\Finder; | ||
|
||
use function defined; | ||
use function sprintf; | ||
|
||
final class OnlyOnWindowsFinder implements CpuCoreFinder | ||
{ | ||
/** | ||
* @var CpuCoreFinder | ||
*/ | ||
private $decoratedFinder; | ||
|
||
public function __construct(CpuCoreFinder $decoratedFinder) | ||
{ | ||
$this->decoratedFinder = $decoratedFinder; | ||
} | ||
|
||
public function diagnose(): string | ||
{ | ||
return self::skip() | ||
? 'Non-windows platform detected (PHP_WINDOWS_VERSION_MAJOR is not set).' | ||
: $this->decoratedFinder->diagnose(); | ||
} | ||
|
||
public function find(): ?int | ||
{ | ||
return self::skip() | ||
? null | ||
: $this->decoratedFinder->find(); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return sprintf( | ||
'OnlyOnWindowsFinder(%s)', | ||
$this->decoratedFinder->toString() | ||
); | ||
} | ||
|
||
private static function skip(): bool | ||
{ | ||
// Skip if not on Windows. Rely on PHP to detect the platform | ||
// rather than reading the platform name or others. | ||
return !defined('PHP_WINDOWS_VERSION_MAJOR'); | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry CPUCounter Config package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\CpuCoreCounter\Finder; | ||
|
||
use function defined; | ||
use function sprintf; | ||
|
||
final class SkipOnWindowsFinder implements CpuCoreFinder | ||
{ | ||
/** | ||
* @var CpuCoreFinder | ||
*/ | ||
private $decoratedFinder; | ||
|
||
public function __construct(CpuCoreFinder $decoratedFinder) | ||
{ | ||
$this->decoratedFinder = $decoratedFinder; | ||
} | ||
|
||
public function diagnose(): string | ||
{ | ||
return self::skip() | ||
? 'Windows platform detected (PHP_WINDOWS_VERSION_MAJOR is set).' | ||
: $this->decoratedFinder->diagnose(); | ||
} | ||
|
||
public function find(): ?int | ||
{ | ||
return self::skip() | ||
? null | ||
: $this->decoratedFinder->find(); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return sprintf( | ||
'SkipOnWindowsFinder(%s)', | ||
$this->decoratedFinder->toString() | ||
); | ||
} | ||
|
||
private static function skip(): bool | ||
{ | ||
// Skip if on Windows. Rely on PHP to detect the platform | ||
// rather than reading the platform name or others. | ||
return defined('PHP_WINDOWS_VERSION_MAJOR'); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry CPUCounter Config package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\CpuCoreCounter\Test\Finder; | ||
|
||
use DomainException; | ||
use Fidry\CpuCoreCounter\Finder\CpuCoreFinder; | ||
|
||
final class FakeFinder implements CpuCoreFinder | ||
{ | ||
public function diagnose(): string | ||
{ | ||
throw new DomainException('Not implemented.'); | ||
} | ||
|
||
public function find(): ?int | ||
{ | ||
throw new DomainException('Not implemented.'); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
throw new DomainException('Not implemented.'); | ||
} | ||
} |
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,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry CPUCounter Config package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\CpuCoreCounter\Test\Finder; | ||
|
||
use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder; | ||
use Fidry\CpuCoreCounter\Finder\NullCpuCoreFinder; | ||
use Fidry\CpuCoreCounter\Finder\OnlyOnWindowsFinder; | ||
use PHPUnit\Framework\TestCase; | ||
use function define; | ||
use function defined; | ||
|
||
/** | ||
* @covers \Fidry\CpuCoreCounter\Finder\OnlyOnWindowsFinder | ||
* | ||
* @internal | ||
*/ | ||
final class OnlyOnWindowsFinderTest extends TestCase | ||
{ | ||
public function test_it_can_describe_itself(): void | ||
{ | ||
$finder = new OnlyOnWindowsFinder(new NullCpuCoreFinder()); | ||
|
||
self::assertSame('OnlyOnWindowsFinder(NullCpuCoreFinder)', $finder->toString()); | ||
} | ||
|
||
public function test_it_enriches_the_decorated_finder_diagnosis(): void | ||
{ | ||
$finder = new OnlyOnWindowsFinder(new NullCpuCoreFinder()); | ||
|
||
// Sanity check | ||
self::assertFalse(defined('PHP_WINDOWS_VERSION_MAJOR')); | ||
|
||
self::assertSame( | ||
<<<'EOF' | ||
Non-windows platform detected (PHP_WINDOWS_VERSION_MAJOR is not set). | ||
EOF | ||
, | ||
$finder->diagnose() | ||
); | ||
} | ||
|
||
/** | ||
* @runInSeparateProcess | ||
*/ | ||
public function test_it_enriches_the_decorated_finder_diagnosis_on_windows(): void | ||
{ | ||
define('PHP_WINDOWS_VERSION_MAJOR', 'some_windows_version'); | ||
$finder = new OnlyOnWindowsFinder(new NullCpuCoreFinder()); | ||
|
||
self::assertSame( | ||
<<<'EOF' | ||
Will return "null". | ||
EOF | ||
, | ||
$finder->diagnose() | ||
); | ||
} | ||
|
||
public function test_it_skips_its_execution_when_not_on_windows(): void | ||
{ | ||
$finder = new OnlyOnWindowsFinder(new FakeFinder()); | ||
|
||
// Sanity check | ||
self::assertFalse(defined('PHP_WINDOWS_VERSION_MAJOR')); | ||
|
||
self::assertNull($finder->find()); | ||
} | ||
|
||
/** | ||
* @runInSeparateProcess | ||
*/ | ||
public function test_it_does_not_skip_its_execution_when_not_on_windows(): void | ||
{ | ||
define('PHP_WINDOWS_VERSION_MAJOR', 'some_windows_version'); | ||
$finder = new OnlyOnWindowsFinder(new DummyCpuCoreFinder(1)); | ||
|
||
self::assertSame(1, $finder->find()); | ||
} | ||
} |
Oops, something went wrong.