Hi This is probably more of a general programming question for PHP, but because it takes CakePHP into account, in particular, I thought I'd try a post here.
I've recently upgraded an old project to CakePHP 2.2.3, and I'm now writing some unit tests (yay for PHPUnit), but they're after the fact (naughty me). One of my models is a non tabled model that has a search function that uses a SoapClient. At one point, the model would instantiate the SoapClient in the search function, but when it came to writing unit tests for the search function, that sort of action made it hard to mock the web service. So I put a $client property on the model. I then moved instantiation of the SoapClient to the constructor, put was also able to assign a mocked SoapClient to the model in the unit test. When I was reading around on how to mock web services, I found this article [ http://pointbeing.net/weblog/2009/04/unit-testing-code-which-consumes-soap-services.html]. It talks a little bit about how the web services should be created in a factory, but doesn't go into detail. So, I'm interested in how, or even if, you would do a web service factory in CakePHP, and how that should look when using the model in a controller, or anywhere else. I understand that in any other language, or perhaps without the restriction of CakePHP (though that's a restriction in my own mind), this would be a great time to use an Inversion of Control container and apply the dependency injection in the production code, not just the unit test. However, this is CakePHP, and models are handled automagically via $uses, or manually via Controller::loadModel() or ClassRegistry::init(). I guess one way would be to refactor usage of the SoapClient as a datasource. I've used the Soap datasource from the datasources plugin a couple of times, but I've always found it wanting for CakePHP 2.X. Maybe that will change for CakePHP 3. When I started writing this, I was looking for the CakePHP way of web service factories, but now I think I've arrived at the answer being Datasources. Thoughts and comments would be appreciated. Regards Reuben Helms -- Like Us on FaceBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. Visit this group at http://groups.google.com/group/cake-php?hl=en.
