|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drupal\Tests\datastore\Kernel\DataDictionary\AlterTableQuery; |
| 6 | + |
| 7 | +use Drupal\Core\Database\Connection; |
| 8 | +use Drupal\Core\Logger\LoggerChannelFactory; |
| 9 | +use Drupal\Core\Logger\LoggerChannelInterface; |
| 10 | +use Drupal\datastore\DataDictionary\AlterTableQuery\MySQLQuery; |
| 11 | +use Drupal\KernelTests\KernelTestBase; |
| 12 | + |
| 13 | +/** |
| 14 | + * @coversDefaultClass \Drupal\datastore\DataDictionary\AlterTableQuery\MySQLQuery |
| 15 | + * |
| 16 | + * @group dkan |
| 17 | + * @group datastore |
| 18 | + * @group kernel |
| 19 | + */ |
| 20 | +class MySQLQueryTest extends KernelTestBase { |
| 21 | + |
| 22 | + protected static $modules = [ |
| 23 | + 'common', |
| 24 | + 'datastore', |
| 25 | + 'metastore', |
| 26 | + ]; |
| 27 | + |
| 28 | + /** |
| 29 | + * @covers ::executeFulltextAlter |
| 30 | + */ |
| 31 | + public function testExecuteFullTextAlterWithException(): void { |
| 32 | + $exception_message = 'Test exception message.'; |
| 33 | + $comment_message = 'comment message'; |
| 34 | + |
| 35 | + // Throw an exception from the connection object. |
| 36 | + $connection = $this->getMockBuilder(Connection::class) |
| 37 | + ->disableOriginalConstructor() |
| 38 | + ->onlyMethods(['prepareStatement']) |
| 39 | + ->getMockForAbstractClass(); |
| 40 | + $connection->expects($this->once()) |
| 41 | + ->method('prepareStatement') |
| 42 | + ->willThrowException(new \Exception($exception_message)); |
| 43 | + |
| 44 | + $this->container->set('database', $connection); |
| 45 | + |
| 46 | + // Set up a logger channel to expect our log message. |
| 47 | + $logger = $this->getMockBuilder(LoggerChannelInterface::class) |
| 48 | + ->onlyMethods(['error']) |
| 49 | + ->getMockForAbstractClass(); |
| 50 | + // Error() must be called once, with our special message. |
| 51 | + $logger->expects($this->once()) |
| 52 | + ->method('error') |
| 53 | + ->with('Error applying fulltext index to dataset ' . $comment_message); |
| 54 | + |
| 55 | + // Mock a logger factory to return our special mocked logger. |
| 56 | + $logger_factory = $this->getMockBuilder(LoggerChannelFactory::class) |
| 57 | + ->disableOriginalConstructor() |
| 58 | + ->onlyMethods(['get']) |
| 59 | + ->getMock(); |
| 60 | + $logger_factory->expects($this->once()) |
| 61 | + ->method('get') |
| 62 | + ->willReturn($logger); |
| 63 | + |
| 64 | + $this->container->set('logger.factory', $logger_factory); |
| 65 | + |
| 66 | + // Get a MySQLQuery object to test. |
| 67 | + $table = 'foo'; |
| 68 | + $mysql_query = new MySQLQuery( |
| 69 | + $connection, |
| 70 | + $this->container->get('pdlt.converter.strptime_to_mysql'), |
| 71 | + $table, |
| 72 | + [], |
| 73 | + [] |
| 74 | + ); |
| 75 | + |
| 76 | + // Set executeFulltextAlter() to be public and run it. |
| 77 | + $ref_full_text_alter = new \ReflectionMethod($mysql_query, 'executeFulltextAlter'); |
| 78 | + $ref_full_text_alter->setAccessible(TRUE); |
| 79 | + $ref_full_text_alter->invokeArgs($mysql_query, ['', '', '', $comment_message]); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments