Skip to content

Commit

Permalink
test(order): 实现标准订单创建和支付功能测试
Browse files Browse the repository at this point in the history
- 新增 OrderStandardFlowTest 测试类,实现标准订单创建和支付的端到端测试
- 修改 OrderFake 类,增加单位和数量字段,用于测试订单商品信息
- 更新 TestCase 基类,设置测试环境配置和用户认证
- 在 composer.json 中添加 pest 测试命令
- 调整 phpunit.xml 和 testbench.yaml,配置测试环境
  • Loading branch information
liushoukun committed Oct 21, 2024
1 parent f1b071c commit 61e00d5
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 36 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@
],
"lint": [
"@php vendor/bin/phpstan analyse --verbose --ansi"
],
"test": [
"@php vendor/bin/pest"
]
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
}
10 changes: 5 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_HOST" value="mysql"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_PASSWORD" value="root"/>
<env name="DB_DATABASE" value="testing"/>
<env name="DB_CONNECTION" value="mysql"/>
<!-- <env name="DB_HOST" value="mysql"/>-->
<!-- <env name="DB_USERNAME" value="root"/>-->
<!-- <env name="DB_PASSWORD" value="root"/>-->
<!-- <env name="DB_DATABASE" value="testing"/>-->
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>
Expand Down
3 changes: 3 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
providers:
- Workbench\App\Providers\WorkbenchServiceProvider
- RedJasmine\Order\OrderPackageServiceProvider
- RedJasmine\Order\Application\OrderApplicationServiceProvider
- RedJasmine\Ecommerce\EcommercePackageServiceProvider
- RedJasmine\Order\Domain\OrderDomainServiceProvider

migrations:
- workbench/database/migrations
Expand Down
8 changes: 0 additions & 8 deletions tests/Feature/ExampleTest.php

This file was deleted.

21 changes: 0 additions & 21 deletions tests/Feature/Order/Application/CreateCommandTest.php

This file was deleted.

65 changes: 65 additions & 0 deletions tests/Feature/Order/Application/OrderStandardFlowTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'<?php


use RedJasmine\Order\Application\Services\OrderCommandService;
use RedJasmine\Order\Application\UserCases\Commands\OrderCreateCommand;
use RedJasmine\Order\Application\UserCases\Commands\OrderPayingCommand;
use RedJasmine\Order\Domain\Events\OrderPayingEvent;
use RedJasmine\Order\Domain\Models\Enums\OrderTypeEnum;
use RedJasmine\Order\Domain\Models\Order;
use RedJasmine\Order\Domain\Models\OrderPayment;
use RedJasmine\Tests\Feature\Order\Fixtures\OrderFake;


beforeEach(function () {

$this->orderCommandService = app(OrderCommandService::class);
//
});

test('can crate a new order', function () {

$orderFake = new OrderFake();
$orderFake->orderType = OrderTypeEnum::STANDARD;
$command = OrderCreateCommand::from($orderFake->order());


$result = $this->orderCommandService->create($command);

$this->assertInstanceOf(Order::class, $result, '创建订单失败');


$this->order = $result;

return $result;
});


test('cna paying a order', function (Order $order) {


Event::fake();

$command = OrderPayingCommand::from(
[
'id' => $order->id,
'amount' => $order->payable_amount

]

);


$result = $this->orderCommandService->paying($command);

\Illuminate\Support\Facades\Event::assertDispatched(OrderPayingEvent::class, null);

$this->assertInstanceOf(OrderPayment::class, $result, '创建支付记录失败');


return $result;

})->depends('can crate a new order');



9 changes: 8 additions & 1 deletion tests/Feature/Order/Fixtures/OrderFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class OrderFake
public int $productCount = 3;


public int $unit = 1;
public string $unit = '';
public int $unitQuantity = 1;

public function order(array $order = []) : array
{
Expand All @@ -45,6 +46,10 @@ public function fakeOrderArray(array $order = []) : array
{
$user = User::find(1);





$fake = [
'buyer' => [
'type' => $user->getType(),
Expand Down Expand Up @@ -139,6 +144,7 @@ public function fakeProductArray(array $product = []) : array
'barcode' => fake()->ean13(),
'num' => fake()->numberBetween(1, 10),
'unit' => $this->unit,
'unit_quantity' => $this->unitQuantity,
'price' => fake()->randomFloat(2, 90, 100),
'cost_price' => fake()->randomFloat(2, 70, 80),
'tax_amount' => fake()->randomFloat(2, 10, 20),
Expand All @@ -152,6 +158,7 @@ public function fakeProductArray(array $product = []) : array
'buyer_extends' => [],
'other_extends' => [],
'tools' => [],
'form' => [],
];
return array_merge($fake, $product);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,44 @@

namespace RedJasmine\Tests;

use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Workbench\App\Models\User;

abstract class TestCase extends \Orchestra\Testbench\TestCase
{

//
use WithWorkbench;


protected function setUp() : void
{
parent::setUp(); // TODO: Change the autogenerated stub

$this->actingAs(User::find(1));
}


/**
* Automatically enables package discoveries.
*
* @var bool
*/
protected $enablesPackageDiscoveries = true;


protected function defineEnvironment($app)
{

tap($app['config'], function (Repository $config) {
$config->set('app.locale', 'zh_CN');
$config->set('app.faker_locale', 'zh_CN');
});

}

/**
* Get the application timezone.
*
Expand Down

0 comments on commit 61e00d5

Please sign in to comment.