在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
PHPUnit WebServices ExtensionThe possibility to register test listeners to PHPUnit makes it extremly easy to execute actions on a certain state ofthe test runner (e.g. when the test is started). WSUnit make highly use of this possibility and listens to some ofthe emmited signals. PurposeWSUnit lifts unit tests to functional tests by configuration. Once configured the test listener sends a request to aspecified location, records the respond body & header, and persists it onto the filesystem. The test itself thene.g. verifies the correctness of the response. The Idea behind this was not to be forced to write API test more thanonce and to increase the reuse of already written verifications. InstallationThere is not much to install but to download the sources, configure the test listener in the phpunit.xml.dist,and create the configuration file to tell the listener which test shall call which url ot fetch the response. If youare using composer you simply have to define the dependency to WSUnit in your configuration and set up WSUnitaccourding to the following description. Composer{ "require-dev": { "lapistano/wsunit": " 2.*" }} This composer configuration will checkout the sources tagged as the 2nd release. In case your want the 'cutting eadge' versionreplace '2.*' by 'dev-master'. But be alarmed that this might be broken sometimes. NOTE:In case you do not know what this means the composer project website is a good place to start. GithubThus I recommend the composer way to make proxy-object a dependency to your project.The sources are also available via github. Just clone it as you might be familiar with. $ git clone git://github.com/lapistano/wsunit.git$ mkdir -p wsunit/vendor/lapistano$ cd wsunit/vendor/lapistano$ git clone git://github.com/lapistano/proxy-object.git ConfiguationThe configuration has two parts. One is the registration of the actual test listener to PHPUnit,the 2nd is the definition of which test (identified by it's name) shall be request the response of which location. PHPUnitFor the PHPUnit configuration please visit http://www.phpunit.de/manual/current/en/appendixes.configuration.html andread the 'Test Listener' section or copy the example to your configuration and simply adapt the location of yourconfiguration file. <listeners> <listener class="\lapistano\wsunit\WebServiceListener"> <arguments> <object class="\lapistano\wsunit\ExtensionsWebserviceListenerFactory"/> <object class="\lapistano\wsunit\Loader\LoaderConfiguration"/> <array> <element key="httpClient"> <string>lapistano\wsunit\Http\HttpClient</string> </element> <element key="logger"> <string>lapitano\wsunit\Logger\LoggerFilesystem</string> </element> <element key="configuration"> <string>/path/to/configuration.xml</string> </element> </array> </arguments> </listener> </listeners> Arguments
Test listener configurationBeside making PHPUnit aware of the test listener and to actually make each test aware of the loaction the responseshall be fetched from, a 2nd configuration file is needed. The following example show such a configuration. NOTE:The name and the location of the configuration file is set in the WARNING:Beware that if you decide to use namespaces they also have to be used in the phpunit configuration file to identify the used classes.In case you did something wrong here PHPUnit will just ignore your listener without any warning or error being thrown. Don't ask me why I know this.This behavior is fixed in PHPUnit 3.7. <?xml version="1.0" encoding="UTF-8"?><listener> <serializer>\lapistano\wsunit\Serializer\Http\Extensions_Webservice_Serializer_Http_Response</serializer> <test case="Example_TestCase" name="testGetData"> <location href="http://example.org/data.txt" /> </test> <test case="\lapistano\wsunit\Extensions_Webservice_Constraint_JsonErrorMessageProviderTest" name='testTranslateTypeToPrefix with data set "expected"' > <serializer>\lapistano\wsunit\Serializer\Http\Extensions_Webservice_Serializer_Http_Response</serializer> <location dataName="expected" href="http://blog.bastian-feder.de/blog.rss"> <query> <param name="mascott[]">tux</param> <param name="mascott[RedHat]">beastie</param> <param name="os">Linux</param> </query> </location> </test></listener> ###Available tags
DependenciesOptional
To be doneAs far as ws unit has come by now, unfortunately it is far from being complete. The following list shall give you anidea about what to be expected next:
If you have other usecases, ideas and/or demands feel free to fork and contribute. You are more than welcome ;) |
请发表评论