Z
Z
zencd2016-12-29 14:20:25
PHPUnit
zencd, 2016-12-29 14:20:25

Why doesn't PHP find the class in a separate file?

The Laravel project has a phpunit test that works great.

<?php

class FooBar { }

class ExampleTest extends TestCase {
  ...
  new FooBar();

But as soon as I move "FooBar" to a separate adjacent file "FooBar.php", this class is no longer found by the PHP interpreter: "Class 'FooBar' not found". Adding the construction "use FooBar;" doesn't change anything.
How to put the class in a separate file?
I run the test through "phpunit tests\ExampleTest.php". There are no namespaces in the tests.
>ls tests
ExampleTest.php  FooBar.php

Laravel 5.x, PHP 7.1, phpunit ~5.0

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2016-12-29
@zencd

In order for use and namespacY to work, you need to first load the file with the class into the project. It can be done by hand through require / include, or by a semi-automatic composer through its autoload.
Most likely it will be necessary to correctly throw this file into the source folder (src) and do composer dump-autoload . Then check that the compositor loader file is loaded during the phpunit test

I
Ilya Karavaev, 2016-12-29
@Quieteroks

Have you tried including the file with the class (include 'FooBar.php')?

V
Vlad, 2016-12-29
@Result007

Did you write the namespace in the class file?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question