Skip to content

Commit

Permalink
Add class ReflectionHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Sep 3, 2015
1 parent 3ec1f0e commit a06bedc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
31 changes: 19 additions & 12 deletions application/tests/_ci_phpunit_test/CIPHPUnitTestAutoloader.php
Expand Up @@ -10,6 +10,11 @@

class CIPHPUnitTestAutoloader
{
private $alias = [
'MonkeyPatch',
'ReflectionHelper',
];

/**
* @var CIPHPUnitTestFileCache
*/
Expand All @@ -30,10 +35,20 @@ public function load($class)
}
}

$this->loadCIPHPUnitTestAliasClass($class);
$this->loadCIPHPUnitTestClass($class);
$this->loadApplicationClass($class);
}

protected function loadCIPHPUnitTestAliasClass($class)
{
if (in_array($class, $this->alias))
{
$dir = __DIR__ . '/alias';
$this->loadClassFile($dir, $class);
}
}

protected function loadCIPHPUnitTestClass($class)
{
if (substr($class, 0, 13) !== 'CIPHPUnitTest')
Expand All @@ -43,21 +58,13 @@ protected function loadCIPHPUnitTestClass($class)

if (substr($class, -9) !== 'Exception')
{
$class_file = __DIR__ . '/' . $class . '.php';
require $class_file;
if ($this->cache)
{
$this->cache[$class] = $class_file;
}
$dir = __DIR__;
$this->loadClassFile($dir, $class);
}
else
{
$class_file = __DIR__ . '/exceptions/' . $class . '.php';
require $class_file;
if ($this->cache)
{
$this->cache[$class] = $class_file;
}
$dir = __DIR__ . '/exceptions';
$this->loadClassFile($dir, $class);
}
}

Expand Down
5 changes: 5 additions & 0 deletions application/tests/_ci_phpunit_test/alias/MonkeyPatch.php
@@ -0,0 +1,5 @@
<?php

class MonkeyPatch extends Kenjis\MonkeyPatch\MonkeyPatch
{
}
5 changes: 5 additions & 0 deletions application/tests/_ci_phpunit_test/alias/ReflectionHelper.php
@@ -0,0 +1,5 @@
<?php

class ReflectionHelper extends CIPHPUnitTestReflection
{
}
1 change: 0 additions & 1 deletion application/tests/_ci_phpunit_test/patcher/bootstrap.php
Expand Up @@ -19,7 +19,6 @@

const __GO_TO_ORIG__ = '__GO_TO_ORIG__';

class_alias('Kenjis\MonkeyPatch\MonkeyPatch', 'MonkeyPatch');
class_alias('Kenjis\MonkeyPatch\MonkeyPatchManager', 'MonkeyPatchManager');

// And you have to configure for your application
Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -18,6 +18,7 @@
* You can add query string in URI string of `$this->request()`. See [#51](https://github.com/kenjis/ci-phpunit-test/pull/51).
* Autoloading for libraries
* Add `application/libraries/Session/MY_Session.php` as a sample
* `ReflectionHelper` class to access non-public method or property. See [Function/Class Reference](https://github.com/kenjis/ci-phpunit-test/blob/master/docs/FunctionAndClassReference.md#class-reflectionhelper).

### Fixed

Expand Down
57 changes: 57 additions & 0 deletions docs/FunctionAndClassReference.md
Expand Up @@ -30,6 +30,10 @@ version: **master** |
- [`TestCase::verifyNeverInvoked($mock, $method, $params)`](#testcaseverifyneverinvokedmock-method-params)
- [`TestCase::warningOff()`](#testcasewarningoff)
- [`TestCase::warningOn()`](#testcasewarningon)
- [*class* ReflectionHelper](#class-reflectionhelper)
- [`ReflectionHelper::getPrivateProperty($obj, $property)`](#reflectionhelpergetprivatepropertyobj-property)
- [`ReflectionHelper::setPrivateProperty($obj, $property, $value)`](#reflectionhelpersetprivatepropertyobj-property-value)
- [`ReflectionHelper::getPrivateMethodInvoker($obj, $method)`](#reflectionhelpergetprivatemethodinvokerobj-method)
- [*class* MonkeyPatch](#class-monkeypatch)
- [`MonkeyPatch::patchFunction($function, $return_value, $class_method)`](#monkeypatchpatchfunctionfunction-return_value-class_method)
- [`MonkeyPatch::resetFunctions()`](#monkeypatchresetfunctions)
Expand Down Expand Up @@ -414,6 +418,59 @@ Turn off WARNING and Notice in PHP error reporting.

Restore PHP error reporting.

### *class* ReflectionHelper

#### ReflectionHelper::getPrivateProperty($obj, $property)

| param | type | description |
|-----------|---------------|---------------------|
|`$obj` | object/string | object / class name |
|`$property`| string | property name |

`returns` (mixed) property value

Get private or protected property value.

#### ReflectionHelper::setPrivateProperty($obj, $property, $value)

| param | type | description |
|-----------|---------------|---------------------|
|`$obj` | object/string | object / class name |
|`$property`| string | property name |
|`$value` | mixed | value |

Set private or protected property value.

~~~php
$obj = new SomeClass();
ReflectionHelper::setPrivateProperty(
$obj,
'private_propery',
'new value'
);
~~~

#### ReflectionHelper::getPrivateMethodInvoker($obj, $method)

| param | type | description |
|---------|---------------|---------------------|
|`$obj` | object/string | object / class name |
|`$method`| string | method name |

`returns` (closure) method invoker

Get private or protected method invoker.

~~~php
$obj = new SomeClass();
$method = ReflectionHelper::getPrivateMethodInvoker(
$obj, 'privateMethod'
);
$this->assertEquals(
'return value of the privateMethod() method', $method()
);
~~~

### *class* MonkeyPatch

To use this class, you have to enable monkey patching. See [How to Write Tests](HowToWriteTests.md#monkey-patching).
Expand Down

0 comments on commit a06bedc

Please sign in to comment.