the browser-facing portion of osu!
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add Order reserveItems test

bakaneko f4f57572 c647d398

+65 -30
+4 -4
app/Models/Store/Order.php
··· 599 599 { 600 600 // locking bottleneck 601 601 $this->getConnection()->transaction(function () { 602 - [$items, $products] = $this->lockForReserve(); 602 + $items = $this->lockForReserve(); 603 603 604 604 $items->each->releaseProduct(); 605 605 }); ··· 609 609 { 610 610 // locking bottleneck 611 611 $this->getConnection()->transaction(function () { 612 - [$items, $products] = $this->lockForReserve(); 612 + $items = $this->lockForReserve(); 613 613 $items->each->reserveProduct(); 614 614 }); 615 615 } ··· 683 683 684 684 $items = $query->get(); 685 685 $productIds = array_pluck($items, 'product_id'); 686 - $products = Product::lockForUpdate()->whereIn('product_id', $productIds)->get(); 686 + Product::lockForUpdate()->whereIn('product_id', $productIds)->get(); 687 687 688 - return [$items, $products]; 688 + return $items; 689 689 } 690 690 691 691 private function removeOrderItem(array $params)
+5
database/factories/Store/OrderItemFactory.php
··· 26 26 ]; 27 27 } 28 28 29 + public function reserved(): static 30 + { 31 + return $this->state(['reserved' => true]); 32 + } 33 + 29 34 public function supporterTag(): static 30 35 { 31 36 return $this->state([
+16 -26
tests/Models/Store/OrderItemTest.php
··· 18 18 */ 19 19 public function testDelete(string $status, ?string $expectedException) 20 20 { 21 - [$product, $orderItem] = $this->createProductAndOrderItem(5, false); 21 + [, $orderItem] = $this->createProductAndOrderItem(5, false); 22 22 23 23 $orderItem->order->update(['status' => $status]); 24 24 ··· 35 35 { 36 36 [$product, $orderItem] = $this->createProductAndOrderItem(5, false); 37 37 38 + $this->expectCountChange(fn () => $product->fresh()->stock, -2); 39 + 38 40 $orderItem->reserveProduct(); 39 41 40 - $orderItem->refresh(); 41 - $product->refresh(); 42 - 43 - $this->assertTrue($orderItem->reserved); 44 - $this->assertSame($product->stock, 3); 42 + $this->assertTrue($orderItem->fresh()->reserved); 45 43 } 46 44 47 45 public function testReserveReservedProduct() 48 46 { 49 47 [$product, $orderItem] = $this->createProductAndOrderItem(5, true); 50 48 49 + $this->expectCountChange(fn () => $product->fresh()->stock, 0); 50 + 51 51 $orderItem->reserveProduct(); 52 52 53 - $orderItem->refresh(); 54 - $product->refresh(); 55 - 56 - $this->assertTrue($orderItem->reserved); 57 - $this->assertSame($product->stock, 5); 53 + $this->assertTrue($orderItem->fresh()->reserved); 58 54 } 59 55 60 56 public function testReleaseUnreservedProduct() 61 57 { 62 58 [$product, $orderItem] = $this->createProductAndOrderItem(5, false); 63 59 64 - $orderItem->releaseProduct(); 60 + $this->expectCountChange(fn () => $product->fresh()->stock, 0); 65 61 66 - $orderItem->refresh(); 67 - $product->refresh(); 62 + $orderItem->releaseProduct(); 68 63 69 - $this->assertFalse($orderItem->reserved); 70 - $this->assertSame($product->stock, 5); 64 + $this->assertFalse($orderItem->fresh()->reserved); 71 65 } 72 66 73 67 public function testReleaseReservedProduct() 74 68 { 75 69 [$product, $orderItem] = $this->createProductAndOrderItem(5, true); 76 70 71 + $this->expectCountChange(fn () => $product->fresh()->stock, 2); 72 + 77 73 $orderItem->releaseProduct(); 78 74 79 - $orderItem->refresh(); 80 - $product->refresh(); 81 - 82 - $this->assertFalse($orderItem->reserved); 83 - $this->assertSame($product->stock, 7); 75 + $this->assertFalse($orderItem->fresh()->reserved); 84 76 } 85 77 86 78 public function testReserveInsufficientStock() ··· 95 87 { 96 88 [$product, $orderItem] = $this->createProductAndOrderItem(0, true); 97 89 98 - $orderItem->releaseProduct(); 90 + $this->expectCountChange(fn () => $product->fresh()->stock, 0); 99 91 100 - $orderItem->refresh(); 101 - $product->refresh(); 92 + $orderItem->releaseProduct(); 102 93 103 - $this->assertFalse($orderItem->reserved); 104 - $this->assertSame($product->stock, 0); 94 + $this->assertFalse($orderItem->fresh()->reserved); 105 95 } 106 96 107 97 public static function deleteDataProvider()
+40
tests/Models/Store/OrderTest.php
··· 47 47 OrderItem::each(fn ($item) => $this->assertFalse($item->extra_data->hidden)); 48 48 } 49 49 50 + public function testReserveItems() 51 + { 52 + $productFactory = Product::factory()->state(['stock' => 5, 'max_quantity' => 5]); 53 + $orderItemFactory = OrderItem::factory(); 54 + 55 + $product1 = $productFactory->create(); 56 + $product2 = $productFactory->create(); 57 + 58 + $order = Order::factory() 59 + ->has($orderItemFactory->state(['product_id' => $product1, 'quantity' => 2]), 'items') 60 + ->has($orderItemFactory->state(['product_id' => $product2, 'quantity' => 1]), 'items') 61 + ->create(); 62 + 63 + $this->expectCountChange(fn () => $product1->fresh()->stock, -2); 64 + $this->expectCountChange(fn () => $product2->fresh()->stock, -1); 65 + $this->expectCountChange(fn () => $order->fresh()->items()->where('reserved', true)->count(), 2); 66 + 67 + $order->reserveItems(); 68 + } 69 + 70 + public function testReserveItemsAlreadyReserved() 71 + { 72 + $productFactory = Product::factory()->state(['stock' => 5, 'max_quantity' => 5]); 73 + $orderItemFactory = OrderItem::factory()->reserved(); 74 + 75 + $product1 = $productFactory->create(); 76 + $product2 = $productFactory->create(); 77 + 78 + $order = Order::factory() 79 + ->has($orderItemFactory->state(['product_id' => $product1, 'quantity' => 2]), 'items') 80 + ->has($orderItemFactory->state(['product_id' => $product2, 'quantity' => 1]), 'items') 81 + ->create(); 82 + 83 + $this->expectCountChange(fn () => $product1->fresh()->stock, 0); 84 + $this->expectCountChange(fn () => $product2->fresh()->stock, 0); 85 + $this->expectCountChange(fn () => $order->fresh()->items()->where('reserved', true)->count(), 0); 86 + 87 + $order->reserveItems(); 88 + } 89 + 50 90 public function testSwitchOrderItemReservation() 51 91 { 52 92 $product1 = Product::factory()->create(['stock' => 5, 'max_quantity' => 5]);