. * * The copyright holder may be reached by emailing john@kleijnweb.nl * * * * @copyright Copyright (C) 2010 Kleijn Web Development * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License */ namespace kwd\blog\virtualproxy; require_once 'PHPUnit/Framework/TestCase.php'; require_once 'domain/SomeDomainType.php'; require_once 'ProxyGenerator.php'; require_once 'Loader.php'; /** * Test case for the proxy generator * * @category virtualproxy * @package kwd\blog\virtualproxy */ class GeneratorTestCase extends \PHPUnit_Framework_TestCase { /** * Assert the generated proxy has the same type as the domain class */ public function testProxyIsSameType() { $proxyGenerator = new ProxyGenerator('kwd\blog\virtualproxy\domain'); $code = $proxyGenerator->generate('SomeDomainType'); $this->assertNull(eval($code)); $this->assertType('kwd\blog\virtualproxy\domain\SomeDomainType', new domain\SomeDomainTypeProxy()); } /** * Assert that all public and private methods are overriden by the proxy */ public function testOveridesAllNonPrivateMethods() { $subjectRefl = new \ReflectionClass('kwd\blog\virtualproxy\domain\SomeDomainType'); $proxyRefl = new \ReflectionClass('kwd\blog\virtualproxy\domain\SomeDomainTypeProxy'); foreach($subjectRefl->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $method) { if(!$proxyRefl->hasMethod($method->getName())) { $this->fail("Expected method '{$method->getName()}'"); } if($proxyRefl->getMethod($method->getName())->getDeclaringClass()->getName() !== 'kwd\blog\virtualproxy\domain\SomeDomainTypeProxy') { $this->fail("Did not override method '{$method->getName()}'"); } } } /** * Assert that the proxy does not define public or protected methods of its own */ public function testDoesNotExposeOtherMethods() { $subjectRefl = new \ReflectionClass('kwd\blog\virtualproxy\domain\SomeDomainType'); $proxyRefl = new \ReflectionClass('kwd\blog\virtualproxy\domain\SomeDomainTypeProxy'); foreach($proxyRefl->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $method) { if(!$subjectRefl->hasMethod($method->getName())) { $this->fail("Defined method '{$method->getName()}', which is not in subject class"); } } } /** * Assert that all requests are delegated to the loaded subject */ public function testDelegatesToSubjectWhenUsingInterface() { $proxy = new domain\SomeDomainTypeProxy(); $subjectRefl = new \ReflectionClass('kwd\blog\virtualproxy\domain\SomeDomainType'); $proxyRefl = new \ReflectionClass('kwd\blog\virtualproxy\domain\SomeDomainTypeProxy'); $subject = $this->getMock('kwd\blog\virtualproxy\domain\SomeDomainType'); $property = $proxyRefl->getProperty('_subject'); $property->setAccessible(true); $property->setValue($proxy, $subject); $subject ->expects($this->once()) ->method('doSomethingWithMoreState') ->will($this->returnValue('mockedValue')); $this->assertSame('mockedValue', $proxy->doSomethingWithMoreState(new domain\SomeDomainType())); } }