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 testCreate()
 {
     $this->assertEquals(0, Search::query('observer')->count());
     $p = new Product();
     $p->name = 'observer';
     $p->publish = 1;
     $p->save();
     $this->assertEquals(1, Search::query('observer')->count());
 }
Exemplo n.º 3
0
 public function setUp()
 {
     parent::setUp();
     $category = new Category(['name' => 'cat']);
     $category->save();
     $product1 = new Product(['name' => 'foo', 'category' => $category]);
     $product1->save();
     $product2 = new Product(['name' => 'bar', 'category' => $category]);
     $product2->save();
     $list = new ProductList(['name' => 'test']);
     $list->save();
     $list->products->link($product1);
     $list->products->link($product2);
     $list = new ProductList(['name' => 'asd']);
     $list->save();
     $list->products->link($product1);
 }
Exemplo n.º 4
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();
 }
Exemplo n.º 5
0
 public function setUp()
 {
     parent::setUp();
     $category = new Category();
     $category->name = 'test';
     $category->save();
     $product_list = new ProductList();
     $product_list->name = 'First product list';
     $product_list->date_action = '2014-04-29 10:35:45';
     $product_list->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;
     }
     $order = new Order();
     $order->customer = $customer;
     $order->save();
     $order->products = $products;
     $order->save();
     $model = new Category();
     $this->prefix = $model->getDb()->tablePrefix;
 }
Exemplo n.º 6
0
 public function testSetter()
 {
     $model = new Product();
     $model->name = 'example';
     $this->assertEquals('example', $model->name);
     $this->assertEquals('SIMPLE', $model->type);
     $model->type = '123';
     $this->assertEquals('123', $model->type);
     $category = new Category();
     $category->name = 'Toys';
     $category->save();
     $product = new Product();
     $product->name = 'Bear';
     $product->price = 100;
     $product->description = 'Funny white bear';
     $this->assertNull($product->category);
     $this->assertNull($product->category_id);
     $this->assertEquals(1, $category->pk);
     // Also working
     // $product->category = $category;
     $product->category_id = $category->pk;
     $this->assertEquals(['id', 'name'], $category->attributes());
     $this->assertEquals(['id', 'name', 'price', 'description', 'category_id'], $product->attributes());
     $this->assertEquals(1, $product->category_id);
     $this->assertEquals(1, $product->getAttribute('category_id'));
     $product->save();
     $this->assertInstanceOf('\\Tests\\Models\\Category', $product->category);
     $this->assertTrue(is_numeric($product->category_id));
 }
 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));
 }