Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3943806
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:49:31+00:00 2026-05-20T00:49:31+00:00

I have a following method, which retrieves top visited pages from Google Analytics: public

  • 0

I have a following method, which retrieves top visited pages from Google Analytics:

public function getData($limit = 10)
{
    $ids = '12345';
    $dateFrom = '2011-01-01';
    $dateTo = date('Y-m-d');

    // Google Analytics credentials
    $mail = 'my_mail';
    $pass = 'my_pass';

    $clientLogin = Zend_Gdata_ClientLogin::getHttpClient($mail, $pass, "analytics");
    $client = new Zend_Gdata($clientLogin);

    $reportURL = 'https://www.google.com/analytics/feeds/data?';

    $params = array(
        'ids' => 'ga:' . $ids,
        'dimensions' => 'ga:pagePath,ga:pageTitle',
        'metrics' => 'ga:visitors',
        'sort' => '-ga:visitors',
        'start-date' => $dateFrom,
        'end-date' => $dateTo,
        'max-results' => $limit
    );

    $query = http_build_query($params, '');
    $reportURL .= $query;

    $results = $client->getFeed($reportURL);

    $xml = $results->getXML();
    Zend_Feed::lookupNamespace('default');
    $feed = new Zend_Feed_Atom(null, $xml);

    $top = array();
    foreach ($feed as $entry) {
        $page['visitors'] = (int) $entry->metric->getDOM()->getAttribute('value');
        $page['url'] = $entry->dimension[0]->getDOM()->getAttribute('value');
        $page['title'] = $entry->dimension[1]->getDOM()->getAttribute('value');
        $top[] = $page;
    }

    return $top;
}

It needs some refactoring for sure, but the question is:

  • How would you write PHPUnit tests for this method?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:49:32+00:00Added an answer on May 20, 2026 at 12:49 am

    David Weinraub gave you the first half (how to set up your class to be mockable), so I’ll address the second half (how to build the mock).

    PHPUnit provides a great mocking facility with a simple API. Passing the user and password is too simple to test in my book, so I’d mock just the handling of the query and results. This requires mocks for Zend_Gdata and Zend_Gdata_App_Feed.

    public function testGetData() {
        // expected input to and output from mocks
        $url = 'https://www.google.com/analytics/feeds/data?ids=ga:12345...';
        $xml = <<<XML
    <feed>
        ...
    </feed>
    XML;
        // setup the mocks and method expectations
        $client = $this->getMock('Zend_Gdata', array('getFeed'));
        $feed = $this->getMock('Zend_Gdata_App_Feed', array('getXML'));
        $client->expects($this->once())
               ->method('getFeed')
               ->with($url)
               ->will($this->returnValue($feed));
        $feed->expects($this->once())
             ->method('getXML')
             ->will($this->returnValue($xml));
        // create the report (SUT) and call the method being tested
        $report = new MyReport();
        $report->setClient($client);
        $top = $report->getData();
        // check the final output; mocks are verified automatically
        $this->assertEquals(10, count($top));
        $this->assertEquals(array(
                'visitors' => 123, 
                'url' => 'http://...', 
                'title' => 'My Home Page'
            ), $top[0]);
    }
    

    The above will test that the URL was correct and return the XML feed expected from Google. It removes all dependence on the Zend_Gdata classes. If you don’t use type hinting on setClient(), you can even use stdClass as the base for the two mocks since you will only be using mocked methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method which is not capturing anything from the user.If I
I have the following C# helper class which retrieves a Json String from a
I have the following method which serialises an object to a HTML tag. I
I have a view with which I have a button calling the following method.
I have a CATiledLayer into which I render content in the following method -
My problem is the following. I have a method which simply takes an XML
I have the following code which works just fine when the method is POST,
Which of the following has the best performance? I have seen method two implemented
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have an application which retrieves a lot of numerical data from a server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.