mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
Reflection - getPropertyValue() method - verify if getter is accessible publicly
This commit is contained in:
54
tests/Meritoo/Common/Test/Utilities/Reflection/F.php
Normal file
54
tests/Meritoo/Common/Test/Utilities/Reflection/F.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Test\Utilities\Reflection;
|
||||
|
||||
/**
|
||||
* The F class.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class F
|
||||
{
|
||||
protected $username;
|
||||
private $accountBalance;
|
||||
private $city;
|
||||
private $country;
|
||||
private $gInstance;
|
||||
|
||||
public function __construct($accountBalance, $city, $country, $username, $firstName = 'John', $lastName = 'Scott')
|
||||
{
|
||||
$this->accountBalance = $accountBalance;
|
||||
$this->city = $city;
|
||||
$this->country = $country;
|
||||
$this->username = $username;
|
||||
$this->gInstance = new G($firstName, $lastName);
|
||||
|
||||
/*
|
||||
* Called to avoid "Unused private method getAccountBalance" warning only
|
||||
*/
|
||||
$this->getAccountBalance();
|
||||
}
|
||||
|
||||
public function getCountry()
|
||||
{
|
||||
return $this->country;
|
||||
}
|
||||
|
||||
protected function getCity()
|
||||
{
|
||||
return $this->city;
|
||||
}
|
||||
|
||||
private function getAccountBalance()
|
||||
{
|
||||
return $this->accountBalance;
|
||||
}
|
||||
}
|
||||
38
tests/Meritoo/Common/Test/Utilities/Reflection/G.php
Normal file
38
tests/Meritoo/Common/Test/Utilities/Reflection/G.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Test\Utilities\Reflection;
|
||||
|
||||
/**
|
||||
* The G class.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class G
|
||||
{
|
||||
private $firstName;
|
||||
private $lastName;
|
||||
|
||||
public function __construct($firstName = 'John', $lastName = 'Scott')
|
||||
{
|
||||
$this->firstName = $firstName;
|
||||
$this->lastName = $lastName;
|
||||
}
|
||||
|
||||
public function getFirstName()
|
||||
{
|
||||
return $this->firstName;
|
||||
}
|
||||
|
||||
public function getLastName()
|
||||
{
|
||||
return $this->lastName;
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ use Meritoo\Common\Test\Utilities\Reflection\B;
|
||||
use Meritoo\Common\Test\Utilities\Reflection\C;
|
||||
use Meritoo\Common\Test\Utilities\Reflection\D;
|
||||
use Meritoo\Common\Test\Utilities\Reflection\E;
|
||||
use Meritoo\Common\Test\Utilities\Reflection\F;
|
||||
use Meritoo\Common\Utilities\Reflection;
|
||||
use ReflectionProperty;
|
||||
|
||||
@@ -239,6 +240,31 @@ class ReflectionTest extends BaseTestCase
|
||||
self::assertCount(2, Reflection::getProperties(B::class, null, true));
|
||||
}
|
||||
|
||||
public function testGetPropertyValueWithPublicGetter()
|
||||
{
|
||||
$country = 'USA';
|
||||
$f = new F(1000, 'New York', $country, 'john.scott');
|
||||
|
||||
self::assertEquals($country, Reflection::getPropertyValue($f, 'country'));
|
||||
}
|
||||
|
||||
public function testGetPropertyValueWithProtectedGetter()
|
||||
{
|
||||
$city = 'New York';
|
||||
$f = new F(1000, $city, 'USA', 'john.scott');
|
||||
|
||||
self::assertEquals($city, Reflection::getPropertyValue($f, 'city'));
|
||||
}
|
||||
|
||||
|
||||
public function testGetPropertyValueWithPrivateGetter()
|
||||
{
|
||||
$accountBalance = 1000;
|
||||
$f = new F($accountBalance, 'New York', 'USA', 'john.scott');
|
||||
|
||||
self::assertEquals($accountBalance, Reflection::getPropertyValue($f, 'accountBalance'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides invalid class and trait
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user