laravel 4 many to many relationship integration test not passing - php

Every entity is being saved and has an ai id. Only the test on line 34 is not passing.
<?php
class AssignmentTest extends TestCase
{
protected $assignment;
public function setUp()
{
parent::setUp();
$this->assignment = new Assignment;
}
public function testAssignmentAssociations()
{
$facility = Facility::create(array(
'name' => 'test facility',
'code' => 'test-facility'
));
$user = new User;
$user->id = 1;
$shift = Shift::create(array(
'name' => 'test shift',
'start_time' => 0,
'end_time' => 600
));
$this->assignment->facilities()->save($facility);
$this->assignment->shift()->associate($shift);
$this->assignment->user()->associate($user);
$this->assignment->save();
$this->assertEquals(1, $facility->id);
$this->assertEquals(1, $this->assignment->facilities()->count());
$this->assertEquals(1, $this->assignment->shift_id);
$this->assertEquals(1, $this->assignment->user_id);
}
}
// Facility model
public function assignments()
{
return $this->belongsToMany('Assignment', 'assignment_facilities');
}
// Assignment model
public function facilities()
{
return $this->belongsToMany('Facility', 'assignment_facilities');
}
Edit
Apparently removing this line will make the test successful. Now I'm interested as to why when saving the assignment the association will no longer return a successful count?
<?php
$this->assignment->save();

I havent tested - but dont you need to 'save' the shift and user records themselves, before you try and associate them against the assignment?
$user = new User;
$user->id = 1;
$shift = Shift::create(array(
'name' => 'test shift',
'start_time' => 0,
'end_time' => 600
));
$shift->id = 1;
$this->assignment->facilities()->save($facility);
$this->assignment->shift()->save($shift);
$this->assignment->user()->save($user);
$this->assignment->shift()->associate($shift);
$this->assignment->user()->associate($user);
$this->assignment->save();

Related

Laravel test class property not persisting

I'm trying to set a sensorId property that will be used by every method in my feature test. The problem is that it is not persisting between tests.
Here is my test class:
class LogApiPostTest extends TestCase
{
public $sensorId;
public function setUp(): void
{
parent::setUp();
$this->sensorId = 'SEN' . rand(10000000, 999999999);
}
public function test_creates_sensor()
{
$response = $this->post('/api/logs', $this->data([
//
]));
$response->assertStatus(200);
$response->assertSeeText('Sensor created');
}
public function test_updates_sensor()
{
$response = $this->post('/api/logs', $this->data([
//
]));
$response->assertStatus(200);
$response->assertSeeText('Sensor updated');
}
public function test_creates_car()
{
$response = $this->post('/api/logs', $this->data([
'CarID' => 'B',
]));
$response->assertStatus(200);
$response->assertSeeText('Car created');
}
public function test_updates_car()
{
$response = $this->post('/api/logs', $this->data([
'CarID' => 'B',
]));
$response->assertStatus(200);
$response->assertSeeText('Car updated');
}
public function test_is_unauthorized()
{
$response = $this->post('/api/logs', $this->data([
'token_id' => 'thisShouldntWork',
]));
$response->assertUnauthorized();
}
public function data($merge = [])
{
return array_merge([
'token_id' => config('api.token'),
'sensorID' => $this->sensorId,
'CarID' => 'A',
'LogType' => 'CarLog',
'Time' => time(),
'DrctnFlr' => '-02',
'MCSS' => 'STB',
'OpMode' => 'INA',
'DoorStats' => '][**',
'DoorCmd' => 'C6:C1>',
'OCSS' => 'GTN02',
'Load' => '002%',
], $merge);
}
}
I just want the sensorId property to persist for all tests in this class.
When there is a dependency between tests, you must use the #depends annotation.
This allows to ensure the execution order of tests is correct and also to pass values between tests. Instead of making sensorId to persist, just pass it from test to test.
https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#test-dependencies

Phpunit test a method using a service

I'm trying to test a method which is using a service, and apparently it's not possible to test it like a normal method.
Does someone know what to do ?
I have this code for the moment :
namespace PlatformBundle\Tests;
use PlatformBundle\Controller\PaymentController;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PaymentControllerTest extends WebTestCase
{
private $payment;
public function __construct() { parent::__construct(); $this->payment = new PaymentController(); }
public function testSendEmail()
{
$param = array(
'info' => array(
'email' => 'test#test.com', 'name' => 'test', 'fare' => 'test', 'id' => 'test'
)
);
$this->assertEquals(true, $this->invokeMethod($this->payment, 'sendEmail', $param));
}
/**
* Call protected/private method of a class.
*
* #param object &$object Instantiated object that we will run method on.
* #param string $methodName Method name to call
* #param array $parameters Array of parameters to pass into method.
*
* #return mixed Method return.
*/
public function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
}
The controller where the method sendEmail is :
<?php
namespace PlatformBundle\Controller;
use PlatformBundle\Entity\Customer;
use PlatformBundle\Entity\Promocode;
use PlatformBundle\Entity\Transfer;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class PaymentController extends Controller
{
public function checkoutAction(Request $req)
{
if (! $req->isMethod('POST')) throw new AccessDeniedHttpException();
$info = $req->request->all();
$this->container->get('platform.formSecurity')->testAllInformation($info);
$this->saveCustomerIntoDb($info);
$info['payed'] = false;
$session = $req->getSession();
$session->set('info', $info);
$info['date'] = $this->container->get('platform.useful')->reverseDateFormat($info['date']);
return $this->render('PlatformBundle:Payment:checkout.html.twig', array(
'isIndex' => false,
'info' => $info,
'stripe' => $this->stripeConfig()
));
}
public function cancelAction(Request $req)
{
$req->getSession()->invalidate();
return $this->render('PlatformBundle:Payment:cancel.html.twig', array('isIndex' => false));
}
public function successAction(Request $req)
{
$session = $req->getSession();
$info = $session->get('info');
if ($info['payed']) {
$req->getSession()->invalidate();
if ($info === null) throw new Exception('Please contact us to make sure that the payment has been done and that your order has been taken into account.');
$this->saveTransferIntoDb($info);
$customer = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Customer')->findOneBy(array(
'email' => $info['email']
));
$transfer = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Transfer')->findOneBy(
array('customer' => $customer->getId()),
array('id' => 'desc'),
1
);
$info['id'] = $transfer->getId();
$info['date'] = $this->container->get('platform.useful')->reverseDateFormat($info['date']);
$this->sendEmail($info);
// if 5 payments done, send a promocode
if (is_int($customer->getPayments() / 5)) {
$this->createAndSendNewPromocode($customer);
}
return $this->render('PlatformBundle:Payment:success.html.twig', array(
'isIndex' => false,
'info' => $info
));
} else return new RedirectResponse('cancel');
}
private function sendEmail($info)
{
$mail = $this->container->get('platform.mail');
$mail->send(
$info['email'],
'You have ordered a transfer for Dublin',
$this->renderView('PlatformBundle:Mail:orderSucceed.html.twig', array('info' => $info)),
'info#dubair.ie'
);
$mail->send(
'info#airportcollections.net, info#dubair.ie, info#365onlineholidays.com',
'A customer ordered a transfer for Dublin',
$this->renderView('PlatformBundle:Mail:report.html.twig', array('info' => $info)),
'info#dubair.ie'
);
}
private function saveCustomerIntoDb($info)
{
// test if the customer already exist
$customersList = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Customer')
->findByEmail($info['email']);
$customerExists = (sizeof($customersList) == 1 ? true : false);
if ($customerExists) {
$customer = $customersList[0];
} else {
// Create the entity
$customer = new Customer();
// dateRegistration, country and ip are automatically created in the constructor
$customer->setEmail($info['email']);
$customer->setPayments(0);
}
$customer->setName($info['name']);
$customer->setPhone($info['phone']);
$em = $this->getDoctrine()->getManager();
$em->persist($customer);
$em->flush();
}
private function saveTransferIntoDb($info)
{
$customers = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Customer')
->findByEmail($info['email']);
$customer = $customers[0];
$customer->setPayments($customer->getPayments() + 1);
// make promocode outdated
if ($info['promocode'] != '') {
$promocode = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Promocode')
->findOneBy(array(
'value' => $info['promocode'],
'outdated' => 0,
'type' => 'short'
));
$promocode->setOutdated(1);
}
// test if transfer already exist
$transferList = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Transfer')->findBy(
array(
'customer' => $customer,
'pickup' => $info['pickup'],
'destination' => $info['destination'],
'pickupTime' => $info['pickupTime'],
'address' => $info['address']
), // criteria
array('pickup' => 'desc'), // sorting
5, // Limit
0 // Offset
);
// if transfer doesn't already exist, create it
if (sizeof($transferList) == 0) {
$transfer = new Transfer();
$transfer->setPickup($info['pickup']);
$transfer->setDestination($info['destination']);
$dateArray = explode('-', $info['date']);
$transfer->setDate(new \DateTime($dateArray[2].'-'.$dateArray[1].'-'.$dateArray[0]));
$transfer->setAddress($info['address']);
$transfer->setFlightTime($info['flightTime']);
$transfer->setPickupTime($info['pickupTime']);
$transfer->setSeats($info['seats']);
$transfer->setAirline($info['airline']);
$transfer->setFlight($info['flight']);
$transfer->setType($info['type']);
$transfer->setBags($info['bags']);
$transfer->setFare($info['fare']);
// join
$transfer->setCustomer($customer);
$em = $this->getDoctrine()->getManager();
$em->persist($transfer);
$em->flush();
}
}
private function createAndSendNewPromocode($customer)
{
$newPromocode = $this->container->get('platform.useful')->createRandomPassword();
$promocode = new Promocode();
$promocode->setValue($newPromocode);
$promocode->setType('short');
$promocode->setDiscount(10);
$em = $this->getDoctrine()->getManager();
$em->persist($promocode);
$em->flush();
$mail = $this->container->get('platform.mail');
$mail->send(
$customer->getEmail(),
'A promotional code for your next transfer on dubair.ie !',
$this->renderView('PlatformBundle:Mail:promocode.html.twig', array(
'customer' => $customer,
'promocode' => $newPromocode
)),
'info#dubair.ie'
);
}
private function stripeConfig()
{
$stripe = array(
"secret_key" => "xx",
"publishable_key" => "xx"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
return $stripe;
}
public function stripeChargeAction(Request $req)
{
$this->stripeConfig();
$info = $req->getSession()->get('info');
$amount = ($info['fare'] * 100);
$info['payed'] = true;
$req->getSession()->set('info', $info);
$token = $req->request->get('stripeToken');
$customer = \Stripe\Customer::create(array(
'email' => $req->request->get('email'),
'card' => $token
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $amount,
'currency' => 'eur'
));
return new RedirectResponse('success');
}
}
thanks

Laravel seeder not working

Can anyone see what seems to be the problem with my seeder?
public function run()
{
$faker = Faker\Factory::create();
$limit = 30;
$userId = App\User::all()->lists('id')->toArray();
$status = App\OrderStatus::all()->lists('id')->toArray();
for ($i = 0; $i < $limit; $i++) {
$chosenUserId = $faker->randomElement($userId);
$user = App\User::find($chosenUserId);
DB::table('orders')->insert([
'created_at' => $faker->date('Y-m-d'),
'user_id' => $chosenUserId,
'status_id' => $faker->randomElement($status),
'draft' => false,
'address_id' => $user->addresses->first()->id
]);
}
}
I keep getting trying to get property of non-object error, and I suppose it's because of the last line where I'm attaching address_id.
When I take a look at the DB what was created until that point, users and addresses are created fine, and each user has an address assigned to him.
In user model i have:
public function addresses()
{
return $this->hasMany('App\Address');
}
Here:
addresses -> addresses()
Since your User model have a function to define the relationship with address model, you should call it as a function not as object attributes:
DB::table('orders')->insert([
'created_at' => $faker->date('Y-m-d'),
'user_id' => $chosenUserId,
'status_id' => $faker->randomElement($status),
'draft' => false,
'address_id' => $user->addresses()->first()->id
]);

Laravel Validation With Multi Select

I'm making a multi select form element for updating schools and specialties pivot table school_specialty. The problem is that when I change only something in multi select not other inputs or textareas, I can't listen model events so I can't sync school_specialty table. But when I fill in any other input it's works perfect. Here's my multi select from blade:
{{Form::select('specialties[]', $specialties_data, $school->specialties, array('multiple' => 'true', 'id' => 'multi-select'))}}
This is my update method from school controller:
public function update($id)
{
$data = Input::only('name', 'type_id', 'description', 'info_specialties', 'contacts', 'specialties', 'financing_id', 'district_id', 'city_id');
$school = School::find($id);
$school->name = $data['name'];
$school->type_id = $data['type_id'];
$school->description = $data['description'];
$school->info_specialties = $data['info_specialties'];
$school->contacts = $data['contacts'];
$school->cover_photo = Input::file('cover_photo');
$school->set_specialties = $data['specialties'];
$school->financing_id = $data['financing_id'];
$school->set_district_id = $data['district_id'];
$school->city_id = $data['city_id'];
try {
$school->save();
} catch (ValidationException $errors) {
return Redirect::route('admin.schools.edit', array($id))
->withErrors($errors->getErrors())
->withInput();
}
return Redirect::route('admin.schools.edit', array($id))
->withErrors(array('mainSuccess' => 'School was created.'));
}
And here's my example school model:
<?php
class School extends Eloquent {
protected $table = 'schools';
protected $fillable = array('name', 'type_id', 'description', 'city');
protected $guarded = array('id');
protected $appends = array('specialties');
public $set_specialties;
public $set_district_id;
protected static function boot()
{
parent::boot();
static::updating(function($model)
{
$data = array(
'name' => $model->name,
'type_id' => $model->type_id,
'description' => $model->description,
'specialties' => $model->set_specialties,
'city_id' => $model->city_id
);
$rules = array(
'name' => 'required|min:3|max:50',
'type_id' => 'required|min:1|max:300000',
'description' => 'required|min:10',
'specialties' => 'required|array',
'city_id' => 'required|min:1|max:300000'
);
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
throw new ValidationException(null, null, null, $validator->messages());
} else {
return true;
}
});
static::updated(function($model)
{
if ( $model->set_specialties != null )
{
$model->specialty()->sync($model->set_specialties);
}
});
}
public function specialty()
{
return $this->belongsToMany('Specialty', 'school_specialty');
}
}
?>
When updating only school specialities the School model events aren't triggered because the School model stays the same.
I think the simplest and most elegant solution is to touch the school model instance. This will modify the updated_at field for the School object and thus trigger the model events.
To do this add the following lines before the try/catch block:
if ($school->set_specialties !== null) {
$school->touch();
}
Also, validation shouldn't be handled in the model observers. Check form request validation here: https://laravel.com/docs/5.6/validation#form-request-validation.

Seeding database - [ErrorException] Trying to get property of non-object

I am using "zizaco/confide": "~4.0#dev" and "zizaco/entrust": "1.2.*#dev".
I have set everything up as described in the two tutorials(confide migrations). Furthermore, I have created the following models:
User:
<?php
use Zizaco\Confide\ConfideUser;
use Zizaco\Confide\Confide;
use Zizaco\Confide\ConfideEloquentRepository;
use Zizaco\Entrust\HasRole;
use Carbon\Carbon;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface{
use ConfideUser;
use HasRole;
/**
* Get user by username
* #param $username
* #return mixed
*/
public function getUserByUsername( $username )
{
return $this->where('username', '=', $username)->first();
}
public function joined()
{
return String::date(Carbon::createFromFormat('Y-n-j G:i:s', $this->created_at));
}
public function saveRoles($inputRoles)
{
if(! empty($inputRoles)) {
$this->roles()->sync($inputRoles);
} else {
$this->roles()->detach();
}
}
public function currentRoleIds()
{
$roles = $this->roles;
$roleIds = false;
if( !empty( $roles ) ) {
$roleIds = array();
foreach( $roles as &$role )
{
$roleIds[] = $role->id;
}
}
return $roleIds;
}
public static function checkAuthAndRedirect($redirect, $ifValid=false)
{
// Get the user information
$user = Auth::user();
$redirectTo = false;
if(empty($user->id) && ! $ifValid) // Not logged in redirect, set session.
{
Session::put('loginRedirect', $redirect);
$redirectTo = Redirect::to('user/login')
->with( 'notice', Lang::get('user/user.login_first') );
}
elseif(!empty($user->id) && $ifValid) // Valid user, we want to redirect.
{
$redirectTo = Redirect::to($redirect);
}
return array($user, $redirectTo);
}
public function currentUser()
{
return (new Confide(new ConfideEloquentRepository()))->user();
}
public function getReminderEmail()
{
return $this->email;
}
}
Role:
<?php
use Zizaco\Entrust\EntrustRole;
class Role extends EntrustRole {
public function validateRoles( array $roles )
{
$user = Confide::user();
$roleValidation = new stdClass();
foreach( $roles as $role )
{
// Make sure theres a valid user, then check role.
$roleValidation->$role = ( empty($user) ? false : $user->hasRole($role) );
}
return $roleValidation;
}
}
Permission:
<?php
use Zizaco\Entrust\EntrustPermission;
class Permission extends EntrustPermission
{
public function preparePermissionsForDisplay($permissions)
{
// Get all the available permissions
$availablePermissions = $this->all()->toArray();
foreach($permissions as &$permission) {
array_walk($availablePermissions, function(&$value) use(&$permission){
if($permission->name == $value['name']) {
$value['checked'] = true;
}
});
}
return $availablePermissions;
}
/**
* Convert from input array to savable array.
* #param $permissions
* #return array
*/
public function preparePermissionsForSave( $permissions )
{
$availablePermissions = $this->all()->toArray();
$preparedPermissions = array();
foreach( $permissions as $permission => $value )
{
// If checkbox is selected
if( $value == '1' )
{
// If permission exists
array_walk($availablePermissions, function(&$value) use($permission, &$preparedPermissions){
if($permission == (int)$value['id']) {
$preparedPermissions[] = $permission;
}
});
}
}
return $preparedPermissions;
}
}
Furthermore, I would like to seed my database in the beginning with values, therefore I created several seeders for user, role and permission. However, I get an error in my permission seeder:
UserTableSeeder:
<?php
class UsersTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
$users = array(
array(
'username' => 'admin',
'email' => 'admin#example.org',
'password' => Hash::make('admin'),
'confirmed' => 1,
'confirmation_code' => md5(microtime().Config::get('app.key')),
'created_at' => new DateTime,
'updated_at' => new DateTime,
),
array(
'username' => 'moderator',
'email' => 'moderator#example.org',
'password' => Hash::make('moderator'),
'confirmed' => 1,
'confirmation_code' => md5(microtime().Config::get('app.key')),
'created_at' => new DateTime,
'updated_at' => new DateTime,
),
array(
'username' => 'user',
'email' => 'user#example.org',
'password' => Hash::make('user'),
'confirmed' => 1,
'confirmation_code' => md5(microtime().Config::get('app.key')),
'created_at' => new DateTime,
'updated_at' => new DateTime,
)
);
DB::table('users')->insert( $users );
}
}
RolesTableSeeder:
<?php
class RolesTableSeeder extends Seeder {
public function run()
{
DB::table('roles')->delete();
$adminRole = new Role;
$adminRole->name = 'adminRole';
$adminRole->save();
$standRole = new Role;
$standRole->name = 'userRole';
$standRole->save();
$modRole = new Role;
$modRole->name = 'modRole';
$modRole->save();
$user = User::where('username','=','admin')->first();
$user->attachRole( $adminRole );
$user = User::where('username','=','user')->first();
$user->attachRole( $standRole );
$user = User::where('username','=','moderator')->first();
$user->attachRole( $modRole );
}
}
PermissionsTableSeeder:
<?php
class PermissionsTableSeeder extends Seeder {
public function run()
{
DB::table('permissions')->delete();
$permissions = array(
array( // 1
'name' => 'manage_users',
'display_name' => 'manage users'
),
array( // 2
'name' => 'manage_roles',
'display_name' => 'manage roles'
),
array( // 3
'name' => 'standart_user_role',
'display_name' => 'standart_user_role'
),
);
DB::table('permissions')->insert( $permissions );
DB::table('permission_role')->delete();
$role_id_admin = Role::where('name', '=', 'admin')->first()->id;
$role_id_mod = Role::where('name', '=', 'moderator')->first()->id;
$role_id_stand = Role::where('name', '=', 'user')->first()->id;
$permission_base = (int)DB::table('permissions')->first()->id - 1;
$permissions = array(
array(
'role_id' => $role_id_admin,
'permission_id' => $permission_base + 1
),
array(
'role_id' => $role_id_admin,
'permission_id' => $permission_base + 2
),
array(
'role_id' => $role_id_mod,
'permission_id' => $permission_base + 1
),
array(
'role_id' => $role_id_mod,
'permission_id' => $permission_base + 3
),
array(
'role_id' => $role_id_stand,
'permission_id' => $permission_base + 3
),
);
DB::table('permission_role')->insert( $permissions );
}
}
This is the error I get when running db:seed:
$ php artisan db:seed
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command? Y
Seeded: UsersTableSeeder
Seeded: RolesTableSeeder
[ErrorException]
Trying to get property of non-object
db:seed [--class[="..."]] [--database[="..."]] [--force]
Any recommendations what I am doing wrong in my seeding?
I appreciate your answers!
I think the problem is with:
$role_id_admin = Role::where('name', '=', 'admin')->first()->id;
$role_id_mod = Role::where('name', '=', 'moderator')->first()->id;
$role_id_stand = Role::where('name', '=', 'user')->first()->id;
You want to get id but such records don't exists so you get the error. There are probably no records with admin, moderator or user name because looking at RolesTableSeeder you create roles this way:
$adminRole = new Role;
$adminRole->name = 'adminRole';
$adminRole->save();
$standRole = new Role;
$standRole->name = 'userRole';
$standRole->save();
$modRole = new Role;
$modRole->name = 'modRole';
$modRole->save();
with names adminRole, userRole, modRole. So either change the names inside RolesTableSeeder or change them in PermissionsTableSeeder

Categories