Example #1
0
 /**
  * @group integration-test
  */
 public function testPostInitialize()
 {
     $tableGatewayMock = $this->getMockForAbstractClass('Zend\\Db\\TableGateway\\AbstractTableGateway');
     $metadataMock = $this->getMock('Zend\\Db\\Metadata\\MetadataInterface');
     $metadataMock->expects($this->any())->method('getColumnNames')->will($this->returnValue(array('id', 'name')));
     $constraintObject = new ConstraintObject('id_pk', 'table');
     $constraintObject->setColumns(array('id'));
     $constraintObject->setType('PRIMARY KEY');
     $metadataMock->expects($this->any())->method('getConstraints')->will($this->returnValue(array($constraintObject)));
     $feature = new MetadataFeature($metadataMock);
     $feature->setTableGateway($tableGatewayMock);
     $feature->postInitialize();
     $this->assertEquals(array('id', 'name'), $tableGatewayMock->getColumns());
 }
Example #2
0
 /**
  * @cover FeatureSet::addFeature
  * @group ZF2-4993
  */
 public function testAddFeatureThatFeatureHasTableGatewayButFeatureSetDoesnotHas()
 {
     $tableGatewayMock = $this->getMockForAbstractClass('Zend\\Db\\TableGateway\\AbstractTableGateway');
     $metadataMock = $this->getMock('Zend\\Db\\Metadata\\MetadataInterface');
     $metadataMock->expects($this->any())->method('getColumnNames')->will($this->returnValue(array('id', 'name')));
     $constraintObject = new ConstraintObject('id_pk', 'table');
     $constraintObject->setColumns(array('id'));
     $constraintObject->setType('PRIMARY KEY');
     $metadataMock->expects($this->any())->method('getConstraints')->will($this->returnValue(array($constraintObject)));
     //feature have tableGateway, but FeatureSet doesn't has
     $feature = new MetadataFeature($metadataMock);
     $feature->setTableGateway($tableGatewayMock);
     $featureSet = new FeatureSet();
     $this->assertInstanceOf('Zend\\Db\\TableGateway\\Feature\\FeatureSet', $featureSet->addFeature($feature));
 }
Example #3
0
 protected function setTableData(ZfObject\AbstractTableObject $table, array $data)
 {
     $columns = [];
     foreach ($data['columns'] as $col) {
         $c = new Object\ColumnObject($col['name'], $col['tableName'], $col['schemaName']);
         $c->setType($col['type']);
         $c->setOrdinalPosition($col['ordinalPosition']);
         $c->setColumnDefault($col['columnDefault']);
         $c->setIsNullable($col['isNullable']);
         $c->setDataType($col['dataType']);
         $c->setCharacterMaximumLength($col['characterMaximumLength']);
         $c->setCharacterOctetLength($col['characterOctetLength']);
         $c->setNumericPrecision($col['numericPrecision']);
         $c->setNumericUnsigned($col['numericUnsigned']);
         $c->setErratas($col['errata']);
         $columns[$col['name']] = $c;
     }
     $table->setColumns($columns);
     $constraints = [];
     foreach ($data['constraints'] as $cons) {
         $c = new ZfObject\ConstraintObject($cons['name'], $cons['tableName'], $cons['schemaName']);
         $c->setType($cons['type']);
         $c->setColumns($cons['columns']);
         $c->setReferencedTableSchema($cons['referencedTableSchema']);
         $c->setReferencedTableName($cons['referencedTableName']);
         $c->setReferencedColumns($cons['referencedColumns']);
         $c->setMatchOption($cons['matchOption']);
         $c->setUpdateRule($cons['updateRule']);
         $c->setDeleteRule($cons['deleteRule']);
         $c->setCheckClause($cons['checkClause']);
         $constraints[] = $c;
     }
     $table->setConstraints($constraints);
 }
Example #4
0
    /**
     * Get constraint
     * 
     * @param  string $constraintName
     * @param  string $table
     * @param  string $schema
     * @param  string $database
     * @return Object\ConstraintObject 
     */
    public function getConstraint($constraintName, $table, $schema = null)
    {
        if ($this->constraintData == null) {
            $this->loadConstraintData();
        }

        $found = false;
        foreach ($this->constraintData as $tableName => $constraints) {
            foreach ($constraints as $constraintData) {
                if ($tableName == $table && $constraintData['name'] == $constraintName) {
                    $found = $constraintData;
                    break 2;
                }
            }
        }

        if (!$found) {
            throw new \Exception('invalid constraint, or constraint not found');
        }

        $constraint = new Object\ConstraintObject($found['name'], $table);
        $constraint->setType($found['type']);
        $constraint->setKeys($this->getConstraintKeys($found['name'], $table, $schema));
        return $constraint;
    }
Example #5
0
 /**
  * Get constraint
  * 
  * @param  string $constraintName
  * @param  string $table
  * @param  string $schema
  * @param  string $database
  * @return Object\ConstraintObject 
  */
 public function getConstraint($constraintName, $table, $schema = null)
 {
     // set values for database & schema
     $database = $database ?: '__DEFAULT_DB__';
     if ($schema == null && $this->defaultSchema != null) {
         $schema = $this->defaultSchema;
     }
     if (!isset($this->data[$database][$schema]['constraints'])) {
         $this->loadConstraintData($schema, $database);
     }
     $found = false;
     foreach ($this->data[$database][$schema]['constraints']['names'] as $constraintInfo) {
         if ($constraintInfo['constraint_name'] == $constraintName && $constraintInfo['table_name'] == $table) {
             $found = $constraintInfo;
             break;
         }
     }
     if (!$found) {
         throw new \Exception('Cannot find a constraint by that name in this table');
     }
     $constraint = new Object\ConstraintObject($constraintName, $table, $schema);
     $constraint->setType($found['constraint_type']);
     $constraint->setKeys($this->getConstraintKeys($constraintName, $table, $schema, $database));
     return $constraint;
 }