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

restore missing use file #338

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/book/table-gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ This allows for a wider array of possible mixing and matching of features to
achieve a particular behavior that needs to be attained to make the base
implementation of `TableGateway` useful for a particular problem.

With the `TableGateway` object, features should be injected though the
With the `TableGateway` object, features should be injected through the
constructor. The constructor can take features in 3 different forms:

- as a single `Feature` instance
Expand Down
1 change: 1 addition & 0 deletions src/TableGateway/AbstractTableGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Db\TableGateway;

use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\ResultSet\ResultSetInterface;
use Zend\Db\Sql\Delete;
use Zend\Db\Sql\Insert;
Expand Down
8 changes: 4 additions & 4 deletions src/TableGateway/Feature/MetadataFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public function postInitialize()
throw new Exception\RuntimeException('A primary key for this column could not be found in the metadata.');
}

$pkck = $pkck->getColumns();
if (count($pkc) === 1) {
$primaryKey = $pkck[0];
$pkcColumns = $pkc->getColumns();
if (count($pkcColumns) === 1) {
$primaryKey = $pkcColumns[0];
} else {
$primaryKey = $pkc;
$primaryKey = $pkcColumns;
}

$this->sharedData['metadata']['primaryKey'] = $primaryKey;
Expand Down
29 changes: 29 additions & 0 deletions test/unit/TableGateway/AbstractTableGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ protected function setUp()
->getMock()
));

$this->mockFeatureSet = $this->getMockBuilder('Zend\Db\TableGateway\Feature\FeatureSet')->getMock();

$this->table = $this->getMockForAbstractClass(
'Zend\Db\TableGateway\AbstractTableGateway'
//array('getTable')
Expand All @@ -104,6 +106,9 @@ protected function setUp()
case 'sql':
$tgPropReflection->setValue($this->table, $this->mockSql);
break;
case 'featureSet':
$tgPropReflection->setValue($this->table, $this->mockFeatureSet);
break;
}
}
}
Expand Down Expand Up @@ -369,6 +374,30 @@ public function testGetLastInsertValue()
self::assertEquals(10, $this->table->getLastInsertValue());
}

public function testInitializeBuildsAResultSet()
{
$stub = $this->getMockForAbstractClass(AbstractTableGateway::class);

$tgReflection = new \ReflectionClass('Zend\Db\TableGateway\AbstractTableGateway');
foreach ($tgReflection->getProperties() as $tgPropReflection) {
$tgPropReflection->setAccessible(true);
switch ($tgPropReflection->getName()) {
case 'table':
$tgPropReflection->setValue($stub, 'foo');
break;
case 'adapter':
$tgPropReflection->setValue($stub, $this->mockAdapter);
break;
case 'featureSet':
$tgPropReflection->setValue($stub, $this->mockFeatureSet);
break;
}
}

$stub->initialize();
$this->assertInstanceOf(ResultSet::class, $stub->getResultSetPrototype());
}

/**
* @covers \Zend\Db\TableGateway\AbstractTableGateway::__get
*/
Expand Down
87 changes: 86 additions & 1 deletion test/unit/TableGateway/Feature/MetadataFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
namespace ZendTest\Db\TableGateway\Feature;

use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use Zend\Db\Metadata\MetadataInterface;
use Zend\Db\Metadata\Object\ConstraintObject;
use Zend\Db\Metadata\Object\TableObject;
use Zend\Db\Metadata\Object\ViewObject;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\TableGateway\Feature\MetadataFeature;

class MetadataFeatureTest extends TestCase
Expand All @@ -21,7 +26,6 @@ class MetadataFeatureTest extends TestCase
public function testPostInitialize()
{
$tableGatewayMock = $this->getMockForAbstractClass('Zend\Db\TableGateway\AbstractTableGateway');

$metadataMock = $this->getMockBuilder('Zend\Db\Metadata\MetadataInterface')->getMock();
$metadataMock->expects($this->any())->method('getColumnNames')->will($this->returnValue(['id', 'name']));

Expand All @@ -37,4 +41,85 @@ public function testPostInitialize()

self::assertEquals(['id', 'name'], $tableGatewayMock->getColumns());
}

public function testPostInitializeRecordsPrimaryKeyColumnToSharedMetadata()
{
/** @var AbstractTableGateway $tableGatewayMock */
$tableGatewayMock = $this->getMockForAbstractClass(AbstractTableGateway::class);
$metadataMock = $this->getMockBuilder(MetadataInterface::class)->getMock();
$metadataMock->expects($this->any())->method('getColumnNames')->will($this->returnValue(['id', 'name']));
$metadataMock->expects($this->any())
->method('getTable')
->will($this->returnValue(new TableObject('foo')));


$constraintObject = new ConstraintObject('id_pk', 'table');
$constraintObject->setColumns(['id']);
$constraintObject->setType('PRIMARY KEY');

$metadataMock->expects($this->any())->method('getConstraints')->will($this->returnValue([$constraintObject]));

$feature = new MetadataFeature($metadataMock);
$feature->setTableGateway($tableGatewayMock);
$feature->postInitialize();

$r = new ReflectionProperty(MetadataFeature::class, 'sharedData');
$r->setAccessible(true);
$sharedData = $r->getValue($feature);

self::assertTrue(
isset($sharedData['metadata']['primaryKey']),
'Shared data must have metadata entry for primary key'
);
self::assertSame($sharedData['metadata']['primaryKey'], 'id');
}

public function testPostInitializeRecordsListOfColumnsInPrimaryKeyToSharedMetadata()
{
/** @var AbstractTableGateway $tableGatewayMock */
$tableGatewayMock = $this->getMockForAbstractClass(AbstractTableGateway::class);
$metadataMock = $this->getMockBuilder(MetadataInterface::class)->getMock();
$metadataMock->expects($this->any())->method('getColumnNames')->will($this->returnValue(['id', 'name']));
$metadataMock->expects($this->any())
->method('getTable')
->will($this->returnValue(new TableObject('foo')));


$constraintObject = new ConstraintObject('id_pk', 'table');
$constraintObject->setColumns(['composite', 'id']);
$constraintObject->setType('PRIMARY KEY');

$metadataMock->expects($this->any())->method('getConstraints')->will($this->returnValue([$constraintObject]));

$feature = new MetadataFeature($metadataMock);
$feature->setTableGateway($tableGatewayMock);
$feature->postInitialize();

$r = new ReflectionProperty(MetadataFeature::class, 'sharedData');
$r->setAccessible(true);
$sharedData = $r->getValue($feature);

self::assertTrue(
isset($sharedData['metadata']['primaryKey']),
'Shared data must have metadata entry for primary key'
);
self::assertEquals($sharedData['metadata']['primaryKey'], ['composite', 'id']);
}

public function testPostInitializeSkipsPrimaryKeyCheckIfNotTable()
{
/** @var AbstractTableGateway $tableGatewayMock */
$tableGatewayMock = $this->getMockForAbstractClass(AbstractTableGateway::class);
$metadataMock = $this->getMockBuilder(MetadataInterface::class)->getMock();
$metadataMock->expects($this->any())->method('getColumnNames')->will($this->returnValue(['id', 'name']));
$metadataMock->expects($this->any())
->method('getTable')
->will($this->returnValue(new ViewObject('foo')));

$metadataMock->expects($this->never())->method('getConstraints');

$feature = new MetadataFeature($metadataMock);
$feature->setTableGateway($tableGatewayMock);
$feature->postInitialize();
}
}