Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ composer.lock
vendor
test/coverage
tests/unit/.phpunit.result.cache
tests/unit/.phpunit.cache/*

# Compiled source #
###################
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@
"g4/version": "0.0.*"
},
"require-dev": {
"phpunit/phpunit" : "9.*",
"squizlabs/php_codesniffer" : "3.*",
"g4/code-coverage" : "2.*"
"phpunit/phpunit" : "^11.5",
"squizlabs/php_codesniffer" : "*",
"g4/code-coverage" : "*"
},
"scripts": {
"unit-test": [
"vendor/bin/phpunit -c tests/unit/phpunit.xml --colors=always --coverage-html tests/unit/coverage"
"php8.3 vendor/bin/phpunit -c tests/unit/phpunit.xml --colors=always"
],
"test-coverage": [
"./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-text"
"XDEBUG_MODE=coverage php8.3 ./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-text --coverage-html tests/unit/coverage"
],
"test-report": [
"./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-clover=tests/unit/coverage/code-coverage.xml"
"XDEBUG_MODE=coverage php8.3 ./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-clover=tests/unit/coverage/code-coverage.xml"
],
"code-coverage": [
"./vendor/bin/code-coverage -p 90 -f tests/unit/coverage/code-coverage.xml"
"php8.3 ./vendor/bin/code-coverage -p 50 -f tests/unit/coverage/code-coverage.xml"
],
"psr2": [
"./vendor/bin/phpcs --colors --standard=PSR2 src/"
"php8.3 ./vendor/bin/phpcs --colors --standard=PSR2 src/"
],
"psr2-fix": [
"./vendor/bin/phpcbf --colors --standard=PSR2 src/"
"php8.3 ./vendor/bin/phpcbf --colors --standard=PSR2 src/"
]
}
}
37 changes: 35 additions & 2 deletions src/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Profiler
const LOG_OFF = 0;
const LOG_ERRORS_ONLY = 1;
const LOG_ALWAYS = 2;
const LOG_ABOVE_THRESHOLD = 3;

/**
* @var array
Expand Down Expand Up @@ -80,11 +81,27 @@ public function getProfilerOutput($httpCode, $dbProfiler = 0, $responseElapsedTi
: [];
}

public function getTaskerProfilerOutput($taskStatus, $executionTime = 0)
{
if (!$this->hasProfilers()) {
return null;
}
if (!$this->shouldLogTaskerProfiler($taskStatus, $executionTime)) {
return null;
}
return $this->getFormatted(2, $executionTime);
}

public function getProfilerSummary()
{
return (new ProfilerSummary($this->profilers))->getSummary();
}

/**
* @param $httpCode int HTTP Response Code
* @param $responseElapsedTime int Response time in seconds
* @return bool
*/
private function shouldLogProfiler($httpCode, $responseElapsedTime)
{
if ($this->logLevel === self::LOG_OFF) {
Expand All @@ -93,14 +110,30 @@ private function shouldLogProfiler($httpCode, $responseElapsedTime)
if ($this->logLevel === self::LOG_ALWAYS) {
return true;
}

if($this->isRequestTresholdExceded($responseElapsedTime)){
if ($this->isRequestTresholdExceded($responseElapsedTime)) {
return true;
}

return self::LOG_ERRORS_ONLY && substr($httpCode, 0, 1) != 2;
}

private function shouldLogTaskerProfiler($taskStatus, $execTime)
{
if ($this->logLevel === self::LOG_OFF) {
return false;
}
if ($this->logLevel === self::LOG_ALWAYS) {
return true;
}
if ($this->logLevel === self::LOG_ABOVE_THRESHOLD
&& $this->isRequestTresholdExceded($execTime)
) {
return true;
}

return self::LOG_ERRORS_ONLY && (int) $taskStatus != 2;
}

public function isRequestTresholdExceded($responseElapsedTime)
{
return $this->threshold && $responseElapsedTime > $this->threshold;
Expand Down
20 changes: 13 additions & 7 deletions tests/unit/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="bootstrap.php"
colors="true"
cacheDirectory=".phpunit.cache"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd">
<testsuites>
<testsuite name="Test Suite">
<directory>./src</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../../src/</directory>
<directory suffix=".php">../../src</directory>
</include>
</coverage>
<testsuite name="Test Suite">
<directory>./src</directory>
</testsuite>
</source>
</phpunit>
25 changes: 25 additions & 0 deletions tests/unit/src/Exception/InvalidModuleExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace G4\Runner\Exception;

use G4\Runner\Route\ErrorCodes;
use PHPUnit\Framework\TestCase;

class InvalidModuleExceptionTest extends TestCase
{
public function testExceptionMessage(): void
{
$moduleName = 'invalid_module';
$exception = new InvalidModuleException($moduleName);

$this->assertStringContainsString($moduleName, $exception->getMessage());
$this->assertStringContainsString('Module is not valid', $exception->getMessage());
}

public function testExceptionCode(): void
{
$exception = new InvalidModuleException('test');

$this->assertEquals(ErrorCodes::INVALID_MODULE, $exception->getCode());
}
}
25 changes: 25 additions & 0 deletions tests/unit/src/Exception/InvalidServiceExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace G4\Runner\Exception;

use G4\Runner\Route\ErrorCodes;
use PHPUnit\Framework\TestCase;

class InvalidServiceExceptionTest extends TestCase
{
public function testExceptionMessage(): void
{
$serviceName = 'invalid_service';
$exception = new InvalidServiceException($serviceName);

$this->assertStringContainsString($serviceName, $exception->getMessage());
$this->assertStringContainsString('Service is not valid', $exception->getMessage());
}

public function testExceptionCode(): void
{
$exception = new InvalidServiceException('test');

$this->assertEquals(ErrorCodes::INVALID_SERVICE, $exception->getCode());
}
}
94 changes: 94 additions & 0 deletions tests/unit/src/LoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace G4\Runner;

use G4\CleanCore\Application;
use G4\Log\Logger as LogLogger;
use PHPUnit\Framework\TestCase;

class LoggerTest extends TestCase
{
private Logger $logger;

protected function setUp(): void
{
$this->logger = new Logger();
}

public function testConstructorInitializesLogger(): void
{
$this->assertInstanceOf(Logger::class, $this->logger);
}

public function testSetLogger(): void
{
$logLoggerMock = $this->createMock(LogLogger::class);
$this->logger->setLogger($logLoggerMock);

// Test that logger is set by calling logRequest which should register shutdown function
$applicationMock = $this->createMock(Application::class);
$applicationMock->method('getRequest')->willReturn(
$this->createMock(\G4\CleanCore\Request\Request::class)
);

$this->logger->logRequest($applicationMock);

// If no exception is thrown, the logger was set correctly
$this->assertTrue(true);
}

public function testLogRequestWithoutLoggerDoesNothing(): void
{
$applicationMock = $this->createMock(Application::class);

// Should not throw any exception
$this->logger->logRequest($applicationMock);

$this->assertTrue(true);
}

public function testLogResponseWithoutLoggerDoesNothing(): void
{
$applicationMock = $this->createMock(Application::class);
$profilerMock = $this->createMock(Profiler::class);

// Should not throw any exception
$this->logger->logResponse($applicationMock, $profilerMock);

$this->assertTrue(true);
}

public function testLogRequestWithLoggerRegistersShutdownFunction(): void
{
$logLoggerMock = $this->createMock(LogLogger::class);
$this->logger->setLogger($logLoggerMock);

$applicationMock = $this->createMock(Application::class);
$requestMock = $this->createMock(\G4\CleanCore\Request\Request::class);
$applicationMock->method('getRequest')->willReturn($requestMock);

// This should register a shutdown function
$this->logger->logRequest($applicationMock);

// If no exception is thrown, the shutdown function was registered
$this->assertTrue(true);
}

public function testLogResponseWithLoggerRegistersShutdownFunction(): void
{
$logLoggerMock = $this->createMock(LogLogger::class);
$this->logger->setLogger($logLoggerMock);

$applicationMock = $this->createMock(Application::class);
$responseMock = $this->createMock(\G4\CleanCore\Response\Response::class);
$applicationMock->method('getResponse')->willReturn($responseMock);

$profilerMock = $this->createMock(Profiler::class);

// This should register a shutdown function
$this->logger->logResponse($applicationMock, $profilerMock);

// If no exception is thrown, the shutdown function was registered
$this->assertTrue(true);
}
}
4 changes: 2 additions & 2 deletions tests/unit/src/Presenter/ContentTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testInvalidFormat($format)
*
* @return array
*/
public function validDataProvider()
public static function validDataProvider()
{
return [
['gif', 'image/gif'],
Expand All @@ -128,7 +128,7 @@ public function validDataProvider()
*
* @return array
*/
public function invalidDataProvider()
public static function invalidDataProvider()
{
return [
['ppam'],
Expand Down
83 changes: 83 additions & 0 deletions tests/unit/src/Presenter/DataTransferTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace G4\Runner\Presenter;

use G4\Http\Request as HttpRequest;
use G4\Runner\Profiler;
use G4\CleanCore\Request\Request;
use G4\CleanCore\Response\Response;
use PHPUnit\Framework\TestCase;

class DataTransferTest extends TestCase
{
private DataTransfer $dataTransfer;
private HttpRequest $httpRequest;
private Profiler $profiler;
private Request $request;
private Response $response;

protected function setUp(): void
{
$this->httpRequest = $this->createMock(HttpRequest::class);
$this->profiler = $this->createMock(Profiler::class);
$this->request = $this->createMock(Request::class);
$this->response = $this->createMock(Response::class);

$this->dataTransfer = new DataTransfer(
$this->httpRequest,
$this->profiler,
$this->request,
$this->response,
'1.0.0'
);
}

public function testGetHttpRequest(): void
{
$result = $this->dataTransfer->getHttpRequest();

$this->assertSame($this->httpRequest, $result);
}

public function testGetProfiler(): void
{
$result = $this->dataTransfer->getProfiler();

$this->assertSame($this->profiler, $result);
}

public function testGetRequest(): void
{
$result = $this->dataTransfer->getRequest();

$this->assertSame($this->request, $result);
}

public function testGetResponse(): void
{
$result = $this->dataTransfer->getResponse();

$this->assertSame($this->response, $result);
}

public function testGetVersion(): void
{
$result = $this->dataTransfer->getVersion();

$this->assertEquals('1.0.0', $result);
}

public function testGetVersionWithNullVersion(): void
{
$dataTransfer = new DataTransfer(
$this->httpRequest,
$this->profiler,
$this->request,
$this->response
);

$result = $dataTransfer->getVersion();

$this->assertNull($result);
}
}
Loading