Skip to content

Commit

Permalink
Fix issues with additional trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell authored Jun 26, 2024
2 parents 99f9c5f + fcd6c97 commit 23fc270
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 7 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Test Suite

on:
push:
pull_request:

jobs:
php_tests:
if: "!contains(github.event.head_commit.message, 'changelog')"

runs-on: ${{ matrix.os }}

strategy:
matrix:
php: [8.3, 8.2]
laravel: [11.*]
statamic: [^5.0]
os: [ubuntu-latest]

name: ${{ matrix.php }} - ${{ matrix.statamic }} - ${{ matrix.laravel }}

steps:
- name: Checkout code
uses: actions/checkout@v1

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "statamic/cms:${{ matrix.statamic }}" --no-interaction --no-update
composer install --no-interaction
- name: Run Pest
run: vendor/bin/pest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ composer.lock
mix-manifest.json
package-lock.json
yarn.lock
.idea/*
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ The addon comes with a Facade for interacting with the Tracker:


### Adding your own tracking data
If you have your own custom tracking data, for example for one of your own tags, you can register then on the Facade. Please bear in mind tracking only happens while the response is generated, so where possible use augmentation hooks.
If you have your own custom tracking data, for example for one of your own tags, you can register them on the Facade. Please bear in mind tracking only happens while the response is generated, so where possible use augmentation hooks.

#### Using a closure

```php
Tracker::addAdditionalTracker(function ($tracker, $next) {
Expand All @@ -50,6 +52,18 @@ Tracker::addAdditionalTracker(function ($tracker, $next) {
});
```

#### Using an invokable class

```php
class AdditionalTrackerClass {
public function __invoke($tracker, $next) {
$tracker->addContentTag('additional::tag');
}
}

Tracker::addAdditionalTracker(AdditionalTrackerClass::class);
```

### Invalidating tracked data
To invalidate pages containing your tracked data, use a listener or observer, and call:

Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"Thoughtco\\StatamicCacheTracker\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Thoughtco\\StatamicCacheTracker\\Tests\\": "tests"
}
},
"require": {
"php": "^8.1",
"pixelfear/composer-dist-plugin": "^0.1.5",
Expand All @@ -13,10 +18,10 @@
"require-dev": {
"laravel/pint": "^1.13",
"mockery/mockery": "^1.3.1",
"nunomaduro/collision": "^6.0 || ^7.0",
"orchestra/testbench": "^7.0 || ^8.0",
"nunomaduro/collision": "^8.0",
"orchestra/testbench": "^9.0",
"pestphp/pest": "^2.24",
"phpunit/phpunit": "^9.0 || ^10.0"
"phpunit/phpunit": "^10.0"
},
"extra": {
"statamic": {
Expand Down
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" processIsolation="false" stopOnFailure="false" beStrictAboutTestsThatDoNotTestAnything="false" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
16 changes: 13 additions & 3 deletions src/Tracker/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Thoughtco\StatamicCacheTracker\Tracker;

use Closure;
use Illuminate\Support\Facades\Cache;
use InvalidArgumentException;
use Statamic\StaticCaching\Cacher;
Expand All @@ -14,7 +15,7 @@ class Manager

public function add(string $url, array $tags = [])
{
$storeData = $this->cacheStore()->get($this->cacheKey) ?? [];
$storeData = $this->all();
$storeData[md5($url)] = [
'url' => $url,
'tags' => collect($tags)->unique()->values()->all(),
Expand All @@ -25,13 +26,22 @@ public function add(string $url, array $tags = [])
return $this;
}

public function addAdditionalTracker(string $class)
public function addAdditionalTracker(Closure|string $class)
{
if (is_string($class)) {
$class = new $class;
}

$this->pipelines[] = $class;

return $this;
}

public function all()
{
return $this->cacheStore()->get($this->cacheKey) ?? [];
}

public function cacheStore()
{
try {
Expand All @@ -50,7 +60,7 @@ public function getAdditionalTrackers()

public function invalidate(array $tags = [])
{
$storeData = $this->cacheStore()->get($this->cacheKey) ?? [];
$storeData = $this->all();

$urls = [];
foreach ($storeData as $key => $data) {
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Thoughtco\StatamicCacheTracker\Tests;

use Illuminate\Encryption\Encrypter;
use Statamic\Testing\AddonTestCase;
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
use Thoughtco\StatamicCacheTracker\ServiceProvider;

class TestCase extends AddonTestCase
{
use PreventsSavingStacheItemsToDisk;

protected string $addonServiceProvider = ServiceProvider::class;

protected $shouldFakeVersion = true;

protected function resolveApplicationConfiguration($app)
{
parent::resolveApplicationConfiguration($app);

$app['config']->set('app.key', 'base64:'.base64_encode(Encrypter::generateKey($app['config']['app.cipher'])));

// Assume the pro edition within tests
$app['config']->set('statamic.editions.pro', true);

// enable caching
$app['config']->set('statamic.static_caching.strategy', 'half');
}
}
40 changes: 40 additions & 0 deletions tests/Unit/AdditionalTrackerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Thoughtco\StatamicCacheTracker\Tests\Unit;

use PHPUnit\Framework\Attributes\Test;
use Thoughtco\StatamicCacheTracker\Facades\Tracker;
use Thoughtco\StatamicCacheTracker\Tests\TestCase;

class AdditionalTrackerTest extends TestCase
{
#[Test]
public function tracks_additional_closures()
{
Tracker::addAdditionalTracker(function ($tracker, $next) {
$tracker->addContentTag('test::tag');
});

$this->get('/');

$this->assertSame(['test::tag'], collect(Tracker::all())->firstWhere('url', 'http://localhost/')['tags']);
}

#[Test]
public function tracks_additional_classes()
{
Tracker::addAdditionalTracker(AdditionalTrackerClass::class);

$this->get('/');

$this->assertSame(['additional::tag'], collect(Tracker::all())->firstWhere('url', 'http://localhost/')['tags']);
}
}

class AdditionalTrackerClass
{
public function __invoke($tracker, $next)
{
$tracker->addContentTag('additional::tag');
}
}
Empty file.

0 comments on commit 23fc270

Please sign in to comment.