Added unit tests for the Metadata class #4
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Db\Metadata; | ||
|
||
use Zend\Db\Adapter\Platform\Mysql as MysqlPlatform; | ||
use Zend\Db\Adapter\Platform\SqlServer as SqlServerPlatform; | ||
use Zend\Db\Adapter\Platform\Sqlite as SqlitePlatform; | ||
use Zend\Db\Adapter\Platform\Postgresql as PostgresqlPlatform; | ||
use Zend\Db\Adapter\Platform\Oracle as OraclePlatform; | ||
use Zend\Db\Metadata\Metadata; | ||
|
||
class MetadataTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testTheConstructorSetsTheAdaptersAndTheSource() | ||
{ | ||
$platformMock = new MysqlPlatform(); | ||
|
||
/** @var \Zend\Db\Adapter\Adapter $adapterMock */ | ||
$adapterMock = $this->getMockBuilder('Zend\Db\Adapter\Adapter') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$adapterMock->expects($this->once()) | ||
->method('getPlatform') | ||
->willReturn($platformMock); | ||
|
||
$metadata = new Metadata($adapterMock); | ||
|
||
$adapterReflectionProperty = new \ReflectionProperty($metadata, 'adapter'); | ||
$adapterReflectionProperty->setAccessible(true); | ||
$adapterPropertyValue = $adapterReflectionProperty->getValue($metadata); | ||
|
||
$sourceReflectionProperty = new \ReflectionProperty($metadata, 'source'); | ||
$sourceReflectionProperty->setAccessible(true); | ||
$sourcePropertyValue = $sourceReflectionProperty->getValue($metadata); | ||
|
||
$this->assertSame($adapterMock, $adapterPropertyValue); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Place this assertion after adapterProperyValue assignment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better if use |
||
$this->assertInstanceOf( | ||
'Zend\Db\Metadata\Source\MysqlMetadata', | ||
$sourcePropertyValue | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider createSourceFromAdapterDataProvider | ||
* | ||
* @param $platform | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add param types or remove the tag |
||
* @param $sourceExpectation | ||
*/ | ||
public function testCreateSourceFromAdapterCreatesPlatformSpecificSource($platform, $sourceExpectation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is redundant with First one could use the data provider |
||
{ | ||
/** @var \Zend\Db\Adapter\Adapter $adapterMock */ | ||
$adapterMock = $this->getMockBuilder('Zend\Db\Adapter\Adapter') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$adapterMock->expects($this->once()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you extract all the thing about I think will improve test readiblity. |
||
->method('getPlatform') | ||
->willReturn($platform); | ||
|
||
$metadata = new Metadata($adapterMock); | ||
|
||
$sourceReflectionProperty = new \ReflectionProperty($metadata, 'source'); | ||
$sourceReflectionProperty->setAccessible(true); | ||
$sourcePropertyValue = $sourceReflectionProperty->getValue($metadata); | ||
|
||
$this->assertInstanceOf($sourceExpectation, $sourcePropertyValue); | ||
} | ||
|
||
public static function createSourceFromAdapterDataProvider() | ||
{ | ||
return [ | ||
[new MysqlPlatform(), 'Zend\Db\Metadata\Source\MysqlMetadata'], | ||
[new SqlServerPlatform(), 'Zend\Db\Metadata\Source\SqlServerMetadata'], | ||
[new SqlitePlatform(), 'Zend\Db\Metadata\Source\SqliteMetadata'], | ||
[new PostgresqlPlatform(), 'Zend\Db\Metadata\Source\PostgresqlMetadata'], | ||
[new OraclePlatform(), 'Zend\Db\Metadata\Source\OracleMetadata'], | ||
]; | ||
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage cannot create source from adapter | ||
*/ | ||
public function testCreateSourceFromAdapterThrowsExceptionIfPlatformIsUnknown() | ||
{ | ||
$platformMock = $this->getMockForAbstractClass('Zend\Db\Adapter\Platform\AbstractPlatform'); | ||
|
||
/** @var \Zend\Db\Adapter\Adapter $adapterMock */ | ||
$adapterMock = $this->getMockBuilder('Zend\Db\Adapter\Adapter') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$platformMock->expects($this->once()) | ||
->method('getName') | ||
->willReturn('ThisPlatformDoesNotExist'); | ||
|
||
$adapterMock->expects($this->once()) | ||
->method('getPlatform') | ||
->willReturn($platformMock); | ||
|
||
new Metadata($adapterMock); | ||
} | ||
|
||
/** | ||
* @param $method | ||
* @param $proxyMethod | ||
* @param $args | ||
* | ||
* @dataProvider gettersDataProvider | ||
*/ | ||
public function testGettersProxyToSourceMethods($method, $proxyMethod, $args) | ||
{ | ||
$returnValue = rand(); | ||
|
||
$platformMock = new MysqlPlatform(); | ||
|
||
/** @var \Zend\Db\Adapter\Adapter $adapterMock */ | ||
$adapterMock = $this->getMockBuilder('Zend\Db\Adapter\Adapter') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$adapterMock->expects($this->once()) | ||
->method('getPlatform') | ||
->willReturn($platformMock); | ||
|
||
$metadata = new Metadata($adapterMock); | ||
|
||
$sourceMock = $this->getMockForAbstractClass( | ||
'Zend\Db\Metadata\Source\AbstractSource', | ||
[], | ||
'', | ||
false, | ||
true, | ||
true, | ||
[$proxyMethod] | ||
); | ||
|
||
$sourceMock->expects($this->once()) | ||
->method($proxyMethod) | ||
->willReturn($returnValue); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you test arguments received too? |
||
|
||
$metadataReflection = new \ReflectionClass('Zend\Db\Metadata\Metadata'); | ||
$sourceReflectionProperty = $metadataReflection->getProperty('source'); | ||
$sourceReflectionProperty->setAccessible(true); | ||
$sourceReflectionProperty->setValue($metadata, $sourceMock); | ||
|
||
$this->assertEquals( | ||
$returnValue, | ||
call_user_func_array([$metadata, $method], $args) | ||
); | ||
} | ||
|
||
public function gettersDataProvider() | ||
{ | ||
return [ | ||
['getTables', 'getTables', ['test', false]], | ||
['getViews', 'getViews', ['test']], | ||
['getTriggers', 'getTriggers', ['test']], | ||
['getConstraints', 'getConstraints', [ 'a', 'b']], | ||
['getColumns', 'getColumns', ['a', 'b']], | ||
['getConstraintKeys', 'getConstraintKeys', ['a', 'b', 'c']], | ||
['getConstraint', 'getConstraint', ['a', 'b', 'c']], | ||
['getSchemas', 'getSchemas', []], | ||
['getTableNames', 'getTableNames', ['a', 'b']], | ||
['getTable', 'getTable', ['a', 'b']], | ||
['getViewNames', 'getViewNames', ['a']], | ||
['getView', 'getView', ['a', 'b']], | ||
['getTriggerNames', 'getTriggerNames', ['a']], | ||
['getTrigger', 'getTrigger', ['a', 'b']], | ||
['getColumnNames', 'getColumnNames', ['a', 'b']], | ||
['getColumn', 'getColumn', ['a', 'b', 'c']], | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add description when this exception is thrown