我无法为我正在处理的一段代码编写测试。我有两个表,我正在尝试为其创建一个联结表,如下所示:
CREATE TABLE charges (
id bigint PRIMARY KEY,
amount float,
some_data hstore
)
CREATE TABLE shipments (
id bigint PRIMARY KEY,
weight float,
some_data hstore
)
CREATE TABLE distributed_details (
charges_id bigint references charges (id),
shipments_id biging references shipments (id),
amount float,
another_calculated_field varchar
)
根据some_data
字段中的某些标准,我必须提取一组费用和一组货物,charges.amount
并按shipments.weight
. 结果将被存储distributed_details
。在进行此分配时,我还必须根据 中的剩余内容执行一些其他计算,some_data
这些内容将存储在distributed_details.another_calculated_field
.
class Distributor
{
public $detailMapper;
public function distribute($charges, $shipments)
{
foreach ($charges as $charge) {
$distributedAmounts = $this->calculateDistributedAmounts($charge->getAmount(), $shipments);
foreach ($shipments as $shipment) {
$distributed_detail = $this->buildDistributedDetail($charge, $shipment, array_shift($distributedAmounts));
$this->detailMapper->save($distributed_detail);
}
}
}
public function calculateDistributedAmounts($shipments) {}
public function buildDistributedDetail($charge, $shipment, $distributedAmount) {}
}
是否有一种测试此功能的好方法,并确保这些分配的金额实际被提取并分配给每条记录?出于内存限制的原因,我已将每个细节的持久性委托给此方法中的 detailMapper - 我有时必须提取数万个货物,并且将所有产生的费用返回到一个数组中会使我的内存不足。