1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\GoogleAnalytics\Tests\Renderer\Script; |
4
|
|
|
|
5
|
|
|
use ByTIC\GoogleAnalytics\Tests\AbstractTest; |
6
|
|
|
use ByTIC\GoogleAnalytics\Tracking\Data\Ecommerce\Item; |
7
|
|
|
use ByTIC\GoogleAnalytics\Tracking\Data\Ecommerce\Transaction; |
8
|
|
|
use ByTIC\GoogleAnalytics\Tracking\Renderer\Script\AnalyticsJs; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AnalyticsJsTest |
12
|
|
|
* @package ByTIC\GoogleAnalytics\Tests |
13
|
|
|
*/ |
14
|
|
|
class AnalyticsJsTest extends AbstractTest |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
public function testRunEmpty() |
18
|
|
|
{ |
19
|
|
|
$script = $this->initScript(); |
20
|
|
|
|
21
|
|
|
self::assertSame( |
22
|
|
|
'', |
23
|
|
|
$script->render() |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function testGenerateBasic() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$script = $this->initScript(); |
30
|
|
|
$script->getGoogleAnalytics()->setTrackingId('UA-9999999-9'); |
31
|
|
|
|
32
|
|
|
self::assertSame( |
33
|
|
|
file_get_contents(TEST_FIXTURE_PATH . '\codes\analytics\basic.html'), |
34
|
|
|
$script->render() |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function testGenerateBasicTwoTrackers() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$script = $this->initScript(); |
41
|
|
|
$script->getGoogleAnalytics()->setTrackingId('UA-9999999-9'); |
42
|
|
|
$script->getGoogleAnalytics()->setTrackingId('UA-9999999-8', 'b'); |
43
|
|
|
|
44
|
|
|
self::assertSame( |
45
|
|
|
file_get_contents(TEST_FIXTURE_PATH . '\codes\analytics\basicTwoTrackers.html'), |
46
|
|
|
$script->render() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGenerateWithTransaction() |
51
|
|
|
{ |
52
|
|
|
$script = $this->initScript(); |
53
|
|
|
$script->getGoogleAnalytics()->setTrackingId('UA-9999999-8', 'b'); |
54
|
|
|
|
55
|
|
|
$transaction = Transaction::createFromArray(['id' => '999']); |
56
|
|
|
$item = Item::createFromArray(['transactionId' => '999', 'name' => 'Test Product']); |
57
|
|
|
$transaction->addItem($item); |
58
|
|
|
|
59
|
|
|
$script->getGoogleAnalytics()->addTransaction($transaction, 'b'); |
60
|
|
|
|
61
|
|
|
self::assertSame( |
62
|
|
|
file_get_contents(TEST_FIXTURE_PATH . '\codes\analytics\basicTransaction.html'), |
63
|
|
|
$script->render() |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return AnalyticsJs |
69
|
|
|
*/ |
70
|
|
|
protected function initScript() |
71
|
|
|
{ |
72
|
|
|
$ga = new \ByTIC\GoogleAnalytics\Tracking\GoogleAnalytics(); |
73
|
|
|
|
74
|
|
|
$script = new AnalyticsJs(); |
75
|
|
|
$script->setGoogleAnalytics($ga); |
76
|
|
|
return $script; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.