How should I prophesy the sort method? - php

I am using the DoctrineMongoDBBundle and I am not sure how to prophesy the sort method.
Source
$qb = $dm->createQueryBuilder('Article')
->sort('createdAt', 'desc');
My code is:
UserRepository - Method All
public function all(array $input = null)
{
$user = UserEntity::class;
$all = $this->dm->createQueryBuilder($user);
$search = $all->sort(['name' => 'asc'])
->getQuery();
return $search;
}
UserRepositoryTest - prophecy
public function testSortingResults()
{
$output = [
'name' => 'John',
'email' => 'john#email.com',
'phone' => '89564789547',
];
$document = $this->prophesize(DocumentManager::class);
$queryBuilder = $this->prophesize(QueryBuilder::class);
$queryBuilder->sort()->willReturn($output)->shouldBeCalled();
$queryBuilder->getQuery()->willReturn($output)->shouldBeCalled();
$document->createQueryBuilder(UsuarioEntidade::class)->willReturn($queryBuilder)->shouldBeCalled();
$repository = new UserRepository($document->reveal());
$all = $repository->all();
$this->assertNotNull($all);
$this->assertEquals($output, $all);
}
The error is always this
Prophecy\Exception\Doubler\MethodNotFoundException: Method Double\Doctrine\ORM\QueryBuilder\P2::sort() is not defined.
I do not understand how to test SORT, because it is not found in QueryBuilder.

Related

Replace Having Cause with Where Clause

Please i have some trouble in my project in symfony
How can i replace having clause wuĆ­th where clause in this code above cause having clause dont work white paginator in symfony
...
...
public function getAllClients($all = true, $user = null)
{
$qb = $this->_em->createQueryBuilder();
//
$qb->select('u, count(ls.id) as nbr')
->from($this->_entityName, 'u')
->join(Client::class, 'c', Join::WITH, 'c.user = u.id')
->join(LicenceShop::class, 'ls', Join::WITH, 'ls.client = c.id')
->groupBy('u.id')
->having('nbr > 1');
$qb->andWhere('u.roles LIKE :roles');
$qb->setParameter('roles', '%"ROLE_CLIENT"%');
return $qb->getQuery();
}
Thanks you
When i use having clause symfony response like that
Cannot count query that uses a HAVING clause. Use the output walkers for pagination
And this the function
enter code public function show($id = null, UserRepository $userRepository, Request $request, PaginatorInterface $paginator)
{
if($id)
$user = $userRepository->find($id);
else
$user = $this->getUser();
$client = $user->getClientInfo();
$myClients = $userRepository->getAllClients(false, $user);
$myClients = $paginator->paginate(
$myClients,
$request->query->getInt('page_c', 100)
);
$referal = null;
$licences = null;
if ($client) {
$referal = $client->getReferal();
$licences = $client->getLicences();
}
//$licences = $module->getLicences();
return $this->render('resellers/show.html.twig', [
'controller_name' => 'ResellersController',
'user' => $user,
'client' => $client,
'referal' => $referal,
'licences' => $licences,
'my_clients' => $myClients,
]);
}

How to fix my "Notice: Undefined offset: 4" error

I have a problem with my code, when a User have all "stations" the code work but if he dont have one i have the (undefined offset *) error,
my code look like that ( in my controller )
public function index()
{
$entityManager = $this->getDoctrine()->getManager();
$stations = $this->getListOfStations();
$stationsJson = [];
$events = $entityManager->getRepository('App:User')
->getEventInRealTime($this->getUser());
foreach ($stations as $station) {
$stationsJson[] = [
'typeEvents' => $events[$station->getId()],
];
}
return new JsonResponse(array('stations' => $stationsJson));
}
private function getListOfStations()
{
/** #var UserInterface $user */
$user = $this->getUser();
$entityManager = $this->getDoctrine()->getManager();
if (!$this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
$stations = $user->getStations();
} else {
$stations = $entityManager->getRepository('App:Station')->findAll();
}
return $stations;
}
and my repository
public function getEventInRealTime($user)
{
$qb = $this->createQueryBuilder('u');
$events = $qb
->select('s.id as stationId, COUNT(e) as number, e.label as type')
->innerJoin('u.stations', 's')
->innerJoin('s.events', 'e')
->where('u = :user')
->setParameter('user', $user)
->andWhere('DAY(e.currentTime) =:day')
->setParameter('day', date('d'))
->andWhere('MONTH(e.currentTime) =:month')
->setParameter('month', date('m'))
->andWhere('YEAR(e.currentTime) =:year')
->setParameter('year', date('Y'))
->groupBy('s.id, type');
$data = $events->getQuery()->getArrayResult();
$result = [];
foreach ($data as $row) {
$result[$row['stationId']][] =
[
'number' => $row['number'],
'type' => $row['type']
];
}
return $result;
}
i think its a problem its like, he dont find the "stations" when i do a findAll(), but i dont know how to fix it, when the user is super_admin he can have see all the stations but if is not super admin he can see only his stations

Laravel 5.6, where to keep additional classes (additional functional)?

I need to make request on some CRM api in my controller. For making this I have pretty big method. It's look like ugly. I know that there are some "Services" and to put additional code into Service is a good way. But I don't know what is this. Is it a custom classes into app folder? Or maybe it's Service-providers? I have read service-providers documentation and I'm not sure that service-providers is suitable for this. Here is my code:
<?php
namespace App\Http\Controllers;
use App\User;
use App\UserInfo;
use Validator;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$users = User::with('info')
->paginate(20);
$users->withPath(DIRECTORY_SEPARATOR . $request->path() .DIRECTORY_SEPARATOR);
return response()->json($users)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = $request->json()->all();
$rules = [
'name' => 'required',
'phone' => 'required|unique:users'
];
$validator = Validator::make($data, $rules);
if ($validator->fails()) return response()->json(['errors'=>$validator->errors()]);
$user = new User();
$user->name = request('name');
$user->phone = request('phone');
$user_info_obj = $this->storeUserInfo();
if($user_info_obj === null){
return response('Impassible to define user geo data', 400);
}
$user->info_id = $user_info_obj->id;
$user->save();
$this->makeAMOLead($user->name,
$user->phone,
$user_info_obj->user_agent,
$user_info_obj->city,
$user_info_obj->country);
return response()->json(['success' => 'User created successfully']);
}
public function storeUserInfo()
{
$ip = request()->ip();
$reader = new \GeoIp2\Database\Reader('../resources/geo-lite2-city_20180807/GeoLite2-City.mmdb');
try {
$record = $reader->city($ip);
}
catch (\Throwable $e){
// Code bellow is for testing on localhost, Because of maybe exception instead of geo obj on localhost.
$info = new UserInfo();
$info->ip = '127.0.0.1';
$info->city = 'Some city';
$info->country = 'Some country';
$info->country_code = 'Some code';
$info->continent = 'Some continent';
$info->continent_code = 'no';
$info->user_agent = 'User agent';
$info->save();
return $info;
//return null;
}
$city = $record->city->names['ru'];
$continent = $record->continent->names['ru'];
$continent_code = $record->continent->code;
$country = $record->country->names['ru'];
$country_code = $record->country->isoCode;
$user_agent = \request()->userAgent();
$info = new UserInfo();
$info->ip = $ip;
$info->city = $city;
$info->country = $country;
$info->country_code = $country_code;
$info->continent = $continent;
$info->continent_code = $continent_code;
$info->user_agent = $user_agent;
$info->save();
return $info;
}
private function makeAMOLead($name, $phone, $userAgent, $city, $country)
{
$domain = env('AMO_DOMAIN');
$login = env('AMO_LOGIN');
$hash = env('AMO_HASH');
try {
$credentials = new \ddlzz\AmoAPI\CredentialsManager($domain, $login, $hash);
$settings = new \ddlzz\AmoAPI\SettingsStorage();
$settings->setCookiePath(env('AMO_COOKIE_FILE_PATH'));
$request = \ddlzz\AmoAPI\ClientFactory::create($credentials, $settings);
$lead = new \ddlzz\AmoAPI\Model\Amo\Lead();
$lead['name'] = $name;
if(env('AMO_PIPELINE_ID', null)){
$lead['pipeline_id'] = intval(env('AMO_PIPELINE_ID'));
}
$lead['name'] = 'New pickup user ' . $name;
$lead['custom_fields'] = [
[
'id' => env('AMO_NAME_FIELD_ID'),
'values' => [
['value' => $name],
]
],
[
'id' => env('AMO_USER_AGENT_FIELD_ID'),
'values' => [
['value' => $userAgent]
]
],
[
'id' => env('AMO_CITY_FIELD_ID'),
'values' => [
['value' => $city]
]
],
[
'id' => env('AMO_COUNTRY_FIELD_ID'),
'values' => [
['value' => $country]
]
],
];
$lead['created_at'] = time();
$result = $request->add($lead);
$pipelineId = json_decode($result)->_embedded->items{0}->id;
// create contact
$contact = new \ddlzz\AmoAPI\Model\Amo\Contact();
$contact['name'] = $name;
$contact['created_at'] = time();
$contact['leads_id'] = "$pipelineId";
// dd($request->accountInfo(), true); // Call this, if you need to know ids of default fields (like phone, or position)
$contact['custom_fields'] = [
[
'id' => env('AMO_CONTACT_PHONE_ID'),
'values' => [
[
'value' => $phone,
'enum' => 'MOB',
],
]
],
];
$result = $request->add($contact);
} catch (Exception $e) {
echo response()->json(['error' => $e->getFile() . ': ' . $e->getMessage()]);
}
}
}
Look on the makeAMOLead. This is big method in my controller and this is not ok for controller conception.
Please use repository pattern to split all the communication between the application and your data source. and call the repository functions inside your controller. It is good practice. Here is an article you can understand about that
Example:
Your functions can be separate from controller to repository.
storeUserInfo
makeAMOLeadin
Move your functions an repository and call them into your controller.

Doctrine QueryBuilder undefined method getQuery()

When I execute the method getMachineSettings I get an error:
Fatal error: Uncaught Error: Call to undefined method Doctrine\DBAL\Query\QueryBuilder::getQuery()
$data is an associative array:
$data['param'] = 'ip';
$data['value'] = '192.168.240.10';
If I replace getQuery()->getResult() with execute(), $result contains the query:
SELECT * FROM machine WHERE ip = ?
public function __construct()
{
try
{
$dbconf = parse_ini_file('.htLogin.ini');
$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array
(
'dbname' => $dbconf['infoDb'],
'user' => $dbconf['infoLogin'],
'password' => $dbconf['infoPw'],
'host' => $dbconf['infoHost'],
'driver' => 'pdo_mysql',
'charset' => 'utf8',
'driverOptions' => array
(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
)
);
$this->mysql = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
catch(PDOException $e)
{
echo $e -> getMessage();
}
public function getMachineSettings($data)
{
$qb = $this->mysql->createQueryBuilder();
$qb->SELECT('*')
->FROM('`machine`')
->WHERE($data['param'] . ' = :value');
$qb->setParameters(
array
(
':value' => $data['value']
)
);
$results = $qb->getQuery()->getResult();
var_dump($result);
return $result;
}
Do you have any idea why the method getQuery() is not recognised?
Just do
$results = $qb->execute()->fetchAll();
Ignore the below - it assumes that your using the Doctrine ORM, which your not
The issue your having is that the QueryBuilder object your working with isn't the Doctrine ORM QueryBuilder - its the DBAL QueryBuilder.
You need to use the createQueryBuilder function from the EntityManager.
/** #var Doctrine\ORM\QueryBuilder $qb */
$qb = $this->entityManager->createQueryBuilder();
Then you can use the select / from etc methods and to get the result of the query you can run
$qb->getQuery()->getResult()
If I was to rewrite your function I would write it like this
public function getMachineSettings(string $field, string $value)
{
$qb = $this->entityManager->createQueryBuilder();
$qb->select('m')
->from('machine')
->where($field.' = :value');
$qb->setParameter('value', $value);
$results = $qb->getQuery()->getResult();
var_dump($result);
return $result;
}
Then you know that the function requires 2 parameters to function, passing an array doesn't let you immediately see what the function requires

laravel write proper test for sending email

I wonder how to write proper unit test for my email sending method. It's a problem because inside method I get data from Auth object. Should I send id of user in Request?
public function sendGroupInvite(Request $request){
foreach ($request->get('data') as $item){
$invitations = new \App\Models\Invitations();
$invitations->user_id = Auth::id();
$invitations->name = $item["name"];
$invitations->email = $item["email"];
$invitations->status = 0;
$invitations->token = \UUID::getToken(20);
$invitations->username = Auth::user()->name;
$invitations->save();
$settings = UserSettings::where('user_id', Auth::id())->first();
$email = $item["email"];
$url = 'https://example.com/invite/accept/'.$invitations->token;
$urlreject = 'https://example.com/invite/reject/'.$invitations->token;
$mailproperties = ['token' => $invitations->token,
'name' => $invitations->name,
'url' => $url,
'email' => $email,
'urlreject' => $urlreject,
'userid' => Auth::id(),
'username' => Auth::user()->name,
'user_name' => $settings->name,
'user_lastname' => $settings->lastname,
'user_link' => $settings->user_link,
];
$this->dispatch(new SendMail(new Invitations($mailproperties)));
}
return json_encode(array('msg' => 'ok'));
}
I'm using Auth to get username and user id. When I testing it it's not works, because Auth it's null.
I would go with mocking the queue, something similar to this. Mock Documentation
class MailTester extends TestCase{
/**
* #test
*/
public function test_mail(){
Queue::fake();
// call your api or method
Queue::assertPushed(SendMail, function(SendMail $job) {
return $job->something = $yourProperties;
});
}
You could try "acting as" to deal with the Auth::user().
...
class MyControllerTest extends TestCase{
/**
* #test
*/
public function foo(){
$user = App\Users::find(env('TEST_USER_ID')); //from phpunit.xml
$route = route('foo-route');
$post = ['foo' => 'bar'];
$this->actingAs($user)//a second param is opitonal here for api
->post($route, $post)
->assertStatus(200);
}
}

Categories