diff --git a/.github/workflows/cs-fix.yml b/.github/workflows/cs-fix.yml index 0395b27..14f585c 100644 --- a/.github/workflows/cs-fix.yml +++ b/.github/workflows/cs-fix.yml @@ -1,4 +1,6 @@ -on: +--- + +on: # yamllint disable-line rule:truthy push: branches: - '*' @@ -10,3 +12,6 @@ jobs: permissions: contents: write uses: spiral/gh-actions/.github/workflows/cs-fix.yml@master + with: + php: >- + ['8.4'] diff --git a/.gitignore b/.gitignore index 4c64f4e..d878fad 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ clover* cover* .DS_Store *.cache + +# Runtime files (php-cs-fixer cache etc.) +/runtime diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 7bad12e..45745a1 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -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(); diff --git a/composer.json b/composer.json index 9cafb82..e77c034 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/tests/Unit/EnvironmentTest.php b/tests/Unit/EnvironmentTest.php index a06293f..46625bb 100644 --- a/tests/Unit/EnvironmentTest.php +++ b/tests/Unit/EnvironmentTest.php @@ -95,4 +95,4 @@ public function testFromGlobals(): void Assert::equals($env->getVersion(), 'global_version'); Assert::equals($env->getRPCAddress(), 'tcp://127.0.0.1:6001'); } -} \ No newline at end of file +} diff --git a/tests/Unit/PayloadFactoryTest.php b/tests/Unit/PayloadFactoryTest.php index 9969136..0876341 100644 --- a/tests/Unit/PayloadFactoryTest.php +++ b/tests/Unit/PayloadFactoryTest.php @@ -156,4 +156,4 @@ public function testMakeControlWithUnknownCommand(string $json): void PayloadFactory::fromFrame($frame); } -} \ No newline at end of file +} diff --git a/tests/Unit/PayloadTest.php b/tests/Unit/PayloadTest.php index 946ec04..a06778b 100644 --- a/tests/Unit/PayloadTest.php +++ b/tests/Unit/PayloadTest.php @@ -46,4 +46,4 @@ public function testPayloadConstructionWithEosFalse(): void Assert::equals($payload->header, ''); Assert::false($payload->eos); } -} \ No newline at end of file +} diff --git a/tests/Unit/StreamResponseTest.php b/tests/Unit/StreamResponseTest.php index 4f57955..a23e58d 100644 --- a/tests/Unit/StreamResponseTest.php +++ b/tests/Unit/StreamResponseTest.php @@ -81,7 +81,7 @@ private function getWorker(): Worker { return $this->worker ??= new Worker( relay: $this->getRelay(), - interceptSideEffects: false + interceptSideEffects: false, ); } -} \ No newline at end of file +} diff --git a/tests/Unit/Stub/TestRelay.php b/tests/Unit/Stub/TestRelay.php index f218ceb..57c5e06 100644 --- a/tests/Unit/Stub/TestRelay.php +++ b/tests/Unit/Stub/TestRelay.php @@ -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); @@ -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)); } diff --git a/tests/Unit/VersionTest.php b/tests/Unit/VersionTest.php index f79150f..a010caf 100644 --- a/tests/Unit/VersionTest.php +++ b/tests/Unit/VersionTest.php @@ -26,7 +26,7 @@ public static function provideVersions(): iterable ], ], '1.9.0', - '1.*' + '1.*', ]; @@ -37,7 +37,7 @@ public static function provideVersions(): iterable ], ], '2.1.0', - '2.*' + '2.*', ]; yield [ @@ -50,7 +50,7 @@ public static function provideVersions(): iterable ], ], '1.9.0', - '1.*' + '1.*', ]; yield [ @@ -60,7 +60,7 @@ public static function provideVersions(): iterable ], ], '1.8.0', - '1.*' + '1.*', ]; yield 'calendar version' => [ @@ -70,7 +70,7 @@ public static function provideVersions(): iterable ], ], '2025.1.15', - '2025.*' + '2025.*', ]; yield 'semver v3 after calendar versions' => [ @@ -83,7 +83,7 @@ public static function provideVersions(): iterable ], ], '3.0.0', - '3.*' + '3.*', ]; yield 'v3 dev branch' => [ @@ -93,7 +93,7 @@ public static function provideVersions(): iterable ], ], '3.x-dev', - '3.*' + '3.*', ]; yield 'branch alias without dots' => [ @@ -103,7 +103,7 @@ public static function provideVersions(): iterable ], ], 'dev-master', - '*' + '*', ]; yield 'non-numeric major version' => [ @@ -113,7 +113,7 @@ public static function provideVersions(): iterable ], ], 'dev-feature/1.x', - '*' + '*', ]; yield 'package without pretty version' => [ @@ -121,7 +121,7 @@ public static function provideVersions(): iterable 'spiral/roadrunner' => [], ], '', - '*' + '*', ]; yield [ @@ -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 { @@ -158,4 +151,11 @@ public function testGetVersion(array $versions, string $expectedVersion, string Assert::same(Version::current(), $expectedVersion); Assert::same(Version::constraint(), $expectedConstraint); } -} \ No newline at end of file + + #[BeforeTest] + protected function setUp(): void + { + $ref = new \ReflectionClass(InstalledVersions::class); + $ref->setStaticPropertyValue('canGetVendors', false); + } +} diff --git a/tests/Unit/WorkerPoolTest.php b/tests/Unit/WorkerPoolTest.php index 17fab2e..7040cf9 100644 --- a/tests/Unit/WorkerPoolTest.php +++ b/tests/Unit/WorkerPoolTest.php @@ -4,7 +4,6 @@ namespace Spiral\RoadRunner\Tests\Worker\Unit; -use Mockery; use Testo\Data\DataProvider; use Mockery\MockInterface; use Testo\Lifecycle\BeforeTest; @@ -35,18 +34,38 @@ 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'); } @@ -54,7 +73,7 @@ public function testAddWorker(): void #[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); } @@ -62,14 +81,14 @@ public function testCountWorkers(int $expected, array $workers): void #[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, @@ -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); @@ -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); } } diff --git a/tests/Unit/WorkerTest.php b/tests/Unit/WorkerTest.php index 6fa6fb6..e4215f5 100644 --- a/tests/Unit/WorkerTest.php +++ b/tests/Unit/WorkerTest.php @@ -4,7 +4,6 @@ namespace Spiral\RoadRunner\Tests\Worker\Unit; -use Mockery; use Psr\Log\LoggerInterface; use Spiral\Goridge\Exception\GoridgeException; use Spiral\Goridge\Exception\TransportException; @@ -26,30 +25,30 @@ #[Test] final class WorkerTest { + public static function respondDataProvider(): \Traversable + { + yield [0, null]; + yield [Frame::CODEC_PROTO, Frame::CODEC_PROTO]; + yield [Frame::CODEC_JSON, Frame::CODEC_JSON]; + } + #[DataProvider('respondDataProvider')] public function testRespond(int $expectedFlags, ?int $codec): void { $expected = new Frame('Hello World!', [0 => 0], $expectedFlags); - $relay = Mockery::mock(RelayInterface::class)->shouldIgnoreMissing(); - $relay->shouldReceive('send')->once()->with(Mockery::isEqual($expected), Mockery::andAnyOtherArgs()); + $relay = \Mockery::mock(RelayInterface::class)->shouldIgnoreMissing(); + $relay->shouldReceive('send')->once()->with(\Mockery::isEqual($expected), \Mockery::andAnyOtherArgs()); $worker = new Worker($relay, false); $worker->respond(new Payload('Hello World!'), $codec); } - public static function respondDataProvider(): \Traversable - { - yield [0, null]; - yield [Frame::CODEC_PROTO, Frame::CODEC_PROTO]; - yield [Frame::CODEC_JSON, Frame::CODEC_JSON]; - } - public function testRespondPrependsHeaderToBody(): void { - $relay = Mockery::mock(RelayInterface::class); - $relay->expects('send')->with(Mockery::isEqual(new Frame('headerbody', [6]))); + $relay = \Mockery::mock(RelayInterface::class); + $relay->expects('send')->with(\Mockery::isEqual(new Frame('headerbody', [6]))); $worker = new Worker($relay, false); @@ -71,8 +70,8 @@ public function testRespondWithStreamChunkSetsStreamFlag(): void public function testErrorSendsErrorFrame(): void { - $relay = Mockery::mock(RelayInterface::class); - $relay->expects('send')->with(Mockery::isEqual(new Frame('Something went wrong', [], Frame::ERROR))); + $relay = \Mockery::mock(RelayInterface::class); + $relay->expects('send')->with(\Mockery::isEqual(new Frame('Something went wrong', [], Frame::ERROR))); $worker = new Worker($relay, false); @@ -81,8 +80,8 @@ public function testErrorSendsErrorFrame(): void public function testStopSendsStopHeader(): void { - $relay = Mockery::mock(RelayInterface::class); - $relay->expects('send')->with(Mockery::isEqual(new Frame('{"stop":true}', [13]))); + $relay = \Mockery::mock(RelayInterface::class); + $relay->expects('send')->with(\Mockery::isEqual(new Frame('{"stop":true}', [13]))); $worker = new Worker($relay, false); @@ -92,7 +91,7 @@ public function testStopSendsStopHeader(): void public function testRelayGoridgeExceptionIsRethrownAsTransportException(): never { $previous = new GoridgeException('Connection lost', 42); - $relay = Mockery::mock(RelayInterface::class); + $relay = \Mockery::mock(RelayInterface::class); $relay->allows('send')->andThrow($previous); $worker = new Worker($relay, false); @@ -107,7 +106,7 @@ public function testRelayGoridgeExceptionIsRethrownAsTransportException(): never public function testRelayErrorIsRethrownAsRoadRunnerException(): never { $previous = new \LogicException('Unexpected failure', 7); - $relay = Mockery::mock(RelayInterface::class); + $relay = \Mockery::mock(RelayInterface::class); $relay->allows('send')->andThrow($previous); $worker = new Worker($relay, false); @@ -345,7 +344,7 @@ public function testDefaultLogger(): void public function testCreateFromEnvironment(): void { - $logger = Mockery::mock(LoggerInterface::class); + $logger = \Mockery::mock(LoggerInterface::class); $worker = Worker::createFromEnvironment(new Environment(['RR_RELAY' => 'pipes']), false, $logger); @@ -355,7 +354,7 @@ public function testCreateFromEnvironment(): void public function testCreateUsesGlobalEnvironment(): void { - $logger = Mockery::mock(LoggerInterface::class); + $logger = \Mockery::mock(LoggerInterface::class); $backup = [$_ENV, $_SERVER]; $_SERVER['RR_RELAY'] = 'pipes';