I am very proud to announce the first release of the proxy-object.
Initiated by Thomas Weinert and me back in 2008, this library was just a first draft with all the functionality we needed at this time. Working with it and using it, proxy-object got grown up. Finally Liip sponsored me some time to do necessary changes and best of all writing unit tests.
The outcome is this little library making it much easier to generate a proxy of your system under test (SUT). Another thought on this library was, that it should be very easy to use if you know the way to mock classes and methods in PHPUnit. Proxy-object has almost the same API, but does not change the behavior of the proxied class/method. The only purpose is to expose hidden methods and members.
There is no specific installation routine to be followed. Just clone or checkout the source into to your project and use it.
Thanks to the feedback of Benjamin Eberlei the source is PSR-0 compatible.
1. Exposing invisible Methods
One of the main purpose of this library is to expose invisible (private or protected) methods to the SUT.
To do so use just create a new ProxyBuilder object and pass the method to be exposed.
$proxy = new lapistanoProxyObject(); // generate and configure proxied object $proxiedObject = $proxy ->getProxyBuilder('myClass') ->setConstructorAgrs(array('Argument1', 'Argument2')) ->setMethods(array('myMethod')) ->getProxy(); // invoke proxied method $proxieObject->myMethod();
2. Exposing invisible Members
Another purpose of this library is to expose invisible members not reachable via a setter. This is to prevent you from writing setter methods just for the purpose of unit testing.
Use the setProperties() method to achieve this.
$proxy = new lapistanoProxyObject(); // generate and configure proxied object $proxiedObject = $proxy ->getProxyBuilder('myClass') ->setProperties(array('myMember')) ->getProxy(); // change content proxied member $proxieObject->myMember = 'another value';
Despite the fact that it is possible to expose private members by naming them in the setProperties array, generating a proxy object without the property declaration will only expose protected members. This is because I am not a big fan of exposing to much from a class necessary. If someone thinks this should be changed, I'd would be more than happy to discuss this topic.
3. Creating a proxied object without calling the constructor
Sometimes it is necessary to suppress the invocation of the defined constructor. Therefore I followed the API of PHPunits MockBuilder and added the disableOriginalConstructor() method.
$proxy = new lapistanoProxyObject(); // generate and configure proxied object $proxiedObject = $proxy ->getProxyBuilder('myClass') ->disableOriginalConstructor() ->getProxy(); // change value of proxied member $proxieObject->myMember = 'another value';
If you got interested in this library, it is published on github:
Feel free to fork it, try it out, and most important give me some feedback of what you think about proxy-object.