예제 #1
0
 public function testOptions()
 {
     $product = new Product();
     $product->setOption('color', 'colorBlue', 'Blue', 'blue');
     $product->setOption('color', 'colorGreen', 'Green', 'green');
     $product->setOption('color', 'colorRed', 'Red', 'red');
     $product->setOption('material', 'materialCotton', 'Cotton', 'cotton');
     $product->setOption('material', 'materialSmall', 'Polyester', 'polyester');
     $product->setOption('size', 'sizeLarge', 'Large', 'large');
     $product->setOption('size', 'sizeSmall', 'Small', 'small');
     $options = $product->getOptions();
     $this->assertCount(7, $options, 'all product options should be returned');
     $options = $product->getOptions('color');
     $this->assertCount(3, $options, 'all product options of color type should be returned');
     $this->assertInstanceOf('Vespolina\\Entity\\Product\\Option', array_shift($options), 'a Option object should be returned');
     $blue = $product->getOption('color', 'colorBlue');
     $this->assertInstanceOf('Vespolina\\Entity\\Product\\Option', $blue, 'the single option object should have been returned');
     $options = $product->getOptionsArray();
     $expected = ['color' => ['colorBlue' => 'Blue', 'colorGreen' => 'Green', 'colorRed' => 'Red'], 'material' => ['materialCotton' => 'Cotton', 'materialSmall' => 'Polyester'], 'size' => ['sizeLarge' => 'Large', 'sizeSmall' => 'Small']];
     $this->assertEquals($expected, $options, 'all product options should be returned');
     $options = $product->getOptionsArray('size');
     $expected = ['sizeLarge' => 'Large', 'sizeSmall' => 'Small'];
     $this->assertEquals($expected, $options, 'all product options of size type should be returned');
     $product->setOption('color', 'colorBlue', 'NewBlue', 'newblue');
     $newBlue = $product->getOption('color', 'colorBlue');
     $this->assertSame('NewBlue', $newBlue->getDisplay(), 'the display should have changed');
     $this->assertSame('newblue', $newBlue->getName(), 'the display should have changed');
     $this->assertSame(spl_object_hash($blue), spl_object_hash($newBlue), 'the object should be the same');
     return $product;
 }
예제 #2
0
 protected function createVariationProduct()
 {
     $options = ['screwSize' => ['M6' => ['M6', 'M6'], 'M8' => ['M8', 'M8'], 'M10' => ['M10', 'M10'], 'M12' => ['M12', 'M12']], 'color' => ['black' => ['BK', 'Black'], 'blue' => ['BL', 'Blue'], 'gold' => ['GO', 'Gold'], 'green' => ['GR', 'Green'], 'red' => ['RD', 'Red']], 'material' => ['plastic' => ['Plastic', 'Plastic'], 'metal' => ['Metal', 'Metal']]];
     $product = new Product();
     foreach ($options as $type => $typeOptions) {
         foreach ($typeOptions as $index => $data) {
             $product->setOption($type, $index, $data[0], $data[1]);
         }
     }
     $data = [['M6' => ['material' => 'metal', 'color' => 'black']], ['M6' => ['material' => 'metal', 'color' => 'blue']], ['M6' => ['material' => 'metal', 'color' => 'green']], ['M10' => ['material' => 'metal', 'color' => 'black']], ['M10' => ['material' => 'metal', 'color' => 'gold']], ['M10' => ['material' => 'metal', 'color' => 'green']]];
     foreach ($data as $options) {
         foreach ($options as $screwSize => $otherOptions) {
             $label = $screwSize;
             foreach ($otherOptions as $type => $name) {
                 $label .= $name;
             }
             $variation = $product->useVariation($label);
         }
         foreach ($options as $screwSize => $otherOptions) {
             $variation->setOption('screwSize', $screwSize);
             foreach ($otherOptions as $type => $index) {
                 $variation->setOption($type, $index);
             }
         }
     }
 }