You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

143 lines
4.6 KiB

  1. <?php declare(strict_types=1);
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2020/5/5
  6. * Time: 10:38 PM
  7. */
  8. namespace tests\unishop\controller;
  9. use addons\unishop\controller\Cart;
  10. use addons\unishop\extend\PhpunitFunctionCustomize;
  11. use addons\unishop\model\Product;
  12. use PHPUnit\Framework\TestCase;
  13. class CartTest extends TestCase
  14. {
  15. use PhpunitFunctionCustomize;
  16. /**
  17. * @test
  18. */
  19. public function getProduct()
  20. {
  21. $products = (new Product)->where(['switch' => Product::SWITCH_ON])->field('id,specTableList,use_spec')->select();
  22. if ($products) {
  23. return collection($products)->append(['spec_table_list'])->toArray();
  24. }
  25. return [];
  26. }
  27. /**
  28. * @test
  29. * @depends getProduct
  30. */
  31. public function add(array $products)
  32. {
  33. $this->userLogin();
  34. foreach ($products as $product) {
  35. $params['id'] = $product['product_id'];
  36. if ($product['use_spec'] == Product::SPEC_ON) {
  37. foreach ($product['spec_table_list'] as $row) {
  38. $params['spec'] = implode(',', $row['value']);
  39. $contents = $this->request(Cart::class, 'add', $params, 'get');
  40. $this->assertArrayHasKey('code', $contents);
  41. $this->assertArrayHasKey('data', $contents);
  42. }
  43. } else {
  44. $contents = $this->request(Cart::class, 'add', $params, 'get');
  45. $this->assertArrayHasKey('code', $contents);
  46. $this->assertArrayHasKey('data', $contents);
  47. }
  48. }
  49. }
  50. /**
  51. * @test
  52. */
  53. public function index()
  54. {
  55. $contents = $this->request(Cart::class, 'index');
  56. $this->assertSame(1, $contents['code']);
  57. $this->assertIsArray($contents['data']);
  58. if (empty($contents['data'])) {
  59. $this->assertEmpty($contents['data']);
  60. } else {
  61. foreach ($contents['data'] as $item) {
  62. $this->assertGreaterThanOrEqual(0, $item['market_price']);
  63. $this->assertGreaterThanOrEqual(0, $item['sales_price']);
  64. $this->assertGreaterThanOrEqual(0, $item['stock']);
  65. $this->assertGreaterThanOrEqual(0, $item['sales']);
  66. $this->assertNotEmpty($item['image']);
  67. $this->assertNotEmpty($item['title']);
  68. $this->assertGreaterThanOrEqual(0, $item['choose']);
  69. $this->assertGreaterThanOrEqual(0, $item['isset']);
  70. $this->assertNotEmpty($item['cart_id']);
  71. $this->assertArrayHasKey('spec', $item);
  72. $this->assertNotEmpty($item['number']);
  73. $this->assertGreaterThanOrEqual(0, $item['oldPrice']);
  74. $this->assertGreaterThanOrEqual(0, $item['nowPrice']);
  75. $this->assertNotEmpty($item['product_id']);
  76. }
  77. }
  78. return $contents['data'];
  79. }
  80. /**
  81. * @test
  82. * @depends index
  83. */
  84. public function number_change(array $carts)
  85. {
  86. foreach ($carts as $cart) {
  87. $this->assertGreaterThanOrEqual(1, $cart['stock']);
  88. $number = mt_rand(1, intval($cart['stock']));
  89. $contents = $this->request(Cart::class, 'number_change', ['id' => $cart['cart_id'], 'number' => $number], 'get');
  90. if ($number == $cart['number']) {
  91. $this->assertEquals(0, $contents['code']);
  92. } else {
  93. $this->assertEquals(1, $contents['code']);
  94. $this->assertEquals($number, $contents['data']);
  95. }
  96. }
  97. }
  98. /**
  99. * @test
  100. * @depends index
  101. */
  102. public function choose_change(array $carts)
  103. {
  104. if (count($carts) > 0) {
  105. $arr = implode(',', array_column($carts, 'cart_id'));
  106. $contents = $this->request(Cart::class, 'choose_change', ['falseArr' => $arr], 'post');
  107. $this->assertSame(1, $contents['code']);
  108. $this->assertSame(1, $contents['data']);
  109. $contents = $this->request(Cart::class, 'choose_change', ['trueArr' => $arr], 'post');
  110. $this->assertSame(1, $contents['code']);
  111. $this->assertSame(1, $contents['data']);
  112. }
  113. }
  114. /**
  115. * @test
  116. * @depends index
  117. */
  118. public function delete(array $carts)
  119. {
  120. if (count($carts) > 0) {
  121. $arr = implode(',', array_column($carts, 'cart_id'));
  122. $contents = $this->request(Cart::class, 'delete', ['id' => $arr]);
  123. $this->assertSame(1, $contents['code']);
  124. $this->assertSame(1, $contents['data']);
  125. }
  126. }
  127. }