Exemplo n.º 1
0
 public function __construct($singular_name, $pluralize = true, Phactory $phactory)
 {
     $this->_singular = $singular_name;
     if ($pluralize) {
         $this->_name = Inflector::pluralize($singular_name);
     } else {
         $this->_name = $singular_name;
     }
     $this->_collection = $phactory->getDb()->selectCollection($this->_name);
 }
Exemplo n.º 2
0
 /**
  * Converts a blueprint array into a persisted object
  * @param array $blueprint
  * @return object
  */
 public function create($blueprint)
 {
     $name = $blueprint->name;
     $type = $blueprint->type;
     if (!isset($this->count[$name])) {
         $this->count[$name] = 0;
     }
     $count = ++$this->count[$name];
     $values = $blueprint->values();
     $strings = array_map(function ($value) use($count) {
         return str_replace('#{sn}', str_pad($count, 4, '0', STR_PAD_LEFT), $value);
     }, $blueprint->strings());
     $relationships = $blueprint->relationships();
     foreach ($relationships as $key => $relationship) {
         $relationships[$key] = $relationship->create($blueprint->persisted);
     }
     $values = array_merge($values, $strings, $relationships);
     $dependencies = array_map(function ($value) use($values) {
         return $value->meet($values);
     }, $blueprint->dependencies());
     $values = array_merge($values, $dependencies);
     //$object = $this->toObject($name, $values);
     $object = $this->to_object($name, $values);
     // @deprecated Backwards compatibility
     if (false == $blueprint->persisted) {
         return $object;
     }
     \Phactory::triggers()->beforeSave($name, $type, $object);
     //$object = $this->saveObject($name, $object);
     $object = $this->save_object($name, $object);
     // @deprecated Backwards compatibility
     \Phactory::triggers()->afterSave($name, $type, $object);
     return $object;
 }
Exemplo n.º 3
0
 public function testOverrideAttributes()
 {
     $user = Phactory::user(array('last_name' => 'Blarg#{sn}', 'address' => (object) array('street' => 'Sesame St.')));
     $this->assertEquals($user, (object) array('first_name' => 'Fronzel', 'last_name' => 'Blarg0001', 'email' => '*****@*****.**', 'address' => (object) array('street' => 'Sesame St.')));
     $admin = Phactory::user('admin', array('first_name' => 'Admin'));
     $this->assertEquals($admin, (object) array('first_name' => 'Admin', 'last_name' => 'Neekburm', 'email' => '*****@*****.**', 'is_admin' => true));
 }
Exemplo n.º 4
0
 public function testToArrayAfterCreate()
 {
     $data = array('id' => 1, 'name' => 'testname');
     Phactory::define('user', $data);
     $user = Phactory::create('user');
     $this->assertEquals($data, $user->toArray());
 }
 protected function tearDown()
 {
     Phactory::reset();
     $this->pdo->exec("DROP TABLE users");
     $this->pdo->exec("DROP TABLE images");
     $this->pdo->exec("DROP TABLE users_images");
 }
 protected function setUp()
 {
     require_once PHACTORY_PATH . '/Phactory.php';
     require_once PHACTORY_PATH . '/Phactory/DbUtil/SqliteUtil.php';
     $this->pdo = new PDO('sqlite:test.db');
     Phactory::setConnection($this->pdo);
 }
Exemplo n.º 7
0
 public function testCustomBuilder()
 {
     Phactory::reset();
     Phactory::builder(new CustomBuilder());
     $cake = Phactory::cake();
     $this->assertInstanceOf('CustomObject', $cake);
 }
Exemplo n.º 8
0
 public static function getDbUtil(Phactory $phactory)
 {
     $db_type = $phactory->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME);
     switch ($db_type) {
         case 'mysql':
             return new DbUtil\MysqlUtil($phactory);
             break;
         case 'pgsql':
             return new DbUtil\PgsqlUtil($phactory);
             break;
         case 'sqlite':
             return new DbUtil\SqliteUtil($phactory);
             break;
         default:
             throw new \Exception("DB type '{$db_type}' not found");
             break;
     }
 }
Exemplo n.º 9
0
 public function testTriggersWithVariation()
 {
     Phactory::reset();
     Phactory::triggers(new FrameworkTriggers());
     $account = Phactory::account('system');
     $this->assertEquals($account->id, 1);
     $this->assertTrue($account->beforeSave);
     $this->assertTrue($account->afterSave);
     $this->assertTrue($account->systemBeforeSave);
     $this->assertTrue($account->systemAfterSave);
 }
Exemplo n.º 10
0
 public static function getDbUtil()
 {
     $db_type = Phactory::getConnection()->getAttribute(PDO::ATTR_DRIVER_NAME);
     switch ($db_type) {
         case 'mysql':
             return new Phactory_DbUtil_MysqlUtil();
             break;
         case 'sqlite':
             return new Phactory_DbUtil_SqliteUtil();
             break;
         default:
             throw new Exception("DB type '{$db_type}' not found");
             break;
     }
 }
Exemplo n.º 11
0
 public function save()
 {
     $pdo = Phactory::getConnection();
     $sql = "INSERT INTO {$this->_table} (";
     $data = array();
     $params = array();
     foreach ($this->_storage as $key => $value) {
         $data["`{$key}`"] = ":{$key}";
         $params[":{$key}"] = $value;
     }
     $keys = array_keys($data);
     $values = array_values($data);
     $sql .= join(',', $keys);
     $sql .= ") VALUES (";
     $sql .= join(',', $values);
     $sql .= ")";
     $stmt = $pdo->prepare($sql);
     $r = $stmt->execute($params);
     if ($r === false) {
         $error = $stmt->errorInfo();
         Phactory_Logger::error('SQL statement failed: ' . $sql . ' ERROR MESSAGE: ' . $error[2] . ' ERROR CODE: ' . $error[1]);
     }
     // only works if table's primary key autoincrements
     $id = $pdo->lastInsertId();
     if ($pk = $this->_table->getPrimaryKey()) {
         if ($id) {
             $this->_storage[$pk] = $id;
         } else {
             // if key doesn't autoincrement, find last inserted row and set the primary key.
             $sql = "SELECT * FROM {$this->_table} WHERE";
             for ($i = 0, $size = sizeof($keys); $i < $size; ++$i) {
                 $sql .= " {$keys[$i]} = {$values[$i]} AND";
             }
             $sql = substr($sql, 0, -4);
             $stmt = $pdo->prepare($sql);
             $stmt->execute($params);
             $result = $stmt->fetch(PDO::FETCH_ASSOC);
             $this->_storage[$pk] = $result[$pk];
         }
     }
     return $r;
 }
Exemplo n.º 12
0
 /**
  * Tears down the fixture, for example, closes a network connection.
  * This method is called after a test is executed.
  */
 protected function tearDown()
 {
     Phactory::reset();
     $this->pdo->exec("DROP TABLE `users`");
 }
Exemplo n.º 13
0
 public function admin()
 {
     return array('author' => Phactory::hasOne('author', 'admin'));
 }
Exemplo n.º 14
0
 public static function reset()
 {
     self::recall();
     self::$_blueprints = array();
     Phactory_Inflector::reset();
 }
Exemplo n.º 15
0
 public function __construct()
 {
     $this->_pdo = Phactory::getConnection();
 }
Exemplo n.º 16
0
 protected function _associateManyToMany($row, $many_to_many)
 {
     $pdo = Phactory::getConnection();
     foreach ($many_to_many as $name => $arr) {
         list($to_rows, $assoc) = $arr;
         foreach ($to_rows as $to_row) {
             $join_table = $assoc->getJoinTable();
             $from_join_column = $assoc->getFromJoinColumn();
             $to_join_column = $assoc->getToJoinColumn();
             $sql = "INSERT INTO `{$join_table}` \n                        (`{$from_join_column}`, `{$to_join_column}`)\n                        VALUES\n                        (:from_id, :to_id)";
             $stmt = $pdo->prepare($sql);
             $r = $stmt->execute(array(':from_id' => $row->getId(), ':to_id' => $to_row->getId()));
             if ($r === false) {
                 $error = $stmt->errorInfo();
                 Phactory_Logger::error('SQL statement failed: ' . $sql . ' ERROR MESSAGE: ' . $error[2] . ' ERROR CODE: ' . $error[1]);
             }
         }
     }
 }
Exemplo n.º 17
0
 public function blueprint()
 {
     return array('type' => 'jpg', 'content' => '@^&#$^#@&*$', 'creator' => Phactory::hasOne('designer'), 'co_creator' => Phactory::hasOne('designer'));
 }
Exemplo n.º 18
0
 public function testRecall()
 {
     $name = 'testuser';
     // define and create user in db
     Phactory::define('user', array('name' => $name));
     $user = Phactory::create('user');
     // recall() deletes from the db
     Phactory::recall();
     // test that the object is gone from the db
     $stmt = $this->pdo->query("SELECT * FROM `users`");
     $db_user = $stmt->fetch();
     $this->assertFalse($db_user);
     // test that the blueprints weren't destroyed too
     $user = Phactory::create('user');
     $this->assertEquals($user->name, $name);
 }
Exemplo n.º 19
0
 public function tearDown()
 {
     Phactory::recall();
 }
Exemplo n.º 20
0
 public function blueprint()
 {
     return array('name' => 'Ore #{sn}', 'mine' => Phactory::hasOne('mine'), 'saved' => false);
 }
Exemplo n.º 21
0
 /**
  * Get triggers events caller. If it is not defined, it will be set.
  * @param object|null $triggers
  * @return \Phactory\Triggers
  */
 public static function triggers($triggers = null)
 {
     if (is_object($triggers)) {
         self::$triggers = new Triggers($triggers);
     }
     return isset(self::$triggers) ? self::$triggers : (self::$triggers = new Triggers());
 }
Exemplo n.º 22
0
 public function blueprint()
 {
     return array('title' => 'project #{sn}', 'category' => Phactory::hasOne('category', 'tshirt'));
 }
 public function admin()
 {
     return array('employer' => Phactory::has_one('employer', 'admin'));
 }
Exemplo n.º 24
0
 public function testRecall()
 {
     $name = 'testuser';
     // define and create user in db
     Phactory::define('user', array('name' => $name));
     $user = Phactory::create('user');
     // recall() deletes from the db
     Phactory::recall();
     // test that the object is gone from the db
     $db_user = $this->db->users->findOne();
     $this->assertNull($db_user);
     // test that the blueprints weren't destroyed too
     $user = Phactory::create('user');
     $this->assertEquals($user['name'], $name);
 }