Mock requests in TYPO3 unit testing - unit-testing

Is it possible to mock client (POST or GET) requests in unit testing using the TYPO3 testing framework with phpunit? I'm looking for something like the static::createClient() of Symfony.
UPDATE: Looking at PHPUnit - test MVC controller with $_POST variable I found the simple solution of just adding to the $_POST array the expected content. Question can be closed.

Related

How to write unit tests for server side Meteor code?

I have some server side code -- meteor methods and simple backend helpers -- that I would like to test. I've read the documentation testing with Meteor, but I am having a hard time connecting the documentation to my very simple use case. Can someone share with me how they've tested a meteor method or a simple backend JS function?
For instance, let's say you have some server method in, some_methods.js
function someHelper() {
// does lots of cool stuff
};
Meteor.methods({
'user/update' (userProperties) {
// updating some user properties
someHelper();
}
})
We have developed unit and integration tests for our open source application called RadGrad (https://radgrad.org).
For details on how we do unit and integration testing, please see:
https://www.radgrad.org/docs/developer-guide-testing.html
Here is an example of a unit (server-side only) test:
https://github.com/radgrad/radgrad/blob/master/app/imports/api/career/CareerGoalCollection.test.js
And here is an example of an integration (client + server) test:
https://github.com/radgrad/radgrad/blob/master/app/imports/api/career/CareerGoalCollection.methods.app-test.js
We do not have extensive UI tests; you'll need to use something like Selenium for that. UI testing in Meteor is no different from UI testing for any other web app.

Functional tests using PHPUnit

I see WebTestCase and Symfony come up a lot...I have some unit tests in PHPUnit already but really need higher level tests...I do not need anything fancy like Selenium (the UI is done in ExtJS but this could change entirely to Polymer in the future) I just need to post data and check the JSON results.
Can this be done via PHPUnit or do I need to bring in Symfony and it's WebTestCase?
Testing RESTful web services using PHPUnit
Note: I am actually looking to test the services -- no mocked objects desired -- I need to login when the test suite is first run -- store the SESSION ID and go through each test...
I have achieved success in writing PHP functional tests using Guzzle.
It allows you to perform HTTP requests like this:
$client = new GuzzleHttp\Client();
$response = $client->get('http://guzzlephp.org');
And the $client will maintain the PHPSESSIONID across requests, allowing you to perform authenticated requests by logging in and then performing requests with the same client.

How to test the upper layers of a ZF application

How do you test the controller and views in a Zend Framework application?
I have been using PHPUnit to test the mapper, domain and services, and that's been working fine. I use dependency injection so that I can isolate the class under test.
When it comes to the controller however I would probably not try to isolate it. I would effectively be doing integration tests at this point, but I think that's fine. I would still use PHPUnit and fire off requests for most tests.
For the views, I am thinking of using the CSS selectors in Zend_Test, but since I have a lot of Javascript I will have to use selenium too.
Does this all sound reasonable? What do you do?

Testing views with Spring Framework at the same time you run unit tests?

Is there a way to test views rendered in freemarker at the same time I run my unit tests? It would be great if the test failed if Freemarker threw an exception.
Rails has a feature where you can render views whenever an action is called. Is there an easy way to configure this type of behavior for Spring too?
Check out this blog post. We used this approach with great success for Velocity template. Looks like here is a working code example.
It requires few extra classes like MockWebApplicationContextLoader but works great with mocked requests.

How to unit test your API?

I'm at the point where I need to write unit tests for a REST API written using CakePHP 1.3. The API supports GET, POST and PUT requests for querying and manipulating data.
Is there any established way to test the correct input/output of an API simulating an HTTP request, using fixtures? I do not want to run actual POST/PUT requests against the live (dev) database. How can I best mock out the system to use temporary models, yet test the rest of the stack as-is?
Testing GET requests is easy enough with controller tests. However, for data manipulation the API uses HTTP headers quite extensively and also parses raw XML and JSON POST/PUT data. The controller unit test methods only mock POST data by setting $this->data in the controller, which does not allow me to properly test the API.
You should either create Mocks or use Isolation Framework in order to simulate API environment. Unit tests should not depend on resources like internet connections, network, endpoints etc.
If you intend to test real API calls you should create integration test project and use it for this purpose. But be aware integration tests are mostly not repeatable and would give you different results on each run.
I'd recommend starting with a little research. These articles should help:
Unit Testing CakePHP Shells
Testing CakePHP controllers - Mock Objects edition
Testing Models with CakePHP 1.2 test suite
Looks like you might be able to test the raw XML PUT and POST data without too much trouble. The CakePHP REST documentation says this:
If a POST or PUT request has an XML content-type, then the input is taken and passed to an instance of Cake's Xml object, which is assigned to the $data property of the controller. Because of this feature, handling XML and POST data in parallel is seamless: no changes are required to the controller or model code. Everything you need should end up in $this->data.
Try stepping through your controller code in debug mode to see what actually comes in through $this->data during an XML request.
As for avoiding the live database, would a SQLite in-memory database be any easier?