Skip to content

Commit

Permalink
Merge pull request #9259 from samsonasik/refactor-move-property-to-lo…
Browse files Browse the repository at this point in the history
…cal-variable

refactor: Move property to local variable on usage only in setUp() method in tests
  • Loading branch information
samsonasik authored Nov 6, 2024
2 parents 52c74b5 + 8628f12 commit c7d012d
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 57 deletions.
6 changes: 0 additions & 6 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -16429,12 +16429,6 @@
'count' => 5,
'path' => __DIR__ . '/tests/system/Pager/PagerTest.php',
];
$ignoreErrors[] = [
// identifier: missingType.iterableValue
'message' => '#^Property CodeIgniter\\\\Publisher\\\\PublisherOutputTest\\:\\:\\$structure type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/tests/system/Publisher/PublisherOutputTest.php',
];
$ignoreErrors[] = [
// identifier: missingType.iterableValue
'message' => '#^Method CodeIgniter\\\\Publisher\\\\PublisherRestrictionsTest\\:\\:provideDefaultPublicRestrictions\\(\\) return type has no value type specified in iterable type iterable\\.$#',
Expand Down
6 changes: 2 additions & 4 deletions tests/system/CLI/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ final class ConsoleTest extends CIUnitTestCase
{
use StreamFilterTrait;

private DotEnv $env;

protected function setUp(): void
{
parent::setUp();

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

// Set environment values that would otherwise stop the framework from functioning during tests.
if (! isset($_SERVER['app.baseURL'])) {
Expand Down
6 changes: 2 additions & 4 deletions tests/system/Cache/Handlers/MemcachedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#[Group('CacheLive')]
final class MemcachedHandlerTest extends AbstractHandlerTestCase
{
private Cache $config;

private static function getKeyArray()
{
return [
Expand All @@ -44,9 +42,9 @@ protected function setUp(): void
$this->markTestSkipped('Memcached extension not loaded.');
}

$this->config = new Cache();
$config = new Cache();

$this->handler = new MemcachedHandler($this->config);
$this->handler = new MemcachedHandler($config);

$this->handler->initialize();
}
Expand Down
1 change: 0 additions & 1 deletion tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
final class CodeIgniterTest extends CIUnitTestCase
{
private CodeIgniter $codeigniter;
protected $routes;

#[WithoutErrorHandler]
protected function setUp(): void
Expand Down
5 changes: 2 additions & 3 deletions tests/system/Config/DotEnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
final class DotEnvTest extends CIUnitTestCase
{
private ?vfsStreamDirectory $root;
private string $path;
private string $fixturesFolder;

#[WithoutErrorHandler]
Expand All @@ -42,8 +41,8 @@ protected function setUp(): void

$this->root = vfsStream::setup();
$this->fixturesFolder = $this->root->url();
$this->path = TESTPATH . 'system/Config/fixtures';
vfsStream::copyFromFileSystem($this->path, $this->root);
$path = TESTPATH . 'system/Config/fixtures';
vfsStream::copyFromFileSystem($path, $this->root);

$file = 'unreadable.env';
$path = rtrim($this->fixturesFolder, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file;
Expand Down
7 changes: 3 additions & 4 deletions tests/system/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#[Group('Others')]
final class ControllerTest extends CIUnitTestCase
{
private App $config;
private ?Controller $controller = null;

/**
Expand All @@ -59,9 +58,9 @@ protected function setUp(): void
{
parent::setUp();

$this->config = new App();
$this->request = new IncomingRequest($this->config, new SiteURI($this->config), null, new UserAgent());
$this->response = new Response($this->config);
$config = new App();
$this->request = new IncomingRequest($config, new SiteURI($config), null, new UserAgent());
$this->response = new Response($config);
$this->logger = Services::logger();
}

Expand Down
5 changes: 2 additions & 3 deletions tests/system/Files/FileWithVfsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ final class FileWithVfsTest extends CIUnitTestCase
{
// For VFS stuff
private ?vfsStreamDirectory $root = null;
private string $path;
private string $start;
private File $file;

Expand All @@ -35,8 +34,8 @@ protected function setUp(): void
parent::setUp();

$this->root = vfsStream::setup();
$this->path = '_support/Files/';
vfsStream::copyFromFileSystem(TESTPATH . $this->path, $this->root);
$path = '_support/Files/';
vfsStream::copyFromFileSystem(TESTPATH . $path, $this->root);
$this->start = $this->root->url() . '/';
$this->file = new File($this->start . 'able/apple.php');
}
Expand Down
10 changes: 4 additions & 6 deletions tests/system/HTTP/Files/FileMovingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@
final class FileMovingTest extends CIUnitTestCase
{
private ?vfsStreamDirectory $root;
private string $path;
private string $start;
private string $destination;

protected function setUp(): void
{
parent::setUp();

$this->root = vfsStream::setup();
$this->path = '_support/Files/';
vfsStream::copyFromFileSystem(TESTPATH . $this->path, $this->root);
$this->start = $this->root->url() . '/';
$path = '_support/Files/';
vfsStream::copyFromFileSystem(TESTPATH . $path, $this->root);
$start = $this->root->url() . '/';

$this->destination = $this->start . 'destination';
$this->destination = $start . 'destination';
if (is_dir($this->destination)) {
rmdir($this->destination);
}
Expand Down
5 changes: 2 additions & 3 deletions tests/system/Helpers/CookieHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#[Group('Others')]
final class CookieHelperTest extends CIUnitTestCase
{
private IncomingRequest $request;
private string $name;
private string $value;
private int $expire;
Expand All @@ -51,8 +50,8 @@ protected function setUp(): void

Services::injectMock('response', new MockResponse(new App()));
$this->response = Services::response();
$this->request = new IncomingRequest(new App(), new SiteURI(new App()), null, new UserAgent());
Services::injectMock('request', $this->request);
$request = new IncomingRequest(new App(), new SiteURI(new App()), null, new UserAgent());
Services::injectMock('request', $request);

helper('cookie');
}
Expand Down
10 changes: 4 additions & 6 deletions tests/system/Images/BaseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use CodeIgniter\Images\Handlers\BaseHandler;
use CodeIgniter\Test\CIUnitTestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\Attributes\Group;

/**
Expand All @@ -35,7 +34,6 @@
#[Group('Others')]
final class BaseHandlerTest extends CIUnitTestCase
{
private vfsStreamDirectory $root;
private string $origin;
private string $start;
private string $path;
Expand All @@ -47,21 +45,21 @@ protected function setUp(): void
}

// create virtual file system
$this->root = vfsStream::setup();
$root = vfsStream::setup();
// copy our support files
$this->origin = SUPPORTPATH . 'Images/';
vfsStream::copyFromFileSystem($this->origin, $this->root);
vfsStream::copyFromFileSystem($this->origin, $root);
// make subfolders
$structure = [
'work' => [],
'wontwork' => [],
];
vfsStream::create($structure);
// with one of them read only
$this->root->getChild('wontwork')->chmod(0400);
$root->getChild('wontwork')->chmod(0400);

// for VFS tests
$this->start = $this->root->url() . '/';
$this->start = $root->url() . '/';
$this->path = $this->start . 'ci-logo.png';
}

Expand Down
5 changes: 2 additions & 3 deletions tests/system/Images/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
final class ImageTest extends CIUnitTestCase
{
private vfsStreamDirectory $root;
private string $origin;
private string $start;
private Image $image;

Expand All @@ -35,8 +34,8 @@ protected function setUp(): void
// create virtual file system
$this->root = vfsStream::setup();
// copy our support files
$this->origin = '_support/Images/';
vfsStream::copyFromFileSystem(TESTPATH . $this->origin, $this->root);
$origin = '_support/Images/';
vfsStream::copyFromFileSystem(TESTPATH . $origin, $this->root);
// make subfolders
$structure = [
'work' => [],
Expand Down
6 changes: 2 additions & 4 deletions tests/system/Log/Handlers/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use CodeIgniter\Test\Mock\MockFileLogger;
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Log\Handlers\TestHandler;

Expand All @@ -27,14 +26,13 @@
#[Group('Others')]
final class FileHandlerTest extends CIUnitTestCase
{
private vfsStreamDirectory $root;
private string $start;

protected function setUp(): void
{
parent::setUp();
$this->root = vfsStream::setup('root');
$this->start = $this->root->url() . '/';
$root = vfsStream::setup('root');
$this->start = $root->url() . '/';
}

public function testHandle(): void
Expand Down
12 changes: 5 additions & 7 deletions tests/system/Publisher/PublisherOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@
#[Group('Others')]
final class PublisherOutputTest extends CIUnitTestCase
{
/**
* Files to seed to VFS
*/
private array $structure;

/**
* Virtual destination
*/
Expand Down Expand Up @@ -60,7 +55,10 @@ protected function setUp(): void
{
parent::setUp();

$this->structure = [
/**
* Files to seed to VFS
*/
$structure = [
'able' => [
'apple.php' => 'Once upon a midnight dreary',
'bazam' => 'While I pondered weak and weary',
Expand All @@ -74,7 +72,7 @@ protected function setUp(): void
'.hidden' => 'There is no spoon',
];

$this->root = vfsStream::setup('root', null, $this->structure);
$this->root = vfsStream::setup('root', null, $structure);

// Add root to the list of allowed destinations
config('Publisher')->restrictions[$this->root->url()] = '*';
Expand Down
5 changes: 2 additions & 3 deletions tests/system/View/CellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
#[Group('Others')]
final class CellTest extends CIUnitTestCase
{
private MockCache $cache;
private Cell $cell;

protected function setUp(): void
{
parent::setUp();

$this->cache = new MockCache();
$this->cell = new Cell($this->cache);
$cache = new MockCache();
$this->cell = new Cell($cache);
}

public function testPrepareParamsReturnsEmptyArrayWithInvalidParam(): void
Expand Down

0 comments on commit c7d012d

Please sign in to comment.