Daniel1836's avatar

Need help with function to pass unit test

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

0 likes
6 replies
Daniel1836's avatar

Thank for the input. At least can anyone help move me in the direction of the solution so I have a framework to experiment on? Because now I don't know where to start. Should I use regex?

martinbean's avatar

@daniel1836 I don’t really understand what you’re asking. You have a test case. So surely you’re writing tests for code you’ve already written?

Daniel1836's avatar

No, the tests were written for me. I am relatively junior as a dev.

Lets just take this one case.

$this->assertEquals('001-0002-0003', $service->normalizeMarks('1-2-3'),
            'Should normalize when no leading zeroes are given');

I need to verify that it is in the form XXX-XXXX-XXXX And I need to add zeroes. What approach can I take in my function to accomplish this?

eggplantSword's avatar

@Daniel1836 Just straight off my head I would start by exploding the string to separate the pieces and validate each one, for example you could explode using a -, a / or a space and maybe check the length of each section to see if you need to add zeros or not. Start with one assert and work from there.

kokoshneta's avatar

I don’t think the last test is self-explanatory – it makes no sense to me. What additional text is in 01-2-0003? Surely, if the other tests are anything to go by, we’d expect “Should validate/normalize when too few leading zeros are given” or something like that.

(And I presume it should be assertTrue, not asserttrue?)

Please or to participate in this conversation.