Skip to content

Commit

Permalink
Extract skipping or not a finder on Windows into a decorator finder (#77
Browse files Browse the repository at this point in the history
)

This offers more flexibility to both the user and internally for diagnosis purposes.
  • Loading branch information
theofidry authored Dec 10, 2022
1 parent bb9bc13 commit 4d56f0b
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 54 deletions.
6 changes: 4 additions & 2 deletions e2e/expected-output-osx
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ NProcessorFinder: .
NProcFinder(all=true): F
NProcFinder(all=false): F
NullCpuCoreFinder: F
WindowsWmicPhysicalFinder: F
WindowsWmicLogicalFinder: F
OnlyOnWindowsFinder(DummyCpuCoreFinder(value=1)): F
SkipOnWindowsFinder(DummyCpuCoreFinder(value=1)): .
WmicPhysicalFinder: F
WmicLogicalFinder: F
6 changes: 4 additions & 2 deletions e2e/expected-output-ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ NProcessorFinder: F
NProcFinder(all=true): .
NProcFinder(all=false): .
NullCpuCoreFinder: F
WindowsWmicPhysicalFinder: F
WindowsWmicLogicalFinder: F
OnlyOnWindowsFinder(DummyCpuCoreFinder(value=1)): F
SkipOnWindowsFinder(DummyCpuCoreFinder(value=1)): .
WmicPhysicalFinder: F
WmicLogicalFinder: F
6 changes: 4 additions & 2 deletions e2e/expected-output-windows
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ NProcessorFinder: F
NProcFinder(all=true): F
NProcFinder(all=false): F
NullCpuCoreFinder: F
WindowsWmicPhysicalFinder: F
WindowsWmicLogicalFinder: F
OnlyOnWindowsFinder(DummyCpuCoreFinder(value=1)): .
SkipOnWindowsFinder(DummyCpuCoreFinder(value=1)): F
WmicPhysicalFinder: F
WmicLogicalFinder: F
14 changes: 10 additions & 4 deletions src/Finder/FinderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ public static function getAllVariants(): array
new NProcFinder(true),
new NProcFinder(false),
new NullCpuCoreFinder(),
new WindowsWmicPhysicalFinder(),
new WindowsWmicLogicalFinder(),
new OnlyOnWindowsFinder(
new DummyCpuCoreFinder(1)
),
new SkipOnWindowsFinder(
new DummyCpuCoreFinder(1)
),
new WmicPhysicalFinder(),
new WmicLogicalFinder(),
];
}

Expand All @@ -41,7 +47,7 @@ public static function getAllVariants(): array
public static function getDefaultLogicalFinders(): array
{
return [
new WindowsWmicLogicalFinder(),
new OnlyOnWindowsFinder(new WmicLogicalFinder()),
new NProcFinder(),
new HwLogicalFinder(),
new LinuxyNProcessorFinder(),
Expand All @@ -56,7 +62,7 @@ public static function getDefaultLogicalFinders(): array
public static function getDefaultPhysicalFinders(): array
{
return [
new WindowsWmicPhysicalFinder(),
new OnlyOnWindowsFinder(new WmicPhysicalFinder()),
new HwPhysicalFinder(),
];
}
Expand Down
59 changes: 59 additions & 0 deletions src/Finder/OnlyOnWindowsFinder.php
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');
}
}
59 changes: 59 additions & 0 deletions src/Finder/SkipOnWindowsFinder.php
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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,13 @@

namespace Fidry\CpuCoreCounter\Finder;

use function defined;

/**
* Find the number of logical CPU cores for Windows.
*
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L912-L916
*/
final class WindowsWmicLogicalFinder extends ProcOpenBasedFinder
final class WmicLogicalFinder extends ProcOpenBasedFinder
{
/**
* @return positive-int|null
*/
public function find(): ?int
{
if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
// Skip if not on Windows. Rely on PHP to detect the platform
// rather than reading the platform name or others.
return null;
}

return parent::find();
}

protected function getCommand(): string
{
return 'wmic cpu get NumberOfLogicalProcessors';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,13 @@

namespace Fidry\CpuCoreCounter\Finder;

use function defined;

/**
* Find the number of physical CPU cores for Windows.
*
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L912-L916
*/
final class WindowsWmicPhysicalFinder extends ProcOpenBasedFinder
final class WmicPhysicalFinder extends ProcOpenBasedFinder
{
/**
* @return positive-int|null
*/
public function find(): ?int
{
if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
// Skip if not on Windows. Rely on PHP to detect the platform
// rather than reading the platform name or others.
return null;
}

return parent::find();
}

protected function getCommand(): string
{
return 'wmic cpu get NumberOfProcessors';
Expand Down
35 changes: 35 additions & 0 deletions tests/Finder/FakeFinder.php
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.');
}
}
90 changes: 90 additions & 0 deletions tests/Finder/OnlyOnWindowsFinderTest.php
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());
}
}
Loading

0 comments on commit 4d56f0b

Please sign in to comment.