Skip to content

Commit b90ce46

Browse files
author
Alex Westergaard
authored
Merge pull request #2 from AlexWestergaard/v1.x
- Updating classes to support chained events - Write PHPUnit testing and fix found issues - Removed Validation of Analytics->post() to avoid echoing unknowingly while testing
2 parents 639e57e + 981c2ca commit b90ce46

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+698
-849
lines changed

.github/workflows/ci.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: PHP CI
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- "master"
7+
workflow_dispatch:
8+
9+
jobs:
10+
render-php:
11+
runs-on: ${{ matrix.operating-system }}
12+
strategy:
13+
max-parallel: 4
14+
fail-fast: false
15+
matrix:
16+
operating-system: ["ubuntu-latest", "windows-latest"]
17+
php-versions: ["7.0", "7.4", "8.0", "8.1"]
18+
phpunit-versions: ["latest"]
19+
steps:
20+
- uses: actions/checkout@v3
21+
with:
22+
fetch-depth: 0
23+
- run: git merge origin/master --no-commit --ff-only
24+
- uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: ${{ matrix.php-versions }}
27+
extensions: mbstring, intl
28+
ini-values: post_max_size=256M, max_execution_time=180
29+
coverage: xdebug
30+
tools: phpunit:${{ matrix.phpunit-versions }}
31+
- if: ${{ matrix.php-versions == 7.0 }}
32+
name: "PHP 7.0"
33+
run: composer require --dev --with-all-dependencies --no-install phpunit/phpunit 6.0
34+
- if: ${{ matrix.php-versions == 7.4 }}
35+
name: "PHP 7.4"
36+
run: composer require --dev --with-all-dependencies --no-install phpunit/phpunit 8.0
37+
- if: ${{ matrix.php-versions >= 8.0 }}
38+
name: "PHP >8.0"
39+
run: composer require --dev --with-all-dependencies --no-install phpunit/phpunit
40+
- name: "Install & Test"
41+
run: |
42+
composer update --no-install --with-all-dependencies
43+
composer install
44+
./vendor/bin/phpunit

.gitignore

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
/vendor/
2-
/test/log/*.log
1+
# Composer
2+
/vendor
3+
/composer.lock
4+
5+
# PHPUnit
6+
/.phpunit.cache
7+
.phpunit.result.cache

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,31 @@ try {
6464
if ($loggedIn) {
6565
$analytics->setUserId($uniqueUserId);
6666
}
67-
68-
$viewCart = new Event\ViewCart();
69-
$viewCart->setCurrency('EUR');
70-
67+
68+
$viewCart = Event\ViewCart::new()
69+
->setCurrency('EUR');
70+
7171
$totalPrice = 0;
7272
foreach ($cartItems as $item) {
73-
$product = new Item();
74-
$product->setItemId($item['id']);
75-
$product->setItemName($item['name']);
76-
$product->setQuantity($item['qty']);
7773
$totalPrice += $item['price_total'];
78-
$product->setPrice(round($item['price_total'] / $item['qty'], 2)); // unit price
79-
$product->setItemVariant($item['colorName']);
80-
74+
$product = Item::new()
75+
->setItemId($item['id'])
76+
->setItemName($item['name'])
77+
->setQuantity($item['qty'])
78+
->setPrice(round($item['price_total'] / $item['qty'], 2)) // unit pric
79+
->setItemVariant($item['colorName']);
80+
8181
$viewCart->addItem($product);
8282
}
83-
83+
8484
$viewCart->setValue($totalPrice);
85-
85+
8686
$analytics->addEvent($viewCart);
87-
87+
8888
if (!$analytics->post()) {
8989
// Handling if post was unsuccessfull
9090
}
91-
91+
9292
// Handling if post was successfull
9393
} catch (GA4Exception $gErr) {
9494
// Handle exception
@@ -172,11 +172,13 @@ class ExampleEvent extends Model\Event
172172
public function setMyVariable(string $value)
173173
{
174174
$this->my_variable = $value;
175+
return $this; // Allows chained events
175176
}
176177

177178
public function setMyRequiredVariable(string $value)
178179
{
179180
$this->my_required_variable = $value;
181+
return $this; // Allows chained events
180182
}
181183
}
182184
```

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
},
1111
"require": {
1212
"guzzlehttp/guzzle": "^6.0"
13+
},
14+
"require-dev": {
15+
"phpunit/phpunit": "^9.0"
1316
}
14-
}
17+
}

0 commit comments

Comments
 (0)