OrdersTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. use Webkul\Checkout\Models\Cart;
  3. use Webkul\Checkout\Models\CartAddress;
  4. use Webkul\Checkout\Models\CartItem;
  5. use Webkul\Checkout\Models\CartPayment;
  6. use Webkul\Customer\Models\Customer;
  7. use Webkul\Customer\Models\CustomerAddress;
  8. use Webkul\Faker\Helpers\Product as ProductFaker;
  9. use Webkul\Sales\Models\Invoice;
  10. use Webkul\Sales\Models\InvoiceItem;
  11. use Webkul\Sales\Models\Order;
  12. use Webkul\Sales\Models\OrderAddress;
  13. use Webkul\Sales\Models\OrderItem;
  14. use Webkul\Sales\Models\OrderPayment;
  15. use function Pest\Laravel\get;
  16. use function Pest\Laravel\getJson;
  17. use function Pest\Laravel\postJson;
  18. it('should returns the index page customers orders', function () {
  19. // Act and Assert.
  20. $this->loginAsCustomer();
  21. get(route('shop.customers.account.orders.index'))
  22. ->assertOk()
  23. ->assertSeeText(trans('shop::app.customers.account.orders.title'));
  24. });
  25. it('should view the order', function () {
  26. // Arrange.
  27. $product = (new ProductFaker([
  28. 'attributes' => [
  29. 5 => 'new',
  30. ],
  31. 'attribute_value' => [
  32. 'new' => [
  33. 'boolean_value' => true,
  34. ],
  35. ],
  36. ]))
  37. ->getSimpleProductFactory()
  38. ->create();
  39. $customer = Customer::factory()->create();
  40. $cart = Cart::factory()->create([
  41. 'customer_id' => $customer->id,
  42. 'customer_first_name' => $customer->first_name,
  43. 'customer_last_name' => $customer->last_name,
  44. 'customer_email' => $customer->email,
  45. 'is_guest' => 0,
  46. ]);
  47. $additional = [
  48. 'product_id' => $product->id,
  49. 'rating' => '0',
  50. 'is_buy_now' => '0',
  51. 'quantity' => '1',
  52. ];
  53. $cartItem = CartItem::factory()->create([
  54. 'cart_id' => $cart->id,
  55. 'product_id' => $product->id,
  56. 'sku' => $product->sku,
  57. 'quantity' => $additional['quantity'],
  58. 'name' => $product->name,
  59. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  60. 'base_price' => $price,
  61. 'total' => $convertedPrice * $additional['quantity'],
  62. 'base_total' => $price * $additional['quantity'],
  63. 'weight' => $product->weight ?? 0,
  64. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  65. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  66. 'type' => $product->type,
  67. 'additional' => $additional,
  68. ]);
  69. $customerAddress = CustomerAddress::factory()->create([
  70. 'cart_id' => $cart->id,
  71. 'customer_id' => $customer->id,
  72. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  73. ]);
  74. $cartBillingAddress = CartAddress::factory()->create([
  75. 'cart_id' => $cart->id,
  76. 'customer_id' => $customer->id,
  77. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  78. ]);
  79. $cartShippingAddress = CartAddress::factory()->create([
  80. 'cart_id' => $cart->id,
  81. 'customer_id' => $customer->id,
  82. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  83. ]);
  84. $cartPayment = CartPayment::factory()->create([
  85. 'cart_id' => $cart->id,
  86. 'method' => $paymentMethod = 'cashondelivery',
  87. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  88. ]);
  89. $order = Order::factory()->create([
  90. 'cart_id' => $cart->id,
  91. 'customer_id' => $customer->id,
  92. 'customer_email' => $customer->email,
  93. 'customer_first_name' => $customer->first_name,
  94. 'customer_last_name' => $customer->last_name,
  95. ]);
  96. $orderItem = OrderItem::factory()->create([
  97. 'product_id' => $product->id,
  98. 'order_id' => $order->id,
  99. 'sku' => $product->sku,
  100. 'type' => $product->type,
  101. 'name' => $product->name,
  102. ]);
  103. $orderBillingAddress = OrderAddress::factory()->create([
  104. 'cart_id' => $cart->id,
  105. 'customer_id' => $customer->id,
  106. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  107. ]);
  108. $orderShippingAddress = OrderAddress::factory()->create([
  109. 'cart_id' => $cart->id,
  110. 'customer_id' => $customer->id,
  111. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  112. ]);
  113. $orderPayment = OrderPayment::factory()->create([
  114. 'order_id' => $order->id,
  115. ]);
  116. // Act and Assert.
  117. $this->loginAsCustomer($customer);
  118. get(route('shop.customers.account.orders.view', $order->id))
  119. ->assertOk()
  120. ->assertSeeText(trans('shop::app.customers.account.orders.view.information.sku'))
  121. ->assertSeeText(trans('shop::app.customers.account.orders.view.information.product-name'))
  122. ->assertSeeText(trans('shop::app.customers.account.orders.view.information.total-due'))
  123. ->assertSeeText(trans('shop::app.customers.account.orders.view.page-title', ['order_id' => $order->increment_id]));
  124. $cart->refresh();
  125. $cartItem->refresh();
  126. $cartBillingAddress->refresh();
  127. $cartShippingAddress->refresh();
  128. $orderBillingAddress->refresh();
  129. $orderShippingAddress->refresh();
  130. $order->refresh();
  131. $orderItem->refresh();
  132. $this->assertModelWise([
  133. Cart::class => [
  134. $this->prepareCart($cart),
  135. ],
  136. CartItem::class => [
  137. $this->prepareCartItem($cartItem),
  138. ],
  139. CartPayment::class => [
  140. $this->prepareCartPayment($cartPayment),
  141. ],
  142. CartAddress::class => [
  143. $this->prepareAddress($cartBillingAddress),
  144. ],
  145. CartAddress::class => [
  146. $this->prepareAddress($cartShippingAddress),
  147. ],
  148. CustomerAddress::class => [
  149. $this->prepareAddress($customerAddress),
  150. ],
  151. Order::class => [
  152. $this->prepareOrder($order),
  153. ],
  154. OrderItem::class => [
  155. $this->prepareOrderItem($orderItem),
  156. ],
  157. OrderAddress::class => [
  158. $this->prepareAddress($orderBillingAddress),
  159. $this->prepareAddress($orderShippingAddress),
  160. ],
  161. OrderPayment::class => [
  162. $this->prepareOrderPayment($orderPayment),
  163. ],
  164. ]);
  165. });
  166. it('should cancel the customer order', function () {
  167. // Arrange.
  168. $product = (new ProductFaker([
  169. 'attributes' => [
  170. 5 => 'new',
  171. ],
  172. 'attribute_value' => [
  173. 'new' => [
  174. 'boolean_value' => true,
  175. ],
  176. ],
  177. ]))
  178. ->getSimpleProductFactory()
  179. ->create();
  180. $customer = Customer::factory()->create();
  181. $cart = Cart::factory()->create([
  182. 'customer_id' => $customer->id,
  183. 'customer_first_name' => $customer->first_name,
  184. 'customer_last_name' => $customer->last_name,
  185. 'customer_email' => $customer->email,
  186. 'is_guest' => 0,
  187. ]);
  188. $additional = [
  189. 'product_id' => $product->id,
  190. 'rating' => '0',
  191. 'is_buy_now' => '0',
  192. 'quantity' => '1',
  193. ];
  194. $cartItem = CartItem::factory()->create([
  195. 'cart_id' => $cart->id,
  196. 'product_id' => $product->id,
  197. 'sku' => $product->sku,
  198. 'quantity' => $additional['quantity'],
  199. 'name' => $product->name,
  200. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  201. 'base_price' => $price,
  202. 'total' => $convertedPrice * $additional['quantity'],
  203. 'base_total' => $price * $additional['quantity'],
  204. 'weight' => $product->weight ?? 0,
  205. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  206. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  207. 'type' => $product->type,
  208. 'additional' => $additional,
  209. ]);
  210. $customerAddress = CustomerAddress::factory()->create([
  211. 'cart_id' => $cart->id,
  212. 'customer_id' => $customer->id,
  213. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  214. ]);
  215. $cartBillingAddress = CartAddress::factory()->create([
  216. 'cart_id' => $cart->id,
  217. 'customer_id' => $customer->id,
  218. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  219. ]);
  220. $cartShippingAddress = CartAddress::factory()->create([
  221. 'cart_id' => $cart->id,
  222. 'customer_id' => $customer->id,
  223. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  224. ]);
  225. $cartPayment = CartPayment::factory()->create([
  226. 'cart_id' => $cart->id,
  227. 'method' => $paymentMethod = 'cashondelivery',
  228. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  229. ]);
  230. $order = Order::factory()->create([
  231. 'cart_id' => $cart->id,
  232. 'customer_id' => $customer->id,
  233. 'customer_email' => $customer->email,
  234. 'customer_first_name' => $customer->first_name,
  235. 'customer_last_name' => $customer->last_name,
  236. ]);
  237. $orderItem = OrderItem::factory()->create([
  238. 'product_id' => $product->id,
  239. 'order_id' => $order->id,
  240. 'sku' => $product->sku,
  241. 'type' => $product->type,
  242. 'name' => $product->name,
  243. ]);
  244. $orderBillingAddress = OrderAddress::factory()->create([
  245. 'cart_id' => $cart->id,
  246. 'customer_id' => $customer->id,
  247. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  248. ]);
  249. $orderShippingAddress = OrderAddress::factory()->create([
  250. 'cart_id' => $cart->id,
  251. 'customer_id' => $customer->id,
  252. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  253. ]);
  254. $orderPayment = OrderPayment::factory()->create([
  255. 'order_id' => $order->id,
  256. ]);
  257. // Act and Assert.
  258. $this->loginAsCustomer($customer);
  259. postJson(route('shop.customers.account.orders.cancel', $order->id))
  260. ->assertRedirect();
  261. $cart->refresh();
  262. $cartItem->refresh();
  263. $cartBillingAddress->refresh();
  264. $cartShippingAddress->refresh();
  265. $orderBillingAddress->refresh();
  266. $orderShippingAddress->refresh();
  267. $order->refresh();
  268. $orderItem->refresh();
  269. $this->assertModelWise([
  270. Cart::class => [
  271. $this->prepareCart($cart),
  272. ],
  273. CartItem::class => [
  274. $this->prepareCartItem($cartItem),
  275. ],
  276. CartPayment::class => [
  277. $this->prepareCartPayment($cartPayment),
  278. ],
  279. CartAddress::class => [
  280. $this->prepareAddress($cartBillingAddress),
  281. ],
  282. CartAddress::class => [
  283. $this->prepareAddress($cartShippingAddress),
  284. ],
  285. CustomerAddress::class => [
  286. $this->prepareAddress($customerAddress),
  287. ],
  288. Order::class => [
  289. $this->prepareOrder($order),
  290. ],
  291. OrderItem::class => [
  292. $this->prepareOrderItem($orderItem),
  293. ],
  294. OrderAddress::class => [
  295. $this->prepareAddress($orderBillingAddress),
  296. $this->prepareAddress($orderShippingAddress),
  297. ],
  298. OrderPayment::class => [
  299. $this->prepareOrderPayment($orderPayment),
  300. ],
  301. ]);
  302. });
  303. it('should print the order invoice', function () {
  304. // Arrange.
  305. $product = (new ProductFaker([
  306. 'attributes' => [
  307. 5 => 'new',
  308. ],
  309. 'attribute_value' => [
  310. 'new' => [
  311. 'boolean_value' => true,
  312. ],
  313. ],
  314. ]))
  315. ->getSimpleProductFactory()
  316. ->create();
  317. $customer = Customer::factory()->create();
  318. $cart = Cart::factory()->create([
  319. 'customer_id' => $customer->id,
  320. 'customer_first_name' => $customer->first_name,
  321. 'customer_last_name' => $customer->last_name,
  322. 'customer_email' => $customer->email,
  323. 'is_guest' => 0,
  324. ]);
  325. $additional = [
  326. 'product_id' => $product->id,
  327. 'rating' => '0',
  328. 'is_buy_now' => '0',
  329. 'quantity' => '1',
  330. ];
  331. $cartItem = CartItem::factory()->create([
  332. 'cart_id' => $cart->id,
  333. 'product_id' => $product->id,
  334. 'sku' => $product->sku,
  335. 'quantity' => $additional['quantity'],
  336. 'name' => $product->name,
  337. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  338. 'base_price' => $price,
  339. 'total' => $convertedPrice * $additional['quantity'],
  340. 'base_total' => $price * $additional['quantity'],
  341. 'weight' => $product->weight ?? 0,
  342. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  343. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  344. 'type' => $product->type,
  345. 'additional' => $additional,
  346. ]);
  347. $customerAddress = CustomerAddress::factory()->create([
  348. 'cart_id' => $cart->id,
  349. 'customer_id' => $customer->id,
  350. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  351. ]);
  352. $cartBillingAddress = CartAddress::factory()->create([
  353. 'cart_id' => $cart->id,
  354. 'customer_id' => $customer->id,
  355. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  356. ]);
  357. $cartShippingAddress = CartAddress::factory()->create([
  358. 'cart_id' => $cart->id,
  359. 'customer_id' => $customer->id,
  360. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  361. ]);
  362. $cartPayment = CartPayment::factory()->create([
  363. 'cart_id' => $cart->id,
  364. 'method' => $paymentMethod = 'cashondelivery',
  365. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  366. ]);
  367. $order = Order::factory()->create([
  368. 'cart_id' => $cart->id,
  369. 'customer_id' => $customer->id,
  370. 'customer_email' => $customer->email,
  371. 'customer_first_name' => $customer->first_name,
  372. 'customer_last_name' => $customer->last_name,
  373. ]);
  374. $orderItem = OrderItem::factory()->create([
  375. 'product_id' => $product->id,
  376. 'order_id' => $order->id,
  377. 'sku' => $product->sku,
  378. 'type' => $product->type,
  379. 'name' => $product->name,
  380. ]);
  381. $orderBillingAddress = OrderAddress::factory()->create([
  382. 'cart_id' => $cart->id,
  383. 'customer_id' => $customer->id,
  384. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  385. ]);
  386. $orderShippingAddress = OrderAddress::factory()->create([
  387. 'cart_id' => $cart->id,
  388. 'customer_id' => $customer->id,
  389. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  390. ]);
  391. $orderPayment = OrderPayment::factory()->create([
  392. 'order_id' => $order->id,
  393. ]);
  394. $invoice = Invoice::factory([
  395. 'order_id' => $order->id,
  396. 'state' => 'paid',
  397. ])->create();
  398. $invoiceItem = InvoiceItem::factory()->create([
  399. 'invoice_id' => $invoice->id,
  400. 'order_item_id' => $orderItem->id,
  401. 'name' => $orderItem->name,
  402. 'sku' => $orderItem->sku,
  403. 'qty' => 1,
  404. 'price' => $orderItem->price,
  405. 'base_price' => $orderItem->base_price,
  406. 'total' => $orderItem->price,
  407. 'base_total' => $orderItem->base_price,
  408. 'tax_amount' => (($orderItem->tax_amount / $orderItem->qty_ordered)),
  409. 'base_tax_amount' => (($orderItem->base_tax_amount / $orderItem->qty_ordered)),
  410. 'discount_amount' => (($orderItem->discount_amount / $orderItem->qty_ordered)),
  411. 'base_discount_amount' => (($orderItem->base_discount_amount / $orderItem->qty_ordered)),
  412. 'product_id' => $orderItem->product_id,
  413. 'product_type' => $orderItem->product_type,
  414. 'additional' => $orderItem->additional,
  415. ]);
  416. // Act and Assert.
  417. $this->loginAsCustomer($customer);
  418. getJson(route('shop.customers.account.orders.print-invoice', $invoice->id))
  419. ->assertDownload('invoice-'.$invoice->created_at->format('d-m-Y').'.pdf');
  420. $cart->refresh();
  421. $cartItem->refresh();
  422. $cartBillingAddress->refresh();
  423. $cartShippingAddress->refresh();
  424. $orderBillingAddress->refresh();
  425. $orderShippingAddress->refresh();
  426. $order->refresh();
  427. $orderItem->refresh();
  428. $invoiceItem->refresh();
  429. $this->assertModelWise([
  430. Cart::class => [
  431. $this->prepareCart($cart),
  432. ],
  433. CartItem::class => [
  434. $this->prepareCartItem($cartItem),
  435. ],
  436. CartPayment::class => [
  437. $this->prepareCartPayment($cartPayment),
  438. ],
  439. CartAddress::class => [
  440. $this->prepareAddress($cartBillingAddress),
  441. ],
  442. CartAddress::class => [
  443. $this->prepareAddress($cartShippingAddress),
  444. ],
  445. CustomerAddress::class => [
  446. $this->prepareAddress($customerAddress),
  447. ],
  448. Order::class => [
  449. $this->prepareOrder($order),
  450. ],
  451. OrderItem::class => [
  452. $this->prepareOrderItem($orderItem),
  453. ],
  454. OrderAddress::class => [
  455. $this->prepareAddress($orderBillingAddress),
  456. $this->prepareAddress($orderShippingAddress),
  457. ],
  458. OrderPayment::class => [
  459. $this->prepareOrderPayment($orderPayment),
  460. ],
  461. InvoiceItem::class => [
  462. $this->prepareInvoiceItem($invoiceItem),
  463. ],
  464. ]);
  465. });