Skip to content

Commit ec64f46

Browse files
Merge pull request #17 from verbanent/update/laravel-12
Update compatibility to Laravel 12
2 parents 5593c2e + 7078c55 commit ec64f46

File tree

9 files changed

+145
-19
lines changed

9 files changed

+145
-19
lines changed

.github/workflows/sonarcloud.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: sonarcloud
2+
on:
3+
push:
4+
branches: [ main ]
5+
pull_request:
6+
7+
jobs:
8+
sonarcloud:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5
12+
with:
13+
fetch-depth: 0
14+
- uses: shivammathur/setup-php@44454db4f0199b8b9685a5d763dc37cbf79108e1
15+
with:
16+
php-version: '8.2'
17+
coverage: xdebug
18+
- run: composer install --no-interaction --no-progress --prefer-dist
19+
- run: php -d xdebug.mode=coverage vendor/bin/phpunit --coverage-clover coverage.xml
20+
- uses: SonarSource/sonarcloud-github-action@e44258b109568baa0df60ed515909fc6c72cba92
21+
env:
22+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
23+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ coverage
44
.phpunit.result.cache
55
.idea
66
.scannerwork
7-
sonar-project.properties
87
vendor
98
composer.lock
109
.scannerwork/
1110
phpunit.*.xml
11+
coverage.xml
12+
.phpunit.cache

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
"require-dev": {
4242
"phpunit/phpunit": "^9",
43-
"laravel/framework": "^6.20.26|^7|^8|^9|^10",
43+
"laravel/framework": "^6.20.26|^7|^8|^9|^10.48.29|^11|^12",
4444
"mockery/mockery": "^1"
4545
},
4646
"type": "library"

phpunit.xml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage>
4-
<include>
5-
<directory suffix=".php">src/</directory>
6-
</include>
7-
</coverage>
8-
<testsuites>
9-
<testsuite name="Ordered binary UUID in Laravel / Eloquent Test Suite">
10-
<directory>tests</directory>
11-
</testsuite>
12-
</testsuites>
13-
<php>
14-
<env name="APP_ENV" value="testing"/>
15-
</php>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false"
3+
backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true"
4+
convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
6+
<coverage>
7+
<include>
8+
<directory suffix=".php">src/</directory>
9+
</include>
10+
</coverage>
11+
<testsuites>
12+
<testsuite name="Ordered binary UUID in Laravel / Eloquent Test Suite">
13+
<directory>tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
<php>
17+
<env name="APP_ENV" value="testing"/>
18+
<ini name="xdebug.log" value="/tmp/xdebug.log"/>
19+
<ini name="xdebug.log_level" value="0"/>
20+
</php>
1621
</phpunit>

sonar-project.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
sonar.organization=verbanent
2+
sonar.projectKey=verbanent_eloquent-binary-uuid
3+
sonar.sources=src
4+
sonar.tests=tests
5+
sonar.php.coverage.reportPaths=coverage.xml
6+
sonar.exclusions=vendor/**

src/Providers/BinaryUuidServiceProvider.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Verbanent\Uuid\Providers;
66

77
use Illuminate\Support\ServiceProvider;
8+
use ReflectionClass;
89
use Verbanent\Uuid\Grammars\MySqlGrammar;
910

1011
/**
@@ -23,10 +24,22 @@ public function boot(): void
2324

2425
private function createGrammar($connection): MySqlGrammar
2526
{
26-
$queryGrammar = $connection->getQueryGrammar();
27+
if ($this->doesGrammarRequireConnection()) {
28+
return new MySqlGrammar($connection);
29+
}
30+
31+
$prefix = $connection->getTablePrefix();
2732
$grammar = new MySqlGrammar();
28-
$grammar->setTablePrefix($queryGrammar->getTablePrefix());
33+
$grammar->setTablePrefix($prefix);
2934

3035
return $grammar;
3136
}
37+
38+
private function doesGrammarRequireConnection(): bool
39+
{
40+
$reflection = new ReflectionClass(MySqlGrammar::class);
41+
$constructor = $reflection->getConstructor();
42+
43+
return $constructor !== null && $constructor->getNumberOfRequiredParameters() > 0;
44+
}
3245
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Verbanent\Uuid\Test\Fixtures;
6+
7+
final class MySqlGrammarNoConnection
8+
{
9+
private $tablePrefix = '';
10+
11+
public function setTablePrefix(string $prefix): void
12+
{
13+
$this->tablePrefix = $prefix;
14+
}
15+
16+
public function getTablePrefix(): string
17+
{
18+
return $this->tablePrefix;
19+
}
20+
}

tests/Grammars/MySqlGrammarTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Verbanent\Uuid\Test\Grammars;
66

7+
use Illuminate\Database\Connection;
8+
use Mockery;
79
use PHPUnit\Framework\TestCase;
810
use ReflectionException;
911
use ReflectionMethod;
@@ -14,6 +16,8 @@
1416
*/
1517
class MySqlGrammarTest extends TestCase
1618
{
19+
private $connection;
20+
1721
/**
1822
* Allows to test private and protected methods.
1923
*
@@ -31,14 +35,24 @@ protected static function getMethod(string $name): ReflectionMethod
3135
return $method;
3236
}
3337

38+
public function setUp(): void
39+
{
40+
$this->connection = Mockery::mock(Connection::class);
41+
}
42+
43+
protected function tearDown(): void
44+
{
45+
Mockery::close();
46+
}
47+
3448
/**
3549
* Test for MySqlGrammar::typeUuid.
3650
*
3751
* @throws ReflectionException
3852
*/
3953
public function testTypeUuid(): void
4054
{
41-
$mySqlGrammar = new MySqlGrammar();
55+
$mySqlGrammar = new MySqlGrammar($this->connection);
4256
$typeUuid = self::getMethod('typeUuid');
4357
$uuidMySqlType = $typeUuid->invokeArgs($mySqlGrammar, [new \Illuminate\Support\Fluent()]);
4458

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Verbanent\Uuid\Test\Providers;
6+
7+
use Illuminate\Contracts\Foundation\Application;
8+
use Mockery;
9+
use PHPUnit\Framework\TestCase;
10+
use ReflectionMethod;
11+
use Verbanent\Uuid\Providers\BinaryUuidServiceProvider;
12+
use Verbanent\Uuid\Test\Fixtures\MySqlGrammarNoConnection;
13+
14+
class BinaryUuidServiceProviderLegacyGrammarTest extends TestCase
15+
{
16+
/**
17+
* @runInSeparateProcess
18+
* @preserveGlobalState disabled
19+
*/
20+
public function testCreateGrammarWithoutConnectionRequirementUsesTablePrefix(): void
21+
{
22+
// Simulate older Illuminate grammar with no required constructor params.
23+
class_alias(
24+
MySqlGrammarNoConnection::class,
25+
'Verbanent\\Uuid\\Grammars\\MySqlGrammar'
26+
);
27+
28+
$provider = new BinaryUuidServiceProvider(Mockery::mock(Application::class));
29+
$connection = new class () {
30+
public function getTablePrefix(): string
31+
{
32+
return 'legacy_';
33+
}
34+
};
35+
36+
$method = new ReflectionMethod(BinaryUuidServiceProvider::class, 'createGrammar');
37+
$method->setAccessible(true);
38+
39+
$grammar = $method->invoke($provider, $connection);
40+
41+
$this->assertInstanceOf(MySqlGrammarNoConnection::class, $grammar);
42+
$this->assertSame('legacy_', $grammar->getTablePrefix());
43+
}
44+
}

0 commit comments

Comments
 (0)