Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergiu committed Sep 9, 2024
1 parent 7d7bcac commit d2ef1ef
Show file tree
Hide file tree
Showing 30 changed files with 1,495 additions and 6 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
],
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"laminas/laminas-filter": "^2.37",
"laminas/laminas-servicemanager": "^3.22",
"laminas/laminas-validator": "^2.64"
},
"require-dev": {
"phpunit/phpunit": "^10.2",
"laminas/laminas-coding-standard": "^2.5",
"vimeo/psalm": "^5.13"
"vimeo/psalm": "^5.13",
"ext-curl": "*"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 4 additions & 0 deletions log/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
5 changes: 2 additions & 3 deletions src/Filter/Priority.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Dot\Log\Exception\InvalidArgumentException;
use Traversable;

use function ctype_digit;
use function gettype;
use function is_array;
use function is_int;
Expand All @@ -34,14 +33,14 @@ public function __construct(iterable|int $priority, ?string $operator = null)
$operator = $priority['operator'] ?? null;
$priority = $priority['priority'] ?? null;
}
if (! is_int($priority) && ! ctype_digit($priority)) {
if (! is_int($priority)) {
throw new InvalidArgumentException(sprintf(
'Priority must be a number, received "%s"',
gettype($priority)
));
}

$this->priority = (int) $priority;
$this->priority = $priority;
$this->operator = $operator ?? '<=';
}

Expand Down
6 changes: 6 additions & 0 deletions src/Manager/FilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Psr\Container\ContainerInterface;

use function gettype;
use function is_object;
Expand Down Expand Up @@ -50,6 +51,11 @@ class FilterPluginManager extends AbstractPluginManager
*/
protected $sharedByDefault = false;

public function __construct(ContainerInterface $container, array $config = [])
{
parent::__construct($container, $config);
}

/**
* Validate the plugin is of the expected type.
*
Expand Down
6 changes: 6 additions & 0 deletions src/Manager/FormatterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Psr\Container\ContainerInterface;

use function gettype;
use function is_object;
Expand Down Expand Up @@ -40,6 +41,11 @@ class FormatterPluginManager extends AbstractPluginManager
*/
protected $sharedByDefault = false;

public function __construct(ContainerInterface $container, array $config = [])
{
parent::__construct($container, $config);
}

/**
* Validate the plugin is of the expected type.
*
Expand Down
6 changes: 6 additions & 0 deletions src/Manager/ProcessorPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Psr\Container\ContainerInterface;

use function gettype;
use function is_object;
Expand Down Expand Up @@ -49,6 +50,11 @@ class ProcessorPluginManager extends AbstractPluginManager
*/
protected $sharedByDefault = false;

public function __construct(ContainerInterface $container, array $config = [])
{
parent::__construct($container, $config);
}

/**
* Validate the plugin is of the expected type.
*
Expand Down
6 changes: 6 additions & 0 deletions src/Manager/WriterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Dot\Log\Writer\WriterInterface;
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Psr\Container\ContainerInterface;

use function gettype;
use function is_object;
Expand Down Expand Up @@ -48,6 +49,11 @@ class WriterPluginManager extends AbstractPluginManager
*/
protected $sharedByDefault = false;

public function __construct(ContainerInterface $container, array $config = [])
{
parent::__construct($container, $config);
}

/**
* Validate the plugin is of the expected type.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Processor/RequestId.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class RequestId implements ProcessorInterface
{
protected string $identifier;
protected ?string $identifier = null;

/**
* Adds an identifier for the request to the log, unless one has already been set.
Expand Down
2 changes: 1 addition & 1 deletion src/Writer/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct(

if (! is_string($streamOrUrl) && ! is_resource($streamOrUrl)) {
throw new InvalidArgumentException(sprintf(
'Resource is not a stream nor a string; received "%s',
'Resource is not a stream nor a string; received "%s"',
gettype($streamOrUrl)
));
}
Expand Down
47 changes: 47 additions & 0 deletions test/Factory/WriterFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace DotTest\Log\Factory;

use Dot\Log\Factory\WriterFactory;
use Dot\Log\Writer\AbstractWriter;
use Dot\Log\Writer\Stream;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

class WriterFactoryTest extends TestCase
{
private ContainerInterface|MockObject $container;

private WriterFactory $subject;

/**
* @throws Exception
*/
protected function setUp(): void
{
$this->container = $this->createMock(ContainerInterface::class);
$this->subject = new WriterFactory();
}

public function testWillInstantiate(): void
{
$factory = (new WriterFactory())(
$this->container,
Stream::class,
['stream' => __DIR__ . '/../../log/dk.log']
);

$this->assertInstanceOf(AbstractWriter::class, $factory);
}

public function testSetCreationOptions(): void
{
$input = [];

$this->assertNull($this->subject->setCreationOptions($input));
}
}
60 changes: 60 additions & 0 deletions test/Filter/PriorityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace DotTest\Log\Filter;

use Dot\Log\Exception\InvalidArgumentException;
use Dot\Log\Filter\Priority;
use PHPUnit\Framework\TestCase;

class PriorityTest extends TestCase
{
private Priority $subject;

public function setUp(): void
{
$this->subject = new Priority(47);
}

public function testWillInstantiateWithInt(): void
{
$this->assertInstanceOf(Priority::class, $this->subject);
}

public function testWillInstantiateWithArray(): void
{
$input = ['priority' => 47];

$result = new Priority($input);

$this->assertInstanceOf(Priority::class, $result);
}

public function testWillNotInstantiateWithEmptyArray(): void
{
$input = [];

$this->expectExceptionMessage('Priority must be a number, received "NULL"');
$this->expectException(InvalidArgumentException::class);
new Priority($input);
}

public function testFilterWillAcceptMessage(): void
{
$input = ['priority' => 47];

$result = $this->subject->filter($input);

$this->assertTrue($result);
}

public function testFilterWillNotAcceptMessage(): void
{
$input = ['priority' => 244];

$result = $this->subject->filter($input);

$this->assertFalse($result);
}
}
58 changes: 58 additions & 0 deletions test/Filter/RegexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace DotTest\Log\Filter;

use Dot\Log\Filter\Regex;
use ErrorException;
use PHPUnit\Framework\TestCase;
use TypeError;

class RegexTest extends TestCase
{
private Regex $subject;

/**
* @throws ErrorException
*/
public function setUp(): void
{
$this->subject = new Regex(['regex' => '/(a)(b)*(c)/']);
}

public function testWillInstantiate(): void
{
$this->assertInstanceOf(Regex::class, $this->subject);
}

/**
* @throws ErrorException
*/
public function testWillNotInstantiateWithEmptyArray(): void
{
$this->expectExceptionMessage(
'preg_match(): Argument #1 ($pattern) must be of type string, null given'
);
$this->expectException(TypeError::class);
new Regex([]);
}

public function testFilterWillAcceptMessage(): void
{
$input = ['message' => 'ac'];

$result = $this->subject->filter($input);

$this->assertTrue($result);
}

public function testFilterWillNotAcceptMessage(): void
{
$input = ['message' => 'What a wonderful day to write tests'];

$result = $this->subject->filter($input);

$this->assertFalse($result);
}
}
45 changes: 45 additions & 0 deletions test/Filter/SuppressFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace DotTest\Log\Filter;

use Dot\Log\Exception\InvalidArgumentException;
use Dot\Log\Filter\SuppressFilter;
use PHPUnit\Framework\TestCase;

class SuppressFilterTest extends TestCase
{
private SuppressFilter $subject;

public function setUp(): void
{
$this->subject = new SuppressFilter();
}

public function testWillInstantiate(): void
{
$this->assertInstanceOf(SuppressFilter::class, $this->subject);
}

public function testWillNotInstantiate(): void
{
$this->expectExceptionMessage(
'Suppress must be a boolean; received "string"'
);
$this->expectException(InvalidArgumentException::class);
new SuppressFilter(['suppress' => 'oups']);
}

public function testFilter(): void
{
$result = $this->subject->filter([]);

$this->assertIsBool($result);
}

public function testSuppress(): void
{
$this->assertNull($this->subject->suppress(true));
}
}
Loading

0 comments on commit d2ef1ef

Please sign in to comment.