You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单元测试POST请求传入FALSE,数据库存储为TRUE的问题排查

问题描述

单元测试中发送POST请求时,type字段(数据库为tinyint类型)传入值false,但最终数据库中该字段被存储为1(即true),此问题已多次出现。


POST请求数据

[
  "title" => "Une fausse formation",
  "number" => "faux-numer0",
  "organism" => "Un faux organisme",
  "type" => false,
  "status" => 0,
  "costs" => array:2 [
    0 => array:4 [
      "title" => "Frais pédagogiques",
      "type" => 0,
      "amount" => 102.5,
      "isCollective" => true
    ]
    1 => array:4 [
      "title" => "Frais de transport",
      "type" => 0,
      "amount" => 10,
      "isCollective" => true
    ]
  ],
  "sessions" => [],
  "visibility" => 2
]

数据库插入后的数据

[
  "title" => "Une fausse formation",
  "number" => "faux-numer0",
  "type" => true,
  "status" => 0,
  "organism" => "Un faux organisme",
  "visibility" => array:2 [
    "establishment" => [],
    "employees" => array:1 [
      0 => []
    ]
  ],
  "costs" => array:2 [
    0 => array:4 [
      "title" => "Frais pédagogiques",
      "type" => 0,
      "amount" => "102.50",
      "is_collective" => true
    ]
    1 => array:4 [
      "title" => "Frais de transport",
      "type" => 0,
      "amount" => "10.00",
      "is_collective" => true
    ]
  ]
]

实体类字段声明

/**
 * @var bool
 *
 * @ORM\Column(name="`type`", type="boolean")
 * @Serializer\Groups({"get_employee", "get_training", "get_trainings", "get_training_user", "get_training_session"})
 */
private $type;

POST测试请求代码

public function testPostTraining($user, $data, $result)
{
    $this->authUser($user[0], $user[1], $user[2]);

    // Retrieve created visibility id
    $visibilityData = array(
        'establishment' => 1
    );
    $this->apiCall('POST', '/api/secure/trainings/visibilities.json', $visibilityData);
    // Check status code (visibility created)
    $this->assertEquals(201, $this->client->getResponse()->getStatusCode());
    $data['visibility'] = json_decode($this->client->getResponse()->getContent(), true)['id'];
    $this->apiCall('POST', '/api/secure/trainings.json', $data);

   // Check status code (created)
    $this->assertEquals(201, $this->client->getResponse()->getStatusCode());
    // Check if id is returned
    $response = json_decode($this->client->getResponse()->getContent(), true);
    
    $this->assertArrayHasKey('id', $response);
    //$this->assertInternalType('integer', $response['id']);
    $this->assertEquals('integer', gettype($response['id']));
    
    // Check data
    $this->apiCall('GET', '/api/secure/trainings/'.$response['id'].'.json');
    $training = json_decode($this->client->getResponse()->getContent(), true);
    // Remove ids
    $this->recursive_unset($training, 'id');
    $this->recursive_unset($training, 'created_at');
    $this->recursive_unset($training, 'updated_at');
    $this->recursive_unset($training['visibility']['establishment'], 'roles');

    // Check content
    $this->assertEquals($training, $result);
}

POST请求数据(测试用例二)

$data_two= array(
    'title'             =>  'Une vrai formation',
    'number'            =>  'vrai-numer0',
    'organism'          =>  'Un vrai organisme',
    'type'              =>  false,
    'status'            =>  1,
    'costs'             =>  array(
        array(
            'title'     =>  'Frais pédagogiques',
            'type'      =>  1,
            'amount'    =>  10.25,
            'isCollective' => true
        ),
    ),
    'sessions'          =>  array()
);

POST控制器代码

public function postAction(Request $request)
{
    try {
        if (($employee = $this->getEmployee()) == null)
            return FOSView::create(null, Response::HTTP_FORBIDDEN);

        $manager = $this->getTrainingManager();
        $training = $manager->createTraining();
        $form = $this->createForm(TrainingType::class, $training, array(
            'method' => $request->getMethod()
        ));
        $this->removeExtraFields($request, $form);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->denyAccessUnlessGranted('create', $training);
            $manager->save($training);

            $dispatcher = $this->eventDisptacher;
            $event = new TrainingActionEvent($training, $this->getEmployee(), $request->getMethod());
            $dispatcher->dispatch($event,TrainingActionEvent::TRAINING_ACTION);

            return $training;
        }
    } catch(\Exception $e) {
        return FOSView::create($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
    }

    return FOSView::create($form, Response::HTTP_BAD_REQUEST);
}

内容的提问来源于stack exchange,提问作者oracle972

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:40:47