1

I'm having trouble with using PHPUnit to test my Laravel package that I am writing. I am writing a class which extends the formbuilder, I have tried it against my own class and the parent class, (Illuminate\Html\Formbuilder), but I am getting the same error.

My test is

use Illuminate\Html\FormBuilder as Form;

class FormBuilderTest extends PHPUnit_Framework_TestCase {
    function test_basic_input() {
        $html = Form::text('test');

        $this->assertContains('input', $html);
    }
}

This fails with the following message

Non-static method Illuminate\Html\FormBuilder::text() should not be called statically, assuming $this from incompatible context

I can't figure out what is going on with it, as far as I can tell this is the same static call that is made from blade in the framework.

Can anyone point me in the write direction with this one?

3 Answers 3

1

It's not a static method as the error indicates.

Try:

$form = new Form;

$html = $form->text('test');

But I'm not familiar with Laravel, or with Illuminate\Html\FormBuilder and I'm not sure if it has a constructor that requires some options..

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you alias FormBuilder to Form. That's only half correct. Because the Form class is the facade to access the FormBuilder and if you want to call it with a static call Form::text() you need to use the actual facade.

That means just remove

use Illuminate\Html\FormBuilder as Form;

And in case you have "namespace problems" add this use statement instead

use Illuminate\Support\Facades\Form;

Alternatively you can also get a FormBuilder instance by using $this->app

$form = $this->app['form'];
$html = $form->text();

Comments

0

I've figured out where I went wrong with this, I was running phpunit from the workbench directory in Laravel. This wasn't picking up the necessary classes and I was trying to change my class to get around this. I stored my test in the workbench/tests directory and pointed phpunit.xml to it, but ran it from the root Laravel project directory. My class now looks much simpler. Thanks for the help guys.

<?php
class FormBuilderTest extends TestCase {
    function test_basic_input() {
        $html = Form::text('text');

        $this->assertContains('input', $html);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.