function checkBaseCounts($type = '', $clientCode = '', $customers = 0, $addresses = 0, $phones = 0, $orders = 0, $items = 0, $attributes = 0, $countries = 2, $products = 4) { if ($type != '') { switch ($type) { case 'full': $customers = 1; $addresses = 1; $phones = 1; $orders = 1; $items = 3; $attributes = $clientCode == 'CLIENT2' ? 5 : 4; break; case 'lessoneitem': $customers = 1; $addresses = 1; $phones = 1; $orders = 1; $items = 2; $attributes = $clientCode == 'CLIENT2' ? 5 : 4; break; case 'lessaddress': $customers = 1; $addresses = 0; $phones = 1; $orders = 1; $items = 3; $attributes = $clientCode == 'CLIENT2' ? 5 : 4; break; } } $this->assertEquals($customers, Customer::find()->count()); $this->assertEquals($addresses, Address::find()->count()); $this->assertEquals($attributes, AttributeValues::find()->count()); $this->assertEquals($orders, Order::find()->count()); $this->assertEquals($items, Item::find()->count()); $this->assertEquals($countries, Country::find()->count()); $this->assertEquals($products, Product::find()->count()); }
public function modelRelationMap() { return ['product' => array('type' => 'hasOne', 'class' => Product::className(), 'link' => array('productCode' => 'productCode'), 'allToArray' => true), 'order' => array('type' => 'belongsTo', 'class' => Order::className(), 'link' => array('id' => 'orderId')), 'customer' => array('type' => 'belongsTo', 'class' => Customer::className(), 'link' => array('id' => 'customerId'))]; }
/** * Test multiple db resources by looping over 2 clients and connecting * to their and their own specified dbResources which will share the same * connection name across both clients */ public function testLoopThroughDifferentClients() { $dbFactory = \Yii::$app->get('dbFactory'); $clients = Client::find()->orderBy('id')->all(); foreach ($clients as $client) { $this->assertInstanceOf(Client::className(), $client); $this->setService('client', $client); // get the dbClient connection (should be based on the 'client' component setup above //$db = $dbFactory->getConnection('dbClient', true); $db = $dbFactory->getClientConnection(true); // command equivalent to the above // $db should now be the connection to the current client (not connected) $this->assertInstanceOf(Connection::className(), $db); $this->assertEquals($client->dbDsn, $db->dsn); $this->assertEquals(2, $dbFactory->getResourceCount()); // we should be able to get a connection $this->assertTrue($dbFactory->connectResource('dbClient')); $this->assertTrue($dbFactory->isResourceConnected('dbClient')); $this->assertInstanceOf('\\PDO', $db->pdo); $customer = new Customer(); if ($client->clientCode == 'CLIENT1') { $this->assertTrue(!$customer->hasAttribute('extraField')); } else { // the customer table for CLIENT2 has an extra extraField attribute $this->assertTrue($customer->hasAttribute('extraField')); } // find details of the current clients dbRemote connection $dbResource = ClientDbResource::find()->where(['resourceName' => 'dbRemote'])->one(); // now attempt to get a connection to the specific clients dbRemote resource as defined // in the clients.dbResources table //$dbRemote = $dbFactory->getConnection('dbClient', true, false, true); $dbRemote = $dbFactory->getClientResourceConnection('dbRemote', true); // identical to the getConnection() call above // $dbRemote should now be the connection to the current clients dbRemote resource (not connected) $this->assertInstanceOf(Connection::className(), $dbRemote); $this->assertEquals($dbResource->dbDsn, $dbRemote->dsn); $this->assertEquals(3, $dbFactory->getResourceCount()); // we should be able to get a connection $this->assertTrue($dbFactory->connectResource('dbRemote')); $this->assertTrue($dbFactory->isResourceConnected('dbRemote')); $this->assertInstanceOf('\\PDO', $dbRemote->pdo); if ($client->clientCode == 'CLIENT1') { $this->assertRegExp('/dbTestRemote1/', $dbRemote->dsn); } else { $this->assertRegExp('/dbTestRemote2/', $dbRemote->dsn); } $robot = Robot::findOne(1); if ($client->clientCode == 'CLIENT1') { $this->assertTrue(!$robot->hasAttribute('extraField')); } else { // the robot table for CLIENT2s dbRemote connection has an extra extraField attribute $this->assertTrue($robot->hasAttribute('extraField')); } $this->assertTrue($dbFactory->removeAllResources()); $this->assertEquals(1, $dbFactory->getResourceCount()); } $this->assertTrue($dbFactory->removeAllResources(true)); $this->assertEquals(0, $dbFactory->getResourceCount()); }
/** * Test active record array deleteFull() where canDelete has been set to false * * @expectedException \fangface\db\Exception * @expectedExceptionMessage Attempting to delete Item(s) model flagged as not deletable */ function testActiveRecordDeleteFullOnNonCanDeleteArrayFails() { $client = Client::findOne(1); $this->assertInstanceOf(Client::className(), $client); $this->setService('client', $client); $customerId = $this->createTestCustomerAndOrder($client->clientCode); $this->assertTrue($customerId !== false && $customerId > 0); $this->checkBaseCounts('full', $client->clientCode); $customer = Customer::findOne($customerId); $this->assertInstanceOf(Customer::className(), $customer); $customer->orders[1]->items->setCanDelete(false); $customer->orders[1]->items->deleteFull(); }
public function modelRelationMap() { return ['items' => array('type' => 'hasMany', 'class' => Item::className(), 'link' => array('orderId' => 'id'), 'onSaveAll' => ActiveRecord::SAVE_CASCADE, 'onDeleteFull' => ActiveRecord::DELETE_CASCADE, 'autoLinkType' => ActiveRecord::LINK_FROM_PARENT_MAINT, 'allToArray' => true, 'autoLink' => array('fromParent' => array('orderId' => 'id', 'customerId' => 'customerId'))), 'customer' => array('type' => 'belongsTo', 'class' => Customer::className(), 'link' => array('id' => 'customerId'))]; }
public function modelRelationMap() { return ['country' => array('type' => 'hasOne', 'class' => Country::className(), 'link' => array('countryCode' => 'countryCode'), 'readOnly' => true, 'canDelete' => false, 'allToArray' => true), 'customer' => array('type' => 'belongsTo', 'class' => Customer::className(), 'link' => array('id' => 'customerId'))]; }
/** * Amend customer id at top level of a relation and test that a push() * request forces relations that have customerId set are updated even though * no other changes have been made */ function testActiveRecordChangeTopLevelIdFeedsDownToRelations() { $client = Client::findOne(1); $this->assertInstanceOf(Client::className(), $client); $this->setService('client', $client); $customerId = $this->createTestCustomerAndOrder($client->clientCode); $this->assertTrue($customerId !== false && $customerId > 0); $this->checkBaseCounts('full', $client->clientCode); $customer = Customer::findOne($customerId); $this->assertInstanceOf(Customer::className(), $customer); $customer->id = 95; $customerPreChangeWithNewId = $this->updateCustomerIdForComparison($this->cleanDatesForComparison($customer->toArray()), 95); $customer->push(); $customer = Customer::findOne(95); $this->assertInstanceOf(Customer::className(), $customer); $customerReloaded = $this->cleanDatesForComparison($customer->toArray()); $this->assertEquals($customerPreChangeWithNewId, $customerReloaded, 'Failed to push new customerId into relations after reload'); }