Inheritance: extends lithium\data\Entity
Exemplo n.º 1
0
	public function testErrorsPropertyAccess() {
		$errors = array(
			'title' => 'please enter a title',
			'email' => array('email is empty', 'email is not valid')
		);

		$record = new Record();
		$result = $record->errors($errors);
		$this->assertEqual($errors, $result);

		$result = $record->errors();
		$this->assertEqual($errors, $result);

		$expected = 'please enter a title';
		$result = $record->errors('title');
		$this->assertEqual($expected, $result);

		$expected = array('email is empty', 'email is not valid');
		$result = $record->errors('email');
		$this->assertEqual($expected, $result);

		$result = $record->errors('not_a_field');
		$this->assertNull($result);

		$result = $record->errors('not_a_field', 'badness');
		$this->assertEqual('badness', $result);
	}
Exemplo n.º 2
0
 /**
  * Will allow you to call SplFileObject methods
  *
  * Methods priority:
  *  * Model
  *  * SplFileObject
  *
  * @link http://www.php.net/manual/en/class.splfileinfo.php
  * @param  string $method
  * @param  array $params
  * @return mixed
  */
 public function __call($method, $params)
 {
     try {
         return parent::__call($method, $params);
     } catch (BadMethodCallException $e) {
         if (method_exists('SplFileObject', $method)) {
             return call_user_func_array(array($this->fileObj(), $method), $params);
         } else {
             throw $e;
         }
     }
 }
Exemplo n.º 3
0
 /**
  * __call
  *
  * The heart of the RecordProxy. The method and params go in, the result is then cached and servered next time.
  *
  * @param string $method
  * @param array $params
  * @return mixed
  */
 public function __call($method, $params)
 {
     // Is filterable?
     if (!in_array($method, Memoize::$objectNames[$this->_model])) {
         return parent::__call($method, $params);
     }
     // Variables
     $hash = Memoize::hashArgs($params);
     // Create array if it doesn't exist
     if (!isset($this->_memoizeResults[$method])) {
         $this->_memoizeResults[$method] = array();
     }
     // Check if method + params have been ran already
     if (isset($this->_memoizeResults[$method][$hash])) {
         return $this->_memoizeResults[$method][$hash];
     }
     // Set and return
     return $this->_memoizeResults[$method][$hash] = parent::__call($method, $params);
 }
 public function testDelete()
 {
     $entity = new Record(array('model' => $this->_model, 'data' => array('id' => 1, 'title' => 'new post', 'body' => 'the body'), 'exists' => true));
     $query = new Query(compact('entity') + array('type' => 'delete'));
     $this->assertTrue($entity->exists());
     $this->assertTrue($this->db->delete($query));
     $this->assertEqual(1, $query->entity()->id);
     $expected = "DELETE FROM {mock_database_posts} WHERE {id} = 1;";
     $this->assertEqual($expected, $this->db->sql);
     $this->assertFalse($entity->exists());
 }
Exemplo n.º 5
0
 public function testErrorWithCustomConfiguration()
 {
     $this->form->config(array('error' => array('class' => 'custom-error-class')));
     $record = new Record();
     $record->errors(array('name' => array('Please enter a name')));
     $this->form->create($record);
     $result = $this->form->field('name');
     $expected = '<div><label for="name">Name</label><input type="text" name="name" />';
     $expected .= '<div class="custom-error-class">Please enter a name</div></div>';
     $this->assertEqual($expected, $result);
 }
Exemplo n.º 6
0
 public function testErrorWithCustomConfiguration()
 {
     $this->form->config(array('error' => array('class' => 'custom-error-class')));
     $record = new Record(array('model' => $this->_model));
     $record->errors(array('name' => array('Please enter a name')));
     $this->form->create($record);
     $result = $this->form->field('name');
     $this->assertTags($result, array('<div', 'label' => array('for' => 'MockFormPostName'), 'Name', '/label', 'input' => array('type' => "text", 'name' => 'name', 'id' => 'MockFormPostName'), 'div' => array('class' => "custom-error-class"), 'Please enter a name', '/div', '/div'));
 }
Exemplo n.º 7
0
 public function testFormErrorMultipleBindings()
 {
     $record1 = new Record(array('model' => $this->_model, 'data' => array('author_id' => '2', 'title' => 'New post', 'body' => 'New post body')));
     $record2 = new Record(array('model' => $this->_model2, 'data' => array('section' => 'New post section', 'notes' => 'New post notes')));
     $record1->errors(array('title' => 'Not a cool title'));
     $record2->errors(array('section' => 'Not a cool section'));
     $this->form->create(compact('record1', 'record2'));
     $result = $this->form->error('title');
     $this->assertTags($result, array('div' => array('class' => 'error'), 'Not a cool title', '/div'));
     $result = $this->form->error('body');
     $this->assertEmpty($result);
     $result = $this->form->error('record1.title');
     $this->assertTags($result, array('div' => array('class' => 'error'), 'Not a cool title', '/div'));
     $result = $this->form->error('record2.section');
     $this->assertTags($result, array('div' => array('class' => 'error'), 'Not a cool section', '/div'));
 }
Exemplo n.º 8
0
 public function testIncrement()
 {
     $entity = new Record(array('model' => $this->_model, 'data' => array('id' => 1, 'balance' => 10), 'exists' => true));
     $entity->increment('balance', 10);
     $query = new Query(compact('entity') + array('type' => 'update'));
     $result = $this->_db->update($query);
     $expected = "UPDATE {mock_database_posts} SET {id} = 1, {balance} = {balance} + 10 WHERE {id} = 1;";
     $this->assertEqual($expected, $this->_db->sql);
     $entity->increment('balance', 10);
     $entity->decrement('balance', 20);
     $query = new Query(compact('entity') + array('type' => 'update'));
     $result = $this->_db->update($query);
     $expected = "UPDATE {mock_database_posts} SET {id} = 1, {balance} = {balance} + -10 WHERE {id} = 1;";
     $this->assertEqual($expected, $this->_db->sql);
     $entity->increment('balance', 10);
     $entity->balance = 20;
     $query = new Query(compact('entity') + array('type' => 'update'));
     $result = $this->_db->update($query);
     $expected = "UPDATE {mock_database_posts} SET {id} = 1, {balance} = 20 WHERE {id} = 1;";
     $this->assertEqual($expected, $this->_db->sql);
     $this->assertException("Field 'name' cannot be incremented.", function () use($entity) {
         $entity->name = 'Ali';
         $entity->increment('name', 10);
     });
 }