When I run
phpunit command it throws all info from phpunit help with PHPUnit 3.7.21 by Sebastian Bergmann. So I suggest it should work, but not yet;
when I run
phpunit ExampleTest.php
PHP Fatal error: Class 'Tests\TestCase' not found in C:\xampp7\htdocs\projects\heroes\tests\Feature\ExampleTest.php on line 8
In Laravel I've got 'tests' folder with
TestCase.php
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}
folder 'Feature' with
ExampleTest.php
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testBasicTest()
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
and folder 'Unit'
In Laravel root folder I've got
composer.json
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.6.*",
"laravel/tinker": "^1.0",
"laravelcollective/html": "^5.4.0",
"laravelcollective/html": "~5.0"
},
"require-dev": {
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^2.0",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
],
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
I just simply need to start unit testing
It looks like your using phpunit installed globally in your system, which is super old and it is not connected with your project autoloading. You should use phpunit installed in your project by Composer:
vendor/bin/phpunit
When I run
vendor\bin\phpunit
It works, however It would be nice if somebody explained why
You can use this
./vendor/bin/phpunit
like this it will run phpunit from the root of your application
First install PHP Unit using this comand
composer require --dev phpunit/phpunit
After that check whether it is installed or not
./vendor/bin/phpunit --version
More Details visit - https://technicalguide.net/how-to-use-phpunit-in-laravel/
Related
I wanted to use the the
composer require osiset/laravel-shopify
but the issue is I'm running in laravel 8, I wanted to downgrade my laravel 9.2 to laravel 8.0 but when I tried downgrading it by changing the composer.json into
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.3|^8.0",
"fideloper/proxy": "^4.4.1",
"fruitcake/laravel-cors": "^2.0.3",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.0",
"laravel/passport": "^10.0",
"laravel/tinker": "^2.5|dev-develop",
"paragonie/random_compat": "2.*"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
"mockery/mockery": "^1.4.2",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.3.3"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi"
],
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate --ansi"
]
}
}
but I'm getting these kind of errors
In Facade.php line 178:
Facade does not implement getFacadeAccessor method.
Script #php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
I'm new to laravel and I don't know what to do... thank you
You can specify the version you want easily by running
composer create-project laravel/laravel="5.1.*" myProject
Firstly, Degrade your php version from 8.1 to 7.3.
Secondly, Create new laravel project with following instruction.
composer create-project laravel/laravel:^8.0 example-app
Thirdly, move blade, controller, route, model and so on from old project to new project.
Whenever I try to install "unisharp/laravel-filemanager" using composer in a fresh Laravel environment I get the following issues.
https://puu.sh/FqzWP/ff3f22090c.png
https://puu.sh/FqzTJ/dc7e35be88.png
https://puu.sh/FqzUd/156f987ba1.png
https://puu.sh/FqzUL/e50b127661.png
https://puu.sh/FqzVb/dd5c552d21.png
To reproduce this all I have to do is run the following commands to setup a fresh Laravel project...
composer global require laravel/installer
laravel new MyNewLaravelWebsite
cd MyNewLaravelWebsite
composer require unisharp/laravel-filemanager
For whatever reason if I simply run "composer require unisharp/laravel-filemanager" outside of the MyNewLaravelWebsite folder the command works, but isn't applied in my Laravel project (obviously)...
My PHP version is 7.4.3. Here is my composer.json also...
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.2.5",
"fideloper/proxy": "^4.2",
"fruitcake/laravel-cors": "^1.0",
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "^7.0",
"laravel/tinker": "^2.0"
},
"require-dev": {
"facade/ignition": "^2.0",
"fzaninotto/faker": "^1.9.1",
"mockery/mockery": "^1.3.1",
"nunomaduro/collision": "^4.1",
"phpunit/phpunit": "^8.5"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi"
],
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate --ansi"
]
}
}
I found that version 1.9.2 of the package provided support for Laravel 7/7.3. So instead of installing 2.0 I installed 1.9.2 and this worked... I'm guessing support for Laravel 7 in the package version 2 will come later.... Here is how to require the correct package.
composer require unisharp/laravel-filemanager 1.9.2
Hope this helps someone.
I'm developing a Role-Permission package in Laravel and I want to use this package;
Laravel permission github
Problem is I can not use some functions in the main project when I install this package in my own package. example "HasRoles"
My packages composer.json file
"require": {
"spatie/laravel-permission": "dev-master"
},
"autoload": {
"psr-4": {
"Modul\\Permission\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Spatie\\Permission\\PermissionServiceProvider"
]
}
}
main project composer file
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.8.*",
"laravel/tinker": "^1.0"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^7.5"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Modul\\Permission\\": "packages/modul/permission/src"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover"
],
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate --ansi"
]
}
}
and my User model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use Notifiable;
use HasRoles;
when I serve its show this error message...
Symfony \ Component \ Debug \ Exception \ FatalErrorException
(E_UNKNOWN) Trait 'Spatie\Permission\Traits\HasRoles' not found
What am I doing wrong here?
[1]: https://i.stack.imgur.com/KRUT0.png
The problem is you are just autoloading your packages files into your project. This way your project's composer doesn't know anything about your package's dependencies (hence why spatie/permission package is not being installed).
The correct way to do this is to require your package into your project. Usually you would create a repository for your project, register it at https://packagist.org as modul/permission and then run composer require modul/permission for you project.
But if your package is still not fully developed, I would recommend you require it not from packagist, but from the so-called path repository. Inside your project's composer.json add the following section:
{
...
"repositories": [
{
"type": "path",
"url": "packages/modul/permission"
},
]
...
}
This will make composer look into your packages/modul/permission directory for your package when you require it. So do this and remove manual autoload of your package's source files from your project's composer.json (composer will use your package's autoload section to bind /src to Module\Permission namespace):
"psr-4": {
"App\\": "app/",
"Modul\\Permission\\": "packages/modul/permission/src" <--- remove this line
}
Lastly, run composer require modul/permission. Composer will find it inside the path repository we specified for him and symlink the packages/modul/permission directory to vendor/modul/permission and install it's dependencies as well.
Now you can edit your package inside packages/modul/permission folder. Once it's done be sure to publish it online on github/packagist, so that it can be remotely available from packagist repository to everyone, not just from your local path one.
I downgraded my application from 5.5 to 5.4 and i am getting this error when i run php artisan serve
Call to undefined method App\Console\Kernel::load() - Laravel 5.4
Looking at other solutions, i run composer dump-autoload to resolve this issue but the issue persist. Same error. I don't really know if this is caused by my packages in the composer.json file posted below
What could i do to resolve this now?
Thanks in advance
Composer.Json
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"barryvdh/laravel-dompdf": "0.8.*",
"fideloper/proxy": "~4.0",
"guzzle/guzzle": "~3.0",
"laravel/framework": "5.4.*",
"laravel/passport": "^3.0",
"laravel/tinker": "~1.0",
"laravelcollective/html": "~5.0",
"maatwebsite/excel": " ~2.1.0",
"pda/pheanstalk": "~2.0",
"picqer/php-barcode-generator": "^0.2.0",
"spatie/laravel-permission": "^2.12",
"yajra/laravel-datatables-oracle": "~8.0"
},
"require-dev": {
"filp/whoops": "~2.0",
"nunomaduro/collision": "~1.1",
"fzaninotto/faker": "~1.4",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~7.0",
"symfony/thanks": "^1.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/commands",
"app/models",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
The issue is you did not ensure you updated your code to be Laravel 5.4 compatible.
In the Kernel.php boilerplate that comes with Laravel 5.5+ there's the line
$this->load(__DIR__.'/Commands');
This command was added in 5.5 to automatically detect any commands in the app/Console/Commands directory.
In order to get this code to work in 5.4 you need to remove that line and register all your commands manually under the protected $commands = [ ] array in the same file.
The source code for load is in https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Console/Kernel.php#L196 if you want to try to adapt it and put it in your own Kernel.php
Delete vendor folder and composer.lock file after run composer install command
Go to your_project/app/Console/Kernel.php and remove line
$this->load(DIR.'/Commands');
in commands() function
I use unisharp file manager in my project. on my local machine everything works fine, but on remote server it throws error [Symfony\Component\Debug\Exception\FatalErrorException] Class 'Unisharp\Laravelfilemanager\LaravelFilemanagerServiceProvider' not found
in config/app.php everything's correct
/*FileMananger*/
Unisharp\Laravelfilemanager\LaravelFilemanagerServiceProvider::class,
Intervention\Image\ImageServiceProvider::class,
/*CKEditor*/
Unisharp\Ckeditor\ServiceProvider::class,
there is directory unisharp in vedor(so all files and directories are there). when i try to run composer install/update i get this error again. so everything in its right place, but laravel doesn't see this class.
comoser.json
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.3.*",
"maatwebsite/excel": "~2.1.0",
"appzcoder/crud-generator": "^1.2",
"laravelcollective/html": "^5.3",
"lavary/laravel-menu": "dev-master",
"nesbot/carbon": "^1.21",
"laracasts/flash": "^2.0",
"spatie/laravel-medialibrary": "^3.11",
"firebase/php-jwt": "^4.0",
"doctrine/dbal": "^2.5"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.0",
"symfony/css-selector": "3.1.*",
"symfony/dom-crawler": "3.1.*"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Http/helpers.php"
]
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
]
},
"config": {
"preferred-install": "dist"
}
}
Try to comment those lines in config/app.php and run composer require unisharp/laravel-filemanager again.
You can't set up service providers before they are downloaded through composer.
You need to run:
composer require unisharp/laravel-filemanager
Make sure your config/app.php unisharp providers is matched with the content in folder vendor unisharp composer.json
check below:
Config/app.php
vendor/unisharp-composer.json
the solution is very easy :)
just make sure you have this in your service provider:
Unisharp\Laravelfilemanager\LaravelFilemanagerServiceProvider::class,
not this one:
UniSharp\Laravelfilemanager\LaravelFilemanagerServiceProvider::class,