Skip to content

Commit 4fffa77

Browse files
authored
Merge pull request #21 from ppavlovic/master
Synchronization with release/0.x branch
2 parents 37daef2 + bbc89fd commit 4fffa77

17 files changed

Lines changed: 949 additions & 29 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ composer.lock
1818
vendor
1919
test/coverage
2020
tests/unit/.phpunit.result.cache
21+
tests/unit/.phpunit.cache/*
2122

2223
# Compiled source #
2324
###################

composer.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,28 @@
3636
"g4/version": "0.0.*"
3737
},
3838
"require-dev": {
39-
"phpunit/phpunit" : "9.*",
40-
"squizlabs/php_codesniffer" : "3.*",
41-
"g4/code-coverage" : "2.*"
39+
"phpunit/phpunit" : "^11.5",
40+
"squizlabs/php_codesniffer" : "*",
41+
"g4/code-coverage" : "*"
4242
},
4343
"scripts": {
4444
"unit-test": [
45-
"vendor/bin/phpunit -c tests/unit/phpunit.xml --colors=always --coverage-html tests/unit/coverage"
45+
"php8.3 vendor/bin/phpunit -c tests/unit/phpunit.xml --colors=always"
4646
],
4747
"test-coverage": [
48-
"./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-text"
48+
"XDEBUG_MODE=coverage php8.3 ./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-text --coverage-html tests/unit/coverage"
4949
],
5050
"test-report": [
51-
"./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-clover=tests/unit/coverage/code-coverage.xml"
51+
"XDEBUG_MODE=coverage php8.3 ./vendor/bin/phpunit --colors=always -c tests/unit/phpunit.xml --coverage-clover=tests/unit/coverage/code-coverage.xml"
5252
],
5353
"code-coverage": [
54-
"./vendor/bin/code-coverage -p 90 -f tests/unit/coverage/code-coverage.xml"
54+
"php8.3 ./vendor/bin/code-coverage -p 50 -f tests/unit/coverage/code-coverage.xml"
5555
],
5656
"psr2": [
57-
"./vendor/bin/phpcs --colors --standard=PSR2 src/"
57+
"php8.3 ./vendor/bin/phpcs --colors --standard=PSR2 src/"
5858
],
5959
"psr2-fix": [
60-
"./vendor/bin/phpcbf --colors --standard=PSR2 src/"
60+
"php8.3 ./vendor/bin/phpcbf --colors --standard=PSR2 src/"
6161
]
6262
}
6363
}

src/Profiler.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Profiler
99
const LOG_OFF = 0;
1010
const LOG_ERRORS_ONLY = 1;
1111
const LOG_ALWAYS = 2;
12+
const LOG_ABOVE_THRESHOLD = 3;
1213

1314
/**
1415
* @var array
@@ -80,11 +81,27 @@ public function getProfilerOutput($httpCode, $dbProfiler = 0, $responseElapsedTi
8081
: [];
8182
}
8283

84+
public function getTaskerProfilerOutput($taskStatus, $executionTime = 0)
85+
{
86+
if (!$this->hasProfilers()) {
87+
return null;
88+
}
89+
if (!$this->shouldLogTaskerProfiler($taskStatus, $executionTime)) {
90+
return null;
91+
}
92+
return $this->getFormatted(2, $executionTime);
93+
}
94+
8395
public function getProfilerSummary()
8496
{
8597
return (new ProfilerSummary($this->profilers))->getSummary();
8698
}
8799

100+
/**
101+
* @param $httpCode int HTTP Response Code
102+
* @param $responseElapsedTime int Response time in seconds
103+
* @return bool
104+
*/
88105
private function shouldLogProfiler($httpCode, $responseElapsedTime)
89106
{
90107
if ($this->logLevel === self::LOG_OFF) {
@@ -93,14 +110,30 @@ private function shouldLogProfiler($httpCode, $responseElapsedTime)
93110
if ($this->logLevel === self::LOG_ALWAYS) {
94111
return true;
95112
}
96-
97-
if($this->isRequestTresholdExceded($responseElapsedTime)){
113+
if ($this->isRequestTresholdExceded($responseElapsedTime)) {
98114
return true;
99115
}
100116

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

120+
private function shouldLogTaskerProfiler($taskStatus, $execTime)
121+
{
122+
if ($this->logLevel === self::LOG_OFF) {
123+
return false;
124+
}
125+
if ($this->logLevel === self::LOG_ALWAYS) {
126+
return true;
127+
}
128+
if ($this->logLevel === self::LOG_ABOVE_THRESHOLD
129+
&& $this->isRequestTresholdExceded($execTime)
130+
) {
131+
return true;
132+
}
133+
134+
return self::LOG_ERRORS_ONLY && (int) $taskStatus != 2;
135+
}
136+
104137
public function isRequestTresholdExceded($responseElapsedTime)
105138
{
106139
return $this->threshold && $responseElapsedTime > $this->threshold;

tests/unit/phpunit.xml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<?xml version="1.0"?>
2-
<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">
3-
<coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
bootstrap="bootstrap.php"
4+
colors="true"
5+
cacheDirectory=".phpunit.cache"
6+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd">
7+
<testsuites>
8+
<testsuite name="Test Suite">
9+
<directory>./src</directory>
10+
</testsuite>
11+
</testsuites>
12+
<source>
413
<include>
5-
<directory suffix=".php">../../src/</directory>
14+
<directory suffix=".php">../../src</directory>
615
</include>
7-
</coverage>
8-
<testsuite name="Test Suite">
9-
<directory>./src</directory>
10-
</testsuite>
16+
</source>
1117
</phpunit>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace G4\Runner\Exception;
4+
5+
use G4\Runner\Route\ErrorCodes;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class InvalidModuleExceptionTest extends TestCase
9+
{
10+
public function testExceptionMessage(): void
11+
{
12+
$moduleName = 'invalid_module';
13+
$exception = new InvalidModuleException($moduleName);
14+
15+
$this->assertStringContainsString($moduleName, $exception->getMessage());
16+
$this->assertStringContainsString('Module is not valid', $exception->getMessage());
17+
}
18+
19+
public function testExceptionCode(): void
20+
{
21+
$exception = new InvalidModuleException('test');
22+
23+
$this->assertEquals(ErrorCodes::INVALID_MODULE, $exception->getCode());
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace G4\Runner\Exception;
4+
5+
use G4\Runner\Route\ErrorCodes;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class InvalidServiceExceptionTest extends TestCase
9+
{
10+
public function testExceptionMessage(): void
11+
{
12+
$serviceName = 'invalid_service';
13+
$exception = new InvalidServiceException($serviceName);
14+
15+
$this->assertStringContainsString($serviceName, $exception->getMessage());
16+
$this->assertStringContainsString('Service is not valid', $exception->getMessage());
17+
}
18+
19+
public function testExceptionCode(): void
20+
{
21+
$exception = new InvalidServiceException('test');
22+
23+
$this->assertEquals(ErrorCodes::INVALID_SERVICE, $exception->getCode());
24+
}
25+
}

tests/unit/src/LoggerTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace G4\Runner;
4+
5+
use G4\CleanCore\Application;
6+
use G4\Log\Logger as LogLogger;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class LoggerTest extends TestCase
10+
{
11+
private Logger $logger;
12+
13+
protected function setUp(): void
14+
{
15+
$this->logger = new Logger();
16+
}
17+
18+
public function testConstructorInitializesLogger(): void
19+
{
20+
$this->assertInstanceOf(Logger::class, $this->logger);
21+
}
22+
23+
public function testSetLogger(): void
24+
{
25+
$logLoggerMock = $this->createMock(LogLogger::class);
26+
$this->logger->setLogger($logLoggerMock);
27+
28+
// Test that logger is set by calling logRequest which should register shutdown function
29+
$applicationMock = $this->createMock(Application::class);
30+
$applicationMock->method('getRequest')->willReturn(
31+
$this->createMock(\G4\CleanCore\Request\Request::class)
32+
);
33+
34+
$this->logger->logRequest($applicationMock);
35+
36+
// If no exception is thrown, the logger was set correctly
37+
$this->assertTrue(true);
38+
}
39+
40+
public function testLogRequestWithoutLoggerDoesNothing(): void
41+
{
42+
$applicationMock = $this->createMock(Application::class);
43+
44+
// Should not throw any exception
45+
$this->logger->logRequest($applicationMock);
46+
47+
$this->assertTrue(true);
48+
}
49+
50+
public function testLogResponseWithoutLoggerDoesNothing(): void
51+
{
52+
$applicationMock = $this->createMock(Application::class);
53+
$profilerMock = $this->createMock(Profiler::class);
54+
55+
// Should not throw any exception
56+
$this->logger->logResponse($applicationMock, $profilerMock);
57+
58+
$this->assertTrue(true);
59+
}
60+
61+
public function testLogRequestWithLoggerRegistersShutdownFunction(): void
62+
{
63+
$logLoggerMock = $this->createMock(LogLogger::class);
64+
$this->logger->setLogger($logLoggerMock);
65+
66+
$applicationMock = $this->createMock(Application::class);
67+
$requestMock = $this->createMock(\G4\CleanCore\Request\Request::class);
68+
$applicationMock->method('getRequest')->willReturn($requestMock);
69+
70+
// This should register a shutdown function
71+
$this->logger->logRequest($applicationMock);
72+
73+
// If no exception is thrown, the shutdown function was registered
74+
$this->assertTrue(true);
75+
}
76+
77+
public function testLogResponseWithLoggerRegistersShutdownFunction(): void
78+
{
79+
$logLoggerMock = $this->createMock(LogLogger::class);
80+
$this->logger->setLogger($logLoggerMock);
81+
82+
$applicationMock = $this->createMock(Application::class);
83+
$responseMock = $this->createMock(\G4\CleanCore\Response\Response::class);
84+
$applicationMock->method('getResponse')->willReturn($responseMock);
85+
86+
$profilerMock = $this->createMock(Profiler::class);
87+
88+
// This should register a shutdown function
89+
$this->logger->logResponse($applicationMock, $profilerMock);
90+
91+
// If no exception is thrown, the shutdown function was registered
92+
$this->assertTrue(true);
93+
}
94+
}

tests/unit/src/Presenter/ContentTypeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function testInvalidFormat($format)
114114
*
115115
* @return array
116116
*/
117-
public function validDataProvider()
117+
public static function validDataProvider()
118118
{
119119
return [
120120
['gif', 'image/gif'],
@@ -128,7 +128,7 @@ public function validDataProvider()
128128
*
129129
* @return array
130130
*/
131-
public function invalidDataProvider()
131+
public static function invalidDataProvider()
132132
{
133133
return [
134134
['ppam'],
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace G4\Runner\Presenter;
4+
5+
use G4\Http\Request as HttpRequest;
6+
use G4\Runner\Profiler;
7+
use G4\CleanCore\Request\Request;
8+
use G4\CleanCore\Response\Response;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class DataTransferTest extends TestCase
12+
{
13+
private DataTransfer $dataTransfer;
14+
private HttpRequest $httpRequest;
15+
private Profiler $profiler;
16+
private Request $request;
17+
private Response $response;
18+
19+
protected function setUp(): void
20+
{
21+
$this->httpRequest = $this->createMock(HttpRequest::class);
22+
$this->profiler = $this->createMock(Profiler::class);
23+
$this->request = $this->createMock(Request::class);
24+
$this->response = $this->createMock(Response::class);
25+
26+
$this->dataTransfer = new DataTransfer(
27+
$this->httpRequest,
28+
$this->profiler,
29+
$this->request,
30+
$this->response,
31+
'1.0.0'
32+
);
33+
}
34+
35+
public function testGetHttpRequest(): void
36+
{
37+
$result = $this->dataTransfer->getHttpRequest();
38+
39+
$this->assertSame($this->httpRequest, $result);
40+
}
41+
42+
public function testGetProfiler(): void
43+
{
44+
$result = $this->dataTransfer->getProfiler();
45+
46+
$this->assertSame($this->profiler, $result);
47+
}
48+
49+
public function testGetRequest(): void
50+
{
51+
$result = $this->dataTransfer->getRequest();
52+
53+
$this->assertSame($this->request, $result);
54+
}
55+
56+
public function testGetResponse(): void
57+
{
58+
$result = $this->dataTransfer->getResponse();
59+
60+
$this->assertSame($this->response, $result);
61+
}
62+
63+
public function testGetVersion(): void
64+
{
65+
$result = $this->dataTransfer->getVersion();
66+
67+
$this->assertEquals('1.0.0', $result);
68+
}
69+
70+
public function testGetVersionWithNullVersion(): void
71+
{
72+
$dataTransfer = new DataTransfer(
73+
$this->httpRequest,
74+
$this->profiler,
75+
$this->request,
76+
$this->response
77+
);
78+
79+
$result = $dataTransfer->getVersion();
80+
81+
$this->assertNull($result);
82+
}
83+
}

0 commit comments

Comments
 (0)