getContainer()->get('validator'); $violations = $validator->validate($entity); if (!is_array($fieldNames)) { $fieldNames = [$fieldNames]; } $expected = count($fieldNames); $actual = $violations->count(); $this->assertEquals($expected, $actual, sprintf('Expected %s violations, found %s.', $expected, $actual)); $violatedFields = []; /** @var ConstraintViolationInterface $validation */ foreach ($violations as $validation) { $violatedFields[$validation->getPropertyPath()] = $validation->getPropertyPath(); } foreach ($fieldNames as $id => $propertyPath) { $foundField = false; if (in_array($propertyPath, $violatedFields)) { $foundField = true; unset($violatedFields[$propertyPath]); } $this->assertTrue($foundField, 'Failed finding violation for field: ' . $propertyPath); } $this->assertEmpty($violatedFields, sprintf('Unexpected violations found: %s', implode(', ', $violatedFields))); } protected function assertHasNoViolations($entity) { self::bootKernel(); $validator = static::$kernel->getContainer()->get('validator'); $violations = $validator->validate($entity); $actual = $violations->count(); $this->assertEquals(0, $actual, sprintf('Expected 0 violations, found %s.', $actual)); } }