Skip to content

Commit 4e3d87b

Browse files
committed
Fix method visibility
1 parent 0cd3415 commit 4e3d87b

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

libraries/Twig.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public function __construct($params = [])
5151
$this->config = array_merge($this->config, $params);
5252
}
5353

54-
public function resetTwig()
54+
protected function resetTwig()
5555
{
5656
$this->twig = null;
5757
$this->createTwig();
5858
}
5959

60-
public function createTwig()
60+
protected function createTwig()
6161
{
6262
// $this->twig is singleton
6363
if ($this->twig !== null)
@@ -93,7 +93,7 @@ public function createTwig()
9393
$this->twig = $twig;
9494
}
9595

96-
public function setLoader($loader)
96+
protected function setLoader($loader)
9797
{
9898
$this->loader = $loader;
9999
}
@@ -140,7 +140,7 @@ public function render($view, $params = [])
140140
return $this->twig->render($view, $params);
141141
}
142142

143-
private function addCIFunctions()
143+
protected function addCIFunctions()
144144
{
145145
// Runs only once
146146
if ($this->add_ci_functions)
@@ -198,7 +198,7 @@ private function addCIFunctions()
198198
* @param array $attributes [changed] only array is acceptable
199199
* @return string
200200
*/
201-
public function safe_anchor($uri = '', $title = '', $attributes = [])
201+
protected function safe_anchor($uri = '', $title = '', $attributes = [])
202202
{
203203
$uri = html_escape($uri);
204204
$title = html_escape($title);

phpunit.php

+3
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020
copy('libraries/Twig.php', 'vendor/codeigniter/framework/application/libraries/Twig.php');
2121

2222
require __DIR__ . '/ci_instance.php';
23+
24+
// Load helper class for testing
25+
require __DIR__ . '/tests/ReflectionHelper.php';

tests/ReflectionHelper.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Part of CI PHPUnit Test
4+
*
5+
* @author Kenji Suzuki <https://github.com/kenjis>
6+
* @license MIT License
7+
* @copyright 2015 Kenji Suzuki
8+
* @link https://github.com/kenjis/ci-phpunit-test
9+
*/
10+
11+
class ReflectionHelper
12+
{
13+
/**
14+
* @param object|string $obj object or class name
15+
* @param string $method method name
16+
* @return closure
17+
*/
18+
public static function getPrivateMethodInvoker($obj, $method)
19+
{
20+
$ref_method = new ReflectionMethod($obj, $method);
21+
$ref_method->setAccessible(true);
22+
$obj = (gettype($obj) === 'object') ? $obj : null;
23+
24+
return function () use ($obj, $ref_method) {
25+
$args = func_get_args();
26+
return $ref_method->invokeArgs($obj, $args);
27+
};
28+
}
29+
30+
protected static function getAccessibleRefProperty($obj, $property)
31+
{
32+
if (is_object($obj)) {
33+
$ref_class = new ReflectionObject($obj);
34+
} else {
35+
$ref_class = new ReflectionClass($obj);
36+
}
37+
38+
$ref_property = $ref_class->getProperty($property);
39+
$ref_property->setAccessible(true);
40+
41+
return $ref_property;
42+
}
43+
44+
/**
45+
* @param object|string $obj object or class name
46+
* @param string $property property name
47+
* @param mixed $value value
48+
*/
49+
public static function setPrivateProperty($obj, $property, $value)
50+
{
51+
$ref_property = self::getAccessibleRefProperty($obj, $property);
52+
$ref_property->setValue($obj, $value);
53+
}
54+
55+
/**
56+
* @param object|string $obj object or class name
57+
* @param string $property property name
58+
* @return mixed value
59+
*/
60+
public static function getPrivateProperty($obj, $property)
61+
{
62+
$ref_property = self::getAccessibleRefProperty($obj, $property);
63+
return $ref_property->getValue($obj);
64+
}
65+
}

tests/libraries/TwigHelperTest.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,36 @@ public static function setUpBeforeClass()
1313
public function setUp()
1414
{
1515
$CI =& get_instance();
16-
16+
1717
$loader = new Twig_Loader_Array([
1818
'base_url' => '{{ base_url(\'"><s>abc</s><a name="test\') }}',
1919
'site_url' => '{{ site_url(\'"><s>abc</s><a name="test\') }}',
2020
]);
21-
$CI->twig->setLoader($loader);
22-
$CI->twig->resetTwig();
23-
21+
$setLoader = ReflectionHelper::getPrivateMethodInvoker(
22+
$CI->twig, 'setLoader'
23+
);
24+
$setLoader($loader);
25+
26+
$resetTwig = ReflectionHelper::getPrivateMethodInvoker(
27+
$CI->twig, 'resetTwig'
28+
);
29+
$resetTwig();
30+
2431
$this->obj = $CI->twig;
2532
$this->twig = $CI->twig->getTwig();
2633
}
2734

2835
public function test_safe_anchor()
2936
{
30-
$actual = $this->obj->safe_anchor('news/local/123', 'My News', array('title' => 'The best news!'));
37+
$safe_anchor = ReflectionHelper::getPrivateMethodInvoker(
38+
$this->obj, 'safe_anchor'
39+
);
40+
41+
$actual = $safe_anchor('news/local/123', 'My News', array('title' => 'The best news!'));
3142
$expected = '<a href="http://localhost/index.php/news/local/123" title="The best news!">My News</a>';
3243
$this->assertEquals($expected, $actual);
3344

34-
$actual = $this->obj->safe_anchor('news/local/123', '<s>abc</s>', array('<s>name</s>' => '<s>val</s>'));
45+
$actual = $safe_anchor('news/local/123', '<s>abc</s>', array('<s>name</s>' => '<s>val</s>'));
3546
$expected = '<a href="http://localhost/index.php/news/local/123" &lt;s&gt;name&lt;/s&gt;="&lt;s&gt;val&lt;/s&gt;">&lt;s&gt;abc&lt;/s&gt;</a>';
3647
$this->assertEquals($expected, $actual);
3748
}

0 commit comments

Comments
 (0)