/** * Simple protected method * * @param $a * @return mixed */ protected function protectedMethod($a) { if ($a === 0) { return next::caller(); } return $a + 10; }
/** * @param \PHPUnit_Framework_MockObject_Invocation $invocation * * @return mixed * @throws \Exception */ public function invoke(\PHPUnit_Framework_MockObject_Invocation $invocation) { $exception = null; $hasReturnValue = false; $returnValue = next::caller(); foreach ($this->matchers as $match) { try { if ($match->matches($invocation)) { $value = $match->invoked($invocation); if (!$hasReturnValue) { $returnValue = $value; $hasReturnValue = true; } } } catch (\Exception $e) { $exception = $e; } } if ($exception !== null) { throw $exception; } return $returnValue; }
/** * Allow call to protected methods by using the 'PROTECTED_' prefix * and the __callProtectedMethod * * @return void * @test */ public function testProtectedMethodCall() { $dummy = new DummyClass(); $this->assertNull($dummy->protectedMethod(10)); $this->assertNull($dummy->privateMethod(10)); $this->assertEquals(20, $dummy->publicMethod(10)); $this->assertEquals(20, $dummy->__callProtectedMethod('protectedMethod', [10])); $this->assertEquals(20, $dummy->__callProtectedMethod('privateMethod', [10])); $this->assertEquals(20, $dummy->__callProtectedMethod('publicMethod', [10])); $this->assertNull($dummy->protectedMethod(10)); $this->assertNull($dummy->privateMethod(10)); $this->assertEquals(20, $dummy->publicMethod(10)); $this->assertSame(next::caller(), $dummy->__callProtectedMethod('notExist')); $this->assertNull($dummy->PROTECTED_notExist()); }
/** * Helper method to call a protected method from outside * * @param string $name * @param array $arguments * * @return mixed */ public function __callProtectedMethod($name, $arguments = []) { try { $method = $this->__reflection()->getMethod($name); } catch (\Exception $e) { return next::caller(); } $method->setAccessible(true); $result = $method->invokeArgs($this, $arguments); $method->setAccessible(false); return $result; }