Longman\TelegramBot\Tests\Unit\TestHelpers::setObjectProperty PHP Method

setObjectProperty() public static method

Set the value of a private/protected property of an object
public static setObjectProperty ( object $object, string $property, mixed $value )
$object object Object that contains the property
$property string Name of the property who's value we want to set
$value mixed The value to set to the property
    public static function setObjectProperty($object, $property, $value)
    {
        $ref_object = new \ReflectionObject($object);
        $ref_property = $ref_object->getProperty($property);
        $ref_property->setAccessible(true);
        $ref_property->setValue($object, $value);
    }

Usage Example

Exemplo n.º 1
0
 public function setUp()
 {
     //Default command object
     $this->telegram = new Telegram('apikey', 'testbot');
     $this->command_stub = $this->getMockForAbstractClass($this->command_namespace, [$this->telegram]);
     //Create separate command object that contain a command config
     $this->telegram_with_config = new Telegram('apikey', 'testbot');
     $this->telegram_with_config->setCommandConfig('command_name', ['config_key' => 'config_value']);
     $this->command_stub_with_config = $this->getMockBuilder($this->command_namespace)->disableOriginalConstructor()->getMockForAbstractClass();
     //Set a name for the object property so that the constructor can set the config correctly
     TestHelpers::setObjectProperty($this->command_stub_with_config, 'name', 'command_name');
     $this->command_stub_with_config->__construct($this->telegram_with_config);
 }