1
1
mirror of https://github.com/pterodactyl/panel.git synced 2024-11-22 17:12:30 +01:00
Pterodactyl-Panel/tests/Traits/MocksPdoConnection.php
Matthew Penner cbcf62086f
Upgrade to Laravel 9 (#4413)
Co-authored-by: DaneEveritt <dane@daneeveritt.com>
2022-10-14 10:59:20 -06:00

49 lines
1.2 KiB
PHP

<?php
namespace Pterodactyl\Tests\Traits;
use PDO;
use Mockery;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\ConnectionResolver;
use Illuminate\Database\ConnectionResolverInterface;
trait MocksPdoConnection
{
private static ?ConnectionResolverInterface $initialResolver;
/**
* Generates a mock PDO connection and injects it into the models so that any actual
* DB call can be properly intercepted.
*/
protected function mockPdoConnection(): Mockery\MockInterface
{
self::$initialResolver = Model::getConnectionResolver();
Model::unsetConnectionResolver();
$connection = new MySqlConnection($mock = Mockery::mock(PDO::class), 'testing_mock');
$resolver = new ConnectionResolver(['mocked' => $connection]);
$resolver->setDefaultConnection('mocked');
Model::setConnectionResolver($resolver);
return $mock;
}
/**
* Resets the mock state.
*/
protected function tearDownPdoMock(): void
{
if (!self::$initialResolver) {
return;
}
Model::setConnectionResolver(self::$initialResolver);
self::$initialResolver = null;
}
}