| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dynamic\FoxyStripe\Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dynamic\FoxyStripe\Model\Order; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Dynamic\FoxyStripe\Model\OrderDetail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Dynamic\FoxyStripe\Test\TestOnly\TestOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Dynamic\FoxyStripe\Test\TestOnly\TestProduct; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Dynamic\FoxyStripe\Test\TestOnly\TestProductController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Forms\FieldList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Forms\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class FoxyStripeInventoryManagerTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected static $fixture_file = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         '../fixtures.yml', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected static $extra_dataobjects = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         TestProduct::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         TestOption::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected static $extra_controllers = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         TestProductController::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function testUpdateCMSFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $object = $this->objFromFixture(TestProduct::class, 'one'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $fields = $object->getCMSFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->assertInstanceOf(FieldList::class, $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->assertNotNull($fields->dataFieldByName('ControlInventory')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function testFoxyStripePurchaseForm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         /** @var TestProduct $object */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $object = $this->objFromFixture(TestProduct::class, 'one'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         /** @var TestProductController $controller */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $controller = TestProductController::create($object); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $form = $controller->PurchaseForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->assertInstanceOf(Form::class, $form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function testGetHasInventory() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         /** @var TestProduct $product */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $product = $this->objFromFixture(TestProduct::class, 'one'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $product->ControlInventory = false; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $product->PurchaseLimit = 0; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->assertFalse($product->getHasInventory()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $product->ControlInventory = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $product->PurchaseLimit = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->assertFalse($product->getHasInventory()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $product->ControlInventory = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $product->PurchaseLimit = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->assertFalse($product->getHasInventory()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $product->ControlInventory = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $product->PurchaseLimit = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->assertTrue($product->getHasInventory()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function testGetIsProductAvailable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         /** @var TestProduct $product */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $product = $this->objFromFixture(TestProduct::class, 'one'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         // no inventory control | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $product->ControlInventory = false; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $product->PurchaseLimit = 0; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->assertTrue($product->getIsProductAvailable()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         // inventory control, no limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $product->ControlInventory = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $product->PurchaseLimit = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $this->assertTrue($product->getIsProductAvailable()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         // inventory control, with limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $product->ControlInventory = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $product->PurchaseLimit = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->assertTrue($product->getIsProductAvailable()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         /** @var OrderDetail $detail */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $detail = OrderDetail::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $detail->Quantity = 10; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $detail->ProductID = $product->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $detail->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         // inventory control, no inventory left | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $product->ControlInventory = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $product->PurchaseLimit = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->assertFalse($product->getIsProductAvailable()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     public function testGetNumberPurchased() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         /** @var TestProduct $product */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $product = $this->objFromFixture(TestProduct::class, 'one'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $this->assertEquals(0, $product->getNumberPurchased()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         /** @var OrderDetail $detail */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $detail = OrderDetail::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $detail->Quantity = 10; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $detail->ProductID = $product->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $detail->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $this->assertEquals(10, $product->getNumberPurchased()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |     public function testGetOrders() | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         /** @var TestProduct $product */ | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         $product = $this->objFromFixture(TestProduct::class, 'one'); | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         $this->assertEquals(0, $product->getOrders()->Count()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |         /** @var OrderDetail $detail */ | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         $detail = OrderDetail::create(); | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         $detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID; | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |         $detail->Quantity = 10; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |         $detail->ProductID = $product->ID; | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |         $detail->write(); | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |         $this->assertEquals(1, $product->getOrders()->count()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 162 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |  |