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()); }
/** * Test active record extensions across the two test clients */ public function testActiveRecordExtensions() { $dbFactory = \Yii::$app->get('dbFactory'); $clients = Client::find()->orderBy('id')->all(); $cnt = 0; foreach ($clients as $client) { $this->assertInstanceOf(Client::className(), $client); $this->setService('client', $client); for ($x = 1; $x < 5; $x++) { $cnt++; $cntLocal = 0; $customer = new Customer(); $customer->address->title = 'Mr' . $cnt; $customer->address->forename = 'A' . $cnt; $customer->address->surname = 'Sample' . $cnt; $customer->address->jobTitle = 'Job' . $cnt; $customer->address->company = 'Company' . $cnt; $customer->address->address1 = 'Address1-' . $cnt; $customer->address->address2 = 'Address2-' . $cnt; $customer->address->address3 = 'Address3-' . $cnt; $customer->address->city = 'City' . $cnt; $customer->address->region = 'Region' . $cnt; $customer->address->countryCode = 'GBR'; if ($client->clientCode == 'CLIENT2') { $customer->extraField = 'Extra' . $cnt; } $customer->phone->telno = '0123456789'; $customer->customerAttributes->field1 = 'CAField1-' . $cnt; $customer->customerAttributes->field2 = 'CAField2-' . $cnt; if ($client->clientCode == 'CLIENT2') { $customer->customerAttributes->field3 = 'CAField3-' . $cnt; } $ok = $customer->saveAll(); if (!$ok) { print_r($customer->getActionErrors()); } $this->assertTrue($ok, 'Failed to run saveAll'); if ($ok) { $this->assertEquals($x, $customer->id); $customerId = $customer->id; $customer = Customer::findOne($customerId); $this->assertInstanceOf(Customer::className(), $customer); /////// new order - using calculated new element keys - with assortment of array access, get row functions $cntLocal++; $key = $customer->orders->newElement(); $customer->orders[$key]->field1 = 'Order-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders->get($key)->field2 = 'Order-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders->row($key)->field3 = 'Order-Field-3-' . $cnt . '-' . $cntLocal; $key2 = $customer->orders[$key]->items->newElement(); $customer->orders[$key]->items[$key2]->productCode = 'CODE1' . ($client->clientCode == 'CLIENT2' ? 'B' : ''); $customer->orders[$key]->items[$key2]->quantity = 2; $customer->orders[$key]->items[$key2]->totalValue = 1.5; $customer->orders[$key]->items[$key2]->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders->get($key)->items->get($key2)->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders->row($key)->items->row($key2)->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $key2 = $customer->orders[$key]->items->newElement(); $customer->orders[$key]->items[$key2]->productCode = 'CODE2' . ($client->clientCode == 'CLIENT2' ? 'B' : ''); $customer->orders[$key]->items[$key2]->quantity = 1; $customer->orders[$key]->items[$key2]->totalValue = 1.59; $customer->orders[$key]->items[$key2]->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders->get($key)->items->get($key2)->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders->row($key)->items->row($key2)->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $key2 = $customer->orders[$key]->items->newElement(); $customer->orders[$key]->items[$key2]->productCode = 'POST'; $customer->orders[$key]->items[$key2]->quantity = 1; $customer->orders[$key]->items[$key2]->totalValue = 3.5; $customer->orders[$key]->items[$key2]->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders->get($key)->items->get($key2)->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders->row($key)->items->row($key2)->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; /////// new order - using specified element keys - with assortment of array access, get row functions $cntLocal++; $key = $customer->orders->newElement('xyz'); $customer->orders['xyz']->field1 = 'Order-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders->get('xyz')->field2 = 'Order-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders->row('xyz')->field3 = 'Order-Field-3-' . $cnt . '-' . $cntLocal; $key2 = $customer->orders['xyz']->items->newElement('qwe0'); $customer->orders['xyz']->items['qwe0']->productCode = 'CODE1' . ($client->clientCode == 'CLIENT2' ? 'B' : ''); $customer->orders['xyz']->items['qwe0']->quantity = 3; $customer->orders['xyz']->items['qwe0']->totalValue = 3.33; $customer->orders['xyz']->items['qwe0']->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders->get('xyz')->items->get('qwe0')->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders->row('xyz')->items->row('qwe0')->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $key2 = $customer->orders['xyz']->items->newElement('qwe1'); $customer->orders['xyz']->items['qwe1']->productCode = 'CODE2' . ($client->clientCode == 'CLIENT2' ? 'B' : ''); $customer->orders['xyz']->items['qwe1']->quantity = 2; $customer->orders['xyz']->items['qwe1']->totalValue = 4; $customer->orders['xyz']->items['qwe1']->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders->get('xyz')->items->get('qwe1')->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders->row('xyz')->items->row('qwe1')->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $key2 = $customer->orders['xyz']->items->newElement('qwe2'); $customer->orders['xyz']->items['qwe2']->productCode = 'POST'; $customer->orders['xyz']->items['qwe2']->quantity = 1; $customer->orders['xyz']->items['qwe2']->totalValue = 3.5; $customer->orders['xyz']->items['qwe2']->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders->get('xyz')->items->get('qwe2')->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders->row('xyz')->items->row('qwe2')->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; /////// new order - using array appending of objects with no keys specified $cntLocal++; $newOrder = new Order(); $newOrder->field1 = 'Order-Field-1-' . $cnt . '-' . $cntLocal; $newOrder->field2 = 'Order-Field-2-' . $cnt . '-' . $cntLocal; $newOrder->field3 = 'Order-Field-3-' . $cnt . '-' . $cntLocal; $newItem = new Item(); $newItem->productCode = 'CODE1' . ($client->clientCode == 'CLIENT2' ? 'B' : ''); $newItem->quantity = 3; $newItem->totalValue = 3.36; $newItem->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $newItem->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $newItem->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $newOrder->items[] = $newItem; $newItem = new Item(); $newItem->productCode = 'CODE2' . ($client->clientCode == 'CLIENT2' ? 'B' : ''); $newItem->quantity = 2; $newItem->totalValue = 6; $newItem->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $newItem->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $newItem->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $newOrder->items[] = $newItem; $newItem = new Item(); $newItem->productCode = 'POST'; $newItem->quantity = 1; $newItem->totalValue = 3.52; $newItem->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $newItem->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $newItem->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $newOrder->items->append($newItem); $customer->orders[] = $newOrder; /////// new order - using array appending of objects with initial temporary keys specified $cntLocal++; $newOrder = new Order(); $newOrder->field1 = 'Order-Field-1-' . $cnt . '-' . $cntLocal; $newOrder->field2 = 'Order-Field-2-' . $cnt . '-' . $cntLocal; $newOrder->field3 = 'Order-Field-3-' . $cnt . '-' . $cntLocal; $newItem = new Item(); $newItem->productCode = 'CODE1' . ($client->clientCode == 'CLIENT2' ? 'B' : ''); $newItem->quantity = 3; $newItem->totalValue = 3.36; $newItem->field1 = 'Item-Field-1-' . $cnt; $newItem->field2 = 'Item-Field-2-' . $cnt; $newItem->field3 = 'Item-Field-3-' . $cnt; $newOrder->items['m1'] = $newItem; $newItem = new Item(); $newItem->productCode = 'CODE2' . ($client->clientCode == 'CLIENT2' ? 'B' : ''); $newItem->quantity = 2; $newItem->totalValue = 6; $newItem->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $newItem->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $newItem->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $newOrder->items->appendWithKey($newItem, 'm2'); $newItem = new Item(); $newItem->productCode = 'POST'; $newItem->quantity = 1; $newItem->totalValue = 3.52; $newItem->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $newItem->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $newItem->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $newOrder->items->offsetSet('m3', $newItem); $customer->orders['m'] = $newOrder; /////// new order - using array appending of pre set objects with initial no keys specified $cntLocal++; $newOrder = new Order(array('field1' => 'Order-Field-1-' . $cnt . '-' . $cntLocal, 'field2' => 'Order-Field-2-' . $cnt . '-' . $cntLocal, 'field3' => 'Order-Field-3-' . $cnt . '-' . $cntLocal)); $newOrder->items[] = new Item(array('productCode' => 'CODE1' . ($client->clientCode == 'CLIENT2' ? 'B' : ''), 'quantity' => 3, 'totalValue' => 3.36, 'field1' => 'Item-Field-1-' . $cnt . '-' . $cntLocal, 'field2' => 'Item-Field-2-' . $cnt . '-' . $cntLocal, 'field3' => 'Item-Field-3-' . $cnt . '-' . $cntLocal)); $newOrder->items[] = new Item(array('productCode' => 'CODE2' . ($client->clientCode == 'CLIENT2' ? 'B' : ''), 'quantity' => 2, 'totalValue' => 4.8, 'field1' => 'Item-Field-1-' . $cnt . '-' . $cntLocal, 'field2' => 'Item-Field-2-' . $cnt . '-' . $cntLocal, 'field3' => 'Item-Field-3-' . $cnt . '-' . $cntLocal)); $newOrder->items[] = new Item(array('productCode' => 'POST', 'quantity' => 1, 'totalValue' => 3.98, 'field1' => 'Item-Field-1-' . $cnt . '-' . $cntLocal, 'field2' => 'Item-Field-2-' . $cnt . '-' . $cntLocal, 'field3' => 'Item-Field-3-' . $cnt . '-' . $cntLocal)); $customer->orders[] = $newOrder; /////// new order - using objects within arrays that have not needed to be initiated (default objects are automatically assumed when /////// a new offset is accessed $cntLocal++; $customer->orders['x']->field1 = 'Order-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders['x']->field2 = 'Order-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders['x']->field3 = 'Order-Field-3-' . $cnt . '-' . $cntLocal; $customer->orders['x']->items['a']->productCode = 'CODE1' . ($client->clientCode == 'CLIENT2' ? 'B' : ''); $customer->orders['x']->items['a']->quantity = 4; $customer->orders['x']->items['a']->totalValue = 4.12; $customer->orders['x']->items['a']->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders['x']->items['a']->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders['x']->items['a']->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $customer->orders['x']->items['b']->productCode = 'CODE2' . ($client->clientCode == 'CLIENT2' ? 'B' : ''); $customer->orders['x']->items['b']->quantity = 6; $customer->orders['x']->items['b']->totalValue = 4.12; $customer->orders['x']->items['b']->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders['x']->items['b']->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders['x']->items['b']->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; $customer->orders['x']->items['c']->productCode = 'POST'; $customer->orders['x']->items['c']->quantity = 1; $customer->orders['x']->items['c']->totalValue = 2.56; $customer->orders['x']->items['c']->field1 = 'Item-Field-1-' . $cnt . '-' . $cntLocal; $customer->orders['x']->items['c']->field2 = 'Item-Field-2-' . $cnt . '-' . $cntLocal; $customer->orders['x']->items['c']->field3 = 'Item-Field-3-' . $cnt . '-' . $cntLocal; //////// save all new order and changes $ok = $customer->saveAll(); if (!$ok) { print_r($customer->getActionErrors()); } $this->assertTrue($ok, 'Failed to run saveAll - adding orders'); } else { //foreach ($customer->getActionErrors() as $actionError) { // print_r($actionError); //} } } $fullDataCheck = array(); $customers = Customer::find()->orderBy('id')->all(); foreach ($customers as $customerId => $customer) { $fullDataCheck[$customerId] = $customer->allToArray(); } $fullDataCheck = $this->cleanDatesForComparison($fullDataCheck); $resultsPath = \Yii::getAlias('@fangface/tests/data/results'); $resultsFile = $resultsPath . '/ar-test-' . strtolower($client->clientCode) . '.json'; if (false) { // for use when comparing future tests file_put_contents($resultsFile, json_encode($fullDataCheck)); // readable results file_put_contents(str_replace('.json', '.txt', $resultsFile), print_r($fullDataCheck, true)); } else { $expectedResult = json_decode(file_get_contents($resultsFile), true); if ($fullDataCheck != $expectedResult) { file_put_contents(str_replace('.json', '-testing.txt', $resultsFile), print_r($fullDataCheck, true)); } $this->assertEquals($expectedResult, $fullDataCheck, 'Failed to match results for ' . strtolower($client->clientCode)); } $this->assertTrue($dbFactory->removeAllResources()); $this->assertEquals(1, $dbFactory->getResourceCount()); } $this->assertTrue($dbFactory->removeAllResources(true)); $this->assertEquals(0, $dbFactory->getResourceCount()); }