/** * @param $job * @return BadRequestResponse|null */ private function validateJob($job) { if ($this->getParameter('abc.job.rest.validate')) { $errors = $this->getValidator()->validate($job); if (count($errors) > 0) { $response = new BadRequestResponse('Invalid request', 'The request contains invalid job parameters'); $response->setErrors($errors); return $response; } } return null; }
public function testUpdateActionWithValidationEnabled() { $parameters = ['type' => 'JobType']; $job = new Job(); $request = new Request([], $parameters); $this->container->expects($this->once())->method('getParameter')->with('abc.job.rest.validate')->willReturn(true); $this->serializer->expects($this->once())->method('deserialize')->with(json_encode($parameters, true), Job::class, 'json')->willReturn($job); $this->validator->expects($this->once())->method('validate')->with($job)->willReturn(['error']); $expectedResponse = new BadRequestResponse('Invalid request', 'The request contains invalid job parameters'); $expectedResponse->setErrors(['error']); $this->manager->expects($this->never())->method('add'); $this->serializer->expects($this->once())->method('serialize')->with($this->equalTo($expectedResponse), 'json')->willReturn('data'); $response = $this->subject->addAction($request); $this->assertEquals(400, $response->getStatusCode()); }