Skip to content

Commit

Permalink
Merge pull request #11 from mamitech/add-laravel-10-support
Browse files Browse the repository at this point in the history
add laravel 10 support, handle changes for PHPUnit 10.3
  • Loading branch information
mwnnn authored Oct 17, 2023
2 parents 9eeb541 + 3b9ce8d commit e5a779a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 50 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.1]
laravel: [9.*]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: 7.*
- laravel: 10.*
testbench: 8.*
carbon: ^2.63

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
20 changes: 9 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@
],
"require": {
"php": "^8.1",
"illuminate/contracts": "^9.0",
"scoutapp/scout-apm-laravel": "^2.2",
"spatie/laravel-package-tools": "^1.14.0"
"illuminate/contracts": "^10",
"scoutapp/scout-apm-laravel": "^2.2"
},
"require-dev": {
"laravel/pint": "^1.0",
"laravel/pint": "^1",
"mockery/mockery": "^1.5",
"nunomaduro/collision": "^6.0",
"orchestra/testbench": "^7.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"pestphp/pest-plugin-mock": "^1.0",
"php-mock/php-mock-phpunit": "^2.6",
"phpunit/phpunit": "^9.5"
"nunomaduro/collision": "^7",
"orchestra/testbench": "^8",
"pestphp/pest": "^2",
"pestphp/pest-plugin-laravel": "^2",
"php-mock/php-mock-mockery": "^1.4",
"php-mock/php-mock-phpunit": "^2.6"
},
"autoload": {
"psr-4": {
Expand Down
41 changes: 15 additions & 26 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" backupGlobals="false"
bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false"
executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<testsuites>
<testsuite name="Mamitech Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage" />
<text outputFile="build/coverage.txt" />
<clover outputFile="build/logs/clover.xml" />
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
<junit outputFile="build/report.junit.xml" />
</logging>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
9 changes: 7 additions & 2 deletions src/Middleware/SampleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use Illuminate\Http\Request;
use Mamitech\ScoutApmLaravelExtended\PHPHelper;
use Scoutapm\Logger\FilteredLogLevelDecorator;
use Scoutapm\ScoutApmAgent;
use Throwable;
Expand All @@ -18,10 +19,14 @@ final class SampleRequest
/** @var FilteredLogLevelDecorator */
private $logger;

public function __construct(ScoutApmAgent $agent, FilteredLogLevelDecorator $logger)
/** @var PHPHelper */
private $phpHelper;

public function __construct(ScoutApmAgent $agent, FilteredLogLevelDecorator $logger, PHPHelper $phpHelper)
{
$this->agent = $agent;
$this->logger = $logger;
$this->phpHelper = $phpHelper;
}

/**
Expand All @@ -33,7 +38,7 @@ public function handle(Request $request, Closure $next)
{
try {
$samplingPer = (int) config('scout-apm-laravel-extended.sampling_per', 10);
if (rand(1, $samplingPer) === 1) {
if ($this->phpHelper->rand(1, $samplingPer) === 1) {
$this->agent->connect();
} else {
$this->agent->ignore();
Expand Down
11 changes: 11 additions & 0 deletions src/PHPHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Mamitech\ScoutApmLaravelExtended;

class PHPHelper
{
public function rand(int $min, int $max): int
{
return rand($min, $max);
}
}
5 changes: 3 additions & 2 deletions tests/Middleware/AddCustomContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Mamitech\ScoutApmLaravelExtended\Middleware\AddCustomContext;
use Mamitech\ScoutApmLaravelExtended\PHPHelper;
use Psr\Log\LoggerInterface;
use Scoutapm\Logger\FilteredLogLevelDecorator;
use Scoutapm\ScoutApmAgent;
Expand All @@ -27,7 +28,7 @@
);

$expectedResponse = new Response();
$addCustomContextMiddleware = new AddCustomContext($mockAgent, $filteredLogLevel);
$addCustomContextMiddleware = new AddCustomContext($mockAgent, $filteredLogLevel, new PHPHelper());
$response = $addCustomContextMiddleware->handle(
$request,
static function () use ($expectedResponse) {
Expand Down Expand Up @@ -57,7 +58,7 @@ static function () use ($expectedResponse) {
);

$expectedResponse = new Response();
$addCustomContextMiddleware = new AddCustomContext($mockAgent, $filteredLogLevel);
$addCustomContextMiddleware = new AddCustomContext($mockAgent, $filteredLogLevel, new PHPHelper());
$response = $addCustomContextMiddleware->handle(
$request,
static function () use ($expectedResponse) {
Expand Down
12 changes: 6 additions & 6 deletions tests/Middleware/SampleRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Mamitech\ScoutApmLaravelExtended\Middleware\SampleRequest;
use Mamitech\ScoutApmLaravelExtended\PHPHelper;
use Psr\Log\LoggerInterface;
use Scoutapm\Logger\FilteredLogLevelDecorator;
use Scoutapm\ScoutApmAgent;

uses(\phpmock\phpunit\PHPMock::class);

it('samples request when random hits', function () {
$rand = $this->getFunctionMock("Mamitech\ScoutApmLaravelExtended\Middleware", 'rand');
$rand->expects($this->once())->willReturn(1);
config(['scout-apm-laravel-extended.sampling_per' => 1]);
$mockAgent = Mockery::mock(ScoutApmAgent::class);
$mockAgent->shouldReceive('connect');
$mockLogger = Mockery::mock(LoggerInterface::class);
Expand All @@ -22,7 +22,7 @@
);

$expectedResponse = new Response();
$sampleRequestMiddleware = new SampleRequest($mockAgent, $filteredLogLevel);
$sampleRequestMiddleware = new SampleRequest($mockAgent, $filteredLogLevel, new PHPHelper());
$response = $sampleRequestMiddleware->handle(
new Request(),
static function () use ($expectedResponse) {
Expand All @@ -34,8 +34,8 @@ static function () use ($expectedResponse) {
});

it('doesn\'t sample request when random doesn\'t hit', function () {
$rand = $this->getFunctionMock("Mamitech\ScoutApmLaravelExtended\Middleware", 'rand');
$rand->expects($this->once())->willReturn(6);
$mockPHPHelper = Mockery::mock(PHPHelper::class);
$mockPHPHelper->shouldReceive('rand')->andReturn(6);
$mockAgent = Mockery::mock(ScoutApmAgent::class);
$mockAgent->shouldNotReceive('connect');
$mockLogger = Mockery::mock(LoggerInterface::class);
Expand All @@ -46,7 +46,7 @@ static function () use ($expectedResponse) {
);

$expectedResponse = new Response();
$sampleRequestMiddleware = new SampleRequest($mockAgent, $filteredLogLevel);
$sampleRequestMiddleware = new SampleRequest($mockAgent, $filteredLogLevel, $mockPHPHelper);
$response = $sampleRequestMiddleware->handle(
new Request(),
static function () use ($expectedResponse) {
Expand Down

0 comments on commit e5a779a

Please sign in to comment.