This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
Added unit tests for the Metadata class #4
Closed
robertmarsal
wants to merge
2
commits into
zendframework:develop
from
robertmarsal:feature-metadata-coverage
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?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\AbstractPlatform; | ||
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 | ||
{ | ||
/** | ||
* @dataProvider createSourceFromAdapterDataProvider | ||
* | ||
* @param AbstractPlatform $platform | ||
* @param string $sourceExpectation | ||
*/ | ||
public function testTheConstructorSetsTheAdaptersAndTheSource($platform, $sourceExpectation) | ||
{ | ||
/** @var \Zend\Db\Adapter\Adapter $adapterMock */ | ||
$adapterMock = $this->getMockBuilder('Zend\Db\Adapter\Adapter') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$adapterMock->expects($this->once()) | ||
->method('getPlatform') | ||
->willReturn($platform); | ||
|
||
$metadata = new Metadata($adapterMock); | ||
|
||
$this->assertAttributeSame($adapterMock, 'adapter', $metadata); | ||
$this->assertAttributeInstanceOf($sourceExpectation, 'source', $metadata); | ||
} | ||
|
||
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']], | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could you extract all the thing about
adapterMock
in a method calledcreateAdapterMockWithPlatform($platform)
I think will improve test readiblity.