Skip to content
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
7 changes: 6 additions & 1 deletion .github/workflows/cs-fix.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
on:
---

on: # yamllint disable-line rule:truthy
push:
branches:
- '*'
Expand All @@ -10,3 +12,6 @@ jobs:
permissions:
contents: write
uses: spiral/gh-actions/.github/workflows/cs-fix.yml@master
with:
php: >-
['8.4']
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ clover*
cover*
.DS_Store
*.cache

# Runtime files (php-cs-fixer cache etc.)
/runtime
4 changes: 3 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

return \Spiral\CodeStyle\Builder::create()
->include(__DIR__ . '/src')
->include(__DIR__ . '/tests')
->include(__FILE__)
->allowRisky(true)
->cache('./runtime/php-cs-fixer.cache')
->allowRisky(false)
->build();
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
"buggregator/trap": "^1.13",
"jetbrains/phpstorm-attributes": "^1.0",
"mockery/mockery": "^1.6.7",
"spiral/code-style": "^2.2",
"spiral/code-style": "^2.3.1",
"testo/bridge-mockery": "^0.1.2",
"testo/testo": "^0.10.55",
"vimeo/psalm": "^6 || ^7"
},
"scripts": {
"cs:diff": "php-cs-fixer fix --dry-run -v --diff --show-progress dots",
"cs:diff": "php-cs-fixer fix --dry-run -v --diff",
"cs:fix": "php-cs-fixer fix -v",
"test": "testo",
"psalm": "psalm",
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ public function testFromGlobals(): void
Assert::equals($env->getVersion(), 'global_version');
Assert::equals($env->getRPCAddress(), 'tcp://127.0.0.1:6001');
}
}
}
2 changes: 1 addition & 1 deletion tests/Unit/PayloadFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,4 @@ public function testMakeControlWithUnknownCommand(string $json): void

PayloadFactory::fromFrame($frame);
}
}
}
2 changes: 1 addition & 1 deletion tests/Unit/PayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ public function testPayloadConstructionWithEosFalse(): void
Assert::equals($payload->header, '');
Assert::false($payload->eos);
}
}
}
4 changes: 2 additions & 2 deletions tests/Unit/StreamResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private function getWorker(): Worker
{
return $this->worker ??= new Worker(
relay: $this->getRelay(),
interceptSideEffects: false
interceptSideEffects: false,
);
}
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Stub/TestRelay.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function addFrame(
bool $stream = false,
bool $stopStream = false,
): self {
$head = (string)\json_encode([
$head = (string) \json_encode([
'status' => $status,
'headers' => $headers,
], \JSON_THROW_ON_ERROR);
Expand All @@ -50,7 +50,7 @@ public function getReceived(): array

public function getReceivedBody(): string
{
return \implode('', \array_map(static fn (Frame $frame)
return \implode('', \array_map(static fn(Frame $frame)
=> \substr($frame->payload, $frame->options[0] ?? 0), $this->received));
}

Expand Down
40 changes: 20 additions & 20 deletions tests/Unit/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function provideVersions(): iterable
],
],
'1.9.0',
'1.*'
'1.*',
];


Expand All @@ -37,7 +37,7 @@ public static function provideVersions(): iterable
],
],
'2.1.0',
'2.*'
'2.*',
];

yield [
Expand All @@ -50,7 +50,7 @@ public static function provideVersions(): iterable
],
],
'1.9.0',
'1.*'
'1.*',
];

yield [
Expand All @@ -60,7 +60,7 @@ public static function provideVersions(): iterable
],
],
'1.8.0',
'1.*'
'1.*',
];

yield 'calendar version' => [
Expand All @@ -70,7 +70,7 @@ public static function provideVersions(): iterable
],
],
'2025.1.15',
'2025.*'
'2025.*',
];

yield 'semver v3 after calendar versions' => [
Expand All @@ -83,7 +83,7 @@ public static function provideVersions(): iterable
],
],
'3.0.0',
'3.*'
'3.*',
];

yield 'v3 dev branch' => [
Expand All @@ -93,7 +93,7 @@ public static function provideVersions(): iterable
],
],
'3.x-dev',
'3.*'
'3.*',
];

yield 'branch alias without dots' => [
Expand All @@ -103,7 +103,7 @@ public static function provideVersions(): iterable
],
],
'dev-master',
'*'
'*',
];

yield 'non-numeric major version' => [
Expand All @@ -113,15 +113,15 @@ public static function provideVersions(): iterable
],
],
'dev-feature/1.x',
'*'
'*',
];

yield 'package without pretty version' => [
[
'spiral/roadrunner' => [],
],
'',
'*'
'*',
];

yield [
Expand All @@ -131,23 +131,16 @@ public static function provideVersions(): iterable
],
],
Version::VERSION_FALLBACK,
'*'
'*',
];

yield [
[],
Version::VERSION_FALLBACK,
'*'
'*',
];
}

#[BeforeTest]
protected function setUp(): void
{
$ref = new \ReflectionClass(InstalledVersions::class);
$ref->setStaticPropertyValue('canGetVendors', false);
}

#[DataProvider('provideVersions')]
public function testGetVersion(array $versions, string $expectedVersion, string $expectedConstraint): void
{
Expand All @@ -158,4 +151,11 @@ public function testGetVersion(array $versions, string $expectedVersion, string
Assert::same(Version::current(), $expectedVersion);
Assert::same(Version::constraint(), $expectedConstraint);
}
}

#[BeforeTest]
protected function setUp(): void
{
$ref = new \ReflectionClass(InstalledVersions::class);
$ref->setStaticPropertyValue('canGetVendors', false);
}
}
73 changes: 36 additions & 37 deletions tests/Unit/WorkerPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Spiral\RoadRunner\Tests\Worker\Unit;

use Mockery;
use Testo\Data\DataProvider;
use Mockery\MockInterface;
use Testo\Lifecycle\BeforeTest;
Expand Down Expand Up @@ -35,41 +34,61 @@ final class WorkerPoolTest
private MockInterface|RPCInterface $rpc;
private WorkerPool $workerPool;

#[BeforeTest]
protected function setUp(): void
public static function countDataProvider(): \Traversable
{
$this->rpc = Mockery::mock(RPCInterface::class)->shouldIgnoreMissing();
$this->rpc->shouldReceive('withCodec')->once()->with(Mockery::type(JsonCodec::class), Mockery::andAnyOtherArgs())->andReturnSelf();
yield [0, []];
yield [2, [self::EXAMPLE_WORKER, self::EXAMPLE_WORKER]];
}

$this->workerPool = new WorkerPool($this->rpc);
public static function getWorkersDataProvider(): \Traversable
{
yield [[], []];

$workers = \array_map(static function (array $worker): Worker {
return new Worker(
pid: $worker['pid'],
statusCode: $worker['status'],
executions: $worker['numExecs'],
createdAt: $worker['created'],
memoryUsage: $worker['memoryUsage'],
cpuUsage: $worker['CPUPercent'],
command: $worker['command'],
status: $worker['statusStr'],
);
}, [
self::EXAMPLE_WORKER,
self::EXAMPLE_WORKER,
]);

yield [$workers, [self::EXAMPLE_WORKER, self::EXAMPLE_WORKER]];
}

public function testAddWorker(): void
{
$this->rpc->shouldReceive('call')->once()->with('informer.AddWorker', 'test', Mockery::andAnyOtherArgs());
$this->rpc->shouldReceive('call')->once()->with('informer.AddWorker', 'test', \Mockery::andAnyOtherArgs());

$this->workerPool->addWorker('test');
}

#[DataProvider('countDataProvider')]
public function testCountWorkers(int $expected, array $workers): void
{
$this->rpc->shouldReceive('call')->once()->with('informer.Workers', 'test', Mockery::andAnyOtherArgs())->andReturn(['workers' => $workers]);
$this->rpc->shouldReceive('call')->once()->with('informer.Workers', 'test', \Mockery::andAnyOtherArgs())->andReturn(['workers' => $workers]);

Assert::same($this->workerPool->countWorkers('test'), $expected);
}

#[DataProvider('getWorkersDataProvider')]
public function testGetWorkers(array $expected, array $workers): void
{
$this->rpc->shouldReceive('call')->once()->with('informer.Workers', 'test', Mockery::andAnyOtherArgs())->andReturn(['workers' => $workers]);
$this->rpc->shouldReceive('call')->once()->with('informer.Workers', 'test', \Mockery::andAnyOtherArgs())->andReturn(['workers' => $workers]);

Assert::equals($this->workerPool->getWorkers('test'), new Workers($expected));
}

public function testGetWorkersMapsInformerFields(): void
{
$this->rpc->shouldReceive('call')->once()->with('informer.Workers', 'http', Mockery::andAnyOtherArgs())->andReturn([
$this->rpc->shouldReceive('call')->once()->with('informer.Workers', 'http', \Mockery::andAnyOtherArgs())->andReturn([
'workers' => [[
'pid' => 101,
'status' => 2,
Expand Down Expand Up @@ -98,7 +117,7 @@ public function testGetWorkersMapsInformerFields(): void
public function testRpcExceptionIsPropagated(): never
{
$exception = new ServiceException('Plugin not found');
$this->rpc->shouldReceive('call')->with('informer.Workers', 'unknown', Mockery::andAnyOtherArgs())->andThrow($exception);
$this->rpc->shouldReceive('call')->with('informer.Workers', 'unknown', \Mockery::andAnyOtherArgs())->andThrow($exception);

Expect::exception($exception);

Expand All @@ -107,37 +126,17 @@ public function testRpcExceptionIsPropagated(): never

public function testRemoveWorker(): void
{
$this->rpc->shouldReceive('call')->once()->with('informer.RemoveWorker', 'test', Mockery::andAnyOtherArgs());
$this->rpc->shouldReceive('call')->once()->with('informer.RemoveWorker', 'test', \Mockery::andAnyOtherArgs());

$this->workerPool->removeWorker('test');
}

public static function countDataProvider(): \Traversable
{
yield [0, []];
yield [2, [self::EXAMPLE_WORKER, self::EXAMPLE_WORKER]];
}

public static function getWorkersDataProvider(): \Traversable
#[BeforeTest]
protected function setUp(): void
{
yield [[], []];
$this->rpc = \Mockery::mock(RPCInterface::class)->shouldIgnoreMissing();
$this->rpc->shouldReceive('withCodec')->once()->with(\Mockery::type(JsonCodec::class), \Mockery::andAnyOtherArgs())->andReturnSelf();

$workers = \array_map(static function (array $worker): Worker {
return new Worker(
pid: $worker['pid'],
statusCode: $worker['status'],
executions: $worker['numExecs'],
createdAt: $worker['created'],
memoryUsage: $worker['memoryUsage'],
cpuUsage: $worker['CPUPercent'],
command: $worker['command'],
status: $worker['statusStr'],
);
}, [
self::EXAMPLE_WORKER,
self::EXAMPLE_WORKER,
]);

yield [$workers, [self::EXAMPLE_WORKER, self::EXAMPLE_WORKER]];
$this->workerPool = new WorkerPool($this->rpc);
}
}
Loading
Loading