public function testSimple()
 {
     $categoryToys = new Category(['name' => 'Toys']);
     $this->assertTrue($categoryToys->getIsNewRecord());
     $categoryToys->save();
     $this->assertFalse($categoryToys->getIsNewRecord());
     $category_animals = new Category();
     $category_animals->name = 'Animals';
     $category_animals->save();
     $db = ConnectionManager::getDb();
     $tableSql = $db->schema->quoteColumnName('tests_product');
     $tableAliasSql = $db->schema->quoteColumnName('tests_product_1');
     $categoryIdSql = $db->schema->quoteColumnName('category_id');
     $this->assertEquals("SELECT COUNT(*) FROM {$tableSql} {$tableAliasSql} WHERE ({$tableAliasSql}.{$categoryIdSql}='1')", $categoryToys->products->countSql());
     $this->assertEquals(0, $categoryToys->products->count());
     $product_bear = new Product(['category' => $categoryToys, 'name' => 'Bear', 'price' => 100, 'description' => 'Funny white bear']);
     $product_bear->save();
     $this->assertEquals(1, $categoryToys->products->count());
     $product_rabbit = new Product(['category' => $category_animals, 'name' => 'Rabbit', 'price' => 110, 'description' => 'Rabbit with carrot']);
     $product_rabbit->save();
     $this->assertEquals(1, $categoryToys->products->count());
     $product_rabbit->category = $categoryToys;
     $product_rabbit->save();
     $this->assertEquals(2, $categoryToys->products->count());
 }
 public function testClosure()
 {
     $model = new Product();
     $model->setAttributes(['name' => '12']);
     $this->assertFalse($model->isValid());
     $this->assertTrue($model->hasErrors());
     $this->assertTrue($model->hasErrors('name'));
     $this->assertEquals(['name' => ['Minimal length < 3']], $model->getErrors());
     $this->assertEquals(['Minimal length < 3'], $model->getErrors('name'));
     $model->clearErrors('name');
     $this->assertEquals([], $model->getErrors());
 }
 public function testDelete()
 {
     $this->assertEquals(0, Search::query('observer')->count());
     $p = Product::first();
     $p->name = 'observer';
     $p->save();
     $this->assertEquals(1, Search::query('observer')->count());
     $p->delete();
     $this->assertEquals(0, Search::query('observer')->count());
 }
 public function testIssue64()
 {
     $this->assertEquals(1, Category::objects()->count());
     $this->assertEquals(2, Product::objects()->count());
     $this->assertEquals(2, ProductList::objects()->count());
     $category = Category::objects()->get();
     $this->assertEquals(2, $category->products->count());
     $this->assertEquals(1, $category->products->filter(['name' => 'bar'])->count());
     $this->assertEquals(2, $category->products->filter(['lists__name' => 'test'])->count());
     $this->assertEquals(1, $category->products->filter(['lists__name' => 'asd'])->count());
     $this->assertEquals("SELECT `tests_product_1`.* FROM `tests_product` `tests_product_1` LEFT OUTER JOIN `tests_product_tests_product_list` `tests_product_tests_product_list_2` ON `tests_product_1`.`id` = `tests_product_tests_product_list_2`.`product_id` LEFT OUTER JOIN `tests_product_list` `tests_product_list_3` ON `tests_product_tests_product_list_2`.`product_list_id` = `tests_product_list_3`.`id` WHERE ((`tests_product_1`.`category_id`='1')) AND ((`tests_product_list_3`.`name`='asd')) GROUP BY `tests_product_1`.`id`", $category->products->filter(['lists__name' => 'asd'])->allSql());
 }
Beispiel #5
0
 public function testSync()
 {
     $product = new Product();
     $this->assertEquals(6, count($product->getFieldsInit()));
     $models = [new ProductList(), new Category(), new User(), $product];
     $sync = new Sync($models);
     $sync->delete();
     foreach ($models as $model) {
         $this->assertFalse($sync->hasTable($model));
     }
     // Create all tables. If table exists - skip.
     $sync->create();
     foreach ($models as $model) {
         $this->assertTrue($sync->hasTable($model));
     }
     // Remove all tables. If table does not exists - skip.
     $sync->delete();
     foreach ($models as $model) {
         $this->assertFalse($sync->hasTable($model));
     }
 }
 public function setUp()
 {
     parent::setUp();
     Product::unguard();
     $this->existed[] = Product::create(['name' => 'p1']);
     $this->existed[] = Product::create(['name' => 'p2']);
     $this->existed[] = Product::create(['name' => 'p3']);
     $this->notFilled = new Product();
     $this->notFilled->id = $this->existed[2]->id;
     $this->notExisted = new Product();
     $this->notExisted->id = 999;
 }
Beispiel #7
0
 public function setUp()
 {
     parent::setUp();
     $category = new Category();
     $category->name = 'test';
     $category->save();
     $user = new User();
     $user->password = 123456;
     $user->username = '******';
     $user->save();
     $customer = new Customer();
     $customer->user = $user;
     $customer->address = 'example super address';
     $customer->save();
     $products = [];
     foreach ([1, 2, 3, 4, 5] as $i) {
         $product = new Product();
         $product->name = $i;
         $product->price = $i;
         $product->description = $i;
         $product->category = $category;
         $product->save();
         $products[] = $product;
     }
     $order1 = new Order();
     $order1->customer = $customer;
     $order1->save();
     foreach ($products as $p) {
         $order1->products->link($p);
     }
     $order2 = new Order();
     $order2->customer = $customer;
     $order2->discount = 1;
     $order2->save();
     $order2->products = $products;
     $order2->save();
 }
Beispiel #8
0
 public static function getFields()
 {
     return ['customer' => ['class' => ForeignField::className(), 'modelClass' => Customer::className()], 'products' => ['class' => ManyToManyField::className(), 'modelClass' => Product::className()], 'discount' => ['class' => IntField::className(), 'null' => true]];
 }
 public function testForeignKey()
 {
     $model = new Product();
     $schema = $model->getTableSchema();
     $this->assertTrue(isset($schema->columns['id']));
     $this->assertTrue(isset($schema->columns['category_id']));
     $fk = $model->getField("category");
     $this->assertInstanceOf('\\Mindy\\Orm\\Fields\\ForeignField', $fk);
     $this->assertNull($model->category);
 }
Beispiel #10
0
 public static function getFields()
 {
     return ['name' => ['class' => CharField::className()], 'products' => ['class' => HasManyField::className(), 'modelClass' => Product::className(), 'null' => true, 'relatedName' => 'category', 'editable' => false]];
 }
 public function testExtraLinkRecords()
 {
     $product = new Product();
     $product->name = 'Bear';
     $product->price = 100;
     $product->description = 'Funny white bear';
     $product->save();
     $list1 = new ProductList();
     $list1->name = 'Toys';
     $list1->save();
     $list2 = new ProductList();
     $list2->name = 'Trash';
     $list2->save();
     $this->assertEquals(1, Product::objects()->count());
     $this->assertEquals(2, ProductList::objects()->count());
     $tableName = $product->getField('lists')->getTableName();
     $cmd = ConnectionManager::getDb()->createCommand("SELECT * FROM {$tableName}");
     $all = $cmd->queryAll();
     $this->assertEquals([], $all);
     $this->assertEquals(0, count($all));
     $product->lists = [$list1];
     $product->save();
     $cmd = ConnectionManager::getDb()->createCommand("SELECT * FROM {$tableName}");
     $all = $cmd->queryAll();
     $this->assertEquals([['product_id' => 1, 'product_list_id' => 1]], $all);
     $this->assertEquals(1, count($all));
     $product->lists = [$list2];
     $product->save();
     $cmd = ConnectionManager::getDb()->createCommand("SELECT * FROM {$tableName}");
     $all = $cmd->queryAll();
     $this->assertEquals([['product_id' => 1, 'product_list_id' => 2]], $all);
     $this->assertEquals(1, count($all));
     $product->lists = [$list1];
     $product->save();
     $cmd = ConnectionManager::getDb()->createCommand("SELECT * FROM {$tableName}");
     $all = $cmd->queryAll();
     $this->assertEquals([['product_id' => 1, 'product_list_id' => 1]], $all);
     $this->assertEquals(1, count($all));
     $product->lists = [$list1, $list1, $list1];
     $product->save();
     $cmd = ConnectionManager::getDb()->createCommand("SELECT * FROM {$tableName}");
     $all = $cmd->queryAll();
     $this->assertEquals([['product_id' => 1, 'product_list_id' => 1], ['product_id' => 1, 'product_list_id' => 1], ['product_id' => 1, 'product_list_id' => 1]], $all);
     $this->assertEquals(3, count($all));
 }
 public function testQ()
 {
     $qs = Product::objects()->filter([new OrQ([['name' => 'vasya', 'id__lte' => 7], ['name' => 'petya', 'id__gte' => 4]])]);
     $this->assertEquals("SELECT `tests_product_1`.* FROM `tests_product` `tests_product_1` WHERE (((`tests_product_1`.`name`='vasya') AND ((`tests_product_1`.`id` <= 7))) OR ((`tests_product_1`.`name`='petya') AND ((`tests_product_1`.`id` >= 4))))", $qs->allSql());
     $qs = Product::objects()->filter([new OrQ([['name' => 'vasya', 'id__lte' => 7], ['name' => 'petya', 'id__gte' => 4]]), 'price__gte' => 200]);
     $this->assertEquals("SELECT `tests_product_1`.* FROM `tests_product` `tests_product_1` WHERE (((`tests_product_1`.`name`='vasya') AND ((`tests_product_1`.`id` <= 7))) OR ((`tests_product_1`.`name`='petya') AND ((`tests_product_1`.`id` >= 4)))) AND ((`tests_product_1`.`price` >= 200))", $qs->allSql());
     $qs = Product::objects()->filter([new AndQ([['name' => 'vasya', 'id__lte' => 7], ['name' => 'petya', 'id__gte' => 4]]), 'price__gte' => 200]);
     $this->assertEquals("SELECT `tests_product_1`.* FROM `tests_product` `tests_product_1` WHERE (((`tests_product_1`.`name`='vasya') AND ((`tests_product_1`.`id` <= 7))) AND ((`tests_product_1`.`name`='petya') AND ((`tests_product_1`.`id` >= 4)))) AND ((`tests_product_1`.`price` >= 200))", $qs->allSql());
 }
 public static function getFields()
 {
     return ['name' => ['class' => CharField::className()], 'products' => ['class' => ManyToManyField::className(), 'modelClass' => Product::className()], 'date_action' => ['class' => DateTimeField::className(), 'required' => false, 'null' => true]];
 }
 public function testAllSql()
 {
     $qs = Product::objects()->filter(['id' => 1]);
     $this->assertEquals("SELECT \"tests_product_1\".* FROM \"tests_product\" \"tests_product_1\" WHERE ((\"tests_product_1\".\"id\"=1))", $qs->getSql());
     $this->assertEquals("SELECT \"tests_product_1\".* FROM \"tests_product\" \"tests_product_1\" WHERE ((\"tests_product_1\".\"id\"=1))", $qs->allSql());
     $this->assertEquals("SELECT COUNT(*) FROM \"tests_product\" \"tests_product_1\" WHERE ((\"tests_product_1\".\"id\"=1))", $qs->countSql());
 }