|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\SentryBundle\Tests\Tracing\Doctrine\DBAL; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Connection; |
| 8 | +use Doctrine\DBAL\Driver as DriverInterface; |
| 9 | +use Doctrine\DBAL\Driver\API\ExceptionConverter; |
| 10 | +use Doctrine\DBAL\Driver\Connection as DriverConnectionInterface; |
| 11 | +use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 12 | +use Doctrine\DBAL\Schema\AbstractSchemaManager; |
| 13 | +use Doctrine\DBAL\VersionAwarePlatformDriver as VersionAwarePlatformDriverInterface; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use Sentry\SentryBundle\Tests\DoctrineTestCase; |
| 16 | +use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverConnectionFactoryInterface; |
| 17 | +use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverConnectionInterface; |
| 18 | +use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverForV3Point2; |
| 19 | + |
| 20 | +final class TracingDriverForV3Point2Test extends DoctrineTestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var MockObject&TracingDriverConnectionFactoryInterface |
| 24 | + */ |
| 25 | + private $connectionFactory; |
| 26 | + |
| 27 | + public static function setUpBeforeClass(): void |
| 28 | + { |
| 29 | + if (!self::isDoctrineDBALVersion3Point2Installed()) { |
| 30 | + self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be >= 3.2.'); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + $this->connectionFactory = $this->createMock(TracingDriverConnectionFactoryInterface::class); |
| 37 | + } |
| 38 | + |
| 39 | + public function testConnect(): void |
| 40 | + { |
| 41 | + $params = ['host' => 'localhost']; |
| 42 | + $databasePlatform = $this->createMock(AbstractPlatform::class); |
| 43 | + $driverConnection = $this->createMock(DriverConnectionInterface::class); |
| 44 | + $tracingDriverConnection = $this->createMock(TracingDriverConnectionInterface::class); |
| 45 | + $decoratedDriver = $this->createMock(DriverInterface::class); |
| 46 | + |
| 47 | + $decoratedDriver->expects($this->once()) |
| 48 | + ->method('connect') |
| 49 | + ->with($params) |
| 50 | + ->willReturn($driverConnection); |
| 51 | + |
| 52 | + $decoratedDriver->expects($this->once()) |
| 53 | + ->method('getDatabasePlatform') |
| 54 | + ->willReturn($databasePlatform); |
| 55 | + |
| 56 | + $this->connectionFactory->expects($this->once()) |
| 57 | + ->method('create') |
| 58 | + ->with($driverConnection, $databasePlatform, $params) |
| 59 | + ->willReturn($tracingDriverConnection); |
| 60 | + |
| 61 | + $driver = new TracingDriverForV3Point2($this->connectionFactory, $decoratedDriver); |
| 62 | + |
| 63 | + $this->assertSame($tracingDriverConnection, $driver->connect($params)); |
| 64 | + } |
| 65 | + |
| 66 | + public function testGetDatabasePlatform(): void |
| 67 | + { |
| 68 | + $databasePlatform = $this->createMock(AbstractPlatform::class); |
| 69 | + |
| 70 | + $decoratedDriver = $this->createMock(DriverInterface::class); |
| 71 | + $decoratedDriver->expects($this->once()) |
| 72 | + ->method('getDatabasePlatform') |
| 73 | + ->willReturn($databasePlatform); |
| 74 | + |
| 75 | + $driver = new TracingDriverForV3Point2($this->connectionFactory, $decoratedDriver); |
| 76 | + |
| 77 | + $this->assertSame($databasePlatform, $driver->getDatabasePlatform()); |
| 78 | + } |
| 79 | + |
| 80 | + public function testGetSchemaManager(): void |
| 81 | + { |
| 82 | + $connection = $this->createMock(Connection::class); |
| 83 | + $databasePlatform = $this->createMock(AbstractPlatform::class); |
| 84 | + $schemaManager = $this->createMock(AbstractSchemaManager::class); |
| 85 | + |
| 86 | + $decoratedDriver = $this->createMock(DriverInterface::class); |
| 87 | + $decoratedDriver->expects($this->once()) |
| 88 | + ->method('getSchemaManager') |
| 89 | + ->with($connection, $databasePlatform) |
| 90 | + ->willReturn($schemaManager); |
| 91 | + |
| 92 | + $driver = new TracingDriverForV3Point2($this->connectionFactory, $decoratedDriver); |
| 93 | + |
| 94 | + $this->assertSame($schemaManager, $driver->getSchemaManager($connection, $databasePlatform)); |
| 95 | + } |
| 96 | + |
| 97 | + public function testGetExceptionConverter(): void |
| 98 | + { |
| 99 | + $exceptionConverter = $this->createMock(ExceptionConverter::class); |
| 100 | + |
| 101 | + $decoratedDriver = $this->createMock(DriverInterface::class); |
| 102 | + $decoratedDriver->expects($this->once()) |
| 103 | + ->method('getExceptionConverter') |
| 104 | + ->willReturn($exceptionConverter); |
| 105 | + |
| 106 | + $driver = new TracingDriverForV3Point2($this->connectionFactory, $decoratedDriver); |
| 107 | + |
| 108 | + $this->assertSame($exceptionConverter, $driver->getExceptionConverter()); |
| 109 | + } |
| 110 | + |
| 111 | + public function testCreateDatabasePlatformForVersion(): void |
| 112 | + { |
| 113 | + $databasePlatform = $this->createMock(AbstractPlatform::class); |
| 114 | + |
| 115 | + $decoratedDriver = $this->createMock(VersionAwarePlatformDriverInterface::class); |
| 116 | + $decoratedDriver->expects($this->once()) |
| 117 | + ->method('createDatabasePlatformForVersion') |
| 118 | + ->with('5.7') |
| 119 | + ->willReturn($databasePlatform); |
| 120 | + |
| 121 | + $driver = new TracingDriverForV3Point2($this->connectionFactory, $decoratedDriver); |
| 122 | + |
| 123 | + $this->assertSame($databasePlatform, $driver->createDatabasePlatformForVersion('5.7')); |
| 124 | + } |
| 125 | + |
| 126 | + public function testCreateDatabasePlatformForVersionWhenDriverDoesNotImplementInterface(): void |
| 127 | + { |
| 128 | + $databasePlatform = $this->createMock(AbstractPlatform::class); |
| 129 | + |
| 130 | + $decoratedDriver = $this->createMock(DriverInterface::class); |
| 131 | + $decoratedDriver->expects($this->once()) |
| 132 | + ->method('getDatabasePlatform') |
| 133 | + ->willReturn($databasePlatform); |
| 134 | + |
| 135 | + $driver = new TracingDriverForV3Point2($this->connectionFactory, $decoratedDriver); |
| 136 | + |
| 137 | + $this->assertSame($databasePlatform, $driver->createDatabasePlatformForVersion('5.7')); |
| 138 | + } |
| 139 | +} |
0 commit comments