I'm trying to test a model I've created, and I'm getting the error:
InvalidArgumentException: Unable to locate factory with name [default]
[App\Company].
Here's what my very simple model looks like:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
/**
* #return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function incidents()
{
return $this->hasMany(Incident::class);
}
}
here's the factory:
<?php
$factory->define(\App\Company::class, function () {
return [
'name' => 'ACME Company'
];
});
and the setup method in my unit test that's throwing the error:
<?php
use Illuminate\Foundation\Testing\DatabaseMigrations;
class CompanyModelTest extends TestCase
{
use DatabaseMigrations;
public function setUp()
{
factory(\App\Company::class)->create();
}
When I run the test I get this:
laradock#63912b4222e6:/var/www/laravel$ vendor/bin/phpunit
PHPUnit 5.5.6 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 528 ms, Memory: 4.00MB
There was 1 error:
1) CompanyModelTest::testCompanyName
InvalidArgumentException: Unable to locate factory with name [default] [App\Company].
/var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:126
/var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2280
/var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:139
/var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:106
/var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:84
/var/www/laravel/tests/CompanyModelTest.php:13
So far I've tried the suggestions on this other Stackoverflow question about the same error, and also this one.
I've tried moving my new factory into the ModelFactory.php file that's there by default and I've tried using defineAs() instead of define.
I've cleared the cache and run composer dump-autoload as suggested in other posts around the web.
I've dumped out the contents of FactoryBuilder::definitions which is empty, and that's what's causing the exception to be thrown.
I've confirmed that none of the factory files in database\factories directory are being called with debug code & a line that would cause a parse error.
Why aren't those files being called as per the documentation?
In my situation the TestCase was wrong class.
class CompanyModelTest extends TestCase
Which TestCase do you extend? I had
use PHPUnit\Framework\TestCase;
Instead of Laravel's own. Now it is working!
use Tests\TestCase;
Make sure you extend the correct TestCase class.
Related
So, I am just playing around with PHPUnit, I don't really need it but I want to learn how it works so I am trying it out on a random class in a plugin of mine.
The issue I have rn is that whenever I run phpunit I says the Plot class in my test was not found. In my composer.json file I have
{
"require": {
"phpunit/phpunit": "^8.5"
},
"autoload":{
"psr-4" : { "\\mohagames\\PlotArea\\utils\\" : "src/mohagames/PlotArea/utils/"}
}
}
And my Plot.php file is in the C:\Users\moham\Documents\GitHub\PlotArea\src\mohagames\PlotArea\utils\ directory and has the mohagames\PlotArea\utils namespace
But for some reason it still says
C:\Users\moham\Documents\GitHub\PlotArea>C:\Users\moham\Documents\Github\PlotArea\vendor\bin\phpunit --debug
PHPUnit 8.5.0 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.4
Configuration: C:\Users\moham\Documents\GitHub\PlotArea\phpunit.xml
Test 'SampleTest::testPlot' started
Test 'SampleTest::testPlot' ended
Time: 95 ms, Memory: 4.00 MB
There was 1 error:
1) SampleTest::testPlot
Error: Class 'Plot' not found
C:\Users\moham\Documents\GitHub\PlotArea\tests\unit\SampleTest.php:8
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
And the SampleTest test class:
<?php
use PHPUnit\Framework\TestCase;
class SampleTest extends TestCase{
public function testPlot(){
$plot = new Plot();
}
}
I've tried all sort of solutions on the internet but none of them worked
Like Alister pointed out importing the class first works otherwise, PHP doesn't know what class the code is referring to.
<?php
use PHPUnit\Framework\TestCase;
use mohagames\PlotArea\utils\Plot;
class SampleTest extends TestCase{
public function testPlot(){
$plot = new Plot();
}
}
My terminal says that a certain class doesn't exist when updating schema through the command "php doctrine orm:schema-tool:update".
This is the response:
Fatal error: Class 'Emily\Controller\PageController' not found in /src/Emily/Controller/Page/Login.php on line 8
This is the source:
<?php
namespace Emily\Controller\Page;
use Emily\Controller\PageController;
class Login extends PageController
{
}
This is the parent/PageController:
<?php
namespace Emily\Controller;
use Emily\Controller\Controller;
class PageController extends Controller
{
}
I've checked names, paths, but all is in place. Why does doctrine keep telling me the class doesn't exist?
Many thanks!
The manual of PHPUnit shows, that I can use class-constants in annotations for #expectedExceptionCode, see PHPUnit #expectedExceptionCode
I try to use it within my name spaced model which extends from Eloquent.
When I run my tests I got an: "PHP Fatal error: Class 'Eloquent' not found"
Running the App is all fine, so it depends on the Unit-Tests, isn't it?
Any ideas?
namespace Foo\Models;
use \Eloquent;
class Bar extends Eloquent {
const ERRORCODE = 150;
...
}
class BarTest extends TestCase {
/**
* #expectedExceptionCode Foo\Models\Bar::ERRORCODE
*/
public function testFoobar()
{
$name = 'foobar';
Bar::findBarOrFail($name);
}
}
For clarification:
PHP Fatal error: Class 'Eloquent' not found in PathToProject/app/models/Bar.php
Update
After #j-boschieros comment I got the above code working! Thanks mate!
However, when I provoke the exception in a controller test, the fatal error still occurs.
Even if I use the namespaces or not.
use \Eleoquent;
use Foo\Models\Bar;
class TestController extends TestCase {
/**
* #expectedExceptionCode Foo\Models\Bar::ERRORCODE
*/
public function testStoreActionWithInvalidDatatyp ()
{
$this->call('POST', '/routeToException');
}
Update 2
Got my Unit-Tests working when I extend \Illuminate\Database\Eloquent\Model instead of Eloquent.
namespace Foo\Models;
use \Illuminate\Database\Eloquent\Model;
class Bar extends Model {
const ERRORCODE = 150;
...
}
This differs from Laravel Doc. Is it still okay?
The "class" \Eloquent is actually an alias which is registered at run-time by the Laravel framework. You're making PHP parse the model class file before these aliases have been set up, causing it to fail. The correct approach to this is just to avoid the global namespace aliases and use the real class names instead (Illuminate\Database\Eloquent\Model instead of Eloquent in your case). You can find a list of class aliases in app/config/app.php.
In Symfony2 I get an error:
The autoloader expected class "Website\PublicBundle\Facebook"
to be defined in file
"C:\PHP-XAMPP\htdocs\myProject/src\Website\PublicBundle\Facebook.php".
The file was found but the class was not in it, the class name or
namespace probably has a typo.
500 Internal Server Error - RuntimeException
I cleared cache, restarted Apache and I tried several solutions from similar questions like this, but nothing works.
app/config/config.yml:
services:
facebook:
class: Website\PublicBundle\Facebook
src/Website/PublicBundle/Facebook.php:
namespace Website\PublicBundle\Facebook;
class Facebook {
public function __construct() {
//...
}
}
Your namespace should be
namespace Website\PublicBundle;
Otherwise your class is actually..
Website\PublicBundle\Facebook\Facebook
I try extend Validator class. I need add a few methods, that I'd like extend all class not use Validator::extend();
I added in vendor direcotry structure:
-comjaroapp
-src
-Comjaroapp
-Validation
-Validator.php
-ValidatorServiceProvider.php
In my config/app.php in providers array, I added:
'Comjaroapp\Validation\ValidatorServiceProvider'
Code to test is simple:
Validator:
namespace Comjaroapp\Validation;
class CustomValidator extends \Illuminate\Validation\Validator{
public function validatePesel($attribute,$value,$options=null){
return true;
}
}
ValidatorServiceProvider:
namespace Comjarospp\Validation;
use Illuminate\Support\ServiceProvider;
class ValidatorServiceProvider extends ServiceProvider{
public function register(){}
public function boot(){
$this->app->validator->resolver(function($transator,$data,$rules,$messages){
return new CustomValidator($transator,$data,$rules,$messages);
});
}
}
After run composer update I see error:
> Error Output: PHP Fatal error: Class
> 'Comjaroapp\Validation\ValidatorServiceProvider' not found in
> /vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php
> on line 158
When I looked on other extension all work with same structure.
If someone have idea, what is wrong or when should I search, please help.
Thanks in advance.
You have different names for your namespaces:
Comjarospp\Validation
and
Comjaroapp\Validation
EDIT:
After fixing the namespace name, have you executed
composer dumpautoload
?