PHP code example of gollumsf / reflection-property-test

1. Go to this page and download the library: Download gollumsf/reflection-property-test library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gollumsf / reflection-property-test example snippets



use GollumSF\ReflectionPropertyTest\ReflectionPropertyTrait;

class MyPrivate {
	private int $dataPrivate = 10;
	private function functionPrivate(int $value): int {
		return 11 + $value;
	}
}

class MyExtend extends MyPrivate {
}

class MyTest extends TestCase {

	use ReflectionPropertyTrait;

	public function testMyFunction(): void {
		$obj = new MyPrivate();
		$this->assertEquals($this->reflectionGetValue($obj, 'dataPrivate'), 10);

		$this->reflectionSetValue($obj, 'dataPrivate', 20);
		$this->assertEquals($this->reflectionGetValue($obj, 'dataPrivate'), 20);

		$this->assertEquals($this->reflectionCallMethod($obj, 'functionPrivate', [19]), 30);

		$obj2 = new MyExtend();
		$this->assertEquals($this->reflectionGetValue($obj2, 'dataPrivate', MyPrivate::class), 10);

		$this->reflectionSetValue($obj2, 'dataPrivate', 20, MyPrivate::class);
		$this->assertEquals($this->reflectionGetValue($obj2, 'dataPrivate', MyPrivate::class), 20);

		$this->assertEquals($this->reflectionCallMethod($obj2, 'functionPrivate', [19], MyPrivate::class), 30);
	}

}