Skip to content

Commit

Permalink
Merge pull request #8291 from kenjis/fix-tests
Browse files Browse the repository at this point in the history
test: fix test code
  • Loading branch information
kenjis authored Dec 6, 2023
2 parents d1b7921 + 8dae2a8 commit 7a38206
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
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 <admin@codeigniter.com>
*
* 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

0 comments on commit 7a38206

Please sign in to comment.