I'm new learner of Symfony 4 and I'm looking for help. I've an Entity named "Player" and I want to generate a random confirmation number.
For now, I'm using a variable $confirmNbr and I save the $confirm in my database with $participant->setConfirmationNumber($confirmNbr);.
What I want it's create a function generateRandomNumber() in my Entity Player.php like this :
public function generateConfirmationNumber() : self
{
$this->confirmationNumber = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',6)),0,5);
return $this;
}
This is my Controller file
/**
* #Route("/", name="homepage")
*/
public function new(Player $player, EntityManagerInterface $em, Request $request)
{
$participant = $this->playerrepo->findAll();
$form = $this->createForm(PlayerFormType::class);
$randomNbr = $player->generateConfirmationNumber();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$participant = new Player;
$participant->setName($data['name']);
$participant->setFirstname($data['firstname']);
$participant->setEmail($data['email']);
$participant->setConfirmationNumber($confirmNbr);
$participant->setRegisterAt(new \DateTime);
$em->persist($player);
$em->flush();
$this->addFlash('success', 'Player added!');
return $this->redirectToRoute('homepage');
}
return $this->render('app/subscribe.html.twig', [
'playerForm' => $form->createView(),
'player'=>$player,
]);
}
And this is my error message :
Unable to guess how to get a Doctrine instance from the request
information for parameter "player".
Can you help me please ?
Your method is expecting an instance of the Player object - where should it come from? Doctrine is trying to guess it and get it from the URL, but it cannot. Your method is for creating new players - why do you need an instance of a player? Just remove that parameter from the method signature, i.e. change it to:
public function new(EntityManagerInterface $em, Request $request)
I've found the solution. I've modified my set function and deleted my function that I've added. Everything works!
Related
I have a generate() method on my class which is just a shorthand way to create an instance of the class. It accepts a request which is type hinted on the method. I am trying to unit test this and the only way I know how is to make an answer and pass that through. That doesn’t work tho because it is not a request. Is there a work around for this? Here is the method:
public static function generate(Question $question, Request $request): self
{
return self::create([
'user_id' => Auth::user()->getKey(),
'question_id' => $question->getKey(),
'answer_body' => $request->answer_body,
]);
}
Here is the test
/** #test */
public function it_can_generate_a_new_instance()
{
$user = factory(User::class)->create();
$this->actingAs($user);
$question = factory(Question::class)->create();
$answer = factory(Answer::class)->make();
Answer::generate($question, $answer);
$this->assertEquals($user->getKey(), Answer::first()->user_id);
$this->assertEquals($question->getKey(), Answer::first()->question_id);
$this->assertEquals($answer->answer_body, Answer::first()->answer_body);
}
The test passes until I type hint Request in the method.
You can make a new request object with the given property. It's probably a bit flimsy but it should work:
public function it_can_generate_a_new_instance()
{
$user = factory(User::class)->create();
$this->actingAs($user);
$question = factory(Question::class)->create();
$answer = factory(Answer::class)->make();
$request = new Request([ 'answer_body' => $answer->answer_body ]);
Answer::generate($question, $request);
$this->assertEquals($user->getKey(), Answer::first()->user_id);
$this->assertEquals($question->getKey(), Answer::first()->question_id);
$this->assertEquals($answer->answer_body, Answer::first()->answer_body);
}
a request should only be present on a controller, and not in the Model Answer (Or you will encounter errors like that ^^)
If your process require a request, then you should test an http request instead :
/** #test */
public function it_can_generate_a_new_instance()
{
$user = factory(User::class)->create();
$this->actingAs($user);
$question = factory(Question::class)->create();
$answer = factory(Answer::class)->make();
$this->post(route('answer.store'), $answer->toArray());
// Then your answer will be generated in your controller
$this->assertEquals($user->getKey(), Answer::first()->user_id);
$this->assertEquals($question->getKey(), Answer::first()->question_id);
$this->assertEquals($answer->answer_body, Answer::first()->answer_body);
}
I want to use a single controller to save my comments for multiple models. So I created the CommentController, with the following store method:
public function store(Teacher $teacher, Request $request)
{
$input = $request->all();
$comment = new Comment();
$comment->user_id = Auth::user()->id;
$comment->body = $input['body'];
$teacher->comments()->save($comment);
return redirect()->back();
}
In my view, I have:
{!! Form::open([
'route' => ['teachers.comments.store', $teacher->id]
]) !!}
This is working. If I want to use the same CommentController to store the comments for a school, how should I modify the store method of the controller?
Adam's solution is great, but I would not hard-code the model's namespace that way. Instead, what I would do is make use of Laravel's Relation::morphMap(), you can check it out here: https://laravel.com/docs/5.6/eloquent-relationships#polymorphic-relations
That way, you will also make your database entries more readable. I recommend using a service provider to map the morphs.
Also, the Model base class has a getMorphClass() method, so instead of
$comment->commentable_type = 'App\\Models\\'.$model;
I would use
$comment->commentable_type = $model->getMorphClass();
That way you integrate Laravel's logic into your code.
Im not sure if this is the Laravel convension, but i have done the following:
Made a route:
Route::post('/Comment/{model}/{id}', [
// etc
]);
Then in the controller get the model and check against an array of allowed models, pass the id through and attach:
public function store(Request $request, $model, $id) {
$allowed = ['']; // list all models here
if(!in_array($model, $allowed) {
// return redirect back with error
}
$comment = new Comment();
$comment->user_id = $request->user()->id;
$comment->commentable_type = 'App\\Models\\'.$model;
$comment->commentable_id = $id;
$comment->body = $request->body;
$comment->save();
return redirect()->back();
}
Like I say, there is most likely a much better way to accomplish, but this is how I've done it. It keeps it short and sweet and checks if the model can take a comment.
I implemented this way if you want, according to me it's the one of the bests way to do that.
// Route::post('/comments/{model}/{id}', 'CommentController#store');
class CommentController extends Controller {
protected $model;
public function __construct()
{
$this->model = Relation::getMorphedModel(
request()->route()->parameter('model')
);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($this->model); // return 'App\Post' or null
}
}
For an API I'm currently building I'd like to be able to send a request with a JSON body with the following content
{"title": "foo"}
to create a new database record for an Entity called Project.
I made a controller which subclasses FOSRestController. To create a project, I made an action
/**
* #Route("/")
*
* #ApiDoc(
* section="Project",
* resource=true,
* input={"class"="AppBundle\Form\API\ProjectType"},
* description="Creates a new project",
* statusCodes={
* 201="Returned when successful",
* }
* )
*
* #Method("POST")
* #Rest\View(statusCode=201)
*/
public function createProjectAction(Request $request)
{
$project = new Project();
$form = $this->createForm(ProjectType::class, $project);
$form->submit(($request->request->get($form->getName())));
if ($form->isSubmitted() && $form->isValid()) {
return $project;
}
return View::create($form, 400);
}
The ProjectType looks like this
class ProjectType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Project'
));
}
}
However, when I try to post said JSON to the API, it responds that the title property cannot be blank, which is good because that's the validation rule set for it. However, it IS set. I suddenly realized I have to send the JSON prefixed by the actual object's name to make this work:
{"project":{"title": "bla"}}
Which feels a little strange to be fair, it should be enough to just post the properties.
So, based on this information I simply have 2 questions:
Why do I need to "submit" this form with ($request->request->get($form->getName())), shouldn't $request be enough?
What do I need to change for the FormType to validate the entity as is, instead of prefixing it with the entity's name?
Edit 1: adding or removing the data_class in the Default Options does not change the behaviour at all.
This is because of how Symfony Controller "createForm" helper method works. Reasoning behind it is that multiple forms could have same target URL. By prefixing with form name, Symfony can know which form was submitted.
This can be seen by looking at "createForm" method implementation:
public function createForm($type, $data = null, array $options = array())
{
return $this->container->get('form.factory')->create($type, $data, $options);
}
If you don't want this behavior, it's really easy to change it:
public function createProjectAction(Request $request)
{
$project = new Project();
$form = $this->get('form.factory')->createNamed(null, new ProjectType(), $project);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
return $project;
}
return View::create($form, 400);
}
So you're basically creating a "nameless" form. Since you're building an API, it's probably a good idea to pull this into a createNamelessForm($type, $data, $options) helper method in your base controller so that you don't have to get Form Factory from container explicitly all the time and make it easier on the eyes.
Comment on your edit
Wrapper key is not generated by "data_class" option, but by "getName()" method on your form type.
Alright so I'm pretty new to both unit testing, mockery and laravel. I'm trying to unit test my resource controller, but I'm stuck at the update function. Not sure if I'm doing something wrong or just thinking wrong.
Here's my controller:
class BooksController extends \BaseController {
// Change template.
protected $books;
public function __construct(Book $books)
{
$this->books = $books;
}
/**
* Store a newly created book in storage.
*
* #return Response
*/
public function store()
{
$data = Input::except(array('_token'));
$validator = Validator::make($data, Book::$rules);
if($validator->fails())
{
return Redirect::route('books.create')
->withErrors($validator->errors())
->withInput();
}
$this->books->create($data);
return Redirect::route('books.index');
}
/**
* Update the specified book in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
$book = $this->books->findOrFail($id);
$data = Input::except(array('_token', '_method'));
$validator = Validator::make($data, Book::$rules);
if($validator->fails())
{
// Change template.
return Redirect::route('books.edit', $id)->withErrors($validator->errors())->withInput();
}
$book->update($data);
return Redirect::route('books.show', $id);
}
}
And here are my tests:
public function testStore()
{
// Add title to Input to pass validation.
Input::replace(array('title' => 'asd', 'content' => ''));
// Use the mock object to avoid database hitting.
$this->mock
->shouldReceive('create')
->once()
->andReturn('truthy');
// Pass along input to the store function.
$this->action('POST', 'books.store', null, Input::all());
$this->assertRedirectedTo('books');
}
public function testUpdate()
{
Input::replace(array('title' => 'Test', 'content' => 'new content'));
$this->mock->shouldReceive('findOrFail')->once()->andReturn(new Book());
$this->mock->shouldReceive('update')->once()->andReturn('truthy');
$this->action('PUT', 'books.update', 1, Input::all());
$this->assertRedirectedTo('books/1');
}
The issue is, when I do it like this, I get Mockery\Exception\InvalidCountException: Method update() from Mockery_0_Book should be called exactly 1 times but called 0 times. because of the $book->update($data) in my controller. If I were to change it to $this->books->update($data), it would be mocked properly and the database wouldn't be touched, but it would update all my records when using the function from frontend.
I guess I simply just want to know how to mock the $book-object properly.
Am I clear enough? Let me know otherwise. Thanks!
Try mocking out the findOrFail method not to return a new Book, but to return a mock object instead that has an update method on it.
$mockBook = Mockery::mock('Book[update]');
$mockBook->shouldReceive('update')->once();
$this->mock->shouldReceive('findOrFail')->once()->andReturn($mockBook);
If your database is a managed dependency and you use mock in your test it causes brittle tests.
Don't mock manage dependencies.
Manage dependencies: dependencies that you have full control over.
I'm getting following exception, when I try to add a task and automaticly add the current user to the accordion field of the task:
Catchable Fatal Error: Argument 1 passed to Seotool\MainBundle\Entity\Task::setUser() must be an instance of Seotool\MainBundle\Entity\User, string given, called in /Applications/MAMP/htdocs/Seotool/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 438 and defined in /Applications/MAMP/htdocs/Seotool/src/Seotool/MainBundle/Entity/Task.php line 174
My Controller looks like this:
/**
#Route(
* path = "/taskmanager/user/{user_id}",
* name = "taskmanager"
* )
* #Template()
*/
public function taskManagerAction($user_id, Request $request)
{
/* #### NEW TASK FORM #### */
$task = new Task();
$addTaskForm = $this->createForm(new TaskType(), $task);
$addTaskForm->handleRequest($request);
if($addTaskForm->isValid()):
$task->setDone(FALSE);
$task->setUser($user_id);
$task->setDateCreated(new \DateTime());
$task->setDateDone(NULL);
$em = $this->getDoctrine()->getManager();
$em->persist($task);
$em->flush();
return $this->redirect($this->generateUrl('taskmanager', array('user_id' => $user_id)));
endif;
Line 174 in Entity/Task.php:
/**
* Set User
*
* #param \Seotool\MainBundle\Entity\User $user
* #return Task
*/
public function setUser(\Seotool\MainBundle\Entity\User $user = null)
{
$this->User = $user;
return $this;
}
Does anybody know how to set the value for my hidden "user" form field with $user_id value?
Thanks in advance
You can't do
$task->setUser($user_id);
because $user_id, here, is a string and your method signature expects an object of Seotool\MainBundle\Entity\User type.
You can proceed in two ways, depending on your needs (but I suppose that only second option will be suitable for you):
1) Modify your setUser() function if you don't need a reference (ORM oriented) or User Object into your Task Object
public function setUser($user = null)
{
$this->User = $user;
return $this;
}
2) Retrieve current user and set it
public function taskManagerAction($user_id, Request $request)
{
[...]
if($addTaskForm->isValid()):
$user = $this->get('security.context')->getToken()->getUser();
$task->setUser($user);
[...]
endif;
$this->get('security.context')->getToken()->getUser(); gives you current logged user but, maybe, you need to change signature aswell (possible inheritance issues?)