|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Framework\DB\Test\Unit\Adapter\Pdo; |
| 7 | + |
| 8 | +use Magento\Framework\ObjectManagerInterface; |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 10 | +use Magento\Framework\DB\LoggerInterface; |
| 11 | +use Magento\Framework\DB\SelectFactory; |
| 12 | +use Magento\Framework\DB\Adapter\Pdo\MysqlFactory; |
| 13 | +use Magento\Framework\DB\Adapter\Pdo\Mysql; |
| 14 | + |
| 15 | +class MysqlFactoryTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var SelectFactory|\PHPUnit_Framework_MockObject_MockObject |
| 19 | + */ |
| 20 | + private $selectFactoryMock; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 24 | + */ |
| 25 | + private $loggerMock; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + private $objectManagerMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var MysqlFactory |
| 34 | + */ |
| 35 | + private $mysqlFactory; |
| 36 | + |
| 37 | + protected function setUp() |
| 38 | + { |
| 39 | + $objectManager = new ObjectManager($this); |
| 40 | + $this->objectManagerMock = $this->getMock(ObjectManagerInterface::class); |
| 41 | + $this->mysqlFactory = $objectManager->getObject( |
| 42 | + MysqlFactory::class, |
| 43 | + [ |
| 44 | + 'objectManager' => $this->objectManagerMock |
| 45 | + ] |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param array $config |
| 51 | + * @param LoggerInterface|null $logger |
| 52 | + * @param SelectFactory|null $selectFactory |
| 53 | + * @param array $arguments |
| 54 | + * @dataProvider createDataProvider |
| 55 | + */ |
| 56 | + public function testCreate( |
| 57 | + array $config, |
| 58 | + LoggerInterface $logger = null, |
| 59 | + SelectFactory $selectFactory = null, |
| 60 | + array $arguments |
| 61 | + ) { |
| 62 | + $this->objectManagerMock->expects($this->once()) |
| 63 | + ->method('create') |
| 64 | + ->with( |
| 65 | + Mysql::class, |
| 66 | + $arguments |
| 67 | + ); |
| 68 | + $this->mysqlFactory->create( |
| 69 | + Mysql::class, |
| 70 | + $config, |
| 71 | + $logger, |
| 72 | + $selectFactory |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @return array |
| 78 | + */ |
| 79 | + public function createDataProvider() |
| 80 | + { |
| 81 | + $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); |
| 82 | + $this->selectFactoryMock = $this->getMock(SelectFactory::class, [], [], '', false); |
| 83 | + return [ |
| 84 | + [ |
| 85 | + ['foo' => 'bar'], |
| 86 | + $this->loggerMock, |
| 87 | + $this->selectFactoryMock, |
| 88 | + [ |
| 89 | + 'config' => ['foo' => 'bar'], |
| 90 | + 'logger' => $this->loggerMock, |
| 91 | + 'selectFactory' => $this->selectFactoryMock |
| 92 | + ] |
| 93 | + ], |
| 94 | + [ |
| 95 | + ['foo' => 'bar'], |
| 96 | + $this->loggerMock, |
| 97 | + null, |
| 98 | + [ |
| 99 | + 'config' => ['foo' => 'bar'], |
| 100 | + 'logger' => $this->loggerMock |
| 101 | + ] |
| 102 | + ], |
| 103 | + [ |
| 104 | + ['foo' => 'bar'], |
| 105 | + null, |
| 106 | + $this->selectFactoryMock, |
| 107 | + [ |
| 108 | + 'config' => ['foo' => 'bar'], |
| 109 | + 'selectFactory' => $this->selectFactoryMock |
| 110 | + ] |
| 111 | + ], |
| 112 | + ]; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @expectedException \InvalidArgumentException |
| 117 | + * @expectedExceptionMessage Invalid class, stdClass must extend Magento\Framework\DB\Adapter\Pdo\Mysql. |
| 118 | + */ |
| 119 | + public function testCreateInvalidClass() |
| 120 | + { |
| 121 | + $this->mysqlFactory->create( |
| 122 | + \stdClass::class, |
| 123 | + [] |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
0 commit comments