Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'settermjd-hotfix/fixCallMagicCallMethods'
Browse files Browse the repository at this point in the history
Close #55
  • Loading branch information
mwillbanks committed Dec 9, 2015
2 parents d0b9e1d + 23dd383 commit 76a39f1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#55](https://github.com/zendframework/zend-db/pull/55) Implement FeatureSet
canCallMagicCall and callMagicCall methods

## 2.6.1 - 2015-10-14

Expand Down
18 changes: 16 additions & 2 deletions src/TableGateway/Feature/FeatureSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,36 @@ public function callMagicSet($property, $value)
}

/**
* Is the method requested available in one of the added features
* @param string $method
* @return bool
*/
public function canCallMagicCall($method)
{
if (!empty($this->features)) {
foreach ($this->features as $feature) {
if (method_exists($feature, $method)) {
return true;
}
}
}
return false;
}

/**
* Call method of on added feature as though it were a local method
* @param string $method
* @param array $arguments
* @return mixed
*/
public function callMagicCall($method, $arguments)
{
$return = null;
return $return;
foreach ($this->features as $feature) {
if (method_exists($feature, $method)) {
return $feature->$method($arguments);
}
}

return;
}
}
95 changes: 93 additions & 2 deletions test/TableGateway/Feature/FeatureSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

namespace ZendTest\Db\TableGateway\Feature;

use ReflectionClass;
use Zend\Db\TableGateway\Feature\FeatureSet;
use Zend\Db\TableGateway\Feature\MasterSlaveFeature;
use Zend\Db\TableGateway\Feature\SequenceFeature;
use Zend\Db\TableGateway\Feature\MetadataFeature;
use Zend\Db\Metadata\Object\ConstraintObject;

Expand All @@ -20,7 +22,7 @@ class FeatureSetTest extends \PHPUnit_Framework_TestCase
* @cover FeatureSet::addFeature
* @group ZF2-4993
*/
public function testAddFeatureThatFeatureDoesnotHasTableGatewayButFeatureSetHas()
public function testAddFeatureThatFeatureDoesNotHaveTableGatewayButFeatureSetHas()
{
$mockMasterAdapter = $this->getMock('Zend\Db\Adapter\AdapterInterface');

Expand Down Expand Up @@ -57,7 +59,7 @@ public function testAddFeatureThatFeatureDoesnotHasTableGatewayButFeatureSetHas(
* @cover FeatureSet::addFeature
* @group ZF2-4993
*/
public function testAddFeatureThatFeatureHasTableGatewayButFeatureSetDoesnotHas()
public function testAddFeatureThatFeatureHasTableGatewayButFeatureSetDoesNotHave()
{
$tableGatewayMock = $this->getMockForAbstractClass('Zend\Db\TableGateway\AbstractTableGateway');

Expand All @@ -77,4 +79,93 @@ public function testAddFeatureThatFeatureHasTableGatewayButFeatureSetDoesnotHas(
$featureSet = new FeatureSet;
$this->assertInstanceOf('Zend\Db\TableGateway\Feature\FeatureSet', $featureSet->addFeature($feature));
}

/**
* @covers Zend\Db\TableGateway\Feature\FeatureSet::canCallMagicCall
*/
public function testCanCallMagicCallReturnsTrueForAddedMethodOfAddedFeature()
{
$feature = new SequenceFeature('id', 'table_sequence');
$featureSet = new FeatureSet;
$featureSet->addFeature($feature);

$this->assertTrue(
$featureSet->canCallMagicCall('lastSequenceId'),
"Should have been able to call lastSequenceId from the Sequence Feature"
);
}

/**
* @covers Zend\Db\TableGateway\Feature\FeatureSet::canCallMagicCall
*/
public function testCanCallMagicCallReturnsFalseForAddedMethodOfAddedFeature()
{
$feature = new SequenceFeature('id', 'table_sequence');
$featureSet = new FeatureSet;
$featureSet->addFeature($feature);

$this->assertFalse(
$featureSet->canCallMagicCall('postInitialize'),
"Should have been able to call postInitialize from the MetaData Feature"
);
}

/**
* @covers Zend\Db\TableGateway\Feature\FeatureSet::canCallMagicCall
*/
public function testCanCallMagicCallReturnsFalseWhenNoFeaturesHaveBeenAdded()
{
$featureSet = new FeatureSet;
$this->assertFalse(
$featureSet->canCallMagicCall('lastSequenceId')
);
}

/**
* @covers Zend\Db\TableGateway\Feature\FeatureSet::callMagicCall
*/
public function testCallMagicCallSucceedsForValidMethodOfAddedFeature()
{
$sequenceName = 'table_sequence';

$platformMock = $this->getMock('Zend\Db\Adapter\Platform\Postgresql');
$platformMock->expects($this->any())
->method('getName')->will($this->returnValue('PostgreSQL'));

$resultMock = $this->getMock('Zend\Db\Adapter\Driver\Pgsql\Result');
$resultMock->expects($this->any())
->method('current')
->will($this->returnValue(['currval' => 1]));

$statementMock = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$statementMock->expects($this->any())
->method('prepare')
->with('SELECT CURRVAL(\'' . $sequenceName . '\')');
$statementMock->expects($this->any())
->method('execute')
->will($this->returnValue($resultMock));

$adapterMock = $this->getMockBuilder('Zend\Db\Adapter\Adapter')
->disableOriginalConstructor()
->getMock();
$adapterMock->expects($this->any())
->method('getPlatform')->will($this->returnValue($platformMock));
$adapterMock->expects($this->any())
->method('createStatement')->will($this->returnValue($statementMock));

$tableGatewayMock = $this->getMockBuilder('Zend\Db\TableGateway\AbstractTableGateway')
->disableOriginalConstructor()
->getMock();

$reflectionClass = new ReflectionClass('Zend\Db\TableGateway\AbstractTableGateway');
$reflectionProperty = $reflectionClass->getProperty('adapter');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($tableGatewayMock, $adapterMock);

$feature = new SequenceFeature('id', 'table_sequence');
$feature->setTableGateway($tableGatewayMock);
$featureSet = new FeatureSet;
$featureSet->addFeature($feature);
$this->assertEquals(1, $featureSet->callMagicCall('lastSequenceId', null));
}
}

0 comments on commit 76a39f1

Please sign in to comment.