LaravelAnalyticsTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. use Carbon\Carbon;
  3. use Illuminate\Support\Collection;
  4. class LaravelAnalyticsTest extends PHPUnit_Framework_TestCase
  5. {
  6. protected $client;
  7. protected $laravelAnalytics;
  8. protected $siteId;
  9. public function setUp()
  10. {
  11. $this->client = Mockery::mock('\Spatie\LaravelAnalytics\GoogleApiHelper');
  12. $this->siteId = '12345';
  13. $this->laravelAnalytics = new \Spatie\LaravelAnalytics\LaravelAnalytics($this->client, $this->siteId);
  14. }
  15. /**
  16. * Test method getVisitorsAndPageViews().
  17. */
  18. public function testGetVisitorsAndPageViews()
  19. {
  20. $startDate = Carbon::now()->subDays('365')->format('Y-m-d');
  21. $endDate = Carbon::now()->format('Y-m-d');
  22. $this->client
  23. ->shouldReceive('performQuery')
  24. ->with($this->siteId, $startDate, $endDate, 'ga:visits,ga:pageviews', ['dimensions' => 'ga:date'])
  25. ->andReturn((object) ['rows' => [['20140101', 2, 3]]]);
  26. $googleResult = $this->laravelAnalytics->getVisitorsAndPageViews();
  27. $resultProperties = ['date', 'visitors', 'pageViews'];
  28. $this->assertTrue(count($googleResult) === 1);
  29. foreach ($resultProperties as $property) {
  30. $this->assertArrayHasKey($property, $googleResult[0]);
  31. }
  32. }
  33. /**
  34. * Test method getTopKeywords().
  35. */
  36. public function testGetTopKeywords()
  37. {
  38. $startDate = Carbon::now()->subDays('365')->format('Y-m-d');
  39. $endDate = Carbon::now()->format('Y-m-d');
  40. $this->client
  41. ->shouldReceive('performQuery')
  42. ->with($this->siteId, $startDate, $endDate, 'ga:sessions', ['dimensions' => 'ga:keyword', 'sort' => '-ga:sessions', 'max-results' => 30, 'filters' => 'ga:keyword!=(not set);ga:keyword!=(not provided)'])
  43. ->andReturn((object) ['rows' => [['first', 'second']]]);
  44. $googleResult = $this->laravelAnalytics->getTopKeyWords();
  45. $this->assertEquals($googleResult, new Collection([['keyword' => 'first', 'sessions' => 'second']]));
  46. }
  47. /**
  48. * Test method getTopReferrers().
  49. */
  50. public function testGetTopReferrers()
  51. {
  52. $startDate = Carbon::now()->subDays('365')->format('Y-m-d');
  53. $endDate = Carbon::now()->format('Y-m-d');
  54. $this->client
  55. ->shouldReceive('performQuery')
  56. ->with($this->siteId, $startDate, $endDate, 'ga:pageviews', ['dimensions' => 'ga:fullReferrer', 'sort' => '-ga:pageviews', 'max-results' => 20])
  57. ->andReturn((object) ['rows' => [['foundUrl', '123']]]);
  58. $googleResult = $this->laravelAnalytics->getTopReferrers();
  59. $this->assertEquals($googleResult, new Collection([['url' => 'foundUrl', 'pageViews' => '123']]));
  60. }
  61. /**
  62. * Test method getTopReferrers().
  63. */
  64. public function testGetTopBrowsers()
  65. {
  66. $startDate = Carbon::now()->subDays('365')->format('Y-m-d');
  67. $endDate = Carbon::now()->format('Y-m-d');
  68. $this->client
  69. ->shouldReceive('performQuery')
  70. ->with($this->siteId, $startDate, $endDate, 'ga:sessions', ['dimensions' => 'ga:browser', 'sort' => '-ga:sessions'])
  71. ->andReturn((object) ['rows' => [['Google Chrome', '123']]]);
  72. $googleResult = $this->laravelAnalytics->getTopBrowsers();
  73. $this->assertEquals($googleResult, new Collection([['browser' => 'Google Chrome', 'sessions' => '123']]));
  74. }
  75. /**
  76. * Test method getTopReferrers().
  77. */
  78. public function testGetMostVisitedPages()
  79. {
  80. $startDate = Carbon::now()->subDays('365')->format('Y-m-d');
  81. $endDate = Carbon::now()->format('Y-m-d');
  82. $this->client
  83. ->shouldReceive('performQuery')
  84. ->with($this->siteId, $startDate, $endDate, 'ga:pageviews', ['dimensions' => 'ga:pagePath', 'sort' => '-ga:pageviews', 'max-results' => 20])
  85. ->andReturn((object) ['rows' => [['visited url', '123']]]);
  86. $googleResult = $this->laravelAnalytics->getMostVisitedPages();
  87. $this->assertEquals($googleResult, new Collection([['url' => 'visited url', 'pageViews' => '123']]));
  88. }
  89. /**
  90. * Test method getSiteIdByUrl().
  91. */
  92. public function testGetSiteIdByUrl()
  93. {
  94. $testUrl = 'www.google.com';
  95. $siteId = 12345;
  96. $this->client->shouldReceive('getSiteIdByUrl')->with($testUrl)->andReturn($siteId);
  97. $result = $this->laravelAnalytics->getSiteIdByUrl($testUrl);
  98. $this->assertEquals($result, $siteId);
  99. }
  100. /**
  101. * Test method performQuery().
  102. */
  103. public function testPerformQuery()
  104. {
  105. $startDate = Carbon::now()->subDays('365');
  106. $endDate = Carbon::now();
  107. $metrics = 'ga:somedummymetric';
  108. $others = ['first', 'second'];
  109. $queryResult = 'result';
  110. $this->client
  111. ->shouldReceive('performQuery')
  112. ->with($this->siteId, $startDate->format('Y-m-d'), $endDate->format('Y-m-d'), $metrics, $others)
  113. ->andReturn($queryResult);
  114. $googleResult = $this->laravelAnalytics->performQuery($startDate, $endDate, $metrics, $others);
  115. $this->assertSame($googleResult, $queryResult);
  116. }
  117. /*
  118. * Test method isEnabled()
  119. */
  120. public function testIsEnabled()
  121. {
  122. $enabledAnalytics = new \Spatie\LaravelAnalytics\LaravelAnalytics($this->client, $this->siteId);
  123. $this->assertTrue($enabledAnalytics->isEnabled());
  124. $disabledAnalytics = new \Spatie\LaravelAnalytics\LaravelAnalytics($this->client);
  125. $this->assertFalse($disabledAnalytics->isEnabled());
  126. }
  127. /**
  128. * Test method performRealTimeQuery().
  129. */
  130. public function testPerformRealTimeQuery()
  131. {
  132. $metrics = 'rt:somedummymetric';
  133. $others = ['first', 'second'];
  134. $queryResult = 'result';
  135. $this->client
  136. ->shouldReceive('performRealTimeQuery')
  137. ->with($this->siteId, $metrics, $others)
  138. ->andReturn($queryResult);
  139. $googleResult = $this->laravelAnalytics->performRealTimeQuery($metrics, $others);
  140. $this->assertSame($googleResult, $queryResult);
  141. }
  142. /**
  143. * Test method getActiveUsers().
  144. */
  145. public function testGetActiveUsers()
  146. {
  147. $others = ['first', 'second'];
  148. $metrics = 'rt:activeUsers';
  149. $this->client
  150. ->shouldReceive('performRealTimeQuery')
  151. ->with($this->siteId, $metrics, $others)
  152. ->andReturn((object) ['rows' => [[0, '500']]]);
  153. $googleResult = $this->laravelAnalytics->getActiveUsers($others);
  154. $this->assertInternalType('int', $googleResult);
  155. }
  156. }