Setup 1: Echoing back from the route works in PHPUnit
routes.php
Route::get('signup', function(){
return "Hello World!";
});
/tests/SignupTest.php
class SignupTest extends TestCase {
public function testIndex(){
$this->call('GET', 'signup');
$this->assertResponseOK();
}
}
Running PHPUnit returns the following:
OK (1 test, 1 assertion)
Setup 2: But PHPUnit can't find the Signup Controller when I route to it
routes.php
Route::get('signup', array('uses' => 'Signup#process'));
/app/controllers/Signup.php
class Signup extends BaseController {
public function process(){
echo "Hello World!";
}
}
/tests/SignupTest.php
class SignupTest extends TestCase {
public function testIndex(){
$this->call('GET', 'signup');
$this->assertResponseOK();
}
}
PHPUnit returns the following:
There was 1 error:
1) SignupTest::testIndex
ReflectionException: Class Signup does not exist
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:476
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:416
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:423
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:77
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:50
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php:900
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php:118
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php:964
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php:934
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:677
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:652
/Applications/MAMP/htdocs/laravel/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:82
/Applications/MAMP/htdocs/laravel/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:319
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:74
/Applications/MAMP/htdocs/laravel/app/tests/SignupTest.php:12
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
Setup information:
Laravel 4.1
PHPUnit 3.7.28
Both installed by Composer
MAMP running PHP 5.4.4 on Mac OS X 10.8
I don't think I've changed anything in the core/config files of Laravel or PHPUnit.
Things I've tried:
I found a few answers about this type of issue (both on SO and other sites), where the answers suggested fixing mistakes in names of class/file/method, e.g.:
Laravel Controller not working
I don't think this is an issue here, because I've checked all the names. Also it returns "Hello World!" as expected for both the above routes when tested in a browser, suggesting that the route is working fine in Laravel. Problem seems to be with PHPUnit.
My Question: Why can't PHPUnit find the Signup Class in app/controllers?
I'd assumed that because there was a phpunit.xml file in the basic Laravel install, that PHPUnit would know about it. That assumption turned out to be incorrect.
The Problem
PHPUnit wasn't loading phpunit.xml, which meant the bootstrap file in bootstrap/autoload.php wasn't being loaded, so PHPUnit had no idea where any of the class files were, including Signup.php.
The Solution
I added the file path to phpunit.xml in the --configuration switch when I ran PHPUnit:
--configuration /Applications/MAMP/htdocs/laravel/phpunit.xml
General Advice
If PHPUnit tells you it can't find a class, check that it's loading phpunit.xml and/or your bootstrap file. Make sure your phpunit.xml file points correctly to your bootstrap file and that your bootstrap file is configured correctly to load the required classes.
Also, as pointed out in my question, problems can also be caused by inconsistencies between class/file/method names.
Related
I followed Codeception's quick start guide (http://codeception.com/quickstart) and read their documentation (http://codeception.com/docs/05-UnitTests).
I have managed to set up the testing environment, and
Created the unit test file (php codecept.phar generate:test unit ExampleTest)
Run the test command (php codecept.phar run unit ExampleTest), which returns an error:
There was 1 error:
1) ExampleTest: Validation
Test tests\unit\ExampleTest.php:testValidation
[Error] Class 'User' not found
#1 ExampleTest->testValidation
#2 C:\laragon\www\kario\vendor\bin\codecept.phar:5
How does the test file know which PHP file to run the test on?
My laragon project is named kario, and sits in C:\laragon\www\kario\resources\views\pages\orders while the test unit file is in C:\laragon\www\kario\vendor\bin\tests\unit.
I had this question too and found the answer for myself. Posted here http://phptest.club/t/beginner-codeception-unit-test-help/1849 but also, here you go:
First, some details. I am using Codeception v2.4.1, powered by PHPUnit 7.1.4. The answer is:
In codeception.yml, add these two lines:
settings:
bootstrap: _bootstrap.php
Here _bootstrap.php can be whatever you want the name of your bootstrap file to be.
You must place _bootstrap.php in each of the following directories as follows:
tests/unit/_bootstrap.php
tests/functional/_bootstrap.pp
tests/acceptance/_bootstrap.php
In my tests/unit/_bootstrap.php file, I placed the following code:
<?php
use Codeception\Util\Autoload;
Autoload::addNamespace('myclassnamespace', __DIR__ . '/../../Classes/');
To make sure I had the right path to Classes, I used trigger_error(__DIR__) in my _bootstrap.php before I added the Autoload line.
Then in my tests/unit/TestAddCest.php, I placed the following line at the beginning of the file:
<?php
use mynamespace;
And in my test function looks like this (note the instantiation of the User class):
public function tryToTest(UnitTester $I)
{
$user = new mynamespace\User('someusername');
$I->assertEquals('someusername', $user->username);
}
I hand typed that function because I'm not on the same machine with the code and didn't feel like getting it over, so it may have a typo or bug, but you get the idea.
Edit 05/01/2018: someone else answered me on http://phptest.club:
It would be better to configure autoloading of your classes in composer.json and let Composer to do the rest, unless you try to avoid using Composer.
https://getcomposer.org/doc/01-basic-usage.md#autoloading
recently upgraded a 5.3 project to 5.4 and all seemed good.
Today I started to implement Dusk however had hit an issue when running the example test
☁ footy-finance [5.4] ⚡ php artisan dusk
PHPUnit 6.0.0 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 162 ms, Memory: 6.00MB
There was 1 error:
1) Tests\Browser\ExampleTest::testBasicExample
ReflectionException: Class config does not exist
/Users/owen/Sites/footy-finance/vendor/laravel/framework/src/Illuminate/Container/Container.php:681
/Users/owen/Sites/footy-finance/vendor/laravel/framework/src/Illuminate/Container/Container.php:565
/Users/owen/Sites/footy-finance/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:105
/Users/owen/Sites/footy-finance/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:263
/Users/owen/Sites/footy-finance/vendor/laravel/dusk/src/TestCase.php:203
/Users/owen/Sites/footy-finance/vendor/laravel/dusk/src/TestCase.php:40
I've had a look at line 40 of TestCase.php and its
public function baseUrl()
{
return config('app.url');
}
So it does look like something to do with the global config helper anybody have any ideas?
I'm running
PHP 7.0.14
Laravel/Framework 5.4.8
Laravel/Dusk 1.0.5
The full composer.lock can be seen https://gist.github.com/OwenMelbz/c05172b33f6eb4483e37a56469b53722
Fingers crossed you guys have some ideas!
Cheers :)
I had this error in the log
Class config does not exist
the problem with me was that in the .env file I had set a configuration variable in the following way:
APP_NAME=Application Name
note the space. When I changed it to this:
APP_NAME="Application Name"
the problem got fixed
The issue is with .env file
App_Name
in the original file its written this way>>> APP_NAME=Application Name
Make it like this APP_NAME="Application Name"
In my case, this solution works:
1) Remove all contents of the bootstrap/cache folder
2) Run the composer dump command
For anybody else who has had this issue.
I had prefer stable set in the composer file, which installed PHPUnit 6.
This was "made stable today" - thus it installed during a composer update.
Downgrading to PHPUnit 5 fixes the issue - so was bad timing starting it today.
I just ran into the the same issue, in my case the .env was all clean, no unwrapped empty spaces.
This error message can also occur when writting/debugging a test case, using the setup() method in that test, forgetting to call parent::setup() as the first statement in that function.
protected $stuf;
function setup() {
parent::setup();
$this->stuf = 'stuf';
}
I found very useful info here on what else could happen when you're getting this error message.
I've also had this issue. For me it was caused by calling the config() function inside a dataProvider method. DataProviders are called before the createApplication() method initialises the application, and populates the DI container. Hence config() fails because the app('config') call in the helper function can't resolve the config class from the container.
I'm very late for the party here but for anyone experiencing the same issue with Laravel's unit test and none of the above solutions work, you can look into mine and see if this might help.
In my case, I was trying to call a method that will remove all the test keys that persisted in my Redis database when I run the unit test. The method is called in the tearDown method of the class. The error occurs because the parent constructor is called before the actual tearDown code is executed. That's the reason why I'm having the error.
Instead of this one......
/**
* tearDown is executed after test stub
*/
protected function tearDown()
{
parent::tearDown();
$this->deleteTestKeys();
}
Change it to this one...
protected function tearDown()
{
$this->deleteTestKeys();
parent::tearDown();
}
In this case, the class' is not totally destroyed yet and the Laravel's config method will get called accordingly.
I had this in a Lumen application today. After some investigation and playing around, I found that it was because in PHPStorm it was adding the --no-configuration option onto the phpunit command because I hadn't configured my PHPUnit setup for the project in the IDE.
I corrected that by clicking 'Run > Edit Configurations' and then under 'Defaults > PHPUnit' click the little button to the far right of the 'Use alternative configuration file:' option and set the 'Default configuration file:' to the full path to your project's phpunit.xml.
Hope this helps!
I saw this error after following some dodgy installation instructions for a third party module, which said to register a service provider in bootstrap/app.php
$app->singleton(...);
$app->singleton(...);
$app->register(\Third\Party\ServiceProvider::class);
This caused $this->app['config'] to generate the error BindingResolutionException: Target class [config] does not exist.
I fixed it by putting it in config/app.php, where it belongs:
/*
* Package Service Providers...
*/
Third\Party\ServiceProvider::class,
So, I'm writing a basic Laravel package and I seem to have stumbled upon yet another problem, this time with testing.
The package in development is currently in a packages folder in the root of the project. I have modified the composer.json file of the package to include the dependencies I need
"require-dev": {
"phpunit/phpunit": "~4.0",
"laravel/laravel": "dev-develop"
}
However , whenever I try running phpunit tests in the package folder (which contains a folder named tests along with a sample test), I get the following error:
PHP Fatal error: Class 'Illuminate\Foundation\Testing\TestCase' not found in /workspace/laravel/packages/sample/http-request/tests/HttpRequestTest.php on line 8
The test file is just the auto-generated stub:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HttpRequestTest extends Illuminate\Foundation\Testing\TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
Any idea why this isn't working? The app tests run without a hitch, but the app itself doesn't have dependencies other than what's in the box.
SOLUTION
Managed to make it work independently by extending the PHPUnit_Framework_TestCase:
class HttpRequestTest extends PHPUnit_Framework_TestCase
However , running it like:
vendor/bin/phpunit packages/yourname/package-name/
Works as well, so I picked it as an answer.
This works for me:
class HttpRequestTest extends TestCase
And running test with:
vendor/bin/phpunit packages/yourname/package-name/
(Posted on behalf of the OP as an answer).
Managed to make it work independently by extending the PHPUnit_Framework_TestCase:
class HttpRequestTest extends PHPUnit_Framework_TestCase
However , running it like:
vendor/bin/phpunit packages/yourname/package-name/
Works as well, so I picked it as an answer.
For Windows environments you need to use backslashes!
vendor\bin\phpunit packages\yourname\package-name
Create a directory named tests in your root of the package and write various test classes inside it. You can have all functioning of your package inside various methods in a single class or you can use multiple classes.
Move to your project's root and run following commands,
vendor/bin/phpunit packages/YOUR_DIRECTORY_NAME/PACKAGE_NAME
Wait a little and you will get the response for your test cases. No. of positive and negative tests will be shown.
So have PHPUnit and CodeIgniter installed:
http://d.hatena.ne.jp/Kenji_s/20120117/1326763908
Couldn't download the PEAR as its been deprecated. So had to download the phpunit phar file:
http://phpunit.de/manual/4.0/en/installation.html#installation.phar
So was able to get some tests to run properly. Moved my phpunit.phar to /usr/local/bin and ran on the tests dir:
php /usr/local/bin/phpunit.phar
And all the tests ran correctly. But when i tried to run the php generate fixtures and php generate.php fixtures:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /www/test/application/third_party/CIUnit/libraries/CIUnitTestCase.php on line 15
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /www/test/application/third_party/CIUnit/libraries/CIUnitTestCase.php on line 15
Seems like its not finding the classes inside the phar file or at least they are not in the correct order? What is funny is that it runs the tests fine but not the generate fixtures.
Additionally i also installed using composer the phpunit so i have a /www/test/vendor/bin/phpunit installed as well.
Any help would be appreciated.
I had the same problem in my code, although I do not use the CodeIgniter. Trying to run tests would result in the error message:
Class 'PHPUnit_Framework_TestCase' not found
For what it's worth I had this fixed by adding a backslash to my test class declaration.
// Before
namespace IMAVendor\Super\Duper;
class MyClassTest extends PHPUnit_Framework_TestCase
// After
namespace IMAVendor\Super\Duper;
class MyClassTest extends \PHPUnit_Framework_TestCase
^
Added this backslash here
This seems to have something to do with namespaces and the autoloader that's built in phpunit. I have my own autoloader for the project code and it seems that it was trying to load the phpunit's classes from my code. I'm not really sure why it didn't try to load it from the 'base' when it wasn't able to find it in the projects namespace (This may very well be due to my own autoloader being faulty).
I know this is an old question, but I'll just leave this here in case it may help somebody somewhere.
I am running the example from the documentation: http://docs.phalconphp.com/en/latest/reference/unit-testing.html#sample-unit-test
I want to create an abstract unit test from Phalcon\Test\UnitTestCase as in the documentation. However when I run my test I become:
PHP Fatal error: Class 'Phalcon\Test\UnitTestCase' not found
I have followed the exact documentation steps. Did anyone have the same problem and solved it?
This class is part of the incubator: https://github.com/phalcon/incubator
$loader = new Phalcon\Loader();
$loader->registerNamespaces(array(
'Phalcon' => '/path/to/incubator/Library/Phalcon/'
));
$loader->register();
I figure it out.
Basically we have to do 2 things.
is whats in the #twistedxtra's answer. (setting up the path to where the incubator is)
in the testsTestUnitTest.php we created, it has the following line
class UnitTest extends \UnitTestCase {
we have to change that line to
class UnitTest extends \Phalcon\Test\UnitTestCase {
What we did was set the proper namespace so that the code knows where the UnitTestCase class is.
Thats it. Cheers...!!!
Make sure you run phpunit command in tests folder. It's very important.
Don't run something like phpunit tests/