I need to make the output the following format XXX-XXXX-XXXX.
I believe the unit tests below are self explanatory. Can anyone help me solve them, I believe they are out of my range of expertise.
public function testMarksNormalizer()
{
$router = $this->createMock(RouterInterface::class);
$security = $this->createMock(Security::class);
$service = new ShipmentService($this->em, $router, $security);
$this->assertEquals('001-0002-0003', $service->normalizeMarks('001-0002-0003'),
'Should not change anything');
$this->assertEquals('001-0002-0003', $service->normalizeMarks('1-2-3'),
'Should normalize when no leading zeroes are given');
$this->assertEquals('001-0002-0003', $service->normalizeMarks('01/2/0003'),
'Should normalize when slashes are used');
$this->assertEquals('001-0002-0003', $service->normalizeMarks('01 2/0003'),
'Should normalize when spaces and slashes are used');
$this->assertEquals('001-0002-0003', $service->normalizeMarks('01-2-0003 EXTRA INFORMATION'),
'Should normalize when extra text is added');
$this->assertFalse($service->normalizeMarks('01-2A-0003'),
'Should fail when non-numerics are inside the marks');
$this->assertEquals('999-0990-9000', $service->normalizeMarks('999/000990/9000'),
'Should normalize when segments have too many leading zeroes');
$this->assertFalse($service->validateMarks('01-2-0003 EXTRA INFORMATION'),
'Should not validate marks with additional text');
$this->assertFalse($service->validateMarks('XT 01-2-0003'),
'Should not validate marks with additional text');
$this->asserttrue($service->validateMarks('01-2-0003'),
'Should not validate marks with additional text');
}
I need to write functions to make the tests pass.
public function normalizeMarks($marks)
{
return $marks;
}
public function validateMarks($marks)
{
$marks = trim($marks);
return preg_match('/^0{0,6}[0-9]{1,3}[\-\/ ]0{0,6}[0-9]{1,4}[\-\/ ]0{0,6}[0-9]{1,4}$/', $marks) === 1;
}
Thanks a lot