Skip to content

Commit 451cf5d

Browse files
author
Alex Westergaard
committed
Fix tests and have better grouping
1 parent 0cb4436 commit 451cf5d

File tree

6 files changed

+128
-128
lines changed

6 files changed

+128
-128
lines changed

src/UserProperty.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace AlexWestergaard\PhpGa4;
44

55
use AlexWestergaard\PhpGa4\Helper\AbstractUserProperty;
6-
use AlexWestergaard\PhpGa4\Facade\Type\UserProperty as TypeUserProperty;
76

8-
class UserProperty extends AbstractUserProperty implements TypeUserProperty
7+
class UserProperty extends AbstractUserProperty
98
{
109
protected null|string $name;
1110
protected null|int|float|string $value;

test/Unit/AbstractionTest.php renamed to test/Unit/AbstractTest.php

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
namespace AlexWestergaard\PhpGa4Test\Unit;
44

5-
use AlexWestergaard\PhpGa4\UserProperty;
65
use AlexWestergaard\PhpGa4\Item;
76
use AlexWestergaard\PhpGa4\Helper;
87
use AlexWestergaard\PhpGa4\Facade\Type;
98
use AlexWestergaard\PhpGa4\Exception;
10-
use AlexWestergaard\PhpGa4\Event;
119
use AlexWestergaard\PhpGa4Test\TestCase;
1210
use AlexWestergaard\PhpGa4Test\Mocks;
1311

14-
final class AbstractionTest extends TestCase
12+
final class AbstractTest extends TestCase
1513
{
1614
/******************************************************************
1715
* ABSTRACT IO | INPUT OUTPUT
@@ -147,78 +145,4 @@ public function getName(): string
147145

148146
$event->toArray();
149147
}
150-
151-
/******************************************************************
152-
* ABSTRACT USER PROPERTY
153-
*/
154-
155-
public function test_abstract_userproperty_interface_capabilities()
156-
{
157-
$userProperty = new Mocks\MockAbstractUserProperty();
158-
159-
$userProperty->setName($name = 'testname');
160-
$userProperty->setValue($value = 'testvalue');
161-
162-
$export = $userProperty->toArray();
163-
164-
$this->assertArrayHasKey($name, $export);
165-
$this->assertArrayHasKey('value', $export[$name]);
166-
$this->assertEquals($value, $export[$name]['value']);
167-
}
168-
169-
public function test_abstract_userproperty_throws_on_reserved_name()
170-
{
171-
$userProperty = new Mocks\MockAbstractUserProperty();
172-
173-
$this->expectException(Exception\Ga4UserPropertyException::class);
174-
$this->expectExceptionCode(Exception\Ga4Exception::PARAM_RESERVED);
175-
176-
$userProperty->setName(UserProperty::RESERVED_NAMES[0]);
177-
}
178-
179-
public function test_abstract_userproperty_throws_on_too_long_name()
180-
{
181-
$userProperty = new Mocks\MockAbstractUserProperty();
182-
183-
$this->expectException(Exception\Ga4UserPropertyException::class);
184-
$this->expectExceptionCode(Exception\Ga4Exception::PARAM_TOO_LONG);
185-
186-
$tooLongName = '';
187-
while (mb_strlen($tooLongName) <= 24) {
188-
$tooLongName .= range('a', 'z')[rand(0, 25)];
189-
}
190-
191-
$userProperty->setName($tooLongName);
192-
}
193-
194-
/******************************************************************
195-
* OTHER
196-
*/
197-
198-
public function test_convert_helper_transforms_array_into_events()
199-
{
200-
$list = Helper\Converter::parseEvents([
201-
['TutorialBegin' => []],
202-
['UnlockAchievement' => ['achievement_id' => '123']],
203-
['NotAnEvent' => ['skip' => 'me']]
204-
]);
205-
206-
$this->assertIsArray($list);
207-
$this->assertCount(2, $list);
208-
$this->assertInstanceOf(Event\TutorialBegin::class, $list[0]);
209-
$this->assertInstanceOf(Event\UnlockAchievement::class, $list[1]);
210-
211-
$this->analytics->addEvent(...$list);
212-
$this->assertCount(2, $this->analytics['events']);
213-
}
214-
215-
public function test_snakecase_helper_transforms_camelcase_names()
216-
{
217-
$this->assertEquals('snake_case', Helper\Converter::snake('snakeCase'));
218-
}
219-
220-
public function test_camelcase_helper_transforms_snakecase_names()
221-
{
222-
$this->assertEquals('snakeCase', Helper\Converter::camel('snake_case'));
223-
}
224148
}

test/Unit/AnalyticsTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,42 @@ public function test_exports_events_to_array()
104104

105105
$this->assertNull($this->analytics->post());
106106
}
107+
108+
public function test_throws_missing_measurement_id()
109+
{
110+
$this->expectException(Ga4Exception::class);
111+
$this->expectExceptionCode(Ga4Exception::REQUEST_MISSING_MEASUREMENT_ID);
112+
113+
Analytics::new('', $this->prefill['api_secret'], true)->post();
114+
}
115+
116+
public function test_throws_missing_apisecret()
117+
{
118+
$this->expectException(Ga4Exception::class);
119+
$this->expectExceptionCode(Ga4Exception::REQUEST_MISSING_API_SECRET);
120+
121+
Analytics::new($this->prefill['measurement_id'], '', true)->post();
122+
}
123+
124+
public function test_throws_on_too_large_request_package()
125+
{
126+
$kB = 1024;
127+
$preparyKB = '';
128+
while (mb_strlen($preparyKB) < $kB) {
129+
$preparyKB .= 'AAAAAAAA'; // 8 bytes
130+
}
131+
132+
$this->expectException(Ga4Exception::class);
133+
$this->expectExceptionCode(Ga4Exception::REQUEST_TOO_LARGE);
134+
135+
$userProperty = UserProperty::new()->setName('large_package');
136+
137+
$overflowValue = '';
138+
while (mb_strlen(json_encode($userProperty->toArray())) <= ($kB * 131)) {
139+
$overflowValue .= $preparyKB;
140+
$userProperty->setValue($overflowValue);
141+
}
142+
143+
$this->analytics->addUserProperty($userProperty)->post();
144+
}
107145
}

test/Unit/ExceptionTest.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

test/Unit/HelperTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4Test\Unit;
4+
5+
use AlexWestergaard\PhpGa4\Helper;
6+
use AlexWestergaard\PhpGa4\Event;
7+
use AlexWestergaard\PhpGa4Test\TestCase;
8+
9+
final class HelperTest extends TestCase
10+
{
11+
public function test_convert_helper_transforms_array_into_events()
12+
{
13+
$list = Helper\Converter::parseEvents([
14+
['TutorialBegin' => []],
15+
['UnlockAchievement' => ['achievement_id' => '123']],
16+
['NotAnEvent' => ['skip' => 'me']]
17+
]);
18+
19+
$this->assertIsArray($list);
20+
$this->assertCount(2, $list);
21+
$this->assertInstanceOf(Event\TutorialBegin::class, $list[0]);
22+
$this->assertInstanceOf(Event\UnlockAchievement::class, $list[1]);
23+
24+
$this->analytics->addEvent(...$list);
25+
$this->assertCount(2, $this->analytics['events']);
26+
}
27+
28+
public function test_snakecase_helper_transforms_camelcase_names()
29+
{
30+
$output = Helper\Converter::snake('snakeCase');
31+
$this->assertEquals('snake_case', $output);
32+
}
33+
34+
public function test_camelcase_helper_transforms_snakecase_names()
35+
{
36+
$output = Helper\Converter::camel('snake_case');
37+
$this->assertEquals('snakeCase', $output);
38+
}
39+
}

test/Unit/UserPropertyTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4Test\Unit;
4+
5+
use AlexWestergaard\PhpGa4\UserProperty;
6+
use AlexWestergaard\PhpGa4\Exception;
7+
use AlexWestergaard\PhpGa4Test\TestCase;
8+
9+
final class UserPropertyTest extends TestCase
10+
{
11+
public function test_can_configure_and_export()
12+
{
13+
$userProperty = new UserProperty();
14+
15+
$userProperty->setName($name = 'testname');
16+
$userProperty->setValue($value = 'testvalue');
17+
18+
$export = $userProperty->toArray();
19+
20+
$this->assertArrayHasKey($name, $export);
21+
$this->assertArrayHasKey('value', $export[$name]);
22+
$this->assertEquals($value, $export[$name]['value']);
23+
}
24+
25+
public function test_throws_on_reserved_name()
26+
{
27+
$userProperty = new UserProperty();
28+
29+
$this->expectException(Exception\Ga4UserPropertyException::class);
30+
$this->expectExceptionCode(Exception\Ga4Exception::PARAM_RESERVED);
31+
32+
$userProperty->setName(UserProperty::RESERVED_NAMES[0]);
33+
}
34+
35+
public function test_throws_on_too_long_name()
36+
{
37+
$userProperty = new UserProperty();
38+
39+
$this->expectException(Exception\Ga4UserPropertyException::class);
40+
$this->expectExceptionCode(Exception\Ga4Exception::PARAM_TOO_LONG);
41+
42+
$tooLongName = '';
43+
while (mb_strlen($tooLongName) <= 24) {
44+
$tooLongName .= range('a', 'z')[rand(0, 25)];
45+
}
46+
47+
$userProperty->setName($tooLongName);
48+
}
49+
}

0 commit comments

Comments
 (0)