示例#1
0
 public function run()
 {
     $faker = TestCase::faker();
     for ($i = 1; $i <= 10; $i++) {
         $price = round($faker->randomFloat(2, 0, 99999999.98999999), 2);
         $sale_price = round($price - $price * (rand(5, 25) / 100), 2);
         $product = Product::create(array('sku' => $faker->unique()->bothify('????????##'), 'name' => $faker->words(3, true), 'description' => $faker->text(), 'price' => (double) $price, 'sale_price' => (double) ($i % 2) === 0 ? null : $sale_price, 'quantity' => $faker->randomNumber(4)));
         TestCase::addFixture('products.product_' . $i, $product);
     }
 }
 public function testIndex()
 {
     $response = $this->callJSON('GET', '/api/v1/products');
     $this->assertResponseOk();
     $this->assertResponseJSONValid();
     $json = $this->getJSONContent();
     $this->assertJSONCollectionResponse('products');
     $this->assertJSONTypes('products[0]', $this->productJSONFormat);
     $this->assertEquals(0, $json->response->offset);
     $this->assertEquals(Product::count(), $json->response->total);
 }
示例#3
0
 /**
  * Create a new Product.
  *
  * @example
  *     $product = Subbly\Model\Product;
  *     Subbly::api('subbly.product')->create($product);
  *
  *     Subbly::api('subbly.product')->create(array(
  *         'firstname' => 'Jon',
  *         'lastname'  => 'Snow',
  *     ), 'en');
  *
  * @param \Subbly\Model\Product|array $product
  *
  * @return \Subbly\Model\Product
  *
  * @api
  */
 public function create($product, $locale = null)
 {
     if (is_array($product)) {
         $product = new Product($product);
     }
     // set locale
     if (!is_null($locale)) {
         $product->setFrontLocale($locale);
     }
     if ($product instanceof Product) {
         if ($this->fireEvent('creating', array($product)) === false) {
             return false;
         }
         $product->setCaller($this);
         $product->saveWithTranslation();
         $product = $this->find($product->sku);
         $this->fireEvent('created', array($product));
         return $product;
     }
     throw new Exception(sprintf(Exception::CANT_CREATE_MODEL, 'Subbly\\Model\\Product', $this->name()));
 }