2020-10-09 05:34:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
namespace Pterodactyl\Tests\Traits;
|
2020-10-09 05:34:52 +00:00
|
|
|
|
|
|
|
use PDO;
|
|
|
|
use Mockery;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\MySqlConnection;
|
|
|
|
use Illuminate\Database\ConnectionResolver;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Database\ConnectionResolverInterface;
|
2020-10-09 05:34:52 +00:00
|
|
|
|
|
|
|
trait MocksPdoConnection
|
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
private static ?ConnectionResolverInterface $initialResolver;
|
2020-10-09 05:34:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a mock PDO connection and injects it into the models so that any actual
|
|
|
|
* DB call can be properly intercepted.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function mockPdoConnection(): Mockery\MockInterface
|
2020-10-09 05:34:52 +00:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function tearDownPdoMock(): void
|
2020-10-09 05:34:52 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!self::$initialResolver) {
|
2020-10-09 05:34:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Model::setConnectionResolver(self::$initialResolver);
|
|
|
|
|
|
|
|
self::$initialResolver = null;
|
|
|
|
}
|
|
|
|
}
|