Lumen 8 - Using Faker in tests makes InvalidArgumentException: Unknown format "name" - testing

I'm using Lumen default Tests only added this line to the test :
$users = \App\Models\User::factory()->count(5)->create();
But i get this error when running the test :
InvalidArgumentException: Unknown format "name"
I did't touch the UserFactory Class i include it below , whats wrong with my code?
public function definition()
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
];
}

Should anybody else end up here looking for a similar issue in Laravel, make sure you include
parent::setUp();
in your setup method (if you have one). For example,
class ManageDocumentTest extends TestCase
{
public $user;
public function setUp():void
{
parent::setUp();
$this->user = User::factory()->create();
...

Uncommented these lines in app.php and its working now :
$app->withFacades();
$app->withEloquent();

You have to extend use Tests\TestCase instead of PHPUnit\Framework\TestCase.
At least, it helped me.

If you are using Tests\TestCase, calling parent::setUp(); and it still doesn't work, make sure not to call $faker before the actual test - ie. in a #dataProvider it won't work

Related

Laravel 9 accessors and mutators - Simple example not working

So I am trying to get my mutators and accessors to work in Laravel 9, in my Tag model I have the following:
protected function name(): Attribute
{
return Attribute::make(
get: fn ($value) => strtolower($value),
set: fn ($value) => strtolower($value),
);
}
When displaying the name in my blade view however, the name is not being displayed in lower cases ({{ $tag->name }}), also not when saving a new model to the database.
The following does work btw:
public function getNameAttribute($value)
{
return strtolower($value);
}
Also when using public it does not work:
public function name(): Attribute
Just trying to understand what I am doing wrong here?
I am using Laravel version 9.44
I dont know if the question's content is exactly your code. I had a similiar problem, get and set not working. But it worked in other model files.
I just found the solution once again, ya, once again.
If the attribute(column name) has two words, you have to make it together, first word should be lowercase.
short_name
protected function shortName(): Attribute
{
return Attribute::make(
get: fn ($value) => strtolower($value),
set: fn ($value) => strtolower($value),
);
}
Inject the class at top of page like below
use Illuminate\Database\Eloquent\Casts\Attribute;

Symfony 3.4 : "Could not load type ... : class does not exist"

Sorry, I'm a beginner at Symfony and I've tried to find an answer but nothing worked. I'm using Symfony3.4 which was updated from Symfony 2.8 a few months ago.
Now, I'm trying to do a rather simple thing : using a formType in a Controller, but no matter what, Symfony keeps showing the following error : Could not load type "Cha\GeneralBundle\Form\StripePaymentType": class does not exist.
Here's my StripePaymentType -really, really basic:
namespace Cha\GeneralBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class StripePaymentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class);
}
public function getBlockPrefix()
{
return 'cha_stripe_payment_form';
}
}
Here's my Controller action, once again, a basic thing (I didn't even write any code in there, because of this strange error) :
public function upgradeOfferPaymentAction(Offers $offers)
{
$form = $this->createForm(StripePaymentType::class);
return $this->render(
'#ChaGeneral/Offers/offer_payment.html.twig', array(
'form' => $form->createView()
));
}
I tried to use my form as a service but it did not work either :
cha.stripe.payment.form:
class: Cha\GeneralBundle\Form\StripePaymentType
tags:
- { name: cha_stripe_payment_form }
I'm probably missing something but I can't figure what...
Thank in advance for your help!
You must add this line at the top of the form class:
use Symfony\Component\Form\Extension\Core\Type\TextType;
Revert the SF version is not a solution, is just dodge the problem.
Well, finally I found what caused this problem : somebody did a composer update and we updated from Symfony 3.4.13 to Symfony 3.4.14. A simple revert did the trick, now everything is working again.
Thanks a lot for your help!

CakePhp cakesession not working with cake php debug kit

I implement cakephp debugkit plugin in my project.
In my appcontroller.php file i add
var $helpers = array('Html', 'Form', 'Paginator', 'Js', 'Session');
public $components = array('DebugKit.Toolbar');
and in before filter i implement
function beforeFilter() {
if ($this->Session->check('GlobalFields')==false) {
$this->Session->write('GlobalFields.tbl_assets.template_information_global', '');
$this->Session->write('GlobalFields.tbl_assets.hilitelibrary', '');
$this->Session->write('GlobalFields.tbl_assets.hilitesortedby', '');
$this->Session->write('GlobalFields.tbl_assets.sc_url_prefix', '');
}
}
but it showing error
Fatal error: Call to a member function check() on a non-object.
and i check that debug($this->Session) returns null.
if i remove public $components = array('DebugKit.Toolbar'); then its run correctly.
but i want to implement that debugkit with session.
please help me to solve this problem.
Well what happens here is that by setting:
public $components = array('DebugKit.Toolbar');
you are overriding the default value of that which includes the SessionComponent.
You are attaching the SessionHelper in the $helpers variable but those two classes are different - the first one (the component) allows you do interact with session data within the Controller and the second one (the helper) is used in your views.
So the solution to your issue is setting:
public $components = array('Session','DebugKit.Toolbar');
Also if you were using any other components globally you should add them there too.

Defaults in Symfony2 routing not being passed properly

I am currently trying to configure a routing option in Symfony2 so /cms will route to /cms/role/view. However, the passing of defaults doesn't seem to work properly.
/src/MyProject/CMSBundle/Resources/config/routing.yml
MyProjectCMS_specific:
pattern: /cms/{page}/{option}
defaults: { _controller: MyProjectCMSBundle:Main:index, page: role, option: view }
requirements:
_method: GET
/src/MyProject/CMSBundle/Controller/MainController.php
<?php
namespace MyProject\CMSBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class MainController extends Controller
{
public function indexAction($page, $option)
{
$response = null;
/* Switch statement that determines the page to be loaded. */
return $response;
}
}
?>
The problem is that when I try to go to `localhost/app_dev.php/cms', it gives me the following error:
Controller "MyProject\CMSBundle\Controller\MainController::indexAction()" requires that you provide a value for the "$page" argument (because there is no default value or because there is a non optional argument after this one).
500 Internal Server Error - RuntimeException
However, if I try to visit localhost/app_dev.php/cms/role or localhost/app_dev.php/cms/role/view, it gives me the correct page. I've tried adding a default route to /cms, but it still gives me the same error. How is this possible and how can I fix this?
Thanks in advance.
I don't know what is the difference between what you wrote and
public function indexAction($page = "role", $option = "view")
but maybe you could try it and tell us.

How to receive a variable in method automatically?

Is it possible to define a method like below to receive $id?
public function action_delete($id){
}
I've defined a route
Route::set('tweet', 'tweet/delete/<id>', array('id' => '\d+'))->defaults(array(
'controller'=>'tweet',
'action'=>'delete'
));
I remember a code snippet something like this few months ago...
Updated: I get the following error message
Missing argument 1 for Controller_Tweet::action_delete()
If your Kohana version < 3.2 then you can use this, however it is highly recommended that you get the id value with $this->request->param('id') -> this is the only way since 3.2 version:
public function action_delete(){
$id = $this->request->param('id');
// Rest of tour code
}
In Kohana 3.2, this doesn't work anymore. You'll have to retrieve the variable through
$id = $this->request->param('id');
See: http://kohanaframework.org/3.2/guide/api/Request#param