|
5 | 5 | use Bernard\Doctrine\MessagesSchema;
|
6 | 6 | use Bernard\Driver\DoctrineDriver;
|
7 | 7 |
|
| 8 | +use Doctrine\DBAL\Connection; |
8 | 9 | use Doctrine\DBAL\DriverManager;
|
| 10 | +use Doctrine\DBAL\Logging\DebugStack; |
9 | 11 | use Doctrine\DBAL\Schema\Schema;
|
10 | 12 |
|
11 | 13 | class DoctrineDriverTest extends \PHPUnit_Framework_TestCase
|
12 | 14 | {
|
| 15 | + /** |
| 16 | + * @var Connection |
| 17 | + */ |
| 18 | + private $connection; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var DoctrineDriver |
| 22 | + */ |
| 23 | + private $driver; |
| 24 | + |
13 | 25 | public function setUp()
|
14 | 26 | {
|
15 | 27 | $this->connection = $this->setUpDatabase();
|
@@ -39,6 +51,64 @@ public function testCreateAndRemoveQueue()
|
39 | 51 | $this->assertEquals(array('send-newsletter'), $this->driver->listQueues());
|
40 | 52 | }
|
41 | 53 |
|
| 54 | + public function testCreateQueueWillNotAttemptDuplicateQueueCreation() |
| 55 | + { |
| 56 | + $logger = new DebugStack(); |
| 57 | + |
| 58 | + $this->connection->getConfiguration()->setSQLLogger($logger); |
| 59 | + |
| 60 | + $this->driver->createQueue('import-users'); |
| 61 | + $this->driver->createQueue('import-users'); |
| 62 | + |
| 63 | + self::assertCount(7, $logger->queries); |
| 64 | + self::assertStringMatchesFormat('%aSTART TRANSACTION%a', $logger->queries[1]['sql']); |
| 65 | + self::assertStringStartsWith('SELECT ', $logger->queries[2]['sql']); |
| 66 | + self::assertStringStartsWith('INSERT ', $logger->queries[3]['sql']); |
| 67 | + self::assertStringMatchesFormat('%aCOMMIT%a', $logger->queries[4]['sql']); |
| 68 | + self::assertStringMatchesFormat('%aSTART TRANSACTION%a', $logger->queries[5]['sql']); |
| 69 | + self::assertStringStartsWith('SELECT ', $logger->queries[6]['sql']); |
| 70 | + self::assertStringMatchesFormat('%aCOMMIT%a', $logger->queries[7]['sql']); |
| 71 | + } |
| 72 | + |
| 73 | + public function testGenericExceptionsBubbleUpWhenThrownOnQueueCreation() |
| 74 | + { |
| 75 | + $connection = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock(); |
| 76 | + $exception = new \Exception(); |
| 77 | + $queryBuilder = $this->connection->createQueryBuilder(); |
| 78 | + |
| 79 | + $connection->expects(self::once())->method('transactional')->willReturnCallback('call_user_func'); |
| 80 | + $connection->expects(self::once())->method('insert')->willThrowException($exception); |
| 81 | + $connection->expects(self::any())->method('createQueryBuilder')->willReturn($queryBuilder); |
| 82 | + |
| 83 | + $driver = new DoctrineDriver($connection); |
| 84 | + |
| 85 | + try { |
| 86 | + $driver->createQueue('foo'); |
| 87 | + |
| 88 | + self::fail('An exception was supposed to be thrown'); |
| 89 | + } catch (\Exception $thrown) { |
| 90 | + self::assertSame($exception, $thrown); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public function testConstraintViolationExceptionsAreIgnored() |
| 95 | + { |
| 96 | + $connection = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock(); |
| 97 | + $queryBuilder = $this->connection->createQueryBuilder(); |
| 98 | + $exception = $this |
| 99 | + ->getMockBuilder('Doctrine\DBAL\Exception\ConstraintViolationException') |
| 100 | + ->disableOriginalConstructor() |
| 101 | + ->getMock(); |
| 102 | + |
| 103 | + $connection->expects(self::once())->method('transactional')->willReturnCallback('call_user_func'); |
| 104 | + $connection->expects(self::once())->method('insert')->willThrowException($exception); |
| 105 | + $connection->expects(self::any())->method('createQueryBuilder')->willReturn($queryBuilder); |
| 106 | + |
| 107 | + $driver = new DoctrineDriver($connection); |
| 108 | + |
| 109 | + $driver->createQueue('foo'); |
| 110 | + } |
| 111 | + |
42 | 112 | public function testPushMessageLazilyCreatesQueue()
|
43 | 113 | {
|
44 | 114 | $this->driver->pushMessage('send-newsletter', 'something');
|
|
0 commit comments