Laravel 5.3 with mysql, PHPUnit 5.7.4
When I create a test in PHPUnit with use DatabaseMigrations;, it destroys the data that it queries.
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ThingsTest extends TestCase
{
use DatabaseMigrations;
/** #test */
public function there_are_more_than_1000_things()
{
$things = App\Thing::all();
$this->assertGreaterThan(1000, count($things));
}
Before I run phpunit, there are lots of things. After running it, mysql says ERROR 1146 (42S02): Table 'database.things' doesn't exist
Any ideas how to stop this?
DatabaseMigrations is a trait and it execs:
before test 'php artisan migrate' // creates your tables, but doesn't seed them
after test 'php artisan migrate:rollback' // remove tables
So, 1st - make sure you're using another database for testing.
2nd - seed with fake data your tables before testing your Things class.
Alternative:
use DatabaseTransactions trait instead of DatabaseMigrations.
In that case each test activity will be wrapped in a database transaction. After test all your changes will be dropped by transaction's rollback automatically.
You can be using a test database with PHPUnit within your Laravel application. Right now your tests are using your main database and will modify existing information.
Please see https://stackoverflow.com/a/35228697/823549 for how to do this.
Related
Working on a task where we need to create Model and Migration files programmatically. Also in the Model there will be some predefined functions. These functions will be same for all models. The functions are just relations with other Models. I have searched for a few options and found that Laravel has Stubs and we can create custom stubs which are great but don't think there is any option to pass params to the stub file when generating it.
In this case, we want to create a model and migration with dynamic columns. May be there is no easy way to do it but in case anyone has done it already, can you please provide me a hint of how you did it.
Trying this laravel package
https://github.com/laravel-shift/blueprint
.It can generate models, migrations, controllers from Yaml file. May be we can create a yaml file dynamically and then publish it.
Thanks
you can write your own command.
for example i wrote a command for generate repository pattern in my projects
php artisan make:repository repoName
You can publish stubs
php artisan stub:publish
Create your custom stub
<?php
// stubs/controller.custom.stub
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
/**
* Hello from the custom controller stub
*/
class {{ class }}
{
//
}
And call it
php artisan make:controller --type=custom MyController
Watch --type option
I would like to use validator for artisan commands (i.e unique user in database table). How to get validator instance inside artisan command in Lumen?
I found the solution. Simply add use Illuminate\Support\Facades\Validator; to class and use Validator like in Laravel.
We are in the process of migrating an old application to Laravel. The old application manages different projects and has an "admin" database for the main application and creates a new database for each project. We would prefer to keep this structure but it creates some problems for things such as migrations.
Initially we created our own base command that implements the handle() function so that any command that extends this command will loop through all the project databases. This means we have changed the structure so that migrations are separated into two directories since we have "admin" and "project" migrations for the different types of databases. We also duplicate each Laravel command that is already there if it doesn't work with our implementation out of the box.
However this creates a lot of duplicate code as we made our own implementation of the migrate command that simply passes calls to Laravels own migrate command. I now find myself having to implement the --force option into our new command for example.
So what I would like to do is override the handle() function in all the Laravel console commands in some way. Is there a better way of implementing this?
You can create your own base class for your artisan classes and extends your commands from it.
What I am trying to acheive - is create a database seeder for Laravel Notifications. As far as i am using database to store my notifications, there should be a way to achieve it. I am creating Factory model :
$user->notify(new NotificationEvent($event));
In fact it creates a notification in database, but artisan returns with error
[Symfony\Component\Debug\Exception\FatalThrowableError]
Call to undefined method Illuminate\Support\Facades\Notification::save()
Anybody knows what to do?
This seems to be a namespace issue, please try the following, place the class reference on top of your seed class:
use Notification;
I am trying to use Laravel and have been following the official Laravel Eloquent documentation and multiple tutorials at credible sources tuts plus
I created a model inside app/models called Stack with a table in the database called stacks with a primary key column called id, as corresponding to Laravels defaults.
<?php (Stack.php)
class Stack extends Eloquent
{
}
$stacks = Stack::all();
However when I run this model I get the following error message.
Fatal error: Class 'Eloquent' not found in C:\www\laravelproject\app\models\Stack.php on line 4
Including the official documentation and the reputable tutorials, I have also watched 2 youtube tutorials and it seems like there is no additional autoloading/including/requiring required to be declared in any new defined model's, so I am assuming something else here maybe wrong.
Do I have to manually find all classes I must autoload? If so, why is this not written in the official documentation?
I downloaded the latest laravel.phar file directly from laravel and used a .bat file to call it. (Not via composer)
Some things I have checked/tried to fix the problem.
Eloquent directory does exist at vendor\laravel\framework\src\Illuminate\Database\Eloquent
Eloquent alias set in app/config/app.php. Default 'Eloquent' => 'Illuminate\Database\Eloquent\Model'
Directly extending class like \Illuminate\Database\Eloquent\Model, error message the same but with \Illuminate\Database\Eloquent\Model instead of just Eloquent
Tried to directly extend through all variations by navigating down the entire Laravel directory structure \vendor\laravel\framework\src\Illuminate\Database\Eloquent, then \laravel\framework\src\Illuminate\Database\Eloquent etc... etc...
Bit the bullet and decided to try the second official method, I installed composer and ran the command composer create-project laravel/laravel --prefer-dist, the command screen alerted me it was downloading files which was then all successful at 100%, then alerted me that a generated application key was set successfully. I then navigate to the new directory model/User.php and receive the exact same error message as when I did it with the previuos method(laravel.phar direct download).
Thanks in advance.
Make sure you are accessing the application from the correct 'entrance'.
Thus, accessing it from app/public/index.php.
The app/public/index.php file loads the autoloader.
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/../bootstrap/autoload.php';
It's possible your is namespacing. Try adding the backslash before the class being extended.
class Stack extends \Eloquent
{
}
Make sure your are setting the Eloquent alias in the app config. (app/config/app.php)
Alternatively use the class directly. I believe it's: Illuminate\Database\Eloquent\Model
class Stack extends \Illuminate\Database\Eloquent\Model {}