Laravel Dusk says Class 'Tests\Browser\Components\...' could not be found - php

After I installed laravel-dusk following the official Laravel documentation and run this command:
php artisan make:component CardComponentTest
Then try to run immediately:
php artisan dusk tests/Browser/Components/CardComponentTest.php
I get this error:
Class 'Tests\Browser\Components\CardComponentTest' could not be found in '/var/www/html/tests/Browser/Components/CardComponentTest.php'.
I tested file and path are correct:
ls -l /var/www/html/tests/Browser/Components/CardComponentTest.php
And it says:
-rw-r--r-- 1 djw djw 6917 Dec 3 11:25 /var/www/html/tests/Browser/Components/CardComponentTest.php
So it is exists and readable.
I checked the namespace in the file:
<?php
namespace Tests\Browser\Components;
It also looks good.
I checked the composer.json and in this I have this section:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
So the file exists, the namespace is good and the namespace is picked up in the composer.json.
I tried to run composer dump-autoload too. All good.
Any ide what's wrong whit this?

the error:
Class 'Tests\Browser\Components\CardComponentTest' could not be found in '/var/www/html/tests/Browser/Components/CardComponentTest.php'.
indicate you don't have CardComponentTest test case. Because CardComponentTest is not test case but just an component/part of test.
As aless said, component is not a test itself. But if you really want to test CardComponentTest you can replace
use Laravel\Dusk\Component as BaseComponent;
class CardComponentTest extends BaseComponent
to :
use Tests\DuskTestCase;
class CardComponentTest extends DuskTestCase
after you do that you can run php artisan dusk tests/Browser/Components/CardComponentTest.php , You just need to add test method first like. An example:
public function testComponentExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/')
->assertSee('Laravel');
});
}
But create test case in component folder is not best practice. You should create test case like official documentation said. Run dusk:make instead of dusk:component. An example:
php artisan dusk:make SimpleBrowserTest
and now you can run
php artisan dusk tests/Browser/SimpleBrowserTest.php

If you take a look at the documentation, the Component is not a test itself. Hence, you should not name the component "*Test" and you should not try to run the component like a dusk test.
You will use your component in your dusk test as described here:
https://laravel.com/docs/9.x/dusk#using-components
Components are for repeated actions, like selecting a date in a picker, which is then further used in your test case for example in a form or maybe some change in your UX happens that is assertable.

Related

How do I run PHPUnit in Laravel from my Windows Command prompt

I have a Laravel app installed in my directory f:\lara_app. I use PHP artisan serve to run the app. I have Laravel version 5.4.36 (via Composer Install)
I am not trying my hand in using the PHP Unit to do testing. Inside f:/lara_app/tests/Unit/ExampleTest.php, I have the following codes:
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
public function myFirstTest()
{
$value = 'Hello World';
$this->assertTrue( 'Hello' === $value, 'Value should be Hello World');
}
}
I now try to run the test from my command prompt:
f:\lara_app> .vendor/phpunit/phpuni/phpunit
But I'm getting the message below:
'.vendor' is not recognized as an internal or external command, operable program or batch file
How do I run the PHPUnit test?
Thanks in advance
Use quotes:
"./vendor/bin/phpunit"
This should work on windows
f:\lara_app> php vendor/phpunit/phpunit/phpunit
This works for me
C:\code\project-name> vendor\bin\phpunit
For every Laravel project from version 7.x, if you want to run tests you can do one of these:
You can use artisan: php artisan test docs
Or you can use phpunit: if you want to run phpunit on windows, only by typing phpunit, you can create a bat file in your project's main directory by running this command:#echo php vendor\phpunit\phpunit\phpunit > phpunit.bat and then you can type phpunit in your command and enjoy.
Happy Testing...
Also for future readers, you can run it on windows powershell, without having to change anything, if you're in the root directory of your project. Simply Type
./vendor/bin/phpunit
Use quotes or use backslashes. Unlike linux, Windows does not like forward slashes.
Check if you have phpunit in composer.json, and run:
composer update
And then run PHPUnit:
vendor/bin/phpunit

Get Laravel Dusk to Run Properly on Ubuntu 16 wt Laravel 5.5

I've been trying to integrate Laravel Dusk into my testing scheme for a week and can't get any test to actually deliver expected results. Here's the situation:
I'm running Laravel 55 on Homestead (per Project install) with php 7.1.*
I installed Dusk following the installation steps in the docs.
Out of the box the tests didn't work
I added the steps found in this article on "Laravel Dusk on Homestead" and the gist found here in "setup-headless-selenium-xvfb.sh" this to my provisioning file. This removed a lot of the exceptions I was getting.
I also added all my existing environment vars to the php node of my phpunit.dusk.xml file exactly as they were done so in the already successfully running phpunit tests from phpunit.xml
However now when I run the tests I just can't get the expected output. This is what I am doing. I add an input field in my home page ('/') view file as such: <input id="dusk-test" value="1234">
I run this test which is a mod of the original example test and is the only test:
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
class ExampleTest extends DuskTestCase
{
public function testBasicExample()
{
$this->browse(function (Browser $browser)
{
$browser->visit('/')->refresh()
->assertValue('#dusk-test', '1234')
;
});
}
}
...by running php artisan dusk and this is my output EVERY time
PHPUnit 6.4.3 by Sebastian Bergmann and contributors.
E 1
/ 1 (100%)
Time: 1.07 seconds, Memory: 12.00MB
There was 1 error:
1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\NoSuchElementException: no such element:
Unable to locate element: {"method":"id","selector":"dusk-test"}
(Session info: headless chrome=62.0.3202.62)
(Driver info: chromedriver=2.32.498513 (2c63aa53b2c658de596ed550eb5267ec5967b351),platform=Linux 4.4.0-92-generic x86_64)
/home/vagrant/landing/vendor/facebook/webdriver/lib/Exception/WebDriverException.php:102 /home/vagrant/landing/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:320
/home/vagrant/landing/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:535
/home/vagrant/landing/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:175
/home/vagrant/landing/vendor/laravel/dusk/src/ElementResolver.php:281
/home/vagrant/landing/vendor/laravel/dusk/src/ElementResolver.php:327
/home/vagrant/landing/vendor/laravel/dusk/src/Concerns/MakesAssertions.php:632
/home/vagrant/landing/tests/Browser/ExampleTest.php:22
/home/vagrant/landing/vendor/laravel/dusk/src/TestCase.php:92
/home/vagrant/landing/tests/Browser/ExampleTest.php:24
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
To make this even more confusing, this is my output when I dump from the test. Here's how I dump
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
class ExampleTest extends DuskTestCase
{
/**
* A basic browser test example.
*
* #return void
*/
public function testBasicExample()
{
$this->browse(function (Browser $browser)
{
$browser->visit('/')
->dump()
;
});
}
}
and my output after running php artisan dusk again is
PHPUnit 6.4.3 by Sebastian Bergmann and contributors.
"<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>"
Which is absolutely NOT my homepage. I also dumped the $url value from vendor/laravel/dusk/src/Browser.php and got my projects correct APP_URL.
I'm at a loss. Dusk is being sent the right location and the page definitely has the input and value. But, I can't get Dusk to give the expected output which would be that 12345 was retrieved from the element.
All help appreciated.
maybe what i'm saying is wrong ... but Laravel dusk seems to need dusk instead of id: like dusk="dusk-test". also call it after :
$browser->visit('/')>refresh()
->assertValue('#dusk-test', '1234') and it should be like the doc.
On Github someone solved his problem by replacing https:// with http://
in the .env file.

Laravel: Created a service but class is not found

Created the following file:
File: App\Services\Custom\Auth\AuthService.php
Name space: App\Services\Custom\Auth
Class name: AuthCustom
Method inside: foo()
In my controller I'm trying to call the foo method from the Service I created.
App\Services\Custom\Auth\AuthService\AuthCustom::foo()
Why does it keep returning Class 'App\Services\Custom\Auth\Authservice\AuthCustom' not found
What am I doing wrong?
Thanks!!
EDIT:
I added this in the composer.json and run composer dump-autoload without errors.
And it works!
"autoload": {
"classmap": [
"database",
"app/Services/Custom/Auth/AuthService.php"
],
"psr-4": {
"App\\": "app/"
}
},
Your namespace does not match your directory structure. If your class is in App\Services\Custom\Auth\AuthService.php, then your namespace needs to be App\Services\Custom\Auth. If you really want your namespace to be App\Custom\Auth, then your file needs to be App\Custom\Auth\AuthService.php.
Once you fix this, make sure you do a composer dump-autoload on the command line.
It seems that you didn't run composer dump-autoload or php composer.phar dump-autoload.
The composer.json is very important for autoloading!
Laravel needs an big file with all your php files required, usually generated via calling either artisan or composer with : php artisan dump-autoload / composer dump-autoload
It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project.
More details: http://developed.be/2014/08/29/composer-dump-autoload-laravel/

About L5 Autoload How add load all class Written?

The follow is my composer.json
autoload": {
"classmap": [
"database",
"app/library/"
if I use folder divide them like follow
how to add the code could load api & Identity
"app/library/* <---could use like wildcard ?
in Uniqid.php
<?php namespace app\library\identity;
in rgAPI,php
<?php namespace app\library\api;
If you see class not found then you can try these commands
composer dump-autoload
php artisan clear-compiled
php artisan optimize
This can make the classes in these folders found again.

Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...?

Why I'm getting this PHP error?
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...
For those arriving here after updating phpunit to version 6 or greater released on 2017-02-03 (e.g. with composer), you may be getting this error because phpunit code is now namespaced (check changelog).
You will need to refactor things like \PHPUnit_Framework_TestCase to \PHPUnit\Framework\TestCase
The PHPUnit documentation says used to say to include/require PHPUnit/Framework.php, as follows:
require_once ('PHPUnit/Framework/TestCase.php');
UPDATE
As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you:
require_once 'PHPUnit/Autoload.php';
Thanks to Phoenix for pointing this out!
For higher version of phpunit such as 6.4
You must use the namespace PHPUnit\Framework\TestCase
use TestCase instead PHPUnit_Framework_TestCase
// use the following namespace
use PHPUnit\Framework\TestCase;
// extend using TestCase instead PHPUnit_Framework_TestCase
class SampleTest extends TestCase {
}
I was running PHPUnit tests on PHP5, and then, I needed to support PHP7 as well. This is what I did:
In composer.json:
"phpunit/phpunit": "~4.8|~5.7"
In my PHPUnit bootstrap file (in my case, /tests/bootstrap.php):
// PHPUnit 6 introduced a breaking change that
// removed PHPUnit_Framework_TestCase as a base class,
// and replaced it with \PHPUnit\Framework\TestCase
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase'))
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
In other words, this will work for tests written originally for PHPUnit 4 or 5, but then needed to work on PHPUnit 6 as well.
You may get this error because you namespaced the file. If so you will need to specify that PHPUnit_Framework_TestCase is in the global namespace by preceding it with a backslash:
namespace AcmeInc\MyApplication\Tests
class StackTest extends \PHPUnit_Framework_TestCase {}
I submitted a crude PR to start conversation for correcting the documentation.
You can simply install PHPUnit to run commands (https://github.com/sebastianbergmann/phpunit/#php-archive-phar):
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
Run single test
And then run PHPunit test:
phpunit test.php
Content of test file is following:
<?php
class StackTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
}
public function testSave()
{
}
}
Run test suite
Configuration of test suite: demosuite.xml. demo is directory containing all tests. Test files must be named as *_test.php (suffix).
<testsuites>
<testsuite name="DemoTestSuite">
<directory suffix="test.php">demo</directory>
</testsuite>
</testsuites>
Test suite runs with following commands:
phpunit -c demosuite.xml --testsuite DemoTestSuite
Assumption:
Phpunit (3.7) is available in the console environment.
Action:
Enter the following command in the console:
SHELL> phpunit "{{PATH TO THE FILE}}"
Comments:
You do not need to include anything in the new versions of PHPUnit unless you do not want to run in the console. For example, running tests in the browser.
I use ZF2 and work for me when replaced 'PHPUnit_Framework_TestCase' to '\PHPUnit\Framework\TestCase'
I got it working with
include("vendor/autoload.php");
at the top of my test function.
If you have Centos or other Linux distribution you have to install phpunit package, I did that with yum install phpunit and it worked. Maybe you can have to add a repository, but I think it has to work smooth with the default ones (I have CentOS 7)
It may well be that you're running WordPress core tests, and have recently upgraded your PhpUnit to version 6. If that's the case, then the recent change to namespacing in PhpUnit will have broken your code.
Fortunately, there's a patch to the core tests at https://core.trac.wordpress.org/changeset/40547 which will work around the problem. It also includes changes to travis.yml, which you may not have in your setup; if that's the case then you'll need to edit the .diff file to ignore the Travis patch.
Download the "Unified Diff" patch from the bottom of https://core.trac.wordpress.org/changeset/40547
Edit the patch file to remove the Travis part of the patch if you don't need that. Delete from the top of the file to just above this line:
Index: /branches/4.7/tests/phpunit/includes/bootstrap.php
Save the diff in the directory above your /includes/ directory - in my case this was the Wordpress directory itself
Use the Unix patch tool to patch the files. You'll also need to strip the first few slashes to move from an absolute to a relative directory structure. As you can see from point 3 above, there are five slashes before the include directory, which a -p5 flag will get rid of for you.
$ cd [WORDPRESS DIRECTORY]
$ patch -p5 < changeset_40547.diff
After I did this my tests ran correctly again.
NOTICE: Command php bin/console generate:doctrine:crud also create TestController in src/Tests so it can throw error when you tried to start server if you don't have UnitTests. Remove the file fix it!
For me, it was because I ran
$ phpunit .
instead of
$ phpunit
when I already had a configured phpunit.xml file in the working directory.
I am using php 5.6 on window 10 with zend 1.12 version for me adding
require_once 'PHPUnit/Autoload.php';
before
abstract class Zend_Test_PHPUnit_ControllerTestCase extends
PHPUnit_Framework_TestCase
worked. We need to add this above statement in ControllerTestCase.php file

Categories