I want to do performant testing, so I thought about using PHPUnit_Framework_TestCase instead of TestCase in my test files. I'm sacrificing http requests testing, but I want at least to use some of the components of Laravel (like its providers and such).
Here's what it looks like :
<?php
class ChatTest extends PHPUnit_Framework_TestCase
{
/** #test */
public function chat_can_be_accepted()
{
$this->chat->setStatus(Chat::CHAT_STATUS_WAIT_MASTER);
$this->chat->accept();
$this->assertEquals(Chat::CHAT_STATUS_WAIT_CONFIRM, $this->chat->getStatus());
}
}
I have an error saying Call to a member function connection() on null. setStatus actually uses Laravel internals in this example (something as simple as a relationship that needs the app).
I already tried switching every providers, but it seems code is not even going through providers. Also tried changing PHP versions, or extensions.
I'm answering my own question for future reminders, and also because I never found this answer anywhere else.
At this point I was using PHPStorm and it doesn't auto load the phpunit.xml file when launching tests by default.
That line in the file launches Laravel (works with Lumen too) along with the tests : bootstrap="bootstrap/app.php".
This is needed to use Laravel internals even without using their test frameworks.
To use this in PHPStorm :
To use this in CLI :
phpunit --configuration phpunit.xml
Related
I am using Netbeans 8.2 and PHPUnit 4.8.36 along with PHP 5.6.36 and I have some issue when there is some Fatal Error Exception within the tests functions.
/**
* #runInSeparateProcess
*/
public function testEquals() {
thisMethodDoesNotExistsAndTestShouldFail();
}
It works fine if I add the annotation #runInSeparateProcess so that means test fails.
But if I have it just like this:
public function testEquals() {
thisMethodDoesNotExistsAndTestShouldFail();
}
The test passes which in my opinion is wrong.
The idea is that I don't want to add the annotations for each test and there are hundred of tests already so I can't just go and add to every of them.
Is there any way that I can configure PHPUnit to run the tests in separate process.
For ex my colleague of mine doesnt need to add this annotation and it works for her as expected.
Any idea where I should look this to solve or anyone had this issue before? Do u think the php version would matter here, since my colleague is using php 5.3.x and it works for her. Also I am using Zend Framework v1
I've just started using Travis CI to test my PHP code. Sometimes, builds fail with the message
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /home/travis/build/ms609/citation-bot/tests/phpunit/expandFnsTest.php on line 13
On other occasions, without my having changed any relevant code, the builds succeed.
This makes me suspect that the issue is at Travis's end rather than my own.
This makes me wonder: is there anything that I can do to reduce the likelihood of my encountering this error? And on builds when the error arises, is it possible to have Travis re-attempt the build without making a new commit?
I had inherited a testcase written using an older version of phpunit. For backwards compatibility, following advice elsewhere, I had added the code
if (!class_exists('\PHPUnit\Framework\TestCase') &&
class_exists('\PHPUnit_Framework_TestCase')) {
class_alias('\PHPUnit_Framework_TestCase', 'PHPUnit\Framework\TestCase');
}
What I needed to do next was replace
class myTest extends PHPUnit_Framework_TestCase {
with
class myTest extends PHPUnit\Framework\TestCase {
With regards to the second part of the question, signing in to Travis CI reveals a "restart build" option on the build page.
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,
I'm using PHPUnit to test my application, in this case I'm testing an API call (I'm doing GET, POST, PUT and DELETE through it). index method responds to GET(/api) route, in this method I have a custom Request:
public function index(\Api\User\Requests\IndexRequest $request)
{
// do some stuff...
}
Api\User\Requests\IndexRequest class looks like this:
class Request extends IndexApiRequest
{
// some methods in here
}
When I execute the test via PHPUnit it prompts:
Class Api\User\Requests\IndexRequest does not exist
Checking the trace route it dies in Illuminate\Routing\RouteDependencyResolverTrait. I couldn't figure out how I can interfere in the execution since it seems to happen between PHPUnit and Laravel.
Does anyone have an idea? I'm using Laravel 5.3, PHPUnit 5.6.5 running on Ubuntu 16.04, PHP 7.0 and nginx.
Thank you!
change class Request extends IndexApiRequest to class IndexRequest extends IndexApiRequest
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.