0

Hi I just download phpunit.phar in my laravel project (which I already developed partly). I started by doing a simple

php phpunit.phar

but it outputs things like whole viewfiles with at the end:

 {"error":{"type":"ErrorException","message":"Undefined variable: currency","file":"C:\\wamp\\www\\project.dev\\app\\views\\front\\settings.php","line":79}}

This is not even a test and the code works when testing out in the browser. I am also using the default phpunit configuration file shipped with laravel.

EDIT: the code

<label class="radio">
<input type="radio" name="settings_currency" id="set_curr_<?=$currency->id?>" value="<?=$currency->id?>" checked>
<span class="symbol"><?=$currency->symbol?></span>
<?=$currency->name?>
<span><?=$currency->abbr?></span>
</label>
Alexander Cogneau
  • 1,286
  • 4
  • 12
  • 25
  • So is `currency` defined? What are your error level settings when run normally? Show the code – Mark Baker Aug 09 '13 at 20:19
  • @MarkBaker it is when you are logged in, and that's the only time the code is used. PHPunit however parses it anyway apparently – Alexander Cogneau Aug 09 '13 at 20:21
  • 1
    If that's all the code, then $currency isn't defined. Show the method definition in your code. Where is your method getting it from? If it's external to the method, are you mocking it for your test? – Mark Baker Aug 09 '13 at 20:23
  • @MarkBaker it is passed as a view variable with Laravel, and there are not even tests yet. – Alexander Cogneau Aug 09 '13 at 20:28
  • http://laravel.com/docs/testing: *"Laravel is built with unit testing in mind. In fact, support for testing with PHPUnit is included out of the box, and a phpunit.xml file is already setup for your application. "* - Maybe you complain that it's pre-configured and you're now caught in the act of using ***Undefined Variables***? – hakre Aug 10 '13 at 08:57

2 Answers2

0

You cannot simply run PHPUnit and expect it to test something useful. You have to configure it!

When run, PHPUnit scans the current directory and all subdirectories for PHP files, includes them and checks if there are any test classes defined that can be run. Including your view files will execute them without context, i.e. you'll get the complete echo and notices from any used, but currently undefined variables.

As a starter, create a "phpunit.xml" in the same directory you are starting PHPUnit. Read the documentation on how to create it's contents. First and foremost you need to specify which directory to scan for test classes with the <testsuites> element.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • If you have read my question, you would have seen that I use the default phpunit.xml shipped with Laravel, it already has testsuites defined. https://github.com/laravel/laravel/blob/master/phpunit.xml is the file – Alexander Cogneau Aug 09 '13 at 20:34
  • No, there is absolutely no reference telling me that you want to execute the laravel test suite. All I see is that you call PHPUnit "somewhere", and I guessed it must have been in the top level directory of your project. Please add more info to your question. – Sven Aug 09 '13 at 20:38
  • I executed php phpunit.phar in the directory where phpunit.xml is, so that should work. – Alexander Cogneau Aug 09 '13 at 20:44
  • @AlexanderCogneau: Like Sven wrote, you can not expect it to work that way. It's like you jump into a rocket and try to fly it and then you wonder when it explodes under you seat or crashes back on the surface yelling: But it's a rocket that was for flying to the space station and I did nothing to it. So please, don't be such an idiot here on stackoverflow. And respect users leaving an answer. Thank you. – hakre Aug 10 '13 at 08:53
  • Also what Sven means is, from the commandline you give in your question, it is not clear which phpunit.xml exactly is being executed (nor from which working directory you're executing the command). So the feedback he gives is totally correct. This is no fight, so try to understand what others are trying to say to you, and if you don't understand why they talk that way, confront by asking, not by running wild assumptions like "that should work" - I mean obviously you complain about that it does not work. Always add more information, don't stop and run in circles. – hakre Aug 10 '13 at 09:05
  • And I think stating that tests need to be configured is true even IF there already is an empty phpunit.xml file with no tests. I'd really like to see some CLI output that is generated, like this: `php phpunit.phar --debug` – Sven Aug 10 '13 at 09:07
0

the code works when testing out in the browser.

Well, as you might can imagine, only having some "code working in the browser" must not mean the code is error-free.

See the error message again you get:

Undefined variable: currency

So what you experience here can be explained shortly as: PHPUnit is more strict with your code than you are. It will mark warnings you didn't care about in the past, like in this example Undefined Variables.

So now learn about how to define variables and why using Undefined Variables is certainly your Problem and then you will easily understand as well why PHPUnit checks for those.

You can as well lower your expectations by lowering the quality standards PHPUnit applies when running tests by configuring it: Appendix C. The XML Configuration File

However, with the default config, it's just giving you that error and you should just fix it. PHPUnit does not only run tests, it also executes some code and deals with issues related to executing code, like getting Warnings, Errors and Notices.

It's you who need to deal with that if you execute phpunit. Otherwise, just step back and do not execute it :)

I am also using the default phpunit configuration file shipped with laravel.

Yes you are. That is why tests are run. And that's why you're caught in the act of using Undefined Variables.


See as well:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836