when im wrote this code, i didn’t use phpunit and functional tests, because i don’t know how to write tests for this code. I know how to write tests for other functions and code, but i don’t know which test’s required for this code. Can anybody explain ?
All functions is google adwords API. Not my own. I don’t need to test them also.
$adStatsSelector = new AdStatsSelector();
$adStatsSelector->dateRange = new
DateRange('20100901','20101001');
$user = new AdWordsUser();
$user->LogDefaults();
$servicedAccountService = $user->GetServicedAccountService('v201008');
$selector = new ServicedAccountSelector();
$graph = $servicedAccountService->get($selector);
foreach($graph->accounts as &$account) {
$user->SetClientId($account->login);
$campaignService = $user->GetCampaignService('v201008');
$selector = new CampaignSelector(null,null,$adStatsSelector);
$page = $campaignService->get($selector);
$account->campaigns = $page->entries;
}
As the code stands it’s hard to define tests because it’s not in the form of something we can call; we can’t vary inputs and make sure we get certain outputs.
So as best I can see the thing you can test is whether graph ends up populated with the expected data. Somehow you invoke this code and have a look at what’s in graph.
Now looking at the code several questions come to mind:
So I would modify this code to generalise it, and put it in a function. We can then test the function. Imagine a function( in pseudo code)
now you can vary the inputs and check the response, but … how do you know what the response should be? You may do best to mock the services you use. You can then also assert that you are calling services with the correct parameters, and for some tests force the services to raise error conditions.
Summary: Writing code to be testable really helps when you want to test things, in doing so you tend to focus on the dull but inportant stuff such as error handling.