-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from matomo-org/more-ga-error
Show last GA error if there was one in exception message when cannot reach GA.
- Loading branch information
Showing
2 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
tests/Integration/Google/GoogleAnalyticsQueryServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Piwik - free/libre analytics platform | ||
* | ||
* @link http://piwik.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
*/ | ||
|
||
namespace Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\Google; | ||
|
||
|
||
use Piwik\Container\StaticContainer; | ||
use Piwik\Date; | ||
use Piwik\Metrics; | ||
use Piwik\Plugins\GoogleAnalyticsImporter\Google\GoogleAnalyticsQueryService; | ||
use Piwik\Plugins\GoogleAnalyticsImporter\Google\GoogleQueryObjectFactory; | ||
use Piwik\Tests\Framework\Fixture; | ||
use Piwik\Tests\Framework\TestCase\IntegrationTestCase; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class GoogleAnalyticsQueryServiceTest extends IntegrationTestCase | ||
{ | ||
public function getTestDataForGaErrorTest() | ||
{ | ||
return [ | ||
[new \Exception(json_encode([ | ||
'error' => [ | ||
'message' => 'this is a test exception', | ||
], | ||
]), 503), 'Failed to reach GA after 2 attempts. Restart the import later. Last GA error message: this is a test exception'], | ||
[new \Exception(json_encode([ | ||
'error' => [ | ||
], | ||
]), 503), 'Failed to reach GA after 2 attempts. Restart the import later.'], | ||
[new \Exception('lakjdsflsdj', 503), 'Failed to reach GA after 2 attempts. Restart the import later.'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getTestDataForGaErrorTest | ||
*/ | ||
public function test_query_returnsGaMessageWhenGaReturnsPersistentError($testEx, $expectedMessage) | ||
{ | ||
Fixture::createWebsite('2012-02-02 00:00:00'); | ||
|
||
$client = new \Google_Client(); | ||
$mockReportingService = new \Google_Service_AnalyticsReporting($client); | ||
|
||
$builder = $this->getMockBuilder(\Google_Service_AnalyticsReporting_Resource_Reports::class); | ||
$mockReportingService->reports = $builder | ||
->disableOriginalConstructor() | ||
->onlyMethods([ 'batchGet' ]) | ||
->getMock(); | ||
$mockReportingService->reports->method('batchGet')->willThrowException($testEx); | ||
|
||
$this->getMockBuilder(\Google_Service_AnalyticsReporting::class); | ||
$gaQueryService = new GoogleAnalyticsQueryService($mockReportingService, 'testviewid', [], 1, 'testuser', | ||
StaticContainer::get(GoogleQueryObjectFactory::class), StaticContainer::get(LoggerInterface::class)); | ||
$gaQueryService->setMaxAttempts(2); | ||
|
||
$this->expectException(\Exception::class); | ||
$this->expectExceptionMessage($expectedMessage); | ||
|
||
$gaQueryService->query(Date::factory('2020-03-04'), ['somedimension'], [Metrics::INDEX_NB_VISITS]); | ||
} | ||
} |