Php 伪造JSON数据的最佳实践Laravel 5.2
嗨,我正在为我用Laravel构建的一个小API编写测试。我有数据通过前端的axios post请求进入API,我用以下内容伪造数据Php 伪造JSON数据的最佳实践Laravel 5.2,php,laravel,phpunit,Php,Laravel,Phpunit,嗨,我正在为我用Laravel构建的一个小API编写测试。我有数据通过前端的axios post请求进入API,我用以下内容伪造数据 public function test_that_the_form_json_data_structure_is_correct() { $lead = [ 'first_name' => 'John', 'last_name' => 'Doe', 'email' => 'johndoe
public function test_that_the_form_json_data_structure_is_correct()
{
$lead = [
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'johndoe@example.com',
'phone' => '000-000-0000',
'street_address' => '123 Main Street',
'city' => 'CityA',
'state' => 'ZZ',
'zip' => '928171',
'spouse' => [
'name' => 'Sarah Aims',
'email' => 'sarahaims@example.com',
'phone' => '000-000-0000',
]
];
$quote = [
'address_is_same' => 1,
'property_street' => '123 Main Street',
'property_city' => 'CityA',
'property_state' => 'ZZ',
'property_zip' => '928171',
'primary_residence' => 1,
'secondary_residence' => 0,
'rental_property' => 0,
'number_of_units' => 2,
'losses' => 0,
'explain' => 'Some explaination here...',
'additional_comments' => 'Additional comments here...'
];
$this->json('POST', '/get-a-quote/home', [
'data' => [
'lead' => $lead,
'quote' => $quote
]
])->seeJsonStructure([
'lead_id',
'quote_id'
]);
}
我意识到我需要在多个测试中为前端的不同形式编写$lead数组变量。我知道Laravel中的模型工厂是专门为类设计的,所以我想知道处理JSON数据的最佳实践是什么,这些JSON数据的格式与您的模型不同,以及如何对其进行最佳测试。我在想,我需要测试实际输入的数据,这就是您在上面看到的数据,然后为API编写一个测试,测试它对输入数据的处理,即创建模型等。您的测试与您的正常代码库一样。干燥原理仍然适用 所以我想首先推荐使用Laravel。例如,我猜您的$lead变量是一个用户模型,在这种情况下,您可以执行以下操作: $lead=factoryUser::class->make->toArray 这将以数组的形式返回完整的用户模型 但是你说: 使用与模型格式不同的JSON数据
因此,在这种情况下,我只需要在测试中使用助手。如果您需要在任何地方都使用它,您甚至可以使用静态方法创建一个类来完全返回数据,然后您可以更改需要在测试中更改的字段。好的,是的,这就是我的想法。使用静态方法创建帮助器类。对,数据的格式不一样。