Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bobbybouwmann's avatar

How to test custom FormBuilder

Hi guys,

So I created a custom FormBuilder based on the FormBuilder from the Collective package: http://laravelcollective.com/docs/5.0/html

The FormBuilder will only return html so I was thinking about a unit test. Then I can compare the output on correctness.

So I have a function like this:

<?php namespace App\Services\Html;

class FormBuilder extends \Collective\Html\FormBuilder {

    public function destroy($value = null, $options = [])
    {
        $options['class'] = 'btn btn-cons btn-danger ' . (isset($options['class']) ? $options['class'] : '');

        return parent::submit($value, $options);
    }

}

What is the best approache on testing this? If you need anything please let me know!

Many thanks :D

0 likes
5 replies
MarkRedeman's avatar

There is a screencast explaining how extend the form builder and test it using phpspec (you can of course also use phpunit).

bobbybouwmann's avatar

@MarkRedeman thanks for your answer. I do know about the screencast, but PHPUnit works a bit different.. I could of course use phpspec as well to give myself a headstart, but I would also like to know how to do this with PHPUnit

bobbybouwmann's avatar

Thanks for the tip, I will check that class out and see if I can use it in my advantage ;)

bobbybouwmann's avatar

@iakio thank you for helping me out, this gave a good headstart on my tests and the first few test are written already ;)

Please or to participate in this conversation.