Skip to content

Commit

Permalink
[2.x] Adds Laravel 10 support (#146)
Browse files Browse the repository at this point in the history
* Adds Laravel 10 support

* Adds support for monolog `^3.2`

* Improves tests around monolog

* Don't use PHPUnit 10

* Update composer.json

Co-authored-by: Joe Dixon <joedixon@users.noreply.github.com>

* Tests Octane on Laravel 10

Co-authored-by: Joe Dixon <joedixon@users.noreply.github.com>
  • Loading branch information
nunomaduro and joedixon authored Jan 10, 2023
1 parent 323cd74 commit 170a1f4
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 26 deletions.
20 changes: 16 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,38 @@ on:

jobs:
tests:
runs-on: ubuntu-22.04

runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php: [7.2, 7.3, 7.4, 8.0, 8.1]
laravel: [^6.0, ^7.0, ^8.0, ^9.0]
php: [7.2, 7.3, 7.4, 8.0, 8.1, 8.2]
laravel: [^6.0, ^7.0, ^8.0, ^9.0, ^10.0]
exclude:
- php: 7.2
laravel: ^8.0
- php: 7.2
laravel: ^9.0
- php: 7.2
laravel: ^10.0
- php: 7.3
laravel: ^9.0
- php: 7.3
laravel: ^10.0
- php: 7.4
laravel: ^9.0
- php: 7.4
laravel: ^10.0
- php: 8.0
laravel: ^10.0
- php: 8.1
laravel: ^6.0
- php: 8.1
laravel: ^7.0
- php: 8.2
laravel: ^6.0
- php: 8.2
laravel: ^7.0

name: P${{ matrix.php }} - L${{ matrix.laravel }}

Expand Down Expand Up @@ -56,7 +68,7 @@ jobs:
run: |
composer require laravel/octane --dev
if: |
matrix.php >= 8.0 && (matrix.laravel == '^8.0' || matrix.laravel == '^9.0')
matrix.php >= 8.0 && (matrix.laravel == '^8.0' || matrix.laravel == '^9.0' || matrix.laravel == '^10.0')
- name: Execute unit tests
run: vendor/bin/phpunit
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
"aws/aws-sdk-php": "^3.80",
"guzzlehttp/guzzle": "^6.3|^7.0",
"hollodotme/fast-cgi-client": "^3.0",
"illuminate/container": "^6.0|^7.0|^8.0|^9.0",
"illuminate/http": "^6.0|^7.0|^8.0|^9.0",
"illuminate/queue": "^6.0|^7.0|^8.0|^9.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0",
"monolog/monolog": "^1.12|^2.0",
"illuminate/container": "^6.0|^7.0|^8.0|^9.0|^10.0",
"illuminate/http": "^6.0|^7.0|^8.0|^9.0|^10.0",
"illuminate/queue": "^6.0|^7.0|^8.0|^9.0|^10.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0",
"monolog/monolog": "^1.12|^2.0|^3.2",
"nyholm/psr7": "^1.0",
"riverline/multipart-parser": "^2.0.9",
"symfony/process": "^4.3|^5.0|^6.0",
"symfony/psr-http-message-bridge": "^1.0|^2.0"
},
"require-dev": {
"mockery/mockery": "^1.2",
"orchestra/testbench": "^4.0|^5.0|^6.0|^7.0",
"orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0",
"phpunit/phpunit": "^8.0|^9.0"
},
"autoload": {
Expand Down
13 changes: 1 addition & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
Expand All @@ -18,18 +19,6 @@
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php>
<env name="APP_VANITY_URL" value="foo.vapor.build"/>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
Expand Down
23 changes: 19 additions & 4 deletions src/Logging/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@
namespace Laravel\Vapor\Logging;

use Monolog\Formatter\JsonFormatter as BaseJsonFormatter;
use Monolog\LogRecord;

class JsonFormatter extends BaseJsonFormatter
{
/**
* {@inheritdoc}
*/
public function format(array $record): string
public function format($record): string
{
$record['context'] = array_merge(
$record['context'] ?? [], ['aws_request_id' => ($_ENV['AWS_REQUEST_ID'] ?? null)]
);
$context = ['aws_request_id' => ($_ENV['AWS_REQUEST_ID'] ?? null)];

if ($record instanceof LogRecord) {
$record = new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
array_merge($record->context, $context),
$record->extra,
$record->formatted
);
} else {
$record['context'] = array_merge(
$record['context'] ?? [], $context
);
}

return parent::format($record);
}
Expand Down
147 changes: 147 additions & 0 deletions tests/Unit/Logging/JsonFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Laravel\Vapor\Tests\Unit\Logging;

use Laravel\Vapor\Logging\JsonFormatter;
use Mockery;
use Monolog\Level;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;

class JsonFormatterTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();

unset($_ENV['AWS_REQUEST_ID']);
}

public function test_format()
{
$formatter = new JsonFormatter();

if (class_exists(LogRecord::class)) {
$record = new LogRecord(
new \DateTimeImmutable(),
'channel-test',
Level::Info,
'message-test',
['context-test' => 'context-value'],
['extra-test' => 'extra-value']
);
} else {
$record = [
'datetime' => new \DateTimeImmutable(),
'channel' => 'channel-test',
'level' => 200,
'level_name' => 'INFO',
'message' => 'message-test',
'context' => ['context-test' => 'context-value'],
'extra' => ['extra-test' => 'extra-value'],
];
}

$_ENV['AWS_REQUEST_ID'] = '123456789';

$record = $formatter->format($record);
$this->assertJson($record);

$record = json_decode($record, true);

if (class_exists(LogRecord::class)) {
$this->assertSame([
'message' => 'message-test',
'context' => [
'context-test' => 'context-value',
'aws_request_id' => '123456789',
],
'level' => 200,
'level_name' => 'INFO',
'channel' => 'channel-test',
'datetime' => $record['datetime'],
'extra' => [
'extra-test' => 'extra-value',
],
], $record);
} else {
$this->assertSame([
'datetime' => $record['datetime'],
'channel' => 'channel-test',
'level' => 200,
'level_name' => 'INFO',
'message' => 'message-test',
'context' => [
'context-test' => 'context-value',
'aws_request_id' => '123456789',
],
'extra' => [
'extra-test' => 'extra-value',
],
], $record);
}
}

public function test_includes_aws_request_id()
{
$formatter = new JsonFormatter();

$_ENV['AWS_REQUEST_ID'] = '123456789';

if (class_exists(LogRecord::class)) {
$record = new LogRecord(
new \DateTimeImmutable(),
'channel-test',
Level::Info,
'message-test',
['foo' => 'bar']
);
} else {
$record = [
'datetime' => new \DateTimeImmutable(),
'channel' => 'channel-test',
'level' => 200,
'level_name' => 'INFO',
'message' => 'message-test',
'context' => ['foo' => 'bar'],
'extra' => [],
];
}

$record = $formatter->format($record);
$this->assertJson($record);

$record = json_decode($record, true);
$this->assertSame(['foo' => 'bar', 'aws_request_id' => '123456789'], $record['context']);
}

public function test_aws_request_id_may_be_null()
{
$formatter = new JsonFormatter();

if (class_exists(LogRecord::class)) {
$record = new LogRecord(
new \DateTimeImmutable(),
'channel-test',
Level::Info,
'message-test'
);
} else {
$record = [
'datetime' => new \DateTimeImmutable(),
'channel' => 'channel-test',
'level' => 200,
'level_name' => 'INFO',
'message' => 'message-test',
'context' => [],
'extra' => [],
];
}

$record = $formatter->format($record);
$this->assertJson($record);

$record = json_decode($record, true);
$this->assertSame(['aws_request_id' => null], $record['context']);
}
}

0 comments on commit 170a1f4

Please sign in to comment.