Skip to content

Commit

Permalink
Merge pull request codeigniter4#4363 from samsonasik/rector-test-aswell
Browse files Browse the repository at this point in the history
[Rector] Apply Rector to tests directory as well
  • Loading branch information
samsonasik authored Feb 28, 2021
2 parents a88e37e + 1a08024 commit 23cc87e
Show file tree
Hide file tree
Showing 148 changed files with 1,016 additions and 673 deletions.
8 changes: 7 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector;
use Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector;
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
Expand All @@ -24,13 +25,17 @@
$parameters = $containerConfigurator->parameters();

// paths to refactor; solid alternative to CLI arguments
$parameters->set(Option::PATHS, [__DIR__ . '/app', __DIR__ . '/system']);
$parameters->set(Option::PATHS, [__DIR__ . '/app', __DIR__ . '/system', __DIR__ . '/tests']);

// is there a file you need to skip?
$parameters->set(Option::SKIP, [
__DIR__ . '/app/Views',
__DIR__ . '/system/Debug/Toolbar/Views/toolbar.tpl.php',
__DIR__ . '/system/ThirdParty',
__DIR__ . '/tests/system/Config/fixtures',
__DIR__ . '/tests/system/Models',
__DIR__ . '/tests/_support',
PassStrictParameterToFunctionParameterRector::class => [__DIR__ . '/tests/system/Database/Live/SelectTest.php'],
]);

// Rector relies on autoload setup of your project; Composer autoload is included by default; to add more:
Expand Down Expand Up @@ -61,4 +66,5 @@
$services->set(InlineIfToExplicitIfRector::class);
$services->set(PreparedValueToEarlyReturnRector::class);
$services->set(ShortenElseIfRector::class);
$services->set(RemoveUnusedForeachKeyRector::class);
};
4 changes: 2 additions & 2 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ public function addColumn(string $table, $field): bool
return false;
}

foreach ($sqls as $i => $sql)
foreach ($sqls as $sql)
{
if ($this->db->query($sql) === false)
{
Expand Down Expand Up @@ -877,7 +877,7 @@ public function modifyColumn(string $table, $field): bool

if ($sqls !== null)
{
foreach ($sqls as $i => $sql)
foreach ($sqls as $sql)
{
if ($this->db->query($sql) === false)
{
Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLSRV/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ protected function _replace(string $table, array $keys, array $values): string
$common = array_intersect($setKeys, $keyFields);

$bingo = [];
foreach ($common as $k => $v)
foreach ($common as $v)
{
$bingo[$v] = $set[$v];
}
Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLSRV/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getFieldCount(): int
public function getFieldNames(): array
{
$fieldNames = [];
foreach (sqlsrv_field_metadata($this->resultID) as $offset => $field)
foreach (sqlsrv_field_metadata($this->resultID) as $field)
{
$fieldNames[] = $field['Name'];
}
Expand Down
2 changes: 1 addition & 1 deletion system/Debug/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function run(float $startTime, float $totalTime, RequestInterface $reques
$data['vars']['post'][esc($name)] = is_array($value) ? '<pre>' . esc(print_r($value, true)) . '</pre>' : esc($value);
}

foreach ($request->headers() as $name => $header)
foreach ($request->headers() as $header)
{
$data['vars']['headers'][esc($header->getName())] = esc($header->getValueLine());
}
Expand Down
2 changes: 1 addition & 1 deletion system/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ protected function attachmentsHaveMultipart($type)
*/
protected function appendAttachments(&$body, $boundary, $multipart = null)
{
foreach ($this->attachments as $i => $attachment)
foreach ($this->attachments as $attachment)
{
if (isset($multipart) && $attachment['multipart'] !== $multipart)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Config/Filters.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
namespace Tests\Support\Config\Filters;

$filters->aliases['test-customfilter'] = \Tests\Support\Filters\Customfilter::class;
3 changes: 1 addition & 2 deletions tests/_support/Filters/Customfilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function before(RequestInterface $request, $arguments = null)

public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{

}

//--------------------------------------------------------------------
}
}
14 changes: 8 additions & 6 deletions tests/system/API/ResponseTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use CodeIgniter\Format\XMLFormatter;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockIncomingRequest;
use CodeIgniter\Test\Mock\MockResponse;
use stdClass;

class ResponseTraitTest extends \CodeIgniter\Test\CIUnitTestCase
class ResponseTraitTest extends CIUnitTestCase
{

protected $request;
Expand Down Expand Up @@ -157,7 +159,7 @@ public function testPHPtoArrayPayload()
{
$this->formatter = null;
$controller = $this->makeController();
$payload = new \stdClass();
$payload = new stdClass();
$payload->name = 'Tom';
$payload->id = 1;
$expected = <<<EOH
Expand Down Expand Up @@ -418,9 +420,9 @@ public function testValidContentTypes()
'application/json',
'application/xml',
];
for ($i = 0; $i < count($goodMimes); $i ++)
foreach ($goodMimes as $goodMime)
{
$this->tryValidContentType($goodMimes[$i], $goodMimes[$i] . $chars);
$this->tryValidContentType($goodMime, $goodMime . $chars);
}
}

Expand All @@ -446,9 +448,9 @@ public function testValidResponses()
'application/json',
'application/xml',
];
for ($i = 0; $i < count($goodMimes); $i ++)
foreach ($goodMimes as $goodMime)
{
$this->tryValidContentType($goodMimes[$i], $goodMimes[$i] . $chars);
$this->tryValidContentType($goodMime, $goodMime . $chars);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/system/Autoloader/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class AutoloaderTest extends CIUnitTestCase
{
/**
* @var \CodeIgniter\Autoloader\Autoloader
* @var Autoloader
*/
protected $loader;

Expand Down
14 changes: 8 additions & 6 deletions tests/system/Autoloader/FileLocatorTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php namespace CodeIgniter\Autoloader;

use CodeIgniter\Test\CIUnitTestCase;
use Config\Autoload;
use Config\Modules;

class FileLocatorTest extends \CodeIgniter\Test\CIUnitTestCase
class FileLocatorTest extends CIUnitTestCase
{
/**
* @var \CodeIgniter\Autoloader\FileLocator
* @var FileLocator
*/
protected $locator;

Expand All @@ -16,7 +18,7 @@ protected function setUp(): void
parent::setUp();

$autoloader = new Autoloader();
$autoloader->initialize(new \Config\Autoload(), new Modules());
$autoloader->initialize(new Autoload(), new Modules());
$autoloader->addNamespace([
'Unknown' => '/i/do/not/exist',
'Tests/Support' => TESTPATH . '_support/',
Expand Down Expand Up @@ -220,7 +222,7 @@ public function testListFilesSimple()

$expectedWin = APPPATH . 'Config\App.php';
$expectedLin = APPPATH . 'Config/App.php';
$this->assertTrue(in_array($expectedWin, $files) || in_array($expectedLin, $files));
$this->assertTrue(in_array($expectedWin, $files, true) || in_array($expectedLin, $files, true));
}

//--------------------------------------------------------------------
Expand All @@ -240,11 +242,11 @@ public function testListFilesFromMultipleDir()

$expectedWin = SYSTEMPATH . 'Filters\DebugToolbar.php';
$expectedLin = SYSTEMPATH . 'Filters/DebugToolbar.php';
$this->assertTrue(in_array($expectedWin, $files) || in_array($expectedLin, $files));
$this->assertTrue(in_array($expectedWin, $files, true) || in_array($expectedLin, $files, true));

$expectedWin = SYSTEMPATH . 'Filters\Filters.php';
$expectedLin = SYSTEMPATH . 'Filters/Filters.php';
$this->assertTrue(in_array($expectedWin, $files) || in_array($expectedLin, $files));
$this->assertTrue(in_array($expectedWin, $files, true) || in_array($expectedLin, $files, true));
}

//--------------------------------------------------------------------
Expand Down
22 changes: 12 additions & 10 deletions tests/system/CLI/CLITest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php namespace CodeIgniter\CLI;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use ReflectionProperty;

class CLITest extends \CodeIgniter\Test\CIUnitTestCase
class CLITest extends CIUnitTestCase
{

private $stream_filter;
Expand Down Expand Up @@ -347,12 +349,12 @@ public function testParseCommandMultipleOptions()

public function testWindow()
{
$height = new \ReflectionProperty(CLI::class, 'height');
$height = new ReflectionProperty(CLI::class, 'height');
$height->setAccessible(true);
$height->setValue(null);
$this->assertTrue(is_int(CLI::getHeight()));

$width = new \ReflectionProperty(CLI::class, 'width');
$width = new ReflectionProperty(CLI::class, 'width');
$width->setAccessible(true);
$width->setValue(null);
$this->assertTrue(is_int(CLI::getWidth()));
Expand All @@ -373,17 +375,17 @@ public function testTable($tbody, $thead, $expected)

public function tableProvider()
{
$head = [
$head = [
'ID',
'Title',
];
$one_row = [
$oneRow = [
[
'id' => 1,
'foo' => 'bar',
],
];
$many_rows = [
$manyRows = [
[
'id' => 1,
'foo' => 'bar',
Expand All @@ -400,14 +402,14 @@ public function tableProvider()

return [
[
$one_row,
$oneRow,
[],
'+---+-----+' . PHP_EOL .
'| 1 | bar |' . PHP_EOL .
'+---+-----+' . PHP_EOL . PHP_EOL,
],
[
$one_row,
$oneRow,
$head,
'+----+-------+' . PHP_EOL .
'| ID | Title |' . PHP_EOL .
Expand All @@ -416,7 +418,7 @@ public function tableProvider()
'+----+-------+' . PHP_EOL . PHP_EOL,
],
[
$many_rows,
$manyRows,
[],
'+---+-----------------+' . PHP_EOL .
'| 1 | bar |' . PHP_EOL .
Expand All @@ -425,7 +427,7 @@ public function tableProvider()
'+---+-----------------+' . PHP_EOL . PHP_EOL,
],
[
$many_rows,
$manyRows,
$head,
'+----+-----------------+' . PHP_EOL .
'| ID | Title |' . PHP_EOL .
Expand Down
13 changes: 9 additions & 4 deletions tests/system/CLI/CommandRunnerTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php
namespace CodeIgniter\CLI;

use CodeIgniter\Config\DotEnv;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use CodeIgniter\Test\Mock\MockCLIConfig;
use Config\Services;

class CommandRunnerTest extends \CodeIgniter\Test\CIUnitTestCase
class CommandRunnerTest extends CIUnitTestCase
{

private $stream_filter;
Expand All @@ -24,7 +29,7 @@ protected function setUp(): void
CITestStreamFilter::$buffer = '';
$this->stream_filter = stream_filter_append(STDOUT, 'CITestStreamFilter');

$this->env = new \CodeIgniter\Config\DotEnv(ROOTPATH);
$this->env = new DotEnv(ROOTPATH);
$this->env->load();

// Set environment values that would otherwise stop the framework from functioning during tests.
Expand All @@ -41,8 +46,8 @@ protected function setUp(): void
CLI::init();

$this->config = new MockCLIConfig();
$this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent());
$this->response = new \CodeIgniter\HTTP\Response($this->config);
$this->request = new IncomingRequest($this->config, new URI('https://somwhere.com'), null, new UserAgent());
$this->response = new Response($this->config);
$this->logger = Services::logger();
$this->runner = new CommandRunner();
$this->runner->initController($this->request, $this->response, $this->logger);
Expand Down
9 changes: 5 additions & 4 deletions tests/system/CLI/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodeIgniter\CLI;

use CodeIgniter\CodeIgniter;
use CodeIgniter\Config\DotEnv;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
Expand All @@ -21,7 +22,7 @@ protected function setUp(): void
CITestStreamFilter::$buffer = '';
$this->stream_filter = stream_filter_append(STDOUT, 'CITestStreamFilter');

$this->env = new \CodeIgniter\Config\DotEnv(ROOTPATH);
$this->env = new DotEnv(ROOTPATH);
$this->env->load();

// Set environment values that would otherwise stop the framework from functioning during tests.
Expand All @@ -47,13 +48,13 @@ public function tearDown(): void

public function testNew()
{
$console = new \CodeIgniter\CLI\Console($this->app);
$console = new Console($this->app);
$this->assertInstanceOf(Console::class, $console);
}

public function testHeader()
{
$console = new \CodeIgniter\CLI\Console($this->app);
$console = new Console($this->app);
$console->showHeader();
$result = CITestStreamFilter::$buffer;
$this->assertTrue(strpos($result, sprintf('CodeIgniter v%s Command Line Tool', CodeIgniter::CI_VERSION)) > 0);
Expand All @@ -64,7 +65,7 @@ public function testRun()
$request = new CLIRequest(config('App'));
$this->app->setRequest($request);

$console = new \CodeIgniter\CLI\Console($this->app);
$console = new Console($this->app);
$console->run(true);
$result = CITestStreamFilter::$buffer;

Expand Down
Loading

0 comments on commit 23cc87e

Please sign in to comment.