summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/coverage.yml5
-rw-r--r--composer.json3
-rw-r--r--composer.lock140
-rw-r--r--src/Drivers/FileSystemDriver.php25
-rw-r--r--tests/FileSystemDriverTest.php129
5 files changed, 224 insertions, 78 deletions
diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml
index 379efcb..a1441fe 100644
--- a/.github/workflows/coverage.yml
+++ b/.github/workflows/coverage.yml
@@ -18,11 +18,6 @@ jobs:
- name: Set-up Composer
uses: php-actions/composer@v6
- - name: Set up logs dir
- run: |
- mkdir -p logs/
- chmod -R u+rw logs/
-
- name: Run tests and collect coverage
run: ./vendor/bin/phpunit tests --coverage-clover coverage.xml tests
diff --git a/composer.json b/composer.json
index affb543..5cc0125 100644
--- a/composer.json
+++ b/composer.json
@@ -26,6 +26,7 @@
"php": ">=8.3"
},
"require-dev": {
- "phpunit/phpunit": "11"
+ "phpunit/phpunit": "11",
+ "mockery/mockery": "^1.6"
}
}
diff --git a/composer.lock b/composer.lock
index b3438f6..230d82c 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,10 +4,144 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "a72050425367efbe00504e8bd2e37500",
+ "content-hash": "68f8adea0f9a14bea333767682748c66",
"packages": [],
"packages-dev": [
{
+ "name": "hamcrest/hamcrest-php",
+ "version": "v2.0.1",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/hamcrest/hamcrest-php.git",
+ "reference": "8c3d0a3f6af734494ad8f6fbbee0ba92422859f3"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/hamcrest/hamcrest-php/zipball/8c3d0a3f6af734494ad8f6fbbee0ba92422859f3",
+ "reference": "8c3d0a3f6af734494ad8f6fbbee0ba92422859f3",
+ "shasum": ""
+ },
+ "require": {
+ "php": "^5.3|^7.0|^8.0"
+ },
+ "replace": {
+ "cordoval/hamcrest-php": "*",
+ "davedevelopment/hamcrest-php": "*",
+ "kodova/hamcrest-php": "*"
+ },
+ "require-dev": {
+ "phpunit/php-file-iterator": "^1.4 || ^2.0",
+ "phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7.0"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "2.1-dev"
+ }
+ },
+ "autoload": {
+ "classmap": [
+ "hamcrest"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "description": "This is the PHP port of Hamcrest Matchers",
+ "keywords": [
+ "test"
+ ],
+ "support": {
+ "issues": "https://github.com/hamcrest/hamcrest-php/issues",
+ "source": "https://github.com/hamcrest/hamcrest-php/tree/v2.0.1"
+ },
+ "time": "2020-07-09T08:09:16+00:00"
+ },
+ {
+ "name": "mockery/mockery",
+ "version": "1.6.12",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/mockery/mockery.git",
+ "reference": "1f4efdd7d3beafe9807b08156dfcb176d18f1699"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/mockery/mockery/zipball/1f4efdd7d3beafe9807b08156dfcb176d18f1699",
+ "reference": "1f4efdd7d3beafe9807b08156dfcb176d18f1699",
+ "shasum": ""
+ },
+ "require": {
+ "hamcrest/hamcrest-php": "^2.0.1",
+ "lib-pcre": ">=7.0",
+ "php": ">=7.3"
+ },
+ "conflict": {
+ "phpunit/phpunit": "<8.0"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "^8.5 || ^9.6.17",
+ "symplify/easy-coding-standard": "^12.1.14"
+ },
+ "type": "library",
+ "autoload": {
+ "files": [
+ "library/helpers.php",
+ "library/Mockery.php"
+ ],
+ "psr-4": {
+ "Mockery\\": "library/Mockery"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Pádraic Brady",
+ "email": "padraic.brady@gmail.com",
+ "homepage": "https://github.com/padraic",
+ "role": "Author"
+ },
+ {
+ "name": "Dave Marshall",
+ "email": "dave.marshall@atstsolutions.co.uk",
+ "homepage": "https://davedevelopment.co.uk",
+ "role": "Developer"
+ },
+ {
+ "name": "Nathanael Esayeas",
+ "email": "nathanael.esayeas@protonmail.com",
+ "homepage": "https://github.com/ghostwriter",
+ "role": "Lead Developer"
+ }
+ ],
+ "description": "Mockery is a simple yet flexible PHP mock object framework",
+ "homepage": "https://github.com/mockery/mockery",
+ "keywords": [
+ "BDD",
+ "TDD",
+ "library",
+ "mock",
+ "mock objects",
+ "mockery",
+ "stub",
+ "test",
+ "test double",
+ "testing"
+ ],
+ "support": {
+ "docs": "https://docs.mockery.io/",
+ "issues": "https://github.com/mockery/mockery/issues",
+ "rss": "https://github.com/mockery/mockery/releases.atom",
+ "security": "https://github.com/mockery/mockery/security/advisories",
+ "source": "https://github.com/mockery/mockery"
+ },
+ "time": "2024-05-16T03:13:13+00:00"
+ },
+ {
"name": "myclabs/deep-copy",
"version": "1.12.1",
"source": {
@@ -1645,7 +1779,9 @@
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
- "platform": [],
+ "platform": {
+ "php": ">=8.3"
+ },
"platform-dev": [],
"plugin-api-version": "2.6.0"
}
diff --git a/src/Drivers/FileSystemDriver.php b/src/Drivers/FileSystemDriver.php
index a721b70..6a359aa 100644
--- a/src/Drivers/FileSystemDriver.php
+++ b/src/Drivers/FileSystemDriver.php
@@ -8,10 +8,10 @@ use DateTime;
/**
* @author Asko Nõmm <asko@faultd.com>
*/
-readonly class FileSystemDriver implements Driver
+class FileSystemDriver implements Driver
{
public function __construct(
- private string $directory,
+ private readonly string $directory,
) {}
/**
@@ -35,18 +35,33 @@ readonly class FileSystemDriver implements Driver
$path = $this->directory . DIRECTORY_SEPARATOR . $file_name;
// If the log file does not exist, try creating one
- if (!file_exists($path) && !touch($path)) {
+ if (!$this->exists($path)) {
throw new \RuntimeException('Log file could not be created at path: ' . $path);
}
// We managed to get this far, but still can't write to the log file
- if (!is_writeable($path)) {
+ if (!$this->isWriteable($path)) {
throw new \RuntimeException('Log file is not writeable at path: ' . $path);
}
+ $this->write($path, $serializedMessage . PHP_EOL);
+ }
+
+ public function exists(string $path): bool
+ {
+ return file_exists($path);
+ }
+
+ public function isWriteable(string $path): bool
+ {
+ return is_writeable($path);
+ }
+
+ public function write(string $path, string $data): void
+ {
file_put_contents(
filename: $path,
- data: $serializedMessage . PHP_EOL,
+ data: $data,
flags: FILE_APPEND
);
}
diff --git a/tests/FileSystemDriverTest.php b/tests/FileSystemDriverTest.php
index 3384c64..eb9b873 100644
--- a/tests/FileSystemDriverTest.php
+++ b/tests/FileSystemDriverTest.php
@@ -5,22 +5,21 @@ namespace Asko\Loggr\Tests;
use DateTime;
use Asko\Loggr\Drivers\FileSystemDriver;
use Asko\Loggr\Loggr;
-use PHPUnit\Framework\TestCase;
+use Mockery;
+use Mockery\Adapter\Phpunit\MockeryTestCase;
-class FileSystemDriverTest extends TestCase
+class FileSystemDriverTest extends MockeryTestCase
{
/**
* @throws \Exception
*/
public function testEmergency(): void
{
- $date = new DateTime();
- $loggr = new Loggr(new FileSystemDriver(__DIR__ . '/../logs'));
+ $data = '';
+ $loggr = $this->mockLogger($data);
$loggr->emergency('test');
- $lines = file(__DIR__ . '/../logs/' . $date->format('Y-m-d') . '.log');
- $last_line = end($lines);
- $full_date = $date->format('Y-m-d H:i:s');
- $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.EMERGENCY: test", $last_line);
+ $full_date = (new DateTime)->format('Y-m-d H:i:s');
+ $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.EMERGENCY: test", $data);
}
/**
@@ -28,13 +27,11 @@ class FileSystemDriverTest extends TestCase
*/
public function testAlert(): void
{
- $date = new DateTime();
- $loggr = new Loggr(new FileSystemDriver(__DIR__ . '/../logs'));
+ $data = '';
+ $loggr = $this->mockLogger($data);
$loggr->alert('test');
- $lines = file(__DIR__ . '/../logs/' . $date->format('Y-m-d') . '.log');
- $last_line = end($lines);
- $full_date = $date->format('Y-m-d H:i:s');
- $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.ALERT: test", $last_line);
+ $full_date = (new DateTime)->format('Y-m-d H:i:s');
+ $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.ALERT: test", $data);
}
/**
@@ -42,13 +39,11 @@ class FileSystemDriverTest extends TestCase
*/
public function testCritical(): void
{
- $date = new DateTime();
- $loggr = new Loggr(new FileSystemDriver(__DIR__ . '/../logs'));
+ $data = '';
+ $loggr = $this->mockLogger($data);
$loggr->critical('test');
- $lines = file(__DIR__ . '/../logs/' . $date->format('Y-m-d') . '.log');
- $last_line = end($lines);
- $full_date = $date->format('Y-m-d H:i:s');
- $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.CRITICAL: test", $last_line);
+ $full_date = (new DateTime)->format('Y-m-d H:i:s');
+ $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.CRITICAL: test", $data);
}
/**
@@ -56,13 +51,11 @@ class FileSystemDriverTest extends TestCase
*/
public function testError(): void
{
- $date = new DateTime();
- $loggr = new Loggr(new FileSystemDriver(__DIR__ . '/../logs'));
+ $data = '';
+ $loggr = $this->mockLogger($data);
$loggr->error('test');
- $lines = file(__DIR__ . '/../logs/' . $date->format('Y-m-d') . '.log');
- $last_line = end($lines);
- $full_date = $date->format('Y-m-d H:i:s');
- $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.ERROR: test", $last_line);
+ $full_date = (new DateTime)->format('Y-m-d H:i:s');
+ $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.ERROR: test", $data);
}
/**
@@ -70,13 +63,11 @@ class FileSystemDriverTest extends TestCase
*/
public function testWarning(): void
{
- $date = new DateTime();
- $loggr = new Loggr(new FileSystemDriver(__DIR__ . '/../logs'));
+ $data = '';
+ $loggr = $this->mockLogger($data);
$loggr->warning('test');
- $lines = file(__DIR__ . '/../logs/' . $date->format('Y-m-d') . '.log');
- $last_line = end($lines);
- $full_date = $date->format('Y-m-d H:i:s');
- $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.WARNING: test", $last_line);
+ $full_date = (new DateTime)->format('Y-m-d H:i:s');
+ $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.WARNING: test", $data);
}
/**
@@ -84,13 +75,11 @@ class FileSystemDriverTest extends TestCase
*/
public function testNotice(): void
{
- $date = new DateTime();
- $loggr = new Loggr(new FileSystemDriver(__DIR__ . '/../logs'));
+ $data = '';
+ $loggr = $this->mockLogger($data);
$loggr->notice('test');
- $lines = file(__DIR__ . '/../logs/' . $date->format('Y-m-d') . '.log');
- $last_line = end($lines);
- $full_date = $date->format('Y-m-d H:i:s');
- $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.NOTICE: test", $last_line);
+ $full_date = (new DateTime)->format('Y-m-d H:i:s');
+ $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.NOTICE: test", $data);
}
/**
@@ -98,13 +87,11 @@ class FileSystemDriverTest extends TestCase
*/
public function testInfo(): void
{
- $date = new DateTime();
- $loggr = new Loggr(new FileSystemDriver(__DIR__ . '/../logs'));
+ $data = '';
+ $loggr = $this->mockLogger($data);
$loggr->info('test');
- $lines = file(__DIR__ . '/../logs/' . $date->format('Y-m-d') . '.log');
- $last_line = end($lines);
- $full_date = $date->format('Y-m-d H:i:s');
- $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.INFO: test", $last_line);
+ $full_date = (new DateTime)->format('Y-m-d H:i:s');
+ $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.INFO: test", $data);
}
/**
@@ -112,13 +99,11 @@ class FileSystemDriverTest extends TestCase
*/
public function testDebug(): void
{
- $date = new DateTime();
- $loggr = new Loggr(new FileSystemDriver(__DIR__ . '/../logs'));
+ $data = '';
+ $loggr = $this->mockLogger($data);
$loggr->debug('test');
- $lines = file(__DIR__ . '/../logs/' . $date->format('Y-m-d') . '.log');
- $last_line = end($lines);
- $full_date = $date->format('Y-m-d H:i:s');
- $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.DEBUG: test", $last_line);
+ $full_date = (new DateTime)->format('Y-m-d H:i:s');
+ $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.DEBUG: test", $data);
}
/**
@@ -126,30 +111,44 @@ class FileSystemDriverTest extends TestCase
*/
public function testMultipleEntries(): void
{
- $date = new DateTime();
- $loggr = new Loggr(new FileSystemDriver(__DIR__ . '/../logs'));
- $loggr->emergency('test');
- $loggr->alert('test');
- $loggr->critical('test');
- $loggr->error('test');
- $loggr->warning('test');
- $loggr->notice('test');
- $loggr->info('test');
- $loggr->debug('test');
+ $data = '';
+ $loggr = $this->mockLogger($data);
+ $loggr->emergency('test1');
+ $loggr->alert('test2');
+ $loggr->critical('test3');
+ $loggr->error('test4');
+ $loggr->warning('test5');
+ $loggr->notice('test6');
+ $loggr->info('test7');
+ $loggr->debug('test8');
- $lines = file(__DIR__ . '/../logs/' . $date->format('Y-m-d') . '.log');
+ $lines = array_filter(explode("\r\n", $data), fn($line) => !empty($line));
$this->assertCount(8, $lines);
$first_line = $lines[0];
$last_line = end($lines);
- $full_date = $date->format('Y-m-d H:i:s');
+ $full_date = (new DateTime())->format('Y-m-d H:i:s');
$this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.EMERGENCY: test", $first_line);
$this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.DEBUG: test", $last_line);
}
- public function tearDown(): void
+ private function mockLogger(string &$result): Loggr
+ {
+ $driver = Mockery::mock(FileSystemDriver::class, [''])->makePartial();
+ $driver->shouldReceive('log')->passthru();
+ $driver->shouldReceive('exists')->andReturn(true);
+ $driver->shouldReceive('isWriteable')->andReturns(true);
+ $driver->shouldReceive('write')->withArgs(function ($path, $data) use (&$result) {
+ $result .= $data;
+
+ return true;
+ });
+
+ return new Loggr($driver);
+ }
+
+ protected function tearDown(): void
{
- array_map('unlink', glob(__DIR__ . '/../logs/*'));
- rmdir(__DIR__ . '/../logs');
+ Mockery::close();
}
} \ No newline at end of file