How to test singleton(a class only new once) on laravel - laravel

This is Residence Facade:
class Residence extends Facade
{
protected static function getFacadeAccessor()
{
return 'residence.manager';
}
}
Here is the provider:
class ResidenceServiceProvider extends ServiceProvider implements DeferrableProvider
{
public $singletons = [
'residence.manager' => ResidenceManager::class,
'residence.japan' => ResidenceJapan::class,
'residence.us' => ResidenceUS::class,
'residence.eu' => ResidenceEU::class,
];
public function provides()
{
return [
'residence.manager',
'residence.japan',
'residence.us',
'residence.eu',
];
}
The ResidenceManager Class:
class ResidenceManager
{
protected $app;
public function __construct()
{
$this->app = app();
}
public function make($data)
{
$residenceService = match ($data['location']) {
'japan' => $this->app['residence.japan'],
'us' => $this->app['residence.us'],
'eu' => $this->app['residence.eu'],
default => false
};
if (!$residenceService ) {
return false;
}
return $residenceService->make($data);
}
}
I try to test if ResidenceJapan::class, ResidenceUS::class, and ResidenceEU::class only new once.
I use spy, bu it give me
Mockery\Exception\InvalidCountException
Method make(<Any Arguments>) from Mockery_0_App_Services_Residence_ResidenceJapan should be called
exactly 1 times but called 3 times.
/**
* #dataProvider residenceSeedProvider
*/
public function test_residence_class_only_new_once($data, $expected)
{
$residence= match ($data['location']) {
'japan' =>
[
'bind' => 'residence.japan',
'class' => ResidenceJapan::class
],
'us' =>
[
'bind' => 'residence.us',
'class' => ResidenceUS::class
],
'eu' => [
'bind' => 'residence.eu',
'class' => ResidenceEU::class
],
default => 'unknown',
};
$spy=$this->spy($residence['class']);
$this->app->instance(
$residence['bind'],
$spy
);
$i = 3;
while ($i > 0) {
//$this->assertNotNull(Residence::make($data));
Residence::make($data);// don't know why the Residence::make($data) return null
$i--;
}
$spy->shouldHaveReceived('make')->once();
}
So I use mock, but it give me
Error
Call to undefined method App\Services\Residence\ResidenceJapan::shouldHaveReceived()
/**
* #dataProvider residenceSeedProvider
*/
public function test_residence_class_only_new_once($data, $expected)
{
$residence= match ($data['location']) {
'japan' =>
[
'bind' => 'residence.japan',
'class' => ResidenceJapan::class
],
'us' =>
[
'bind' => 'residence.us',
'class' => ResidenceUS::class
],
'eu' => [
'bind' => 'residence.eu',
'class' => ResidenceEU::class
],
default => 'unknown',
};
$mock = Mockery::mock($residence['class'], function (MockInterface $mock) use ($expected) {
$mock->shouldReceive('make')->once()->andReturn($expected);
});
$this->app->instance(
$residence['bind'],
$mock
);
$i = 3;
while ($i > 0) {
$this->assertNotNull(Residence::make($data)); // the Residence::make($data) return what I am expected
$i--;
}
$residence['class']::shouldHaveReceived('make')->once();
}
How do I test if ResidenceJapan::class, ResidenceUS::class, and ResidenceEU::class only new once, no matter how many times the Facade Residence::make($data) calls?

Related

Type error: Too few arguments to function Illuminate\Mail\Mailer::__construct(), 0 passed - Laravel 5.5

I keep getting while trying to use the mailer
FatalThrowableError in Mailer.php line 93:
Type error: Too few arguments to function Illuminate\Mail\Mailer::__construct(), 0 passed in /var/www/app/app/Services/SendOtpMail.php on line 42 and at least 2 expected
in Mailer.php line 93
at Mailer->__construct() in SendOtpMail.php line 42
at SendOtpMail->send('test#company.com', array('from' => 'no-reply#company.com', 'from_name' => 'Some Company', 'subject' => 'Login Verification', 'data' => array('token' => '3486', 'user' => object(User)), 'view' => 'emails.password')) in GetOtpForLoginService.php line 59
at GetOtpForLoginService->sendEmail('3486', object(User))
Send mail function
public function sendEmail($otp, $user)
{
$user = User::where('email', $user->email)->firstOrFail();
(new SendOtpMail())->send($user->email, [
'from' => env('MAIL_DEAFULT_SENDER'),
'from_name' => env('MAIL_DEAFULT_SENDER_ALIAS'),
'subject' => 'Login Verification',
'data' => [
'token' => $otp,
'user' => $user
],
'view' => 'emails.password'
]);
return true;
}
SendOtpMail.php
<?php
namespace App\Services;
use Illuminate\Mail\Mailer;
class SendOtpMail
{
public function send($to, array $options = array())
{
$callback = function($message) use ($options, $to) {
$message->from($options['from'], isset($options['from_name']) ? $options['from_name'] : null);
$message->to($to, isset($options['to_name']) ? $options['to_name'] : null);
if(isset($options['subject'])) $message->subject($options['subject']);
if(isset($options['priority'])) $message->priority($options['priority']);
if(isset($options['priority'])) $message->priority($options['priority']);
if(isset($options['files'])) {
if (is_array($options['files'])) {
foreach ($options['files'] as $file) {
$message->attach($options[$file]);
}
} else {
$message->attach($options['files']);
}
}
if(isset($options['cc'])) $message->subject($options['cc'], isset($options['cc_name']) ? $options['cc_name'] : null);
if(isset($options['bcc'])) $message->subject($options['bcc'], isset($options['bcc_name']) ? $options['bcc_name'] : null);
};
if(isset($options['view'])) {
$data = isset($options['data']) ? $options['data'] : array();
(new Mailer())->send($options['view'], $data, $callback);
} else {
(new Mailer())->raw($options['message'], $callback);
}
}
}
You are seeing that error because you are instantiating an Illuminate\Mail\Mailer object without specifying its required parameters in the constructor:
// from Laravel source code
public function __construct(string $name, Factory $views, TransportInterface $transport, Dispatcher $events = null)
{
$this->name = $name;
$this->views = $views;
$this->events = $events;
$this->transport = $transport;
}
I suggest you don't send emails this ways. Pls check the docs and follow the instructions.

Laravel 8: Passing Factory properties into children relationships

we are currently working on a laravel 8 application. We are trying to create factories to create some dummy data for manual / developer based application testing.
The current code of my main Database-Seeder is below:
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
UserTableSeeder::class,
]);
\App\Models\User::factory(10)->create();
\App\Models\Activity::factory(5)->create();
/* 1. try
$tenFact = \App\Models\Tenant::factory(2)->has(
\App\Models\Project::factory(2)->state(
function (array $attributes, \App\Models\Tenant $tenant) {
return ['tenant_id' => $attributes['id']];
}
)->hasTasks(5)->hasLocation()
)->hasContracts(3)->create();
*/
/* Currently being used: */
\App\Models\Tenant::factory(10)->has(
\App\Models\Project::factory(5)->hasTasks(5)->hasLocation()
)->hasContracts(3)->create();
}
ProjectFactory.php:
class ProjectFactory extends Factory
{
protected $model = Project::class;
public function definition()
{
return [
'name' => 'Projekt: '. $this->faker->name,
'budget' => $this->faker->randomDigitNotNull*1000,
'progress' => $this->faker->randomDigitNotNull*10,
'budget_used' => $this->faker->randomDigitNotNull*50,
//'tenant_id' => Tenant::factory(),
'location_id' => Location::factory()->hasTenant(1),
];
}
}
LocationFactory.php:
class LocationFactory extends Factory
{
protected $model = Location::class;
public function definition()
{
return [
'name' => 'Standort: ' . $this->faker->company,
'street' => $this->faker->streetName,
'house_number' => $this->faker->buildingNumber,
'house_addition' => $this->faker->secondaryAddress,
'zip' => $this->faker->postcode,
'city' => $this->faker->city,
'tenant_id' => Tenant::factory(),
];
}
}
Our relationships look like this:
Tenant
|-- Project (has: tenant_id, but also has location_id)
| | -- Task (has: project_id)
|-- Locations (has: tenant_id)
|-- Contracts (has: tenant_id)
When creating datasets with the above named Tenant-Factory the following happens:
Tenant->id is being passed to Project(tenant_id)
but: Tenant->id is not being passend to Location (which depends on the tenants id but is also used for Project).
How can we pass the id of \App\Models\Tenant::factory(10) to Project::factory(5)->hasTasks(5)->hasLocation()?
Additionally we do have the problem, that even though we request 10 tenants, we will get around 60, because Location/Project create new objects when they should be using existing ones.
I gave up using the chained usage of the Tenant-Factory - I finally used some for-Loop that connected the related objects to each user by using laravels for() and state() methods:
for ($i=0; $i < 10 ; $i++) {
$tenant = \App\Models\Tenant::factory()->hasContracts(3)->create();
for ($j=0; $j < 5; $j++) {
$location = \App\Models\Location::factory(1)->for($tenant)->create();
$project = \App\Models\Project::factory(1)->state([
'location_id' => $location->first()['id'],
'tenant_id' => $tenant['id']])->hasTasks(5)->create();
}
}
class ProjectFactory extends Factory
{
$location_ids = App\Models\Location::pluck('id')->toArray();
protected $model = Project::class;
public function definition()
{
return [
'name' => 'Projekt: '. $this->faker->name,
'budget' => $this->faker->randomDigitNotNull*1000,
'progress' => $this->faker->randomDigitNotNull*10,
'budget_used' => $this->faker->randomDigitNotNull*50,
//'tenant_id' => Tenant::factory(),
'location_id'=> $faker->randomElement($location_ids),
];
}
}
class LocationFactory extends Factory
{
$tenant_ids = App\Models\Tenant::pluck('id')->toArray();
protected $model = Location::class;
public function definition()
{
return [
'name' => 'Standort: ' . $this->faker->company,
'street' => $this->faker->streetName,
'house_number' => $this->faker->buildingNumber,
'house_addition' => $this->faker->secondaryAddress,
'zip' => $this->faker->postcode,
'city' => $this->faker->city,
'tenant_id'=> $faker->randomElement($tenant_ids),
];
}
}

Laravel-scout : ElasticSearch with Translatable Entities (astrotomic/laravel-translatable)

I'm trying to use "babenkoivan/scout-elasticsearch-driver" with "astrotomic/laravel-translatable", but i don't understand how I could index the translated words.
My Model looks like :
namespace App\Models;
use Astrotomic\Translatable\Translatable;
use App\Models\Search\ShowIndexConfigurator;
use ScoutElastic\Searchable;
...
class Show extends BaseModel
{
...
use Translatable;
use Searchable;
protected $indexConfigurator = ShowIndexConfigurator::class;
protected $searchRules = [
//
];
protected $mapping = [
'properties' => [
// How to index localized translations ???
'title' => [
'type' => 'string'
],
]
];
....
public $translatedAttributes = [
...,
'title'
...
];
Best regards
I found a solution with the override of the method
public function toSearchableArray() with something like:
public function toSearchableArray(): array
{
$document = [];
if ($this->published) {
$document = [
//...
];
foreach ($this->translations()->get() as $translation)
{
if (!$translation->active) {
continue;
}
$locale = $translation->locale;
$document['title_' . $locale] = $translation->title;
$document['url_' . $locale] = $this->getLink($locale);
$document['sub_title_' . $locale] = $translation->sub_title;
$document['keywords_' . $locale] = "";
}
}
return $document;
}
The purpose of $mapping=[] is only to define the structure of data. Something like that is expected:
protected $mapping = [
'properties' => [
'title_en' => [
'type' => 'string'
],
'title_fr' => [
'type' => 'string'
],
...
]
];

using first() in laravel fractal

I want to transform on the first item of Contacts and here is my code
public function includeContact(Customer $customer)
{
return $this->item($customer->contacts()->first(), new ContactTransformer);
}
but it's not working and I get this error :
Type error: Argument 1 passed to
App\Transformers\ContactTransformer::transform() must be an instance
of App\Models\Contact, null given, called in
*\vendor\league\fractal\src\Scope.php
on line 407
Edited
Here is ContactTransformer
namespace App\Transformers;
use App\Models\Contact;
use League\Fractal\TransformerAbstract;
class ContactTransformer extends TransformerAbstract
{
public function transform(Contact $contact)
{
return [
'value' => $contact->value,
'type' => $contact->communication->title,
'icon' => $contact->communication->icon
];
}
}
Here is CustomerTransformer
class CustomerTransformer extends TransformerAbstract
{
protected $availableIncludes = ['contacts', 'contact'];
public function transform(Customer $customer)
{
return [
'id' => $customer->id,
'name'=>$customer->name,
'status' => $customer->status,
'tags' => $customer->tags->pluck('name'),
'created_at' => Verta::instance($customer->created_at)->format('Y/n/j'),
];
}
public function includeContacts(Customer $customer)
{
return $this->collection($customer->contacts, new ContactTransformer);
}
public function includeContact(Customer $customer)
{
return $this->collection($customer->contacts, new ContactTransformer);
}
}
It's because some of your customers don't have contacts. Looks at your ContactTransformer class on transform() method, it should receive instance of Contact. If you give that method null, of course it'll fail.
Then, you need to have like so,
class ContactTransformer extends TransformerAbstract
{
public function transform(Contact $contact = null)
{
if (is_null($contact)) return null;
return [
'value' => $contact->value,
'type' => $contact->communication->title,
'icon' => $contact->communication->icon
];
}
}

CakePHP login when trying to acces controller

im having troubles with a web app that a teacher ask me to modify, the problem its that i dont know about cakePHP and i have been having troubles.After reading a lot, i think i have grasped the basics of the framework. My problem now its that i have a link in a view so i call a function in the controller to retrive data from the model, the problem its that each time i try to acces the function , the app makes me log in, and the idea its that it shouldnt.
I dont know exactly how the session handling on cakePhp works so, im requesting some help.
the code for the controller its this:
<?php
class RwController extends AppController {
var $name = 'Rw';
// var $paginate = array(
// 'Tip' => array(
// 'limit' => 1,
// 'order' => array(
// 'tip.created' => 'desc'
// ),
// ),
// 'Evento' => array(
// 'limit' => 1,
// 'order' => array(
// 'evento.fecha' => 'desc'
// ),
// )
// );
function map() {
$this->helpers[]='GoogleMapV3';
}
function pageForPagination($model) {
$page = 1;
// $chars = preg_split('/model:/', $this->params['url']['url'], -1, PREG_SPLIT_OFFSET_CAPTURE);
// #print_r($chars);
// if(sizeof($chars) > 1 && sizeof($chars) < 3) {
// #echo "Belongs to ".$model.": \n";
// #echo var_dump($chars);
// }
// $params = Dispatcher::parseParams(Dispatcher::uri());
// echo "<p>".var_dump($params)."</p><br />";
#echo $this->params['named']['model'].$model;
#echo $this->params['named']['page'];
$sameModel = isset($this->params['named']['model']) && $this->params['named']['model'] == $model;
$pageInUrl = isset($this->params['named']['page']);
if ($sameModel && $pageInUrl) {
$page = $this->params['named']['page'];
} else {
#echo var_dump($this->passedArgs);
}
$this->passedArgs['page'] = $page;
return $page;
}
function index() {
$this->log('indexeando esta chingadera','debug');
$this->loadModel('User');
$this->loadModel('Evento');
$this->loadModel('Tip');
$dataEvento = $this->Evento->find('all');
$dataTip = $this->Tip->find('all');
$page = $this->pageForPagination('Evento');
$this->paginate['Evento'] = array(
'contain' => false,
'order' => array('Evento.fecha' => 'desc'),
'limit' => 1,
'page' => $page
);
$dataEvento = $this->paginate('Evento');
$page = $this->pageForPagination('Tip');
$this->paginate['Tip'] = array(
'contain' => false,
'order' => array('Tip.created' => 'desc'),
'limit' => 1,
'page' => $page
);
$dataTip = $this->paginate('Tip');
$this->set('users', $this->User->find('all'));
$this->set('eventos', $dataEvento);
$this->set('tips', $dataTip);
$this->set('rw');
if(isset($this->params['named']['model'])) {
if (strcmp($this->params['named']['model'], 'Evento') == 0) {
if($this->RequestHandler->isAjax()) {
$this->render('/elements/ajax_rw_evento_paginate');
return;
}
} elseif (strcmp($this->params['named']['model'], 'Tip') == 0) {
if($this->RequestHandler->isAjax()) {
$this->render('/elements/ajax_rw_tip_paginate');
return;
}
}
}
}
function about($id = null) {
$this->Rw->recursive = 0;
$this->set('rw', $this->paginate());
}
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow(array('index', 'about'));
}
function getCentros($id){
$this->loadModel('Centro');
$this->log('getcentros','debug');
if( sizeof($id) > 1){
$this->set('centros', $this->Centro->query("SELECT centros.id, name, latitud ,longitud
FROM `centros`,`centrosmateriales`
WHERE centros.id = centro_id
AND material_id ='".$id[0]."'
OR material_id='".$id[1]."'"));
}elseif( sizeof($id) >0) {
if($id == 0){
$this->set('centros', $this->Centro->find('all'));
}else{
$this->set('centros', $this->Centro->query("SELECT centros.id, name, latitud ,longitud
FROM `centros`,`centrosmateriales`
WHERE centros.id = centro_id
AND material_id ='".$id[0]."'"));
}
}
$this->redirect(array('action' => 'index'));
}
}
?>
Edit:
The function im calling is getCentros().
this is what i have in app_controller.
<?php
class AppController extends Controller {
var $components = array('Session', 'Auth', 'RequestHandler');
var $helpers = array('Html', 'Form', 'Time', 'Session', 'Js', 'Paginator', 'GoogleMapV3');
function beforeFilter() {
$this->Auth->userModel = 'User';
$this->Auth->fields = array('username' => 'email', 'password' => 'password');
$this->Auth->loginAction = array('admin' => false, 'controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
}
}
?>
try this
$this->Auth->allow(array('*'));
it allows you to access all functions inside the controller.
But before that make sure that you have access on the controller with out errors because your controller name is like this
class RwController extends AppController {
}
may be it want like this
class RwsController extends AppController {
}
Don't know which function you are trying to call but if cake tries to log you in and you don'want this add the function to:
$this->Auth->allow(array('index', 'about'));
in your beforefilter

Resources