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

Test Improvements #48545

Closed
wants to merge 2 commits into from
Closed
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"league/flysystem-read-only": "^3.3",
"league/flysystem-sftp-v3": "^3.0",
"mockery/mockery": "^1.5.1",
"orchestra/testbench-core": "^8.10",
"orchestra/testbench-core": "^8.12",
"pda/pheanstalk": "^4.0",
"phpstan/phpstan": "^1.4.7",
"phpunit/phpunit": "^10.0.7",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Tests\Integration\Database;

class EloquentTransactionWithAfterCommitTest extends DatabaseTestCase
{
use EloquentTransactionWithAfterCommitTests;
}
104 changes: 104 additions & 0 deletions tests/Integration/Database/EloquentTransactionWithAfterCommitTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\DB;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
use Orchestra\Testbench\Factories\UserFactory;

trait EloquentTransactionWithAfterCommitTests
{
use WithLaravelMigrations;

protected function setUpEloquentTransactionWithAfterCommitTests(): void
{
User::unguard();
}

protected function tearDownEloquentTransactionWithAfterCommitTests(): void
{
User::reguard();
}

public function testObserverIsCalledOnTestsWithAfterCommit()
{
User::observe($observer = EloquentTransactionWithAfterCommitTestsUserObserver::resetting());

$user1 = User::create(UserFactory::new()->raw());

$this->assertTrue($user1->exists);
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
}

public function testObserverCalledWithAfterCommitWhenInsideTransaction()
{
User::observe($observer = EloquentTransactionWithAfterCommitTestsUserObserver::resetting());

$user1 = DB::transaction(fn () => User::create(UserFactory::new()->raw()));

$this->assertTrue($user1->exists);
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
}

public function testObserverIsCalledOnTestsWithAfterCommitWhenUsingSavepoint()
{
User::observe($observer = EloquentTransactionWithAfterCommitTestsUserObserver::resetting());

$user1 = User::createOrFirst(UserFactory::new()->raw());

$this->assertTrue($user1->exists);
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
}

public function testObserverIsCalledOnTestsWithAfterCommitWhenUsingSavepointAndInsideTransaction()
{
User::observe($observer = EloquentTransactionWithAfterCommitTestsUserObserver::resetting());

$user1 = DB::transaction(fn () => User::createOrFirst(UserFactory::new()->raw()));

$this->assertTrue($user1->exists);
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
}

public function testObserverIsCalledEvenWhenDeeplyNestingTransactions()
{
User::observe($observer = EloquentTransactionWithAfterCommitTestsUserObserver::resetting());

$user1 = DB::transaction(function () use ($observer) {
return tap(DB::transaction(function () use ($observer) {
return tap(DB::transaction(function () use ($observer) {
return tap(User::createOrFirst(UserFactory::new()->raw()), function () use ($observer) {
$this->assertEquals(0, $observer::$calledTimes, 'Should not have been called');
});
}), function () use ($observer) {
$this->assertEquals(0, $observer::$calledTimes, 'Should not have been called');
});
}), function () use ($observer) {
$this->assertEquals(0, $observer::$calledTimes, 'Should not have been called');
});
});

$this->assertTrue($user1->exists);
$this->assertEquals(1, $observer::$calledTimes, 'Failed to assert the observer was called once.');
}
}

class EloquentTransactionWithAfterCommitTestsUserObserver
{
public static $calledTimes = 0;

public $afterCommit = true;

public static function resetting()
{
static::$calledTimes = 0;

return new static();
}

public function created($user)
{
static::$calledTimes++;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Foundation\Testing\DatabaseMigrations;

class EloquentTransactionWithAfterCommitUsingDatabaseMigrationsTest extends DatabaseTestCase
{
use EloquentTransactionWithAfterCommitTests;
use DatabaseMigrations;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Orchestra\Testbench\TestCase;

class EloquentTransactionWithAfterCommitUsingDatabaseTransactionsTest extends TestCase
{
use EloquentTransactionWithAfterCommitTests;
use DatabaseTransactions;

/**
* The current database driver.
*
* @return string
*/
protected $driver;

protected function setUp(): void
{
$this->beforeApplicationDestroyed(function () {
foreach (array_keys($this->app['db']->getConnections()) as $name) {
$this->app['db']->purge($name);
}
});

parent::setUp();

if ($this->usesSqliteInMemoryDatabaseConnection()) {
$this->markTestSkipped('Test cannot be used with in-memory SQLite connection.');
}
}

protected function getEnvironmentSetUp($app)
{
$connection = $app->make('config')->get('database.default');

$this->driver = $app['config']->get("database.connections.$connection.driver");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\TestCase;

class EloquentTransactionWithAfterCommitUsingRefreshDatabaseTest extends TestCase
{
use EloquentTransactionWithAfterCommitTests;
use RefreshDatabase;

/**
* The current database driver.
*
* @return string
*/
protected $driver;

protected function setUp(): void
{
$this->beforeApplicationDestroyed(function () {
foreach (array_keys($this->app['db']->getConnections()) as $name) {
$this->app['db']->purge($name);
}
});

parent::setUp();
}

protected function getEnvironmentSetUp($app)
{
$connection = $app['config']->get('database.default');

$this->driver = $app['config']->get("database.connections.$connection.driver");
}
}
Loading