/** * Tests that we get the proper result when setting a profile summary, * whether it's by ID, by name, or the object itself. * * @runInSeparateProcess * @preserveGlobalState disabled */ public function testProfileSetting() { // For this test we will need a mock API object define('GOOGLE_ANALYTICS_API_AUTH_EMAIL', '*****@*****.**'); define('GOOGLE_ANALYTICS_API_AUTH_KEYFILE', __FILE__); define('GOOGLE_ANALYTICS_API_DATA_DIR', sys_get_temp_dir()); define('GOOGLE_ANALYTICS_API_LOG_FILE', GOOGLE_ANALYTICS_API_DATA_DIR . DIRECTORY_SEPARATOR . 'log.txt'); $profile1 = new ProfileSummary(); $profile1->setID('1'); $profile1->setName('Foo'); $profile2 = new ProfileSummary(); $profile2->setID('2'); $profile2->setName('Bar'); $profile3 = new ProfileSummary(); $profile3->setID('3'); $profile3->setName('Baz'); $api = $this->getMockBuilder(__NAMESPACE__ . '\\API')->setMethods(array('getProfileSummaryByID', 'getProfileSummaryByName'))->getMock(); $api->method('getProfileSummaryByID')->will($this->returnValue($profile1)); $api->method('getProfileSummaryByName')->will($this->returnValue($profile2)); $q = new GaDataQuery(); // First of all, make sure this exception is thrown when necessary $q->setProfile('1'); $this->assertThrows(__NAMESPACE__ . '\\LogicException', array($q, 'getProfile')); $q->setAPIInstance($api); /* Now we should get profile #1 (which has nothing to do with the parameter we passed when we set the profile; it's because of the mocked method). */ $this->assertSame($profile1, $q->getProfile()); /* If we set a profile by its name, the existing one should be cleared out. */ $q->setProfileName('Bar'); $this->assertSame($profile2, $q->getProfile()); // And if we set a profile object explicitly, we should always get that foreach (array($profile1, $profile2, $profile3) as $profile) { $q->setProfile($profile); $this->assertSame($profile, $q->getProfile()); } }
/** * Performs a Google Analytics query with the given * Google\Analytics\GaDataQuery object and returns the result as a * Google\Analytics\GaData object, or boolean false if the query's data * has been exhausted. * * @param Google\Analytics\GaDataQuery $query * @return Google\Analytics\GaData, boolean */ public function query(GaDataQuery $query) { $query->setAPIInstance($this); $totalResults = $query->getTotalResults(); do { /* If the given query is already active, meaning that we have made a call for its first page of results already, don't pass any arguments to $this->_makeRequest(). */ $hash = $query->getHash(); if ($hash == $this->_activeQuery) { if ($totalResults !== null && $this->_totalRows >= $totalResults) { // No reason to continue $response = false; break; } $response = $this->_makeRequest(); } else { $this->_activeQuery = $hash; $this->_totalRows = 0; $response = $this->_makeRequest(new APIRequest('data/ga', $query->getAsArray())); } /* The response will be boolean false if the query had data, but it has been exhausted. It will be a Google\Analytics\GaData object with no rows if the query returned no data at all. If there's no chance of achieving a response by iterating, we'll consider the operation as having retrieved a response, even if no data came back. */ if ($response && $response->getRows() || !$query instanceof IterativeGaDataQuery || !$query->iterate()) { $gotResponse = true; } else { $gotResponse = false; } } while (!$gotResponse); if ($response) { if ($query->getSamplingLevel() == GaDataQuery::SAMPLING_LEVEL_NONE && $response->containsSampledData()) { throw new SamplingException('The response contains sampled data.'); } /* I have to do this here instead of allowing it to happen automatically so that I can pass the API instance as an argument. */ $response->setColumnHeaders($this->_responseParsed['columnHeaders'], $this); // I also want the rows object to be aware of the columns object $rows = $response->getRows(); $rows->setColumnHeaders($response->getColumnHeaders()); $rowCount = count($rows); if ($rowCount) { $this->_totalRows += $rowCount; if ($totalResults !== null) { $surplus = $this->_totalRows - $totalResults; if ($surplus > 0) { /* Even though we already got the data, behave as we were asked and lop off the extra rows. */ $rows->discard($surplus); } } } } else { $this->_activeQuery = null; } return $response; }