Exemple #1
0
 function axon()
 {
     $this->set('title', 'SQL/Axon');
     $this->expect(is_null($this->get('ERROR')), 'No errors expected at this point', 'ERROR variable is set: ' . $this->get('ERROR.text'));
     $this->set('DB', new DB('sqlite::memory:'));
     $this->expect(extension_loaded('pdo_sqlite'), 'SQLite PDO available', 'SQLite PDO is not active - unable to continue');
     if (extension_loaded('pdo_sqlite')) {
         DB::sql(array('DROP TABLE IF EXISTS products;', 'CREATE TABLE products (' . 'item INTEGER,' . 'description VARCHAR(255),' . 'quantity INTEGER,' . 'PRIMARY KEY (item)' . ');'));
         $product = new Axon('products');
         $this->expect(is_object($product), 'Axon created', 'Unable to instantiate Axon');
         unset($product);
         $product = Axon::instance('products');
         $this->expect(is_a($product, 'Axon'), 'Axon instance created', 'Unable to instantiate Axon');
         unset($product);
         $product = new axon('products');
         $this->expect(is_object($product), 'Axon created (case-insensitive)', 'Unable to instantiate Axon (case-insensitive)');
         $this->expect($product->dry(), 'Axon in dry state', 'Axon is in hydrated state');
         $product->item = 111;
         $product->description = 'Coca Cola';
         $product->quantity = 3;
         $this->expect(!$product->dry(), 'Axon hydrated manually', 'Axon should be hydrated by now');
         $product->save();
         $this->expect(!$product->dry(), 'Axon expected to remain hydrated', 'Axon should be dry');
         // MySQL always reports an _id of 0 if primary key
         // is not an auto-increment field
         $this->expect($product->_id, 'Last insert ID available; SQLite returns ' . $product->_id, 'No last insert ID available');
         $product->load(array('item=:item', array(':item' => 111)));
         $this->expect($product->item == 111 && $product->description == 'Coca Cola' && $product->quantity == 3, 'Auto-hydration succeeded (SQLite converts numbers to strings)', 'Auto-hydration failed');
         $result = $product->findOne(array('item=:item', array(':item' => 111)));
         $this->expect($result->item == 111 && $result->description == 'Coca Cola' && $result->quantity == 3, 'findOne returned the correct record', 'findOne return value is incorrect');
         $result = $product->find(array('item=:item', array(':item' => 111)));
         $this->expect(get_class($result[0]) == 'Axon' && $result[0]->item == 111 && $result[0]->description == 'Coca Cola' && $result[0]->quantity == 3, 'find returned an array of Axon objects', 'find return type is incorrect');
         $product->quantity++;
         $product->save();
         $product->load(array('item=:item', array(':item' => 111)));
         $this->expect($product->item == 111 && $product->description == 'Coca Cola' && $product->quantity == 4, 'Axon saved - database update succeeded', 'Database update failed');
         $product->copyTo('POST');
         $this->expect($this->get('POST.item') == 111 && $this->get('POST.description') == 'Coca Cola' && $this->get('POST.quantity') == 4, 'Axon properties copied to framework variable', 'Unable to copy Axon properties to framework variable');
         $_POST['description'] = 'Pepsi';
         $product->copyFrom('POST');
         $this->expect($product->item == 111 && $product->description == 'Pepsi' && $product->quantity == 4, 'Axon properties populated by framework variable', 'Unable to fill Axon properties with contents of framework variable');
         $this->set('POST.item', 999);
         $this->set('POST.description', 'Pepsi');
         $this->set('POST.quantity', 11);
         $product->copyFrom('POST', 'item|quantity');
         $this->expect($product->item == 999 && $product->description == 'Pepsi' && $product->quantity == 11, 'Axon properties populated by selected fields in framework variable', 'Unable to fill Axon properties with contents of framework variable');
         $product->reset();
         $this->expect($product->dry(), 'Axon reset completed', 'Axon should be dry');
         $product->item = 222;
         $product->description = 'Mobile Phone';
         $product->quantity = 9;
         $this->expect(!$product->dry(), 'Axon rehydrated manually', 'Axon should hydrated by now');
         $product->save();
         $this->expect(!$product->dry(), 'Axon expected to remain hydrated', 'Axon should not be dry');
         $product->load('item=111');
         $this->expect($product->item == 111 && $product->description == 'Coca Cola' && $product->quantity == 4, 'First record still there', 'First record is missing');
         $product->load('item=222');
         $this->expect($product->item == 222 && $product->description == 'Mobile Phone' && $product->quantity == 9, 'Second record found', 'Second record is missing');
         $product->def('total', 'SUM(quantity)');
         $this->expect($product->isdef('total') === TRUE, 'Virtual field created', 'Problem creating virtual field');
         $product->load();
         $this->expect($product->total == 13, 'Computed value of aggregate value using a virtual field works', 'Virtual field implementation faulty');
         $product->undef('total');
         $this->expect($product->isdef('total') === FALSE, 'Virtual field destroyed', 'Problem destroying virtual field');
         $product->load('item=111');
         $product->erase();
         $product->load('item=111');
         $this->expect($product->dry(), 'First record deleted', 'First record still exists');
         $product->load('item=222');
         $this->expect($product->item == 222 && $product->description == 'Mobile Phone' && $product->quantity == 9, 'Second record still there', 'Second record is missing');
         $product->reset();
         $product->item = 111;
         $product->description = 'Lots of dough';
         $product->quantity = 666;
         $product->save();
         $product->load('quantity>0');
         $this->expect($product->found() == 2, 'New record added - multirecord criteria specified for loading', 'New record was not added');
         $product->skip(1);
         $this->expect(!$product->dry(), 'One more record expected to be retrieved', 'Axon is dry');
         $this->expect($product->item == 222 && $product->description == 'Mobile Phone' && $product->quantity == 9, 'Forward navigation', 'Forward navigation failed');
         $product->skip(-1);
         $this->expect($product->item == 111 && $product->description == 'Lots of dough' && $product->quantity == 666, 'Backward navigation', 'Backward navigation failed');
         $product->skip(-1);
         $this->expect($product->dry(), 'Axon is dry when navigating before the start of the record set', 'Navigation failure');
         $this->set('QUIET', TRUE);
         $product->skip(-1);
         $this->expect(!is_null($this->get('ERROR')), 'Navigating past dry state triggers an error', 'Navigation error handling issue');
         $this->set('QUIET', FALSE);
         $this->clear('ERROR');
         $product->load('quantity>0');
         $product->skip(2);
         $this->expect($product->dry(), 'Axon is dry when navigating beyond the end of the record set', 'Navigation failure');
         $this->set('QUIET', TRUE);
         $product->skip();
         $this->expect(!is_null($this->get('ERROR')), 'Navigating past dry state triggers an error', 'Navigation error handling issue');
         $this->set('QUIET', FALSE);
         $this->clear('ERROR');
         $db = $this->get('DB');
         $result = $db->exec('SELECT * FROM products WHERE item=:item', array(':item' => 111));
         $this->expect($result[0]['item'] == 111 && $result[0]['description'] == 'Lots of dough' && $result[0]['quantity'] == 666, 'Late-binding of parameters to values in SQL statements', 'Late-binding issue encountered');
         $product->load('item=111');
         $product->description = 'quoted "string"';
         $product->save();
         $result = $product->findOne('item=111');
         $this->expect($result->description == 'quoted "string"', 'Double-quoted strings are left untouched', 'Double-quoted strings altered');
     }
     echo $this->render('basic/results.htm');
 }