I'm having some trouble starting automated tests with Yii, PHPUnit and Selenium
I've set up Selenium and PHPUnit and when i run phpunit . I get this error:
Warning: include(PHPUnit_Extensions_Story_TestCase.php): failed to open stream: No such
file or directory in path_to/framework/YiiBase.php on line 427
Warning: include(): Failed opening 'PHPUnit_Extensions_Story_TestCase.php' for inclusion
(include_path='.:') in path_to/framework/YiiBase.php on line 427
PHPUnit_Extensions_Selenium2TestCase_WebDriverException: The path to the driver executable
must be set by the phantomjs.binary.path capability/system property/PATH variable; for more
information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded
from http://phantomjs.org/download.html
I have Selenium RC running in background. I also have PHPunit folder inside tests folder, with Story and selenium extensions.
My code looks looks like this
define('TEST_BASE_URL','http://some_local_url/');
class WebTestCase extends PHPUnit_Extensions_Selenium2TestCase
{
/**
* Sets up before each test method runs.
* This mainly sets the base URL for the test application.
*/
protected function setUp()
{
parent::setUp();
$this->setHost('localhost');
$this->setPort(4444);
$this->setBrowserUrl(TEST_BASE_URL);
}
}
class SiteTest extends WebTestCase
{
public function testIndex()
{
$this->open('');
$this->assertTextPresent('Welcome');
}
}
Turns out that Selenium server needs phantomjs (thank you captain obvious), to run correctly. I simply put the server .jar file in the same folder as phantomjs binary and the tests were running correctly afterwards.
It looks like you haven't configured the path to the framework in your test entry script (index-test.php by default) properly. You should also set your config file here. Without this your tests run with the same settings (databases, sessions) as your application.
This is the content of index-test.php for the demo blog : https://github.com/yiisoft/yii/blob/master/demos/blog/index-test.php
<?php
/**
* This is the bootstrap file for test application.
* This file should be removed when the application is deployed for production.
*/
// change the following paths if necessary
$yii=dirname(__FILE__).'/../../framework/yii.php';
$config=dirname(__FILE__).'/protected/config/test.php';
// remove the following line when in production mode
// defined('YII_DEBUG') or define('YII_DEBUG',true);
require_once($yii);
Yii::createWebApplication($config)->run();
Related
I recently started building an application, locally, using CakePHP 4.X. I installed Composer and successfully installed the CakePHP Authentication and Authorization plugins using it. Now, I'm trying to move on to some community-developed plugins such as
https://github.com/FriendsOfCake/bootstrap-ui
https://github.com/gutocf/page-title
https://github.com/dereuromark/cakephp-feedback
I'm able to install all of the plugins okay but the issue arises when I try to load the plugins. Per the instructions on each of the plugin Git pages, I try to load the plugin from my CLI using the line
bin\cake plugin load BootstrapUI
(I'm on Windows hence the backslash)
I am returned the following message in all cases:
Your Application class does not have a bootstrap() method. Please add one.
My src/Application.php file looks like this
class Application extends BaseApplication
public function bootstrap() : void
{
// Call the parent to `require_once` config/bootstrap.php
parent::bootstrap();
if (PHP_SAPI === 'cli') {
$this->bootstrapCli();
} else {
FactoryLocator::add(
'Table',
(new TableLocator())->allowFallbackClass(false)
);
}
/*
* Only try to load DebugKit in development mode
* Debug Kit should not be installed on a production system
*/
if (Configure::read('debug')) {
$this->addPlugin('DebugKit');
}
// Load more plugins here
$this->addPlugin('Authorization');
$this->addPlugin('Authentication');
$this->addPlugin('BootstrapUI');
}
Your Application class is missing an { after class Application extends BaseApplication but I guess it was pasted/edited incorrectly here.
Your command seems to work because I see the plugin $this->addPlugin('BootstrapUI') already added in the file.
Make sure to be on the correct path (in the root of your app) when you execute the CLI command:
bin\cake plugin load BootstrapUI
You can manually add the plugins in the bootstrap() method without CLI.
I just installed laracast/flash and updated nesbot/carbon via composer. The cmd went nuts while downloading carbon. Cmd interface displayed scattered words and boxes all over the interface for a while and the download was completed.
Did php artisan serve at localhost:8000 and I've got the following errors.
Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
Fatal error: Unknown: Failed opening required 'C:\xampp\htdocs\NC World\ProjectName\server.php' (include_path='C:\xampp\php\PEAR') in Unknown on line 0
This error happens because the server.php file is missing. May be it is deleted or removed from your project directory. I have already faced this problem. Just add the server.php file at root of your project. You can add this file from any other laravel project directory or just create a file named server.php at root directory of your laravel project and paste the following code:
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylor#laravel.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';
This process works for me.
Try to disable your anti-virus, this happens to me, it seems avast deletes my server.php.
So I added it to the exception
Type in the command line:
php -S localhost:8000 -t public
because port is not public
go to main directory and create new server.php file even you can also copy paste from another created project
same happens to me and i just remove antivirus from my system and just create new file of server
You can also go in your antivurus params and restore server.php + add an exception it worked fine for me without recreate a project.
same problem happen to me:
just delete the project and uninstall the avast antivirus and re create the project
This same thing happens with me just create new laravel project and copy server.php from the new project and paste it to your project
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,
I've registered a custom Artisan command:
Artisan::add(new MigrateAll);
The class resides in app/commands (default location)
However when I run Behat I get the error:
Class 'MigrateAll' not found
Artisan is called in Behat for setting up the DB:
/**
* #static
* #beforeSuite
*/
public static function setUpDb()
{
Artisan::call('migrate:install');
//...
}
Do I need to give it a namespace? (I could not find the correct way to call the Artisan::add command with a namespaced class)
This is somewhat related to your earlier question. Your Behat test suite runs in a separate process independently of your app and knows nothing about the configuration. This also applies to the autoloading in your bootstrap and the autoloading would be the most likely reason why classes don't get found. This should be easily fixed by using Composer to autoload your own sources and vendor packages (both in your app and in your test suite).
# composer.json
{
"require": {
"…": "…"
},
"autoload": {
"psr-0": {
"": "../src"
}
}
}
// Include composer's autoloader in your `setUp()` / bootstrap / index.php.
include __DIR__ . '../vendor/autoload.php';
Take that process separation as a rule and keep in mind that Laravel like any other framework requires a whole bunch of other configuration. Since you are trying to use the database component, your next issue will be with that, because it won't be configured in your test suite.
The best approach is to create separate bootstrap file for Behat, which would inherit most lines from your normal bootstrap, where you need to pass the necessary configuration and do this:
/**
* #static
* #beforeSuite
*/
public static function setUp()
{
include_once('bootstrap.php');
}
If you configured your behat environment with this tut (Laravel, BDD And You: Let’s Get Started), after you added a new command, you need to $ composer dump-autoload to make behat to know the command.