Answer the question
In order to leave comments, you need to log in
How to lock private property in phpunit class?
Help the people, even caught a dumbass, in general, there is a beginning of such a class
class Relay implements ConsumerInterface
{
/**
* @var MemcachedAdapter
*/
private $memory;
public function __construct()
{
$client = MemcachedAdapter::createConnection('memcached://localhost');
$this->memory = new MemcachedAdapter($client, $namespace = '',$defaultLifetime = 0);
Answer the question
In order to leave comments, you need to log in
Simulation of your object:
class Relay
{
private $privateProperty;
public function __construct()
{
$this->privateProperty = new \stdClass();
}
// метод, который работает с приватным свойством
public function call()
{
return $this->privateProperty;
}
}
use PHPUnit\Framework\TestCase;
class RelayTest extends TestCase
{
public function testCall(): void
{
$reflectionClass = new \ReflectionClass(Relay::class);
$reflectionProperty = $reflectionClass->getProperty('privateProperty');
$reflectionProperty->setAccessible(true);
// создаем наш объект БЕЗ конструктора
$relay = $reflectionClass->newInstanceWithoutConstructor();
// Меняем свойство и вызываем метод, работающий с этим приватным полем
$reflectionProperty->setValue($relay, 1111);
self::assertEquals(1111, $relay->call());
// Меняем свойство и вызываем метод, работающий с этим приватным полем
$reflectionProperty->setValue($relay, 'aaaa');
self::assertEquals('aaaa', $relay->call());
}
}
class RelayTest extends TestCase
public function testCall(): void
{
/** @var Example $stub */
$stub = Stub::make(Relay::class, [
'privateProperty' => 1111,
]);
self::assertEquals(1111, $stub->call());
$stub = Stub::make(Relay::class, [
'privateProperty' => 'aaaa',
]);
self::assertEquals('aaaa', $stub->call());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question