Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix test code #8291

Merged
merged 2 commits into from
Dec 6, 2023
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
38 changes: 38 additions & 0 deletions tests/_support/Test/TestForReflectionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Test;

class TestForReflectionHelper
{
private string $private = 'secret';
private static string $static_private = 'xyz';

public function getPrivate()
{
return $this->private;
}

public static function getStaticPrivate()
{
return self::$static_private;
}

private function privateMethod($param1, $param2)
{
return 'private ' . $param1 . $param2;
}

private static function privateStaticMethod($param1, $param2)
{
return 'private_static ' . $param1 . $param2;
}
}
44 changes: 10 additions & 34 deletions tests/system/Test/ReflectionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace CodeIgniter\Test;

use Tests\Support\Test\TestForReflectionHelper;

/**
* @internal
*
Expand All @@ -20,30 +22,30 @@ final class ReflectionHelperTest extends CIUnitTestCase
{
public function testGetPrivatePropertyWithObject(): void
{
$obj = new __TestForReflectionHelper();
$obj = new TestForReflectionHelper();
$actual = $this->getPrivateProperty($obj, 'private');
$this->assertSame('secret', $actual);
}

public function testGetPrivatePropertyWithObjectStaticCall(): void
{
$obj = new __TestForReflectionHelper();
$obj = new TestForReflectionHelper();
$actual = CIUnitTestCase::getPrivateProperty($obj, 'private');
$this->assertSame('secret', $actual);
}

public function testGetPrivatePropertyWithStatic(): void
{
$actual = $this->getPrivateProperty(
__TestForReflectionHelper::class,
TestForReflectionHelper::class,
'static_private'
);
$this->assertSame('xyz', $actual);
}

public function testSetPrivatePropertyWithObject(): void
{
$obj = new __TestForReflectionHelper();
$obj = new TestForReflectionHelper();
$this->setPrivateProperty(
$obj,
'private',
Expand All @@ -55,19 +57,19 @@ public function testSetPrivatePropertyWithObject(): void
public function testSetPrivatePropertyWithStatic(): void
{
$this->setPrivateProperty(
__TestForReflectionHelper::class,
TestForReflectionHelper::class,
'static_private',
'abc'
);
$this->assertSame(
'abc',
__TestForReflectionHelper::getStaticPrivate()
TestForReflectionHelper::getStaticPrivate()
);
}

public function testGetPrivateMethodInvokerWithObject(): void
{
$obj = new __TestForReflectionHelper();
$obj = new TestForReflectionHelper();
$method = $this->getPrivateMethodInvoker(
$obj,
'privateMethod'
Expand All @@ -81,7 +83,7 @@ public function testGetPrivateMethodInvokerWithObject(): void
public function testGetPrivateMethodInvokerWithStatic(): void
{
$method = $this->getPrivateMethodInvoker(
__TestForReflectionHelper::class,
TestForReflectionHelper::class,
'privateStaticMethod'
);
$this->assertSame(
Expand All @@ -90,29 +92,3 @@ public function testGetPrivateMethodInvokerWithStatic(): void
);
}
}

class __TestForReflectionHelper
{
private string $private = 'secret';
private static string $static_private = 'xyz';

public function getPrivate()
{
return $this->private;
}

public static function getStaticPrivate()
{
return self::$static_private;
}

private function privateMethod($param1, $param2)
{
return 'private ' . $param1 . $param2;
}

private static function privateStaticMethod($param1, $param2)
{
return 'private_static ' . $param1 . $param2;
}
}
6 changes: 5 additions & 1 deletion tests/system/Test/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CodeIgniter\Events\Events;
use CodeIgniter\HTTP\Response;
use Config\App;
use Tests\Support\Test\TestForReflectionHelper;

/**
* @internal
Expand All @@ -35,7 +36,7 @@ protected function setUp(): void

public function testGetPrivatePropertyWithObject(): void
{
$obj = new __TestForReflectionHelper();
$obj = new TestForReflectionHelper();
$actual = $this->getPrivateProperty($obj, 'private');
$this->assertSame('secret', $actual);
}
Expand All @@ -54,13 +55,16 @@ public function testAssertLogContains(): void

public function testEventTriggering(): void
{
$result = '';

Events::on('foo', static function ($arg) use (&$result): void {
$result = $arg;
});

Events::trigger('foo', 'bar');

$this->assertEventTriggered('foo');
$this->assertSame('bar', $result);
}

public function testStreamFilter(): void
Expand Down
Loading