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

Fix undefined variable bug in MetadataFeature #341

Merged
merged 1 commit into from
Dec 6, 2018
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
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original line had $pkc->getColumns() here: 1ac578f#diff-ca8362b8be8f02af0316d9c0e3979808L80

}

$this->sharedData['metadata']['primaryKey'] = $primaryKey;
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();
}
}