Skip to content

Commit c0e0c63

Browse files
committed
Removed model dependency and brought tests to green.
1 parent cb205bc commit c0e0c63

File tree

11 files changed

+86
-147
lines changed

11 files changed

+86
-147
lines changed

composer.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@
88
"email": "[email protected]"
99
}
1010
],
11-
"repositories": [
12-
{
13-
"type": "vcs",
14-
"url": "https://github.com/hebbet/model.git"
15-
}
16-
],
1711
"require": {
1812
"illuminate/routing": "^10.0|^11.0|^12.0",
19-
"illuminate/support": "^10.0|^11.0|^12.0",
20-
"jenssegers/model": "dev-patch-1@dev"
13+
"illuminate/support": "^10.0|^11.0|^12.0"
2114
},
2215
"require-dev": {
2316
"orchestra/testbench-browser-kit": "^10.0",

phpunit.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
cacheDirectory=".phpunit.cache"
88
>
99
<testsuites>
10-
<testsuite name="Browser">
11-
<directory suffix="Test.php">./tests/Browser</directory>
12-
</testsuite>
1310
<testsuite name="Feature">
1411
<directory suffix="Test.php">./tests/Feature</directory>
1512
</testsuite>

resources/views/script.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ function startInterval(key, callback, delay) {
4545
if ({{ $ageCheckInterval }} > 0) {
4646
startInterval('ageTimer', caffeineReload, {{ $ageCheckInterval }});
4747
}
48-
</script>
48+
</script>

src/Dripper.php

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
1-
<?php namespace GeneaLabs\LaravelCaffeine;
1+
<?php
22

3-
use Jenssegers\Model\Model;
3+
declare(strict_types=1);
44

5-
class Dripper extends Model
5+
namespace GeneaLabs\LaravelCaffeine;
6+
7+
class Dripper
68
{
7-
public function getHtmlAttribute() : string
9+
public function getHtml(): string
810
{
9-
return (string) view('genealabs-laravel-caffeine::script')
11+
return (string) view("genealabs-laravel-caffeine::script")
1012
->with([
11-
'ageCheckInterval' => $this->ageCheckInterval,
12-
'ageThreshold' => $this->ageThreshold,
13-
'interval' => $this->interval,
14-
'url' => $this->url,
13+
"ageCheckInterval" => $this->getAgeCheckInterval(),
14+
"ageThreshold" => $this->getAgeThreshold(),
15+
"interval" => $this->getInterval(),
16+
"url" => $this->getUrl(),
1517
]);
1618
}
1719

18-
public function getAgeCheckIntervalAttribute() : int
20+
protected function getAgeCheckInterval(): int
1921
{
20-
return config(
21-
'genealabs-laravel-caffeine.outdated-drip-check-interval',
22-
2000
23-
);
22+
return config("genealabs-laravel-caffeine.outdated-drip-check-interval", 2000);
2423
}
2524

26-
public function getAgeThresholdAttribute() : int
25+
protected function getAgeThreshold(): int
2726
{
28-
return (config('session.lifetime', 32) - 2) * 60000;
27+
return (config("session.lifetime", 32) - 2) * 60000;
2928
}
3029

31-
public function getIntervalAttribute() : string
30+
protected function getInterval(): int
3231
{
33-
return config(
34-
'genealabs-laravel-caffeine.drip-interval',
35-
300000
36-
);
32+
return config("genealabs-laravel-caffeine.drip-interval", 300000);
3733
}
3834

39-
public function getUrlAttribute() : string
35+
protected function getUrl(): string
4036
{
41-
return trim(config('genealabs-laravel-caffeine.domain') ?? url('/'), '/')
42-
. '/'
43-
. trim(config(
44-
'genealabs-laravel-caffeine.route',
45-
'genealabs/laravel-caffeine/drip'
46-
), '/');
37+
return trim(config("genealabs-laravel-caffeine.domain") ?? url("/"), "/")
38+
. "/"
39+
. trim(
40+
config("genealabs-laravel-caffeine.route", "genealabs/laravel-caffeine/drip"),
41+
"/",
42+
);
4743
}
4844
}

src/Http/Middleware/LaravelCaffeineDripMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public function handle(Request $request, Closure $next)
4242
}
4343

4444
$dripper = (new Dripper);
45-
$content = preg_replace('/(<\/html>)\s*\z/', $dripper->html . "</html>", $content);
45+
$content = preg_replace('/(<\/html>)\s*\z/', "{$dripper->getHtml()}</html>", $content);
4646

4747
if (! preg_match_all('/(<\/html>)\s*\z/', $content, $matches)) {
48-
$content .= $dripper->html;
48+
$content .= $dripper->getHtml();
4949
}
5050

5151
$original = $response->original;

tests/Browser/DripTest.php

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

tests/BrowserTestCase.php

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

tests/Feature/CaffeineTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public function testMiddlewareInjectsDripScript()
2727
public function testSuccessfulDrip()
2828
{
2929
$dripRoute = config('genealabs-laravel-caffeine.route', 'genealabs/laravel-caffeine/drip');
30-
$html = $this
31-
->get(route('genealabs-laravel-caffeine.tests.form'))
30+
$html = $this->get(route('genealabs-laravel-caffeine.tests.form'))
3231
->response
3332
->getContent();
3433
$matches = [];

tests/Fixtures/partial_script.txt

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
<script>
2+
window.timers = window.timers || [];
3+
4+
function startInterval(key, callback, delay) {
5+
// If an interval with the same key already exists, clear it
6+
if (window.timers[key]) {
7+
clearInterval(window.timers[key]);
8+
}
9+
// Start a new interval and store its ID in the timers object
10+
window.timers[key] = setInterval(callback, delay);
11+
}
12+
213
var lastCheck = new Date();
14+
315
var caffeineSendDrip = function () {
416
var ajax = window.XMLHttpRequest
517
? new XMLHttpRequest
@@ -16,6 +28,17 @@
1628
ajax.send();
1729
};
1830

19-
setInterval(function () {
20-
caffeineSendDrip();
21-
}, 50000);
31+
var caffeineReload = function () {
32+
if (new Date() - lastCheck >= -58000) {
33+
setTimeout(function () {
34+
location.reload(true);
35+
}, Math.max(0, 2000 - 500) )
36+
}
37+
};
38+
39+
startInterval('dripTimer', caffeineSendDrip, 50000);
40+
41+
if (2000 > 0) {
42+
startInterval('ageTimer', caffeineReload, 2000);
43+
}
44+
</script>
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
<script>
2+
window.timers = window.timers || [];
3+
4+
function startInterval(key, callback, delay) {
5+
// If an interval with the same key already exists, clear it
6+
if (window.timers[key]) {
7+
clearInterval(window.timers[key]);
8+
}
9+
// Start a new interval and store its ID in the timers object
10+
window.timers[key] = setInterval(callback, delay);
11+
}
12+
213
var lastCheck = new Date();
14+
315
var caffeineSendDrip = function () {
416
var ajax = window.XMLHttpRequest
517
? new XMLHttpRequest
@@ -16,24 +28,17 @@
1628
ajax.send();
1729
};
1830

19-
setInterval(function () {
20-
caffeineSendDrip();
21-
}, 300000);
31+
var caffeineReload = function () {
32+
if (new Date() - lastCheck >= 7082000) {
33+
setTimeout(function () {
34+
location.reload(true);
35+
}, Math.max(0, 2000 - 500) )
36+
}
37+
};
38+
39+
startInterval('dripTimer', caffeineSendDrip, 300000);
2240

2341
if (2000 > 0) {
24-
setInterval(
25-
function () {
26-
if (new Date() - lastCheck >= 7082000) {
27-
location.reload(true);
28-
setTimeout(
29-
function () {
30-
location.reload(true);
31-
},
32-
Math.max(0, 2000 - 500),
33-
);
34-
}
35-
},
36-
2000,
37-
);
42+
startInterval('ageTimer', caffeineReload, 2000);
3843
}
3944
</script>

0 commit comments

Comments
 (0)