Skip to content

Commit

Permalink
test: move class in TestCase file
Browse files Browse the repository at this point in the history
$ vendor/bin/phpunit tests/system/Test/TestCaseTest.php
PHPUnit 9.6.15 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.26
Configuration: /.../CodeIgniter4/phpunit.xml

E........                                                                                                 9 / 9 (100%)

Time: 00:00.086, Memory: 16.00 MB

There was 1 error:

1) CodeIgniter\Test\TestCaseTest::testGetPrivatePropertyWithObject
Error: Class "CodeIgniter\Test\__TestForReflectionHelper" not found

/.../CodeIgniter4/tests/system/Test/TestCaseTest.php:38

ERRORS!
Tests: 9, Assertions: 19, Errors: 1.
  • Loading branch information
kenjis committed Dec 4, 2023
1 parent 37a0406 commit 8dae2a8
Show file tree
Hide file tree
Showing 3 changed files with 50 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;
}
}
3 changes: 2 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 Down

0 comments on commit 8dae2a8

Please sign in to comment.