Im trying to do a real simple test going with Laravel 4 but getting errors.
Here is my super simple test.
class SignupTest extends TestCase {
public function testIndex()
{
$this->call('GET', '/');
$this->assertResponseOk();
}
}
Error Stack Trace
PHP Fatal error: Call to undefined method SignupTest::assertTrue() in /home/domain/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php on line 135
PHP Stack trace:
PHP 1. {main}() /home/domain/vendor/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /home/domain/vendor/bin/phpunit:63
PHP 3. PHPUnit_TextUI_Command->run() /home/domain/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /home/domain/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() /home/domain/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:349
PHP 6. PHPUnit_Framework_TestSuite->runTest() /home/domain/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745
PHP 7. PHPUnit_Framework_TestCase->run() /home/domain/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775
PHP 8. PHPUnit_Framework_TestResult->run() /home/domain/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:783
PHP 9. PHPUnit_Framework_TestCase->runBare() /home/domain/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:648
PHP 10. PHPUnit_Framework_TestCase->runTest() /home/domain/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:838
PHP 11. ReflectionMethod->invokeArgs() /home/domain/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:983
PHP 12. SignupTest->testIndex() /home/domain/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:983
PHP 13. Illuminate\Foundation\Testing\TestCase->assertResponseOk() /home/domain/app/tests/Unit/Controllers/SignupTest.php:9
You can see I'm extending the TestCase so I should have access to the assertTrue method.
Composer Part Incase it help.s
"require-dev": {
"phpunit/phpunit": "3.7.*",
"way/laravel-test-helpers": "dev-master",
"fzaninotto/faker": "dev-master",
"mockery/mockery": "dev-master#dev"
},
I have dumped autoload and cleared compiled.
Hope you can advise.
It turns out the phpunit was an old version and I had to re-install all vendor files.
Related
UPDATE: I think updating to a newer php version (by updating homebrew) causes this problem... Could/should I update the Laravel version of the project I'm working on? Or install a lower php version? I have no experience with that...
I'm working on a Laravel project for quite some time no. All of a sudden an exception is thrown when starting the local server.
The ErrorException is: trim(): Passing null to parameter #1 ($string) of type string is deprecated
It points to a few lines in the routes/web.php file.
ErrorException
trim(): Passing null to parameter #1 ($string) of type string is deprecated
at vendor/laravel/framework/src/Illuminate/Routing/RouteGroup.php:65
61▕ {
62▕ $old = $old['prefix'] ?? null;
63▕
64▕ if ($prependExistingPrefix) {
➜ 65▕ return isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old;
66▕ } else {
67▕ return isset($new['prefix']) ? trim($new['prefix'], '/').'/'.trim($old, '/') : $old;
68▕ }
69▕ }
+6 vendor frames
7 routes/web.php:90
Illuminate\Routing\RouteRegistrar::group(Object(Closure))
+3 vendor frames
11 routes/web.php:91
Illuminate\Routing\RouteRegistrar::group(Object(Closure))
This are the lines in the web.php file
Route::middleware(['auth.isAdminPers'])->group(function () {
Route::get('/admin/admin',function () {
return view('admin.admin');
});
Route::get('admin/dienstverplaatsingen',[AdminDienstverplaatsingController::class,'index'])->name('admin.dienstverplaatsingen');
Route::get('/admin/experiment',[gebruikerController::class, 'experiment']);
Route::get('/admin/export',[AdminDienstverplaatsingController::class,'export']);
Route::get('/admin/fietsritten', [AdminFietsritten::class,'index'])->name('admin.fietsritten');
Route::get('/admin/fietsrittengrouped', [fietsrittengrouped::class, 'index'])->name('admin.fietsrittengrouped');
Route::get('/admin/maaltijden',[AdminMaaltijden::class,'index'])->name('admin.maaltijden');
Route::get('/admin/maaltijdengrouped',[maaltijdengrouped::class,'index'])->name('admin.maaltijdengrouped');
Route::prefix('admin')->name('admin.')->group(function(){
Route::get('/users/export', [UserController::class,'export'])->name('users.export');
Route::resource('/users', UserController::class);
Route::get('/appsettings', function(){
return view('admin.settings');
})->name('appsettings');
});
});
I just had this problem earlier. The way I have solved it was by running php artisan route:cache before composer install. Keep in mind that I am using PHP 7.4 with Laravel 8.
I try running this command :
php bin/phpunit
I get this error :
PHP Fatal error: Uncaught Error: Call to undefined method
PHPUnit\TextUI\TestRunner::doRun() in
C:\wamp64\www\Test___Hanff_Web\bin.phpunit\phpunit-7.5-0\src\TextUI\Command.php:206
I see that it is using phpunit 7.5, but i have require phpunit 9.2 in my composer.json :
// composer.json
"require": {
"php": "^7.4",
...
"phpunit/phpunit": "^9.2", <-- here
"sensio/framework-extra-bundle": "^5.1",
"spatie/enum": "^2.3",
"symfony/asset": "^5.1",
...
"symfony/framework-bundle": "5.1.*",
...
},
"require-dev": {
"dama/doctrine-test-bundle": "^6.3",
"doctrine/doctrine-fixtures-bundle": "^3.3",
"symfony/browser-kit": "^5.1",
"symfony/css-selector": "^5.1",
"symfony/debug-pack": "^1.0",
"symfony/phpunit-bridge": "^5.1", <-- Also here because symfony doc told me to import that
"symfony/stopwatch": "^5.1",
"symfony/twig-bundle": "^5.1",
"symfony/web-profiler-bundle": "^5.1"
},
When i trying to execute this :
> vendor\bin\phpunit --version
PHPUnit 9.2.6 by Sebastian Bergmann and contributors.
I get version 9.2 instead of this :
> php bin/phpunit --version
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
So i try to run composer update but it don't change anything.
Then i try to self update phpunit as described here but nothing do !
I'm actually only being able to run my php test by execute this command :
> vendor\bin\phpunit tests/Controller/WebTest_ArticleController.php
It execute all the tests in one TestCase file, but i can't execute all the test for all the tests/* directory.
I would like to be able to run all my test as :
> php bin/phpunit
... Runing all test
Or something like :
> vendor\bin\phpunit tests/*
... Runing all test in tests/ directory
I have also read this PHPunit Uncaught Error: Call to undefined method PHPUnit_Util_Configuration::getTestdoxGroupConfiguration()
But it doesn't provide me any satisfying solution
You have to use
vendor\bin\phpunit and name your test case in tests folder something like XXXXXXXtest.php else, phpunit would not find it.
tests/
Controller/
ArticleController_WebTest.php
MenuController_WebTest.php
SecurityController_WebTest.php
...
Repository/
ArticleRepository_KernelTest.php
...
Services/
Service_UnitTest.php
...
Utils/
Util_UnitTest.php
...
This is why i wasn't being able to execute all of my test just by running \vendor\bin\phpunit
My code is copyed from the website.
Excel::create('Filename', function($excel) {
// Set the title
$excel->setTitle('Our new awesome title');
// Chain the setters
$excel->setCreator('Maatwebsite')
->setCompany('Maatwebsite');
// Call them separately
$excel->setDescription('A demonstration to change the file properties');
})->download('xls');
I successfully download once.
However the other try is error.
The error Message is that.
Whoops, looks like something went wrong.
FatalErrorException in LaravelExcelWriter.php line 263:
Call to a member function getMergeCells() on a non-object
in LaravelExcelWriter.php line 263
• Write following in cmd
composer require Maatwebsite/excel
• after install /run upper composer........Maalwebsite/excel successfully check in composer.json that
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"laravelcollective/html": "5.2.*",
"Maatwebsite/excel": "^2.1"
},
• config/app.php/ in provides
Maatwebsite\Excel\ExcelServiceProvider::class,
• config/app.php/ in alias
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
• php artisan vendor:publish
• php artisan make:controller ExcelController
• open excel then create first_name,last_name,sex,email,phone then store datas and save as .csv
• in controlller for import
use App\Customer;
use Input;
use DB;
use Excel;
class ExcelController extends Controller
{
//
public function getImport()
{
return view('excel.importCustomer');
}
public function postImport()
{
Excel::load(Input::file('customer'),function($reader){
$reader->each(function($sheet){
Customer::firstOrCreate($sheet->toArray());
});
});
}
}
• in routes for import
Route::get('/getImport','ExcelController#getImport');
Route::post('/postImport','ExcelController#postImport');
• In controller for export
public function getExport()
{
$export=Customer::all();
Excel::create('Export Data',function($excel) use ($export){
$excel->sheet('Sheet 1',function($sheet) use ($export){
$sheet->fromArray($export);
});
})->export('xlsx');
}
I know why the reason.
Because there is not any sheet in my case.
However the sheet is necessary.
Thank you.
When running the example code on the laravel docs php artisan make:request StoreBlogPostRequest to create a new validation controller, I get the following error
[RuntimeException]
Unable to detect application namespace.
I'm not sure what's wrong, I've done some searching, but nothing really explains this error. Any ideas?
In Laravel 5, an "application" is a collection of PHP files under a single namespace, stored in the folder app/
By default, and in most of the Laravel 5 sample code from the docs, this namespace is App\. For example, one controller in your application might look like this.
namespace App\Http\Controller;
class MyController
{
//...
}
When Laravel generates code (i.e. when you use the make:request command), it needs to know what this application namespace is (it's possible to change the namespace with the artisan app:name command). For some reason, in your system, Laravel 5 can't detect the namespace.
If you look at the section of Laravel 5 core code that detects the namespace
#File: vendor/laravel/framework/src/Illuminate/Console/AppNamespaceDetectorTrait.php
protected function getAppNamespace()
{
$composer = json_decode(file_get_contents(base_path().'/composer.json'), true);
foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path)
{
foreach ((array) $path as $pathChoice)
{
if (realpath(app_path()) == realpath(base_path().'/'.$pathChoice)) return $namespace;
}
}
throw new RuntimeException("Unable to detect application namespace.");
}
You'll see that Laravel detects the namespace by looking at your composer.json file, and looking for thefirst valid psr-4 namespace.
My guess is your composer.json file is missing the namespace
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
Add that back in, and you'll be good to go.
Usually, this error can be mapped to syntax issues or errors in composer.json file. Check for any trailing commas or Auto load issue. For e.g.
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
"phpunit/phpunit": "^7.5",
},
This should be..
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
"phpunit/phpunit": "^7.5"
},
See no trailing commas at the end of "phpunit/phpunit": "^7.5"
I have installed laravel/socialite in my project but I can't understand how to make it work...
I have this in my composer.json:
"require": {
"laravel/framework": "5.0.*",
"laravelcollective/html": "~5.0",
"laravel/socialite": "~2.0"
},
I have added in my config/app.php:
'Socialize' => 'Laravel\Socialite\SocialiteServiceProvider'
and:
'Laravel\Socialite\SocialiteServiceProvider',
Then:
composer dump-autoload
Added a new route:
Route::get('auth/facebook', 'Auth\AuthController#getFacebookLogin');
Add the new method:
/**
* #return mixed
*/
public function getFacebookLogin()
{
return \Socialize::with('facebook')->redirect();
}
But all I get is:
FatalErrorException in AuthController.php line 43: Call to undefined method Laravel\Socialite\SocialiteServiceProvider::with()
Where is the error?
You should add
'Socialize' => 'Laravel\Socialite\Facades\Socialite'
as an alias in your app.php.
It seems the Facade alias doesn't really work anymore for Socialite. I think the docs could use some love, in general, too (especially since I don't think it's supposed to be aliased as "socialize").
What I found to work was to alter your use statement to
use Laravel\Socialite\Contracts\Factory as Socialite;
and drop the alias entry altogether.