/** * Test client db resources auto load by looping over 2 clients and pulling in * a remote active active record belonging to a client db resource without * having first defined the connection in the connection manager */ public function testLoopThroughDifferentClientsAutoActiveRecordDbResource() { $dbFactory = \Yii::$app->get('dbFactory'); $clients = Client::find()->orderBy('id')->all(); foreach ($clients as $client) { $this->assertInstanceOf(Client::className(), $client); // setting the cient component up in yii will make the client connection available // when required to determine the clients dbRemote connection for the Robot model $this->setService('client', $client); $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')); } // remove all resources other than core db (typical action taken when switching from one client to another) $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(); }
/** * Test setValuesByArray method */ public function testSetValuesByArray() { $client = Client::findOne(1); $this->assertInstanceOf(Client::className(), $client); $this->setService('client', $client); $customerAttributes = new CustomerAttributes(); $customerAttributes->setValuesByArray(array('field1' => 'a', 'field2' => 'b'), array('modifiedBy' => 54)); $this->assertEquals('a', $customerAttributes->field1); $this->assertEquals('b', $customerAttributes->field2); $this->assertEquals(54, $customerAttributes->modifiedBy); }
/** * Test creating a non empty active record is a success * this model has a mixture of field types with default values * including a long text which has no default value and does not allow nulls */ function testNonEmptyActiveRecordSaveAll3Success() { $client = Client::findOne(1); $this->assertInstanceOf(Client::className(), $client); $this->setService('client', $client); $note = new Pick(); $note->loadDefaultValues(); $note->anotherString = 'B'; $success = $note->saveAll(); $this->assertTrue($success); $note->refresh(); $this->assertEquals('B', $note->anotherString); $this->assertEquals('', $note->anotherText); }
/** * 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'); }