CaiWuXiTong/tests/Unit/SupplierContractSigningPositionsTest.php

62 lines
3.3 KiB
PHP

<?php
namespace Tests\Unit;
use App\Models\Business\CourierAgreementSignature;
use App\Support\CourierAgreementPdf;
use App\Support\SupplierCooperationTemplates;
use DOMElement;
use Illuminate\Foundation\Testing\TestCase;
use Tests\CreatesApplication;
class SupplierContractSigningPositionsTest extends TestCase
{
use CreatesApplication;
/** @dataProvider templates */
public function test_every_body_and_appendix_seal_tracks_its_actual_rendered_counterparty_box(string $template, bool $longValues): void
{
$metadata = SupplierCooperationTemplates::TEMPLATES[$template];
$payload = [
'partner_type' => 'supplier', 'supplier_contract_template' => $template,
'cooperation_party_a_name' => '示例甲方有限公司',
'cooperation_party_b_name' => $longValues ? str_repeat('较长企业名称', 12) : '示例乙方有限公司',
'signature_date' => '2026-10-01',
'supplier_contract_fields' => ['number' => 'SUP-FIXED-SEALS', 'location' => '常州市武进区',
'party_b_address' => $longValues ? str_repeat('较长的地址内容', 30) : '示例地址'],
];
$signature = new CourierAgreementSignature(['document_type' => 'cooperation-contract',
'business_no' => 'AGR-POSITIONS', 'template_code' => $metadata['code'], 'payload' => $payload]);
$pdf = app('dompdf.wrapper');
$this->app->instance('dompdf.wrapper', $pdf);
$boxes = [];
$pdf->getDomPDF()->setCallbacks([['event' => 'end_frame', 'f' => static function ($frame, $canvas) use (&$boxes): void {
$node = $frame->get_node();
if ($node instanceof DOMElement && $node->getAttribute('data-supplier-signature-party') === 'b') {
$boxes[] = ['page' => $canvas->get_page_number(), 'box' => $frame->get_position(),
'width' => $frame->get_margin_width(), 'height' => $frame->get_margin_height()];
}
}]]);
$rendered = app(CourierAgreementPdf::class)->renderForSigning($signature);
$this->assertStringStartsWith('%PDF-', $rendered['bytes']);
$this->assertCount(1 + $metadata['attachments'], $rendered['signing_positions']);
$this->assertCount(count($rendered['signing_positions']), $boxes);
foreach ($rendered['signing_positions'] as $index => $position) {
$box = $boxes[$index];
$this->assertSame((string) $box['page'], $position['positionPage']);
$this->assertGreaterThan(90, $box['height'], 'A full 120px seal needs enough vertical clearance.');
$this->assertGreaterThan(300, $position['positionX'], 'Only the counterparty column receives signing fields.');
$this->assertEqualsWithDelta($box['box']['x'] + $box['width'] / 2, $position['positionX'], 0.01);
$this->assertEqualsWithDelta($pdf->getDomPDF()->getCanvas()->get_height() - $box['box']['y'] - $box['height'] / 2, $position['positionY'], 0.01);
$this->assertGreaterThan(48, $position['positionY']);
$this->assertLessThan(790, $position['positionY']);
}
$this->app->forgetInstance('dompdf.wrapper');
}
public static function templates(): array
{
return [['dropshipping', false], ['consignment', false], ['production', false], ['dropshipping', true]];
}
}