Skip to content

Commit

Permalink
Feature/logs sampling (#19)
Browse files Browse the repository at this point in the history
* logs sampling
  • Loading branch information
mattvb91 authored Jun 9, 2024
1 parent 765f68c commit 016ec89
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
vendor
.idea
.phpunit.result.cache
composer.lock
composer.lock
.phpunit.cache
coverage.xml
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "package",
"license": "MIT",
"scripts": {
"phpunit": "phpunit --testdox --coverage-clover=coverage.xml",
"phpunit": "XDEBUG_MODE=coverage phpunit --testdox --coverage-clover=coverage.xml",
"phpstan": "phpstan analyse",
"codesniffer": "phpcs ./src ./tests/**/*.php --standard=./codesniffer.xml -p",
"codefixer": "phpcbf ./src ./tests/**/*.php --standard=./codesniffer.xml",
Expand All @@ -31,8 +31,8 @@
"symfony/http-foundation": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"dms/phpunit-arraysubset-asserts": "^0.4.0",
"phpunit/phpunit": "^10",
"dms/phpunit-arraysubset-asserts": "^0.5.0",
"phpstan/phpstan": "^1.10",
"squizlabs/php_codesniffer": "^3.7",
"rector/rector": "^1.0",
Expand Down
32 changes: 14 additions & 18 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false"
backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true"
convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false"
forceCoversAnnotation="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Caddy PHP Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false" requireCoverageMetadata="true">
<testsuites>
<testsuite name="Caddy PHP Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
</php>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
23 changes: 20 additions & 3 deletions src/Config/Logs/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,38 @@
class Log implements Arrayable
{
private LogLevel $level;
private ?Sampling $sampling;

public function __construct(LogLevel $level = LogLevel::DEBUG)
{
public function __construct(
LogLevel $level = LogLevel::DEBUG,
Sampling $sampling = null
) {
$this->level = $level;
$this->sampling = $sampling;
}

public function getLevel(): LogLevel
{
return $this->level;
}

public function setSample(Sampling $sample): self
{
$this->sampling = $sample;

return $this;
}

public function toArray(): array
{
return [
$array = [
'level' => $this->getLevel(),
];

if (isset($this->sampling)) {
$array['sampling'] = $this->sampling->toArray();
}

return $array;
}
}
33 changes: 33 additions & 0 deletions src/Config/Logs/Sampling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace mattvb91\CaddyPhp\Config\Logs;

use mattvb91\CaddyPhp\Interfaces\Arrayable;

/**
* https://caddyserver.com/docs/json/logging/logs/sampling/
*/
class Sampling implements Arrayable
{
private int $interval;
private int $first;
private int $thereafter;

public function __construct(int $interval = 0, int $first = 0, int $thereafter = 0)
{
$this->interval = $interval;
$this->first = $first;
$this->thereafter = $thereafter;
}

public function toArray(): array
{
return [
'interval' => $this->interval,
'first' => $this->first,
'thereafter' => $this->thereafter,
];
}
}
3 changes: 2 additions & 1 deletion tests/Integration/CaddyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use mattvb91\CaddyPhp\Config\Apps\Tls;
use mattvb91\CaddyPhp\Config\Logging;
use mattvb91\CaddyPhp\Config\Logs\Log;
use mattvb91\CaddyPhp\Config\Logs\Sampling;
use PHPUnit\Framework\TestCase;

class CaddyTest extends TestCase
Expand All @@ -36,7 +37,7 @@ public function testCanLoadWithLogs(): void
$caddy = new Caddy();
$caddy->setLogging(
(new Logging())
->addLog(new Log())
->addLog(new Log(sampling: new Sampling()))
);

$this->assertTrue($caddy->load());
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Apps/CacheTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Unit\Apps;
namespace Tests\Unit\Apps;

use mattvb91\CaddyPhp\Config\Apps\Cache;
use PHPUnit\Framework\TestCase;
Expand Down
42 changes: 38 additions & 4 deletions tests/Unit/LoggingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use mattvb91\CaddyPhp\Config\Logging;
use mattvb91\CaddyPhp\Config\Logs\Log;
use mattvb91\CaddyPhp\Config\Logs\LogLevel;
use mattvb91\CaddyPhp\Config\Logs\Sampling;
use PHPUnit\Framework\TestCase;

class LoggingTest extends TestCase
Expand All @@ -17,15 +18,48 @@ class LoggingTest extends TestCase
public function testAddingDefaultLog()
{
$caddy = new Caddy();
$caddy->setLogging((new Logging())
->addLog(new Log()));
$caddy->setLogging(
(new Logging())
->addLog(new Log())
);

$this->assertEquals([
'logs' => [
'default' => [
'level' => LogLevel::DEBUG,
]
]
],
],
], $caddy->toArray()['logging']);
}

/**
* @covers \mattvb91\CaddyPhp\Config\Logs\Log::__construct
* @covers \mattvb91\CaddyPhp\Config\Logs\Sampling::toArray
*/
public function testLoggingSampler()
{
$interval = random_int(0, 10);
$first = random_int(0, 10);
$thereAfter = random_int(0, 10);

$caddy = new Caddy();
$caddy->setLogging(
(new Logging())
->addLog(
new Log(
sampling: new Sampling(
$interval,
$first,
$thereAfter
)
)
)
);

$this->assertEquals([
'interval' => $interval,
'first' => $first,
'thereafter' => $thereAfter,
], $caddy->toArray()['logging']['logs']['default']['sampling']);
}
}

0 comments on commit 016ec89

Please sign in to comment.