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());
 }
Example #2
0
 public static function getFields()
 {
     return ['name' => ['class' => CharField::className(), 'default' => 'Product', 'validators' => [function ($value) {
         if (mb_strlen($value, 'UTF-8') < 3) {
             return "Minimal length < 3";
         }
         return true;
     }]], 'price' => ['class' => CharField::className(), 'default' => 0], 'description' => ['class' => TextField::className(), 'null' => true], 'category' => ['class' => ForeignField::className(), 'modelClass' => Category::className(), 'null' => true, 'relatedName' => 'products'], 'lists' => ['class' => ManyToManyField::className(), 'modelClass' => ProductList::className()]];
 }
Example #3
0
 public function testInit()
 {
     $this->assertEquals(5, Product::objects()->count());
     $this->assertEquals(1, Category::objects()->count());
     $this->assertEquals(1, User::objects()->count());
     $this->assertEquals(1, Customer::objects()->count());
     $this->assertEquals(1, Order::objects()->count());
     $this->assertEquals(5, Order::objects()->get(['pk' => 1])->products->count());
     $this->assertEquals(1, ProductList::objects()->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());
 }
Example #5
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();
 }
 public function testIssue79()
 {
     $c = Category::objects()->batch(2);
     foreach ($c as $categories) {
         $this->assertEquals(2, count($categories));
     }
     $sql = Category::objects()->filter(['pk__in' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])->allSql();
     $this->assertTrue(is_string($sql));
     $this->assertEquals($sql, 'SELECT `tests_category_1`.* FROM `tests_category` `tests_category_1` WHERE (`tests_category_1`.`id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10))');
     $c = Category::objects()->filter(['pk__in' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])->batch(2);
     foreach ($c as $categories) {
         $this->assertEquals(2, count($categories));
     }
     $c = Category::objects()->filter(['pk__in' => [1, 2, 3, 4]])->batch(2);
     $total = 0;
     foreach ($c as $categories) {
         $total += count($categories);
         $this->assertEquals(2, count($categories));
     }
     $this->assertEquals(4, $total);
 }
Example #7
0
 public function testUnknownField()
 {
     $model = new Category();
     $field = $model->getField('something', false);
     $this->assertNull($field);
 }
 public function testExclude()
 {
     $this->assertEquals(1, count(Category::objects()->exclude(['name' => 'one'])->all()));
 }
 public function testCross()
 {
     $category = new Category();
     $category->name = 'Toys';
     $category->save();
     $product_bear = new Product();
     $product_bear->category = $category;
     $product_bear->name = 'Bear';
     $product_bear->price = 100;
     $product_bear->description = 'Funny white bear';
     $product_bear->save();
     $product_rabbit = new Product();
     $product_rabbit->category = $category;
     $product_rabbit->name = 'Rabbit';
     $product_rabbit->price = 110;
     $product_rabbit->description = 'Rabbit with carrot';
     $product_rabbit->save();
     $this->assertInstanceOf('\\Mindy\\Orm\\ManyToManyManager', $product_rabbit->lists);
     $this->assertEquals(0, $product_rabbit->lists->count());
     $best_sellers = new ProductList();
     $best_sellers->name = 'Best sellers';
     $best_sellers->save();
     $this->assertEquals(0, $best_sellers->products->count());
     $best_sellers->products->link($product_rabbit);
     $this->assertEquals(1, $best_sellers->products->count());
     $this->assertEquals(1, $product_rabbit->lists->count());
     $product_bear->lists->link($best_sellers);
     $this->assertEquals(2, $best_sellers->products->count());
     $this->assertEquals(1, $product_bear->lists->count());
 }