Beispiel #1
0
 /**
  * @coversNothing
  */
 public function testRels()
 {
     $user = User::find(1);
     $user->name = 'New Name';
     $user->isBlocked = true;
     $user->object = new SaveableObject();
     $user->object->setVar('value');
     $address = $user->getAddress();
     $address->location = 'Somewhere else';
     $address->zipCode = '1234';
     $posts = $user->getPosts();
     $post = $posts->getFirst();
     $post->body = 'Changed Body';
     $post = new Post(['title' => 'new post', 'body' => 'Lorem Ipsum', 'price' => 123.23]);
     $posts->add($post);
     $tags = Tag::whereIn('id', [1, 2])->load();
     $post->getTags()->addModels($tags);
     $this->assertQueries(['SELECT `User`.* FROM `User` WHERE (`id` = 1) AND (`User`.`deletedAt` IS NULL) LIMIT 1', 'SELECT `Address`.* FROM `Address` WHERE (`id` IN (1))', 'SELECT `Post`.`class`, `Post`.* FROM `Post` WHERE (`userId` IN (1))', 'SELECT `Tag`.* FROM `Tag` WHERE (`id` IN (1, 2))']);
     User::save($user);
     $this->assertQueries(['SELECT `User`.* FROM `User` WHERE (`id` = 1) AND (`User`.`deletedAt` IS NULL) LIMIT 1', 'SELECT `Address`.* FROM `Address` WHERE (`id` IN (1))', 'SELECT `Post`.`class`, `Post`.* FROM `Post` WHERE (`userId` IN (1))', 'SELECT `Tag`.* FROM `Tag` WHERE (`id` IN (1, 2))', 'INSERT INTO `Post` (`id`, `title`, `body`, `price`, `tags`, `createdAt`, `updatedAt`, `publishedAt`, `userId`, `class`) VALUES (NULL, "new post", "Lorem Ipsum", "123.23", NULL, NULL, NULL, NULL, NULL, "Harp\\Harp\\Test\\TestModel\\Post")', 'INSERT INTO `PostTag` (`id`, `postId`, `tagId`) VALUES (NULL, NULL, 1), (NULL, NULL, 2)', 'UPDATE `User` SET `name` = "New Name", `isBlocked` = 1, `object` = "C:41:"Harp\\Harp\\Test\\Integration\\SaveableObject":22:{a:1:{i:0;s:5:"value";}}" WHERE (`id` = 1)', 'UPDATE `Address` SET `zipCode` = "1234", `location` = "Somewhere else" WHERE (`id` = 1)', 'UPDATE `Post` SET `body` = CASE `id` WHEN 1 THEN "Changed Body" ELSE `body` END, `userId` = CASE `id` WHEN 5 THEN 1 ELSE `userId` END WHERE (`id` IN (1, 5))', 'UPDATE `PostTag` SET `postId` = CASE `id` WHEN 4 THEN "5" WHEN 5 THEN "5" ELSE `postId` END WHERE (`id` IN (4, 5))']);
     Container::clear();
     $user = User::find(1);
     $this->assertEquals('New Name', $user->name);
     $this->assertEquals('value', $user->object->getVar());
     $this->assertEquals(true, $user->isBlocked);
     $address = $user->getAddress();
     $this->assertEquals('Somewhere else', $address->location);
     $this->assertEquals('1234', $address->zipCode);
     $posts = $user->getPosts();
     $post = $posts->getFirst();
     $this->assertEquals('Changed Body', $post->body);
     $newPost = Post::where('title', 'new post')->loadFirst();
     $this->assertTrue($posts->has($newPost));
     $this->assertEquals([1, 2], $newPost->getTags()->get()->getIds());
 }
 public function setUp()
 {
     parent::setUp();
     DB::setConfig(['dsn' => 'mysql:dbname=harp-orm/random-key;host=127.0.0.1', 'username' => 'root']);
     DB::get()->beginTransaction();
     Container::clear();
 }
Beispiel #3
0
 public function setUp()
 {
     $this->db = new Session();
     $this->db->setAliases(['Address' => __NAMESPACE__ . '\\TestModel\\Address', 'BlogPost' => __NAMESPACE__ . '\\TestModel\\BlogPost', 'City' => __NAMESPACE__ . '\\TestModel\\City', 'Country' => __NAMESPACE__ . '\\TestModel\\Country', 'Post' => __NAMESPACE__ . '\\TestModel\\Post', 'PostTag' => __NAMESPACE__ . '\\TestModel\\PostTag', 'Profile' => __NAMESPACE__ . '\\TestModel\\Profile', 'Tag' => __NAMESPACE__ . '\\TestModel\\Tag', 'User' => __NAMESPACE__ . '\\TestModel\\User']);
     parent::setUp();
     Container::clear();
 }
Beispiel #4
0
 public function setUp()
 {
     parent::setUp();
     $this->logger = new TestLogger();
     DB::setConfig(['dsn' => 'mysql:dbname=harp-orm/nested;host=127.0.0.1', 'username' => 'root']);
     DB::get()->setLogger($this->logger);
     DB::get()->beginTransaction();
     Container::clear();
 }
Beispiel #5
0
 public function setUp()
 {
     parent::setUp();
     $this->logger = new TestLogger();
     DB::setConfig(['dsn' => 'mysql:dbname=harp-orm/range;host=127.0.0.1', 'username' => 'root', 'escaping' => DB::ESCAPING_MYSQL]);
     DB::get()->setLogger($this->logger);
     DB::get()->beginTransaction();
     Container::clear();
 }
 public function setUp()
 {
     parent::setUp();
     date_default_timezone_set('UTC');
     $this->logger = new TestLogger();
     DB::setConfig(['dsn' => 'mysql:dbname=harp-orm/timestamps;host=127.0.0.1', 'username' => 'root']);
     DB::get()->setLogger($this->logger);
     DB::get()->beginTransaction();
     Container::clear();
     TimestampsTrait::setCurrentDate(null);
 }
 public function setUp()
 {
     parent::setUp();
     $this->logger = new TestLogger();
     Converter::initialize(new NullSource());
     DB::setConfig(['dsn' => 'mysql:dbname=clippings/transfer;host=127.0.0.1', 'username' => 'root']);
     DB::get()->execute('ALTER TABLE Basket AUTO_INCREMENT = 1');
     DB::get()->execute('ALTER TABLE ProductItem AUTO_INCREMENT = 1');
     DB::get()->setLogger($this->logger);
     DB::get()->beginTransaction();
     Container::clear();
 }
 public function setUp()
 {
     parent::setUp();
     $this->logger = new TestLogger();
     DB::setConfig(['dsn' => 'mysql:dbname=clippings/purchases;host=127.0.0.1', 'username' => 'root', 'escaping' => DB::ESCAPING_MYSQL]);
     Converter::initialize(new NullSource());
     DB::get()->execute('ALTER TABLE PurchaseItem AUTO_INCREMENT = 6');
     DB::get()->execute('ALTER TABLE StorePurchase AUTO_INCREMENT = 3');
     DB::get()->setLogger($this->logger);
     DB::get()->beginTransaction();
     Container::clear();
 }
Beispiel #9
0
 /**
  * @covers ::execute
  */
 public function testExecute()
 {
     $save = new Save();
     $repo1 = $this->getMock('Harp\\Harp\\Repo', ['deleteModels', 'insertModels', 'updateModels', 'get'], [new Config(__NAMESPACE__ . '\\TestModel\\Country')]);
     $repo1->expects($this->any())->method('get')->will($this->returnValue($repo1));
     $repo2 = $this->getMock('Harp\\Harp\\Repo', ['deleteModels', 'insertModels', 'updateModels', 'get'], [new Config(__NAMESPACE__ . '\\TestModel\\User')]);
     $repo2->expects($this->any())->method('get')->will($this->returnValue($repo2));
     Container::set(__NAMESPACE__ . '\\TestModel\\Country', $repo1);
     Container::set(__NAMESPACE__ . '\\TestModel\\User', $repo2);
     $models = [1 => (new Country(['id' => 1], State::SAVED))->setProperties(['name' => 'changed']), 2 => new Country(['id' => 2], State::VOID), 3 => new Country(['id' => 3], State::PENDING), 4 => new Country(['id' => 4], State::DELETED), 5 => (new User(['id' => 5, 'name' => 'test'], State::DELETED))->setProperties(['deletedAt' => time()]), 6 => new User(['id' => 6, 'name' => 'test'], State::SAVED), 7 => new User(['id' => 7, 'name' => 'test'], State::PENDING), 8 => new User(['id' => 8, 'name' => 'test'], State::DELETED), 9 => (new User(['id' => 9, 'name' => 'test'], State::SAVED))->setProperties(['name' => 'changed'])];
     $save = new Save();
     $save->addArray($models);
     $expected = ['deleteModels' => [$models[4]], 'insertModels' => [$models[3]], 'updateModels' => [$models[1]]];
     foreach ($expected as $method => $values) {
         $repo1->expects($this->once())->method($method)->with($this->callback(function (Models $models) use($values) {
             $this->assertSame($values, $models->toArray());
             return true;
         }));
     }
     $expected = ['deleteModels' => [$models[8]], 'insertModels' => [$models[7]], 'updateModels' => [$models[5], $models[9]]];
     foreach ($expected as $method => $values) {
         $repo2->expects($this->once())->method($method)->with($this->callback(function (Models $models) use($values) {
             $this->assertSame($values, $models->toArray());
             return true;
         }));
     }
     $save->execute();
 }
Beispiel #10
0
 public function setUp()
 {
     parent::setUp();
     Converter::initialize(new NullSource());
     Container::clear();
 }
Beispiel #11
0
 /**
  * @covers ::get
  */
 public function testGet()
 {
     $class1 = 'Harp\\Harp\\Test\\TestModel\\Country';
     $actual1 = 'Harp\\Harp\\Test\\TestModel\\City';
     $class2 = 'Harp\\Harp\\Test\\TestModel\\User';
     $actual2 = 'Harp\\Harp\\Test\\TestModel\\Profile';
     Container::setActualClass($class1, $actual1);
     Container::setActualClass($class2, $actual2);
     $this->assertSame(Container::get($class1), Container::get($actual1));
     $this->assertSame(Container::get($actual2), Container::get($class2));
 }
Beispiel #12
0
 /**
  * Enables Repo "inheritance" allowing multiple repos to share one storage table
  * You will need to call setRootRepo on all the child repos.
  *
  * @param  boolean      $inherited
  * @return Config $this
  */
 public function setInherited($inherited)
 {
     $this->inherited = (bool) $inherited;
     if ($inherited) {
         if (!$this->reflectionModel->isRoot()) {
             $rootRepo = Container::get($this->reflectionModel->getRoot()->getName());
             $this->rootConfig = $rootRepo->getConfig();
         }
         $this->table = $this->rootConfig->getTable();
     }
     return $this;
 }
Beispiel #13
0
 /**
  * @return Repo
  */
 public function getRepo()
 {
     return Container::get($this->foreignModelClass);
 }
Beispiel #14
0
 /**
  * @covers ::initialize
  */
 public function testInitialize()
 {
     Container::clear();
     $reflection = new ReflectionModel('Harp\\Harp\\Test\\TestModel\\City');
     $reflection->initialize(City::getRepo()->getConfig());
 }
Beispiel #15
0
 /**
  * Add a ProductItem for the given product / quantity.
  * If product item exists, increase the quantity
  *
  * @param  Product $product
  * @param  integer $quantity
  * @return static
  */
 public function addProduct(Product $product, $quantity = 1)
 {
     foreach ($this->getProductItems() as $item) {
         if ($item->getProduct() === $product) {
             $item->quantity += $quantity;
             return $this;
         }
     }
     $item = Container::get('CL\\Purchases\\ProductItem')->newModel(['quantity' => $quantity]);
     $item->setProduct($product);
     $this->addPurchaseItem($product->getStore(), $item);
     return $this;
 }
Beispiel #16
0
 /**
  * @return Repo
  */
 public static function getRepo()
 {
     return Container::get(get_called_class());
 }
Beispiel #17
0
 /**
  * @covers ::loadAllRelsFor
  */
 public function testLoadAllRelsFor()
 {
     $repo1 = $this->getMock('Harp\\Harp\\Repo', ['loadRelFor'], [new Config(__NAMESPACE__ . '\\TestModel\\City')]);
     $repo2 = $this->getMock('Harp\\Harp\\Repo', ['loadRelFor'], [new Config(__NAMESPACE__ . '\\TestModel\\Country')]);
     $repo3 = $this->getMock('Harp\\Harp\\Repo', ['loadRelFor'], [new Config(__NAMESPACE__ . '\\TestModel\\User')]);
     Container::set(__NAMESPACE__ . '\\TestModel\\City', $repo1);
     Container::set(__NAMESPACE__ . '\\TestModel\\Country', $repo2);
     Container::set(__NAMESPACE__ . '\\TestModel\\User', $repo3);
     $repo1->getConfig()->addRel(new Rel\BelongsTo('one', $repo1->getConfig(), __NAMESPACE__ . '\\TestModel\\Country'));
     $repo2->getConfig()->addRel(new Rel\HasMany('many', $repo2->getConfig(), __NAMESPACE__ . '\\TestModel\\User'));
     $models1 = new Models([new City()]);
     $models2 = new Models([new Country()]);
     $models3 = new Models([new User()]);
     $repo1->expects($this->once())->method('loadRelFor')->with($this->equalTo($models1), $this->equalTo('one'), $this->equalTo(State::DELETED))->will($this->returnValue($models2));
     $repo2->expects($this->once())->method('loadRelFor')->with($this->equalTo($models2), $this->equalTo('many'), $this->equalTo(State::DELETED))->will($this->returnValue($models3));
     $repo1->loadAllRelsFor($models1, ['one' => 'many'], State::DELETED);
 }