I have this class in a workbench package:
class SomeClass{
//Constructor
function __construct(){
$isAuth = Auth::check();
}
But it gives me following error:
Class 'vendor\pachage\Auth' not found
Is this a namespace issue? I am using the following namespace:
namespace vendor\pachage;
You need Fully qualified name, add a backslash:
\Auth::check();
It all described as Namepace resolution.
Related
In laravel I have written the following test:
public function testUserCanCreateAssortment()
{
$this->signIn();
$this->get('/assortments/create')->assertStatus(200);
$this->followingRedirects()
->post('/assortments', $attributes = Assortment::factory()->raw())
->assertSee($attributes['title'])
->assertSee($attributes['description']);
}
}
When I run it with the command phpunit --filter testUserCanCreateItem I get the following error:
Error: Call to undefined function Tests\factory()
No idea what is causing it. I have looked at my factories and my testcase.php but I could not find a solution. What am I doing wrong?
My testcase.php:
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function signIn($user = null)
{
$user = $user ?: User::factory()->create();
$this->actingAs($user);
return $user;
}
}
Here the lines the error provides:
/var/www/tests/TestCase.php:13
/var/www/tests/Feature/ItemTest.php:29
In Laravel 8, the factory helper is no longer available. Your testcase model class should use HasFactory trait, then you can use your factory like this:
testcase::factory()->count(50)->create();
Please note that you should also update your call to User factory: factory('App\User')->create()->id;
Here is the relevant documentation:
https://laravel.com/docs/8.x/database-testing#creating-models
However, if you prefer to use the Laravel 7.x style factories, you can use the package laravel/legacy-factories You may install it with composer:
composer require laravel/legacy-factories
Guys I found the solution to my answer.
I needed to add the model name in my AssortmentFactory. So I did:
protected $model = Assortment::class;
and I also needed to import the model by doing use App/Models/Assortment.
In the UserFactory I also needed to import the model by doing App/Models/Assortment.
This solved my issue.
First run this command composer require laravel/legacy-factories
public function YourMethodName()
{
$user = factory(User::class)->create();
$this->actingAs($user);
//code...
}
You may have to specify that you are using an other class.
Did you write use Assortment or use \Assortment above ?
You can also use the manager to find the class you are using
I have a class named Service_B which extends a custom service class.
This custom service class requires one single object named Reader in its __construct() in order to instantiate properly.
The parent service is defined as follow
namespace Vendor\Services;
abstract class Service{
function __construct(Vendor\Services\Reader $reader){
}
}
Service_B is defined as follow:
namespace Vendor\Services;
class Service_B extends Service{
function __construct(){
parent::__construct(new \Vendor\Services\Reader());
}
}
Reader do have the following line at the top of the file:
use Vendor\Services;
Class files are organized like this:
Vendor/Services/Service_B.php
Vendor/Services/Reader.php
Question:
When I instantiate Service_B, I get the following error message:
Fatal error: Class 'Vendor\Services\Reader' not found
I don't understand why I get this error since I think I am using the proper namespaces declarations. Thank you
On top of your Reader class place:
//This will declare the Reader class in this namespace
namespace Vendor\Services;
and remove:
//THIS IS A WRONG DIRECTIVE: you're telling PHP to use the Vendor\Services class but it doesn't even exist
use Vendor\Services;
Then modify the Service_B class as follow:
namespace Vendor\Services;
//i think this should extend Service, as it's calling the parent constructor
class Service_B extends Service
{
function __construct(){
parent::__construct( new Reader() );
}
}
This way all yours 3 classes will be in the same namespace, and the Reader class should be found without explicit namespace prefix
I followed these instructions and created a view composer for my default layout.
My DefaultComposer.php is located under app/Http/ViewComposers, hence the namespace used below:
<?php namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
class DefaultComposer {
public function compose(View $view) {
$data['language'] = LanguageController::getDefaultLanguage();
$view->with($data);
}
}
?>
Now, when I load a page, I get the following error:
Class 'App\Http\ViewComposers\LanguageController' not found
This happens because the LanguageController.php is placed under app/Http/Controllers, which is a different namespace.
How can I use the LanguageController class in my DefaultComposer?
Update:
Using this declaration:
use App\Http\Controllers\LanguageController as LanguageController;
throws: Class 'App\Http\Controllers\LanguageController' not found. I'm confused.
I figured this out. As the controllers in my application live in the global namespace, all I needed to do is add a backslash in front of the class name.
So instead of this:
$data['language'] = LanguageController::getDefaultLanguage();
I did this:
$data['language'] = \LanguageController::getDefaultLanguage();
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.
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
?