Skip to content

Commit

Permalink
Merge pull request #1136: apply Rector on tests files
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Sep 5, 2024
2 parents ac677c1 + 5157391 commit 4baf322
Show file tree
Hide file tree
Showing 78 changed files with 246 additions and 472 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,3 @@ clover.xml
.env
builds
build

/src/Prototype/tests/runtime/cache/
/src/Scaffolder/tests/App/runtime/cache/
33 changes: 31 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector;
use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPublicMethodParameterRector;
use Rector\DeadCode\Rector\Expression\RemoveDeadStmtRector;
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector;
Expand All @@ -17,6 +20,7 @@
return RectorConfig::configure()
->withPaths([
__DIR__ . '/src/*/src',
__DIR__ . '/src/*/tests',
__DIR__ . '/tests',
])
->withParallel()
Expand All @@ -30,13 +34,17 @@
__DIR__ . '/src/Scaffolder/src/Command/FilterCommand.php',
__DIR__ . '/src/Scaffolder/src/Command/JobHandlerCommand.php',
__DIR__ . '/src/Scaffolder/src/Command/MiddlewareCommand.php',
__DIR__ . '/src/Console/tests/PromptArgumentsTest.php',
],
RemoveUnusedPrivateMethodRector::class => [
__DIR__ . '/src/Boot/src/Bootloader/ConfigurationBootloader.php',
__DIR__ . '/src/Broadcasting/src/Bootloader/BroadcastingBootloader.php',
__DIR__ . '/src/Cache/src/Bootloader/CacheBootloader.php',
__DIR__ . '/src/Serializer/src/Bootloader/SerializerBootloader.php',
__DIR__ . '/src/Validation/src/Bootloader/ValidationBootloader.php',
__DIR__ . '/src/Translator/tests/IndexerTest.php',
__DIR__ . '/src/Tokenizer/tests/ReflectionFileTest.php',
__DIR__ . '/src/Core/tests/SingletonsTest.php',
],
RemoveUselessVarTagRector::class => [
__DIR__ . '/src/Console/src/Traits/HelpersTrait.php',
Expand All @@ -54,16 +62,37 @@
],
RemoveUnusedPrivateMethodParameterRector::class => [
__DIR__ . '/src/Core/src/Internal/Factory.php',
__DIR__ . '/src/Core/tests/InjectableTest.php',
],
RemoveDoubleAssignRector::class => [
__DIR__ . '/src/Core/tests/Scope/FinalizeAttributeTest.php',
],
RemoveUnusedVariableAssignRector::class => [
__DIR__ . '/src/Core/tests/ExceptionsTest.php',
],
RemoveDeadStmtRector::class => [
__DIR__ . '/src/Core/tests/ExceptionsTest.php',
],

// to be enabled later after upgrade to 1.2.4 merged
// to easier to review
// to be enabled later for bc break 4.x
RemoveUnusedPublicMethodParameterRector::class,
RemoveEmptyClassMethodRector::class,
RemoveUnusedPromotedPropertyRector::class,

// start with short open tag
__DIR__ . '/src/Views/tests/fixtures/other/var.php',
__DIR__ . '/tests/app/views/native.php',

// example code for test
'*/Fixtures/*',
'*/Stub/*',
'*/Stubs/*',
'*/tests/Classes/*',
'*/tests/Internal/*',
__DIR__ . '/src/Console/tests/Configurator',

// cache
'*/runtime/cache/*',
])
->withPhpSets(php74: true)
->withPreparedSets(deadCode: true);
11 changes: 8 additions & 3 deletions src/AnnotatedRoutes/tests/RouteLocatorListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ private function configureRouter(): void
$this->container = new Container();

$this->container->bindSingleton(UriFactoryInterface::class, new Psr17Factory());
$this->container->bindSingleton(RouterInterface::class, static function (UriHandler $handler, Container $container) {
return new Router('/', $handler, $container);
});
$this->container->bindSingleton(
RouterInterface::class,
static fn(UriHandler $handler, Container $container) => new Router(
'/',
$handler,
$container,
),
);
$this->container->bindSingleton(GroupRegistry::class, new GroupRegistry($this->container));

$this->listener = new RouteLocatorListener(
Expand Down
10 changes: 1 addition & 9 deletions src/AuthHttp/tests/Diactoros/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,12 @@ final class ResponseFactory implements ResponseFactoryInterface
/** @var HttpConfig */
protected $config;

/**
* @param HttpConfig $config
*/
public function __construct(HttpConfig $config)
{
$this->config = $config;
}

/**
* @param int $code
* @param string $reasonPhrase
*
* @return ResponseInterface
*/

public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
$response = new Response($code);
Expand Down
4 changes: 0 additions & 4 deletions src/AuthHttp/tests/Diactoros/UriFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

final class UriFactory implements UriFactoryInterface
{
/**
* @param string $uri
* @return UriInterface
*/
public function createUri(string $uri = ''): UriInterface
{
return new Uri($uri);
Expand Down
2 changes: 1 addition & 1 deletion src/Boot/tests/ConfigsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testCustomConfigLoader(): void
'config' => __DIR__ . '/config',
])->run();

/** @var ConfiguratorInterface $config */
/** @var ConfiguratorInterface $configurator */
$configurator = $core->getContainer()->get(ConfiguratorInterface::class);

$this->assertSame(['test-key' => 'test value'], $configurator->getConfig('yaml'));
Expand Down
2 changes: 0 additions & 2 deletions src/Boot/tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public function testSetNullValueWithoutOverwriting(): void
}

/**
* @param array $env
* @return EnvironmentInterface
* @throws \Throwable
*/
protected function getEnv(array $env, bool $overwite= true): EnvironmentInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ public function testAuthorizationEventsShouldBeDispatched(string $event, bool $a
$handler = m::mock(RequestHandlerInterface::class);
$dispatcher = m::mock(EventDispatcherInterface::class);
$status = new AuthorizationStatus($authStatus, ['topic_name'], ['foo' => 'bar']);
$dispatcher->shouldReceive('dispatch')->once()->withArgs(function (Authorized $e) use($event, $status) {
return $e->status === $status && $e::class === $event;
});
$dispatcher->shouldReceive('dispatch')->once()->withArgs(fn(Authorized $e) => $e->status === $status && $e::class === $event);

$middleware = new AuthorizationMiddleware(
$broadcast = m::mock(BroadcastInterface::class, GuardInterface::class),
Expand All @@ -179,11 +177,9 @@ public function testAuthorizationEventsWithoutGuardShouldBeDispatched(): void
$request = m::mock(ServerRequestInterface::class);
$handler = m::mock(RequestHandlerInterface::class);
$dispatcher = m::mock(EventDispatcherInterface::class);
$dispatcher->shouldReceive('dispatch')->once()->withArgs(function (Authorized $event) {
return $event->status->success === true
&& $event->status->topics === null
&& $event->status->response === null;
});
$dispatcher->shouldReceive('dispatch')->once()->withArgs(fn(Authorized $event) => $event->status->success === true
&& $event->status->topics === null
&& $event->status->response === null);

$middleware = new AuthorizationMiddleware(
m::mock(BroadcastInterface::class),
Expand Down
3 changes: 0 additions & 3 deletions src/Console/tests/ConfigureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ public function testNoBreakFailure(): void
$this->assertEquals(1, $output->getCode());
}

/**
* @return Console
*/
private function bindFailure(): Console
{
$core = $this->getCore(
Expand Down
23 changes: 13 additions & 10 deletions src/Console/tests/InterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ public function testInterceptorShouldBeResolved(): void

$interceptor->shouldReceive('process')
->once()
->withArgs(function (string $controller, string $action, array $parameters, CoreInterface $core) {
return $controller === TestCommand::class
&& $action === 'perform'
&& $parameters['input'] instanceof InputInterface
&& $parameters['output'] instanceof OutputInterface
&& $parameters['command'] instanceof TestCommand;
})
->andReturnUsing(function (string $controller, string $action, array $parameters, CoreInterface $core) {
return $core->callAction($controller, $action, $parameters);
});
->withArgs(fn(string $controller, string $action, array $parameters, CoreInterface $core) => $controller === TestCommand::class
&& $action === 'perform'
&& $parameters['input'] instanceof InputInterface
&& $parameters['output'] instanceof OutputInterface
&& $parameters['command'] instanceof TestCommand)
->andReturnUsing(
fn(
string $controller,
string $action,
array $parameters,
CoreInterface $core,
) => $core->callAction($controller, $action, $parameters),
);

$core = $this->getCore($this->getStaticLocator([
TestCommand::class
Expand Down
3 changes: 0 additions & 3 deletions src/Console/tests/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ public function testNoBreakFailure(): void
$this->assertEquals(1, $output->getCode());
}

/**
* @return Console
*/
private function bindFailure(): Console
{
$core = $this->getCore(
Expand Down
24 changes: 7 additions & 17 deletions src/Cookies/tests/CookiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ public function testSetNotProtectedCookie(): void
public function testDecrypt(): void
{
$core = $this->httpCore([CookiesMiddleware::class]);
$core->setHandler(function (ServerRequestInterface $r) {
return $r->getCookieParams()['name'];
});
$core->setHandler(fn(ServerRequestInterface $r) => $r->getCookieParams()['name']);

$value = $this->container->get(EncrypterInterface::class)->encrypt('cookie-value');

Expand All @@ -118,9 +116,7 @@ public function testDecrypt(): void
public function testDecryptArray(): void
{
$core = $this->httpCore([CookiesMiddleware::class]);
$core->setHandler(function (ServerRequestInterface $r) {
return $r->getCookieParams()['name'][0];
});
$core->setHandler(fn(ServerRequestInterface $r) => $r->getCookieParams()['name'][0]);

$value[] = $this->container->get(EncrypterInterface::class)->encrypt('cookie-value');

Expand All @@ -132,9 +128,7 @@ public function testDecryptArray(): void
public function testDecryptBroken(): void
{
$core = $this->httpCore([CookiesMiddleware::class]);
$core->setHandler(function (ServerRequestInterface $r) {
return $r->getCookieParams()['name'];
});
$core->setHandler(fn(ServerRequestInterface $r) => $r->getCookieParams()['name']);

$value = $this->container->get(EncrypterInterface::class)->encrypt('cookie-value') . 'BROKEN';

Expand Down Expand Up @@ -195,9 +189,7 @@ public function testGetUnprotected(): void
]));

$core = $this->httpCore([CookiesMiddleware::class]);
$core->setHandler(function (ServerRequestInterface $r) {
return $r->getCookieParams()['name'];
});
$core->setHandler(fn(ServerRequestInterface $r) => $r->getCookieParams()['name']);

$value = 'cookie-value';

Expand Down Expand Up @@ -228,9 +220,7 @@ public function testHMAC(): void
$cookies = $this->fetchCookies($response);
$this->assertArrayHasKey('name', $cookies);

$core->setHandler(function ($r) {
return $r->getCookieParams()['name'];
});
$core->setHandler(fn($r) => $r->getCookieParams()['name']);

$response = $this->get($core, '/', [], [], $cookies);
$this->assertSame(200, $response->getStatusCode());
Expand Down Expand Up @@ -286,9 +276,9 @@ private function fetchCookies(ResponseInterface $response): array
foreach ($response->getHeaders() as $line) {
$cookie = explode('=', implode('', $line));
$result[$cookie[0]] = rawurldecode(substr(
(string)$cookie[1],
$cookie[1],
0,
(int)strpos((string)$cookie[1], ';')
(int)strpos($cookie[1], ';')
));
}

Expand Down
10 changes: 1 addition & 9 deletions src/Cookies/tests/TestResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,12 @@ final class TestResponseFactory implements ResponseFactoryInterface
/** @var HttpConfig */
protected $config;

/**
* @param HttpConfig $config
*/
public function __construct(HttpConfig $config)
{
$this->config = $config;
}

/**
* @param int $code
* @param string $reasonPhrase
*
* @return ResponseInterface
*/

public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
$response = new Response($code);
Expand Down
8 changes: 2 additions & 6 deletions src/Core/tests/BindingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public function testBasicBinding(): void

$this->assertFalse($container->has('abc'));

$container->bind('abc', function () {
return 'hello';
});
$container->bind('abc', fn() => 'hello');

$this->assertTrue($container->has('abc'));
$this->assertEquals('hello', $container->get('abc'));
Expand All @@ -33,9 +31,7 @@ public function testStringBinding(): void
$container = new Container();

$this->assertFalse($container->has('abc'));
$container->bind('abc', function () {
return 'hello';
});
$container->bind('abc', fn() => 'hello');

$container->bind('dce', 'abc');

Expand Down
11 changes: 2 additions & 9 deletions src/Core/tests/InjectableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ public function testInjector(): void
$container->bind(ConfigsInterface::class, $configurator);

$configurator->shouldReceive('createInjection')
->with(m::on(static function (ReflectionClass $r) {
return $r->getName() === TestConfig::class;
}), null)
->with(m::on(static fn(ReflectionClass $r) => $r->getName() === TestConfig::class), null)
->andReturn($expected);

$this->assertSame($expected, $container->get(TestConfig::class));
Expand All @@ -119,9 +117,7 @@ public function testInjectorWithContext(): void
$container->bind(ConfigsInterface::class, $configurator);

$configurator->shouldReceive('createInjection')
->with(m::on(static function (ReflectionClass $r) {
return $r->getName() === TestConfig::class;
}), 'context')
->with(m::on(static fn(ReflectionClass $r) => $r->getName() === TestConfig::class), 'context')
->andReturn($expected);

$this->assertSame($expected, $container->get(TestConfig::class, 'context'));
Expand Down Expand Up @@ -236,9 +232,6 @@ public function createInjection(\ReflectionClass $class, mixed $context = null):
$this->assertInstanceOf(\ReflectionParameter::class, $result->context);
}

/**
* @param TestConfig $contextArgument
*/
private function methodInjection(TestConfig $contextArgument): void
{
}
Expand Down
8 changes: 2 additions & 6 deletions src/Core/tests/InvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ public function testCallValidClosure(): void
$this->container->bindSingleton(Bucket::class, $bucket = new Bucket('foo'));

$result = $this->container->invoke(
static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') {
return \compact('bucket', 'class', 'name', 'path');
},
static fn(Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') => \compact('bucket', 'class', 'name', 'path'),
['name' => 'bar']
);

Expand All @@ -113,9 +111,7 @@ public function testCallValidClosureWithNotResolvableDependencies(): void
$this->expectExceptionMessage('Unable to resolve required argument `name` when resolving');

$this->container->invoke(
static function (Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') {
return \compact('bucket', 'class', 'name', 'path');
},
static fn(Bucket $bucket, SampleClass $class, string $name, string $path = 'baz') => \compact('bucket', 'class', 'name', 'path'),
['name' => 'bar']
);
}
Expand Down
Loading

0 comments on commit 4baf322

Please sign in to comment.