Schema File not run in tests - laravel

When I run tests I call migrate:fresh. The schema file is not running. I'm using mysql database for testing. migrate:fresh runs well in other environments. please anyone has an idea?
This is the command I'm using:
Artisan::call('migrate:fresh')

This probably happens because your test database doesn't have the same name as your dumped database, so laravel can't find the migration.
A work around could be manually specify the dump path to the RefreshDatabase trait like that:
namespace Tests;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, RefreshDatabase;
protected function migrateFreshUsing()
{
return ['--schema-path' => 'database/schema/mysql-schema.dump'];
}
}

Related

Service Provider not booting in Laravel 5.8

I've created a new service provider to observe a model (App\Providers\EloquentEventServiceProvider.php), like so:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Staff;
use App\Oberservers\StaffObserver;
class EloquentEventServiceProvider extends ServiceProvider
{
public function boot()
{
Staff::observe(StaffObserver::class);
}
}
I've also added it to the config file (config\app.php):
return [
...
'providers' => [
...
App\Providers\EloquentEventServiceProvider::class,
...
]
...
]
The observer methods aren't working though. If I move Staff::observe(StaffObserver::class); to the AppServiceProvider class, it works just fine. So clearly this is an issue with getting my service provider to boot. I've tried php artisan config:clear, php artisan clear-compiled, composer update and composer dump but none have work. Any help is greatly aprpeciated.
your Oberservers name is wrong, as mentioned in the laravel doc observers laravel doc it should be Observers which means all of your observers should be within App\Observers instead of App\Oberservers.
so here we have 2 solutions :
1- if you want to keep the namespace App\Oberservers, you should run these 2 commands below because autoloading of the files may not work properly because we created a new folder Oberservers:
# Autoloading of files
composer dump
# Configure the cache
php artisan config:cache
2- the second solution is to just rename your actual Oberservers folder to Observers, in that way the autoloading of files will work well.

Facing problem while trying to insert data in database, LARAVEL

I am getting this error "Class 'App\Models\Student' not found".
Please check my codes:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller
{
public function data(){
$stud = new Student;
$stud->name = 'Mona Lisa';
$stud->rollnumber = '001';
$stud->save();
}
}
Your error is most likely in the namespace declaration of the Student model.
check in your class file App\Models\Student.php that the namespace is correct.
namespace App\Models;
then try to run in the console:
composer dump-autoload
Change this line:
use App\Models\Student;
to:
use App\Student;
Your error suggests that class is not loaded into the controller. I am guessing from the issue that you have created a separate directory for models if you have then you need to update your composer file to include new class paths for model classes. locate autoload in you composer.json file and add an entery in classmap array as shown below
"autoload": {
"classmap": [
"database",
"app/models"
]
}
then on command line navigate to your project directory and run the following command:
composer dump-autoload
and if you didn't create new models directory then just run
composer dump-autoload
Make sure your namespace of student model
use App\Models\Student If your model is in App\Models directory
use App\Student if your model is in app directory
Finally run composer dump-autoload
check your namespace of student model
use App\Models\Student If your model is in use App\Models directory
use App\Student if your model is in app directory
then go to command line and run composer dump-autoload

Resolve a PHPCS error in a Laravel migration file

In all my laravel migrations I want to use PSR2, but I getting a warning.
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreatePlayersTable extends Migration {
// migration
}
The error phpcs warning is Each class must be in a namespace of at least one level (a top-level vendor name)
How can I fix this warning?

Undefined class OneSignal

I use Laravel 5.2 and want to use berkayk/laravel-onesignal package, and I installed this package step by step according to Guide on github.
But when I want to use this package I get "Undefined class OneSignal", also I run this artisan command in the terminal:
php artisan config:clear
php artisan config:cache
I try this code in controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use OneSignal;
use App\Http\Requests;
class SignalController extends Controller
{
public function index()
{
OneSignal::sendNotificationToAll("Some Message");
}
}
I get this error
You should use full namespace for the facade:
\OneSignal::sendNotificationToAll("Some Message");
Or add this to the top of your class:
use OneSignal;
You should write use OneSignal top of the class underneath the namespace.
Hope this work for you!

phpunit cannot find Class, PHP Fatal error

I get phpunit and install it as this link using the simplest way for test purposes.
I just download the phpunit.phar file, chmod & rename & move to /usr/local/bin Then, I run phpunit --version, its ok.
PHPUnit 3.7.27 by Sebastian Bergmann
I write a simple test
public function testSomething(){
$this -> assertTrue(true)
}
Then I go into the source file folder, phpunit --colors Test
It works. So, I decide write a complex demo.
My project folder structure is like this.
Project Name
--> app
--> api
--> tests
Now I write a simple class in app/api/FlyApi.php
<?php
class FlyApi {
public function makeFly(){
//do something else
}
}
Then I write another test class for FlyApi.php
<?php
class FlyApiTest extends PHPUnit_Framework_TestCase {
public function testFly(){
//new a FlyApi
$flyApi = new FlyApi();
//do something
}
}
At this line $flyApi = new FlyApi() I got the error.
PHP Fatal error: Class 'FlyApi' not found in /home/kevin/Workspace/fly/app/api/FlyApi.php on line 23
Yes, this line $flyApi = new FlyApi()
You didn't load the definition of your FlyApi class.
This solution is Laravel specific:
You should be extending TestCase rather than PHPUnit_Framework_TestCase.
Try adding your /api/ folder into your ClassLoader at app\start\global.php.
You will find this section:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
...
app_path().'/api/
));
Are you using Laravel's phpunit.xml file? It includes Laravel's (and Composer's) autoload.php file which lets you use all your autoloaded classes within it.
Finally, what's the whole error? It should (hopefully) tell you what class it's trying to load (which will give you clues if the namespace is wrong or something).

Resources