I have created a form using Propel which submits fine, and validates. The problem comes when I try to commit the $user object - I get a MappingException. I really have no idea where this is coming from as previous references to $user seem to be fine.
Note that the commented line is taken from some of the form guides, but inserts an empty row into the database (though a var_dump of $user shows it has all of the information. I would be happy if I could get that to work as an alternative.
Here's my code:
namespace LifeMirror\APIBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use LifeMirror\APIBundle\Model\Users;
use LifeMirror\APIBundle\Model\UsersQuery;
use LifeMirror\APIBundle\Form\Type\UsersType;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use FOS\RestBundle\View\View;
class RegisterController extends Controller
{
public function indexAction()
{
return $this->processForm(new Users());
}
private function processForm(Users $user)
{
$statusCode = $user->isNew() ? 201 : 204;
$em = $this->getDoctrine()->getEntityManager();
$form = $this->createForm(new UsersType(), $user);
//die(phpinfo());
$form->bind(array(
"firstName" => $this->getRequest()->request->get('firstName'),
"lastName" => $this->getRequest()->request->get('lastName'),
"email" => $this->getRequest()->request->get('email'),
"password" => $this->getRequest()->request->get('password'),
"dob" => array(
"year" => json_decode($this->getRequest()->request->get('dob'))->year,
"month" => json_decode($this->getRequest()->request->get('dob'))->month,
"day" => json_decode($this->getRequest()->request->get('dob'))->day
),
"location" => $this->getRequest()->request->get('location'),
"tutorialWatched" => $this->getRequest()->request->get('tutorialWatched'),
"challengeEmails" => $this->getRequest()->request->get('challengeEmails'),
"mailingList" => $this->getRequest()->request->get('mailingList')
));
if ($form->isValid()) {
//$user->save();
$em->persist($user);
$em->flush();
$response = new Response();
$response->setStatusCode($statusCode);
return $response;
}
$view = View::create($form, 400);
$view->setFormat('json');
return $view;
}
}
Are you sure that you're field names are firstName, lastName and so on? Typically Propel generates field names in capitalized CamelCase, thus I would expect to see FirstName and LastName. If the field names don't match exactly then Propel will not assign the values, and thus result in an empty INSERT. You can dump a list of the User field names like so:
var_dump(BaseUserPeer::getFieldNames());
It seems you try to persist a Propel object using doctrine.
That's surely not what you want, even if it's theorically possible, if you add some doctrine mapping for the Model\Users class.
What you surely want is to persist the $user's state:
if ($form->isValid()) {
$user->save(); // propel object implements Active Record pattern, they can save themselves, no doctrine entityManager needed.
}
Related
I'm trying to unit testing a service that handles the registration of a user in Laravel.
This is the service:
public function completeRegistration(Collection $data)
{
$code = $data->get('code');
$registerToken = $this->fetchRegisterToken($code);
DB::beginTransaction();
$registerToken->update(['used_at' => Carbon::now()]);
$user = $this->userRepository->update($data, $registerToken->user);
$token = $user->createToken(self::DEFAULT_TOKEN_NAME);
DB::commit();
return [
'user' => $user,
'token' => $token->plainTextToken,
];
}
Where the update method has the following signature:
<?php
namespace App\Repositories\User;
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
interface UserRepositoryInterface
{
public function create(Collection $data): User;
public function update(Collection $data, User $user): User;
}
With my test being:
/**
* Test a user can register
*
* #return void
*/
public function test_user_can_complete_registration()
{
$userRepositoryMock = Mockery::mock(UserRepositoryInterface::class);
$registerTokenRepositoryMock = Mockery::mock(RegisterTokenRepository::class);
$userFactory = User::factory()->make();
$registerTokenFactory = RegisterToken::factory()
->for($userFactory)
->timestamped()
->make(['user_id' => $userFactory->id]);
dd($registerTokenFactory->user);
$userRepositoryMock
->expects($this->any())
->once()
->andReturn($userFactory);
....
}
When I run phpunit --filter=test_user_can_complete_registration I get the following error:
1) Tests\Unit\Services\Auth\AuthServiceTest::test_user_can_complete_registration
TypeError: Argument 2 passed to Mockery_0_App_Repositories_User_UserRepositoryInterface::update() must be an instance of App\Models\User, null given, called in /var/www/app/Services/Auth/AuthService.php on line 64
/var/www/app/Services/Auth/AuthService.php:64
/var/www/tests/Unit/Services/Auth/AuthServiceTest.php:88
This tells me that the user relationship on $registerTokenFactory is null. When I do:
public function test_user_can_complete_registration()
{
...
dd($registerTokenFactory->user);
}
I get the output null. I'm trying to test the service without hitting the database. How can I attach the user relationship to the $registerTokenFactory object? I have tried using for and trying to attach directly:
$registerTokenFactory = RegisterToken::factory()
->for($userFactory)
->timestamped()
->make(['user_id' => $userFactory->id, 'user' => $userFactory]);
In Laravel factories make() does only create the model and does not save it. For relationship to work, you will need your models to be saved.
$userFactory = User::factory()->create();
Since you do not want to use a Database, which is wrong in my opinion. People don't like writing tests, so when we have to do it make it simple, mocking everything to avoid databases is a pain. Instead an alternative is to you Sqlite to run in memory, fast and easy. A drawback is some functionality does not work there JSON fields and the version that are in most Ubuntu distributions does not respect foreign keys.
If you want to follow the path you are already on, assigned the user on the object would work, you have some left out bits of the code i assume.
$userRepositoryMock = Mockery::mock(UserRepositoryInterface::class);
$registerTokenRepositoryMock = Mockery::mock(RegisterTokenRepository::class);
$user = User::factory()->make();
$registerToken = RegisterToken::factory()
->for($userFactory)
->timestamped()
->make(['user_id' => $user->id]);
$registerToken->user = $user;
$registerTokenRepositoryMock
->expects('fetchRegisterToken')
->once()
->andReturn($registerToken);
$userRepositoryMock
->expects($this->any())
->once()
->andReturn($user);
// execute the call
I start with Laravel, I write API. I have a method in TestController that checks if the student has correctly inserted data and has access to the exam solution. I do not think it's a good idea to have the whole method in the controller, but I have no idea how to separate it. I think about politics, but I have to have several models for one policy, maybe I can try to put part of the method on AuthorizeStudentRequest or try it in a different way? Of course, now I am returning 200 with the message, but I have to return 422 or another code with errors, but I have not done it because of my problem.
public function authorizeStudent(AuthorizeStudentRequest $request)
{
$hash = $request->input('hash');
$token = $request->input('token');
$exam = Exam::where([['hash', $hash], ['token', $token]])->first();
if($exam == null)
return ['message' => 'Exam does not exist.'];
$user = $exam->user_id;
$studentFirstname = $request->input('firstname');
$studentLastname = $request->input('lastname');
$student = Student::where([
['firstname', $studentFirstname],
['lastname', $studentLastname],
['user_id', $user]
])->first();
if($student == null)
return ['message' => 'Student does not exist.'];
$classroom = Classroom::where([
['name', $classroomName],
['user_id', $user]
])->first();
if($classroom == null)
return ['message' => 'Classroom does not exist.'];
if($student->classroom_id != $classroom->id)
return ['message' => 'Student is not in classroom.'];
if($exam->classrooms()->where(['classroom_id', $classroom->id], ['access', 1])->first() == null)
return ['message' => 'Class does not access to exam yet.'];
}
I would suggest you rather pass the primary keys of the selected $exam, $student and $classroom models to your controller from the form and validate whether they exist in the corresponding tables, rather than having to check their existence using a bunch of different columns.
If you pass the primary keys, you could use the 'exists' validation rule to check if they exist. For example, in your AuthorizeStudentRequest class you could have the following function:
public function rules()
{
return [
'exam_id' => 'required|exists:exams',
'student_id' => 'required|exists:students',
'classroom_id' => 'required|exists:classrooms',
];
}
Otherwise, if you really need to use the different columns to check the existence of the exam, student and classroom, you could create custom validation rules and use them in your AuthorizeStudentRequest class. For example, create a custom validation rule that checks whether the exam exists as follows:
$php artisan make:rule ExamExists
class ExamExists implements Rule
{
private $token;
private $hash;
public function __construct($token, $hash)
{
$this->token = $token;
$this->hash = $hash;
}
public function passes($attribute, $value)
{
return Exam::where([['hash', $hash], ['token', $token]])->count() > 0;
}
}
And then you can use the custom validation rule in your request as follows:
public function rules()
{
return [
'hash' => ['required', new ExamExists($this->hash, $this->token)],
... other validation rules ...
]
}
For checking whether a student has access to a classroom or a class has access to an exam, you could use policies.
API resources present a way to easily transform our models into JSON responses. It acts as a transformation layer that sits between our Eloquent models and the JSON responses that are actually returned by our API. API resources is made of two entities: a resource class and a resource collection. A resource class represents a single model that needs to be transformed into a JSON structure, while a resource collection is used for transforming collections of models into a JSON structure.
Both the resource class and the resource collection can be created using artisan commands:
// create a resource class
$ php artisan make:resource UserResource
// create a resource collection using either of the two commands
$ php artisan make:resource Users --collection
$ php artisan make:resource UserCollection
Before diving into all of the options available to you when writing resources, let's first take a high-level look at how resources are used within Laravel. A resource class represents a single model that needs to be transformed into a JSON structure. For example, here is a simple User resource class:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
Every resource class defines a toArray method which returns the array of attributes that should be converted to JSON when sending the response. Notice that we can access model properties directly from the $this variable. More information here
https://laravel.com/docs/5.7/eloquent-resources
Symfony project PHPunit coverage test
UserController
public function userEdit($id, Request $request)
{
$user = $this->userRepository->findOneByCode($id);
if (!$user) {
throw new Exception("User not found!");
}
$userForm = $this->createForm(UserForm::class, $user);
$userForm->handleRequest($request);
if ($userForm->isSubmitted() && $userForm->isValid()) {
$this->userService->save($user);
return $this->redirectToRoute('user_list');
}
return $this->render(
'user/user.html.twig', [
'form' => $userForm->createView(),
]
);
}
TestUserController
public function testUserEdit()
{
$client = static::createClient();
$crawler = $client->request('GET', '/user/test/edit');
$formData = array(
'username' => 'test',
'email' => 'test#test.nl',
'roles' => 'ROLE_ADMIN'
);
$this->assertEquals(
200,
$client->getResponse()->getStatusCode()
);
$form = $this->factory->create(UserForm::class);
$object = User::fromArray($formData);
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$this->assertEquals($object, $form->getData());
$view = $form->createView();
$children = $view->children;
foreach (array_keys($formData) as $key) {
$this->assertArrayHasKey($key, $children);
}
}
In the userEdit method we have a if loop. But When we run PHPunit coverage test the if loop is not executed. The other if loop for submit is also not covered.
What goes wrong and what can I do in order to cover the test ? Also is this the best solution for Symfony form test since I am new to PHPunit.
I noticed a few things in your code that seem wrong:
The userEdit method name should be userEditAction.
Select the form like this:
$form = $crawler->selectButton('submit')->form();
That 'submit' text is the label on the submit button (e.g. Save).
And then, after filling the fields:
$crawler = $client->submit($form);
You check if the submit was successful by asserting that the resulting HTML page contains expected element (e.g.):
$this->assertGreaterThan(0, $crawler->filter('h1')->count());
Btw. put: $client->followRedirects(true); after instantiating the Client.
Examples are from the official docs.
Regarding the some lines that were not covered by test: whenever you have if clause, you need to test for both conditions. In your case, first you probably have a valid user, instance of User and the other case should be that you pass there an invalid user (null or whatever else). That is usually accomplished by using #dataProvider annotation and method. The data provider method supplies sets of data to the test method. There can be more than one set, so another set contains invalid data to cover the other outcome of the if() clause.
This blog has great examples.
To cover the content of the if-conditions you have to fulfill the conditions new tests. To enter the first if for example you have to write a test where you mock the userRepository and make findOneByCode return null. Then the following if-condition will be executed and throw an exception. Finally you test for a thrown exception in the test.
For the other if-condition you proceed in a similar manner. Write a new test which is designed to fulfill the condition and test the code inside it.
I'm new to symfony and I'm trying to learn but I'm stuck at the moment :
I'm trying to create a form with input already filled, let me explain :
I've got a Session table, linked with an antibiotic (when I create a session, I choose an Antibiotic), Antibiotic table is linked to Result table.
Like this : Tables
What I'm trying to do, is when I click on Valid Result on Result list page, it creates a form of a new result with antibiotic input already filled (because a Session has one Antibiotic)
This is what i've got now (where 6 is the Antibiotic ID from the Session) :
Add Results page
I've tried to look on how edit pages works with no results.
Thanks
/**
* #Route("/session/resultat/{id}", name="addResultat")
*/
public function voirAction($id, Request $request){
//Find the Session object
$em=$this->getDoctrine()->getManager();
$session=$em->getRepository("AppBundle:Session")
->find($id);
if ($session==null)
{
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException("Ouste !");
}
$resultat = new Resultat();
$form = $this->createForm('AppBundle\Form\ResultatType', $resultat);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($resultat);
$em->flush();
return $this->redirectToRoute('resultat_show', array('id' => $resultat->getId()));
}
return $this->render('resultat/addresultats.html.twig', array(
'resultat' => $resultat,
'session' => $session,
'form' => $form->createView(),
));
}
I got it to work finally thanks to ccKep :
Just set my object before creating the form like this :
So after I initialised my new "resultat", i can set his "antibiotique" with setter method and I get it from the $session with the getter method
$resultat->setAntibiotique($session->getAntibiotique());
This code works just fine :
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class TableManagerController extends Controller
{
public function listAndAddAction(Request $request)
{
// We get the Entity Manager
$entityManager = $this->getDoctrine()->getManager();
// We get the entity repository
$repository = $entityManager->getRepository($this->entityRepository);
// We build the new form through Form Factory service
$form = $this->get('form.factory')->create($this->entityFormObject, $this->entityObject);
// If user sent the form and sent data is valid
if ($form->handleRequest($request)->isValid())
{
// We set the position of the new entity to the higher existing one + 1
$newPosition = $repository->higherPosition() + 1;
$this->entityObject->setPosition($newPosition);
// We insert the data in DB
$entityManager->persist($this->entityObject);
$entityManager->flush();
// We redirect user to the defined homepage
return $this->redirect($this->generateUrl($this->routeHomePage));
}
return $this->render($this->renderIndexTemplate, array(
'dataList' => $repository->listAll(),
'form' => $form->createView()
));
}
}
But when I just split it in 3 methods, like this :
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class TableManagerController extends Controller
{
public function listAndAddAction(Request $request)
{
$dataList = $this->listMethod();
$form = $this->addMethod($request);
return $this->render($this->renderIndexTemplate, array(
'dataList' => $dataList,
'form' => $form
));
}
protected function listMethod()
{
// We get the Entity Manager
$entityManager = $this->getDoctrine()->getManager();
// We get the entity repository
$repository = $entityManager->getRepository($this->entityRepository);
// We generate the entity management homepage view (list + add form)
return $repository->listAll();
}
protected function addMethod(Request $request)
{
// We get the Entity Manager
$entityManager = $this->getDoctrine()->getManager();
// We get the entity repository
$repository = $entityManager->getRepository($this->entityRepository);
// We build the new form through Form Factory service
$form = $this->get('form.factory')->create($this->entityFormObject, $this->entityObject);
// If user sent the form and sent data is valid
if ($form->handleRequest($request)->isValid())
{
// We set the position of the new entity to the higher existing one + 1
$newPosition = $repository->higherPosition() + 1;
$this->entityObject->setPosition($newPosition);
// We insert the data in DB
$entityManager->persist($this->entityObject);
$entityManager->flush();
// We redirect user to the defined homepage
return $this->redirect($this->generateUrl($this->routeHomePage));
}
// We return the generated form
return $form->createView();
}
}
I get this error which appears once I've sent the form :
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\FormRenderer::renderBlock() must be an instance of Symfony\Component\Form\FormView, instance of Symfony\Component\HttpFoundation\RedirectResponse given, called in D:\Websites\CPG-2015\app\cache\dev\twig\d6\80\0e5eee6c7aa1859cedb4cd0cc7317a0ebbdd61af7e80f217ce1d2cf86771.php on line 61 and defined in D:\Websites\CPG-2015\vendor\symfony\symfony\src\Symfony\Component\Form\FormRenderer.php line 106") in IBCPGAdministrationBundle:CourseLevel:index.html.twig at line 19.
for which I understand there is something wrong with the form. But I really don't get why since this same form, from the same view, appears perfectly well before I send it.
The problem is here in your addMethod:
// We redirect user to the defined homepage
return $this->redirect($this->generateUrl($this->routeHomePage));
which in turn gets used here without any handling of that return possibility:
$form = $this->addMethod($request);
return $this->render($this->renderIndexTemplate, array(
'dataList' => $dataList,
'form' => $form
));
By returning $this->redirect inside of an if-statement, you're giving two potential return values of addMethod, a FormView or a RedirectResponse. As a result, you then try to pass that RedirectResponse through form which Twig attempts to render (which it can't, of course.)
The solution is to re-work your return logic!