|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Happyr\GoogleAnalyticsBundle\Tests\Unit\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Happyr\GoogleAnalyticsBundle\Service\Tracker; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class TrackerTest. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Tobias Nyholm |
|
11
|
|
|
*/ |
|
12
|
|
|
class TrackerTest extends \PHPUnit_Framework_TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public function testSend() |
|
15
|
|
|
{ |
|
16
|
|
|
$trackerId = 'foo'; |
|
17
|
|
|
$version = 1; |
|
18
|
|
|
$clientId = 'clientId'; |
|
19
|
|
|
|
|
20
|
|
|
$data = [ |
|
21
|
|
|
'v' => $version, |
|
22
|
|
|
'tid' => $trackerId, |
|
23
|
|
|
'cid' => $clientId, |
|
24
|
|
|
't' => 'pageview', |
|
25
|
|
|
'baz' => 'bar', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
$httpClient = $this->getMockBuilder('Happyr\GoogleAnalyticsBundle\Http\HttpClientInterface') |
|
29
|
|
|
->setMethods(['send']) |
|
30
|
|
|
->disableOriginalConstructor() |
|
31
|
|
|
->getMock(); |
|
32
|
|
|
|
|
33
|
|
|
$httpClient->expects($this->once()) |
|
34
|
|
|
->method('send') |
|
35
|
|
|
->with($data) |
|
36
|
|
|
->willReturn(true); |
|
37
|
|
|
|
|
38
|
|
|
$clientIdProvider = $this->getMockBuilder('Happyr\GoogleAnalyticsBundle\Service\ClientIdProvider') |
|
39
|
|
|
->disableOriginalConstructor() |
|
40
|
|
|
->getMock(); |
|
41
|
|
|
|
|
42
|
|
|
$tracker = $this->getMockBuilder('Happyr\GoogleAnalyticsBundle\Service\Tracker') |
|
43
|
|
|
->setMethods(['getClientId']) |
|
44
|
|
|
->enableOriginalConstructor() |
|
45
|
|
|
->setConstructorArgs([$httpClient, $clientIdProvider, $trackerId, $version]) |
|
46
|
|
|
->getMock(); |
|
47
|
|
|
|
|
48
|
|
|
$tracker->expects($this->once()) |
|
49
|
|
|
->method('getClientId') |
|
50
|
|
|
->willReturn($clientId); |
|
51
|
|
|
|
|
52
|
|
|
$tracker->send(['baz' => 'bar'], 'pageview'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testGetClientId() |
|
56
|
|
|
{ |
|
57
|
|
|
$clientId = 'foobar'; |
|
58
|
|
|
$provider = $this->getMockBuilder('Happyr\GoogleAnalyticsBundle\Service\ClientIdProvider') |
|
59
|
|
|
->setMethods(['getClientId']) |
|
60
|
|
|
->disableOriginalConstructor() |
|
61
|
|
|
->getMock(); |
|
62
|
|
|
$provider->expects($this->once()) |
|
63
|
|
|
->method('getClientId') |
|
64
|
|
|
->willReturn($clientId); |
|
65
|
|
|
|
|
66
|
|
|
$tracker = new TrackerDummy( |
|
67
|
|
|
$this->getMockBuilder('Happyr\GoogleAnalyticsBundle\Http\HttpClientInterface')->getMock(), |
|
68
|
|
|
$provider, |
|
69
|
|
|
'foo', |
|
70
|
|
|
'bar' |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$firstResult = $tracker->getClientId(); |
|
74
|
|
|
$secondResult = $tracker->getClientId(); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals($firstResult, $secondResult, 'getClientId must return the same ID at every call'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
class TrackerDummy extends Tracker |
|
80
|
|
|
{ |
|
81
|
|
|
public function getClientId() |
|
82
|
|
|
{ |
|
83
|
|
|
return parent::getClientId(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|