Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit tests for shutdown and filesize functions #43

Merged
merged 7 commits into from
Apr 29, 2024
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
3 changes: 2 additions & 1 deletion .github/workflows/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ jobs:
- run: composer test
- run: jq 'del(.logs.html)' infection.json > infection.json.new && mv infection.json.new infection.json
- run: composer infection
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: Resources/Public/test-result/clover.xml

ter-release:
Expand Down
2 changes: 1 addition & 1 deletion Classes/Service/SentryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use function Sentry\getBaggage;
use function Sentry\getTraceparent;

final class SentryService implements SingletonInterface
final class SentryService implements SingletonInterface, SentryServiceInterface
{
private ?Transaction $transaction = null;

Expand Down
18 changes: 18 additions & 0 deletions Classes/Service/SentryServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Kanti\ServerTiming\Service;

use Kanti\ServerTiming\Dto\ScriptResult;
use Kanti\ServerTiming\Dto\StopWatch;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

interface SentryServiceInterface
{
public function addSentryTraceHeaders(RequestInterface $request, StopWatch $stopWatch): RequestInterface;

/**
* @param StopWatch[] $stopWatches
*/
public function sendSentryTrace(ScriptResult $result, array $stopWatches): ?ResponseInterface;
}
3 changes: 2 additions & 1 deletion Classes/Utility/GuzzleUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
use GuzzleHttp\Promise\PromiseInterface;
use Kanti\ServerTiming\Service\SentryService;
use Kanti\ServerTiming\Service\SentryServiceInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -36,7 +37,7 @@

$info = $request->getMethod() . ' ' . $request->getUri()->__toString();
$stop = TimingUtility::getInstance()->stopWatchInternal('http.client', $info);
$request = GeneralUtility::makeInstance(SentryService::class)->addSentryTraceHeaders($request, $stop);
$request = GeneralUtility::makeInstance(SentryServiceInterface::class)->addSentryTraceHeaders($request, $stop);

Check warning on line 40 in Classes/Utility/GuzzleUtility.php

View check run for this annotation

Codecov / codecov/patch

Classes/Utility/GuzzleUtility.php#L40

Added line #L40 was not covered by tests

$handlerPromiseCallback = static function ($responseOrException) use ($request, $stop) {
$response = null;
Expand Down
24 changes: 17 additions & 7 deletions Classes/Utility/TimingUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Kanti\ServerTiming\Dto\StopWatch;
use Kanti\ServerTiming\Service\RegisterShutdownFunction\RegisterShutdownFunction;
use Kanti\ServerTiming\Service\RegisterShutdownFunction\RegisterShutdownFunctionInterface;
use Kanti\ServerTiming\Service\SentryService;
use Kanti\ServerTiming\Service\ConfigService;
use Kanti\ServerTiming\Service\SentryServiceInterface;
use Psr\Http\Message\ResponseInterface;
use SplObjectStorage;
use TYPO3\CMS\Core\Context\Context;
Expand All @@ -27,6 +27,12 @@
/** @var bool */
public const IS_CLI = PHP_SAPI === 'cli';

/** @var bool */
public static $isTesting = false;

/** @var int */
public const BYTE_MULTIPLICATOR = 1024;

private bool $alreadyShutdown = false;

public function __construct(private readonly RegisterShutdownFunctionInterface $registerShutdownFunction, private readonly ConfigService $configService)
Expand All @@ -35,7 +41,7 @@

public static function getInstance(): TimingUtility
{
return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService());

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];

Check warning on line 44 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ } public static function getInstance() : TimingUtility { - return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance = GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ private array $order = [];
}

/** @var StopWatch[] */
Expand Down Expand Up @@ -124,7 +130,7 @@
return $result->response;
}

$this->alreadyShutdown = true;

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.1 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 11 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.2 TYPO3: 11 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 12 sentry/sdk: false

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }

Check warning on line 133 in Classes/Utility/TimingUtility.php

View workflow job for this annotation

GitHub Actions / php: 8.3 TYPO3: 12 sentry/sdk: true

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ if (!$this->shouldTrack()) { return $result->response; } - $this->alreadyShutdown = true; + $this->alreadyShutdown = false; foreach (array_reverse($this->order) as $stopWatch) { $stopWatch->stopIfNot(); }


foreach (array_reverse($this->order) as $stopWatch) {
Expand All @@ -132,7 +138,7 @@
}

$container = GeneralUtility::getContainer();
$response = $container->has(SentryService::class) ? $container->get(SentryService::class)->sendSentryTrace($result, $this->order) : $result->response;
$response = $container->has(SentryServiceInterface::class) ? $container->get(SentryServiceInterface::class)->sendSentryTrace($result, $this->order) : $result->response;

if (!$this->shouldAddHeader()) {
return $response;
Expand All @@ -148,10 +154,10 @@

rsort($durations);

$maxNumberOfTimings = $durations[$this->configService->getMaxNumberOfTimings() - 1] ?? 0;
$minimumDuration = $durations[$this->configService->getMaxNumberOfTimings() - 1] ?? PHP_INT_MIN;
foreach ($stopWatches as $index => $time) {
$duration = $time->getDuration();
if ($duration >= $maxNumberOfTimings) {
if ($duration >= $minimumDuration) {
$timings[] = $this->timingString($index, trim($time->key . ' ' . $time->info), $duration);
}
}
Expand All @@ -177,8 +183,8 @@
private function humanReadableFileSize(int $size): string
{
$fileSizeNames = [" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"];
$i = floor(log($size, 1024));
return $size ? round($size / (1024 ** $i), 2) . $fileSizeNames[$i] : '0 Bytes';
$i = floor(log($size, self::BYTE_MULTIPLICATOR));
return $size ? round($size / (self::BYTE_MULTIPLICATOR ** $i), 2) . $fileSizeNames[$i] : '0 Bytes';
}

/**
Expand Down Expand Up @@ -252,8 +258,12 @@
return sprintf('%03d;desc="%s";dur=%0.2f', $index, $description, $durationInSeconds * 1000);
}

public function shouldAddHeader(): bool
private function shouldAddHeader(): bool
{
if (self::$isTesting) {
return true;
}

if (self::IS_CLI) {
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ services:
Kanti\ServerTiming\Service\RegisterShutdownFunction\RegisterShutdownFunctionInterface:
class: Kanti\ServerTiming\Service\RegisterShutdownFunction\RegisterShutdownFunction

Kanti\ServerTiming\Service\SentryServiceInterface:
class: Kanti\ServerTiming\Service\SentryService

Kanti\ServerTiming\EventListener\ConsoleCommandEventListener:
tags:
-
Expand Down
155 changes: 153 additions & 2 deletions Tests/TimingUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@
namespace Kanti\ServerTiming\Tests;

use Generator;
use Kanti\ServerTiming\Dto\ScriptResult;
use Kanti\ServerTiming\Service\ConfigService;
use Kanti\ServerTiming\Service\RegisterShutdownFunction\RegisterShutdownFunctionNoop;
use Kanti\ServerTiming\Service\SentryService;
use Kanti\ServerTiming\Service\SentryServiceInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Kanti\ServerTiming\Dto\StopWatch;
use Kanti\ServerTiming\Utility\TimingUtility;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Reflection;
use ReflectionClass;
use Symfony\Component\DependencyInjection\Container;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Utility\GeneralUtility;

#[CoversClass(TimingUtility::class)]
#[CoversClass(StopWatch::class)]
#[CoversClass(ConfigService::class)]
#[CoversClass(ScriptResult::class)]
#[CoversClass(RegisterShutdownFunctionNoop::class)]
final class TimingUtilityTest extends TestCase
{
Expand Down Expand Up @@ -171,6 +183,138 @@ public static function dataProviderTimingString(): Generator
];
}

#[Test]
#[DataProvider('dataProviderHumanReadableFileSize')]
public function testHumanReadableFileSize(string $expected, int $size): void
{
$reflection = new ReflectionClass(TimingUtility::class);
$reflectionMethod = $reflection->getMethod('humanReadableFileSize');

$result = $reflectionMethod->invoke($this->getTestInstance(), $size);
self::assertSame($expected, $result);
}

public static function dataProviderHumanReadableFileSize(): Generator
{
yield 'simple' => [
'48.89 MB',
51268468,
];
yield 'big' => [
'1 EB',
1024 ** 6,
];
}

/**
* @param StopWatch[] $stopWatches
*/
#[Test]
#[DataProvider('dataProviderShutdown')]
public function shutdown(string $expected, array $stopWatches, ?int $numberOfTimings, ?int $lengthOfDescription): void
{
$timingUtility = $this->getTestInstance($stopWatches);

$container = new Container();
$container->set(SentryService::class, null);
GeneralUtility::setContainer($container);

if ($numberOfTimings) {
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['server_timing']['number_of_timings'] = $numberOfTimings;
}

if ($lengthOfDescription) {
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['server_timing']['length_of_description'] = $lengthOfDescription;
}

$response = $timingUtility->shutdown(ScriptResult::fromRequest(new ServerRequest(), new Response()));
$result = $response?->getHeader('Server-Timing')[0];
self::assertSame($expected, $result);
}

public static function dataProviderShutdown(): Generator
{
$stopWatch1 = new StopWatch('key1', 'info');
$stopWatch1->startTime = 100003.0003;
$stopWatch1->stopTime = 100004.0003;
$stopWatch2 = new StopWatch('key2', 'info for longer description');
$stopWatch2->startTime = 100004.0004;
$stopWatch2->stopTime = 100015.0004;
$stopWatch3 = new StopWatch(' key3', 'info');
$stopWatch3->startTime = 100004.0004;
$stopWatch3->stopTime = 100025.0004;
$stopWatch4 = new StopWatch('key4', 'short duration');
$stopWatch4->startTime = 100003.0003;
$stopWatch4->stopTime = 100004.0000;

yield 'simple' => [
'000;desc="key1 info";dur=1000.00,001;desc="key2 info for longer description";dur=11000.00,002;desc="key3 info";dur=21000.00,003;desc="key4 short duration";dur=999.70',
'stopWatches' => [$stopWatch1, $stopWatch2, $stopWatch3, $stopWatch4],
'numberOfTimings' => null,
'lengthOfDescription' => null,
];
yield 'numberOfTimings' => [
'001;desc="key2 info for longer description";dur=11000.00,002;desc="key3 info";dur=21000.00',
'stopWatches' => [$stopWatch1, $stopWatch2, $stopWatch3],
'numberOfTimings' => 2,
'lengthOfDescription' => null,
];
yield 'lengthOfDescription' => [
'000;desc="key1 info";dur=1000.00,001;desc="key2 info ";dur=11000.00,002;desc="key3 info";dur=21000.00',
'stopWatches' => [$stopWatch1, $stopWatch2, $stopWatch3],
'numberOfTimings' => null,
'lengthOfDescription' => 10,
];
yield 'unsorted_stop_watches' => [
'000;desc="key2 info for longer description";dur=11000.00,002;desc="key3 info";dur=21000.00',
'stopWatches' => [$stopWatch2, $stopWatch1, $stopWatch3],
'numberOfTimings' => 2,
'lengthOfDescription' => null,
];
yield 'less_timings' => [
'000;desc="key1 info";dur=1000.00',
'stopWatches' => [$stopWatch1],
'numberOfTimings' => 5,
'lengthOfDescription' => null,
];
}

#[Test]
public function sendSentryTrace(): void
{
$providedStopWatches = [];
for ($i = 1; $i < 100; $i++) {
$stopWatch = new StopWatch('key-' . $i, 'info');
$stopWatch->startTime = 100001.0000 + $i;
$providedStopWatches[] = $stopWatch;
}

$timingUtility = $this->getTestInstance($providedStopWatches);

$container = new Container();
$container->set(
SentryServiceInterface::class,
new class implements SentryServiceInterface {
public function addSentryTraceHeaders(RequestInterface $request, StopWatch $stopWatch): RequestInterface
{
return $request;
}

public function sendSentryTrace(ScriptResult $result, array $stopWatches): ?ResponseInterface
{
$sortedWatches = $stopWatches;
usort($sortedWatches, static fn($a, $b): int => $b->stopTime <=> $a->stopTime);
TimingUtilityTest::assertSame($sortedWatches, $stopWatches);
TimingUtilityTest::assertNotNull($stopWatches[0]->stopTime);
return new HtmlResponse('');
}
}
);
GeneralUtility::setContainer($container);

$timingUtility->shutdown(ScriptResult::fromRequest(new ServerRequest(), new Response()));
}

/**
* @param StopWatch[] $expected
* @param StopWatch[] $initalStopWatches
Expand Down Expand Up @@ -268,8 +412,15 @@ public function shouldTrack(): void
self::assertFalse($timingUtility->shouldTrack());
}

private function getTestInstance(): TimingUtility
/**
* @param StopWatch[] $stopWatches
*/
private function getTestInstance(array $stopWatches = []): TimingUtility
{
return new TimingUtility(new RegisterShutdownFunctionNoop(), new ConfigService());
$timingUtility = new TimingUtility(new RegisterShutdownFunctionNoop(), new ConfigService());
$timingUtility::$isTesting = true;
$reflection = new \ReflectionClass($timingUtility);
$reflection->getProperty('order')->setValue($timingUtility, $stopWatches);
return $timingUtility;
}
}
2 changes: 1 addition & 1 deletion infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"text": "Resources/Public/test-result/infection.log",
"html": "Resources/Public/test-result/infection.html"
},
"minCoveredMsi": 95,
"minCoveredMsi": 97,
"initialTestsPhpOptions": "-d pcov.enabled=1",
"mutators": {
"@default": true
Expand Down
Loading