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

Add Octane compatibility #22

Merged
merged 2 commits into from
Mar 14, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ If you want to add other arbitrary comments to the SqlComment, you can use the `
```php
use Spatie\SqlCommenter\SqlCommenter;

SqlCommenter::addComment('foo', 'bar');
app(SqlCommenter::class)->addComment('foo', 'bar');

// select * from "users"/*foo='bar'*/;
```
Expand Down
10 changes: 5 additions & 5 deletions src/SqlCommenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
class SqlCommenter
{
/** @var array<Comment> */
protected static array $extraComments = [];
protected array $extraComments = [];

public static function addComment(string $key, ?string $value): void
public function addComment(string $key, ?string $value): void
{
static::$extraComments[$key] = Comment::make($key, $value);
$this->extraComments[$key] = Comment::make($key, $value);
}

public static function enable(): void
Expand Down Expand Up @@ -96,9 +96,9 @@ protected function getCommentsFromCommenters(
*/
protected function addExtraComments(Collection $comments, string $query, Connection $connection): void
{
$comments->push(...self::$extraComments);
$comments->push(...$this->extraComments);

self::$extraComments = [];
$this->extraComments = [];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/SqlCommenterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function configurePackage(Package $package): void

public function packageBooted(): void
{
$this->app->singleton(SqlCommenter::class, function () {
$this->app->scoped(SqlCommenter::class, function () {
$commenterClass = config('sql-commenter.commenter_class');

if (! is_a($commenterClass, SqlCommenter::class, true)) {
Expand Down
8 changes: 4 additions & 4 deletions tests/SqlCommenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
expect($event->sql)->toContainComment('foo', 'bar');
});

SqlCommenter::addComment('foo', 'bar');
app(SqlCommenter::class)->addComment('foo', 'bar');

dispatch(new UsersJob());
});
Expand All @@ -26,7 +26,7 @@
expect($event->sql)->not()->toContainComment('foo', 'bar');
});

SqlCommenter::addComment('foo', 'bar');
app(SqlCommenter::class)->addComment('foo', 'bar');

DB::statement(<<<mysql
select * from users; /*existing='comment'*/
Expand Down Expand Up @@ -89,8 +89,8 @@
});

it('will not include empty comments', function () {
SqlCommenter::addComment('foo', 'bar');
SqlCommenter::addComment('baz', '');
app(SqlCommenter::class)->addComment('foo', 'bar');
app(SqlCommenter::class)->addComment('baz', '');

Event::listen(QueryExecuted::class, function (QueryExecuted $event) {
expect($event->sql)->toContainComment('foo', 'bar');
Expand Down
14 changes: 12 additions & 2 deletions tests/TestSupport/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Spatie\SqlCommenter\Tests\TestSupport;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\TestCase as Orchestra;
use Spatie\SqlCommenter\SqlCommenterServiceProvider;

Expand All @@ -28,8 +30,16 @@ public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');

$migration = include __DIR__.'/../../vendor/orchestra/testbench-core/laravel/migrations/2014_10_12_000000_testbench_create_users_table.php';
$migration->up();
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}

public function addCommenterToConfig(string $commenterClass, array $options = []): self
Expand Down
Loading