Incomplete links generated in hal (zend-expressive-hal + zend-router) - mezzio

My routes def:
'router' => [
'routes' => [
'TimeTable' => [
'type' => 'Literal',
'options' => [
// Change this to something specific to your module
'route' => '/tt',
],
'may_terminate' => false,
'child_routes' => [
'API' => [
'type' => 'Literal',
'options' => [
// Change this to something specific to your module
'route' => '/api',
],
'may_terminate' => false,
'child_routes' => [
'lines' => [
'type' => 'Literal',
'options' => [
// Change this to something specific to your module
'route' => '/lines',
'defaults' => [
'controller' => Controller\LineRestApiController::class,
],
],
'may_terminate' => true,
],
],
],
],
],
],
Medatada map def:
MetadataMap::class => [
[
'__class__' => RouteBasedCollectionMetadata::class,
'collection_class' => LineCollection::class,
'collection_relation' => 'lines',
'route' => 'TimeTable/API/Lines',
],
]
Generated result:
{
"_total_items": 78,
"_page": 1,
"_page_count": 4,
"_links": {
"self": {
"href": "http://xxx.xxx.xx"
},
"next": {
"href": "http://xxx.xxx.xx"
},
"last": {
"href": "http://xxx.xxx.xx"
}
},
"_embedded": {
"lines": [.....] }}
All links are generated with incomplete href , there is only domain part, route part is stripped ..
Expected result is something like this:
"href" : "http://xxx.xxx.xx/xxx/tt/api/lines....."
Im doing something wrong, I have no idea where to start ..
Thanks everyone for giving me some ideas
Simplified Controller code:
$psr7request = Psr7ServerRequest::fromZend($this->getRequest());
$list = this->entityManager->getRepository(Line::class)->getValidLinesCollection();
$resource = $this->resourceGenerator->fromObject($list, $psr7request);
echo Psr7Response::toZend($this->responseFactory->createResponse($psr7request, $resource))->getBody();
exit;
PS: Im not using full zend-expressive just zend-framework..

Sorry i forgot that i had done in last week :(
It must be done some custom implementation for UrlGeneratorInterface to successful integrate zend-expressive-hal to zend framework (original class ExpressiveUrlGenerator uses Expressive\Helper\ServerUrlHelper & UrlHelper, the part of Expressive)
So i used Zend\View\Helper\ServerUrl & Url to done it.
I have small typo in code. The final class is here:
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Hal\LinkGenerator\UrlGeneratorInterface;
use Zend\View\Helper\ServerUrl as ServerUrlHelper;
use Zend\View\Helper\Url as UrlHelper;
class HalUrlGenerator implements UrlGeneratorInterface
{
/**
* #var null|ServerUrlHelper
*/
private $serverUrlHelper;
/**
* #var UrlHelper
*/
private $urlHelper;
public function __construct(UrlHelper $urlHelper, ServerUrlHelper $serverUrlHelper = null)
{
$this->urlHelper = $urlHelper;
$this->serverUrlHelper = $serverUrlHelper;
}
public function generate(
ServerRequestInterface $request,
string $routeName,
array $routeParams = [],
array $queryParams = []
) : string {
$urlHelper = $this->urlHelper;
$path = $urlHelper($routeName, $routeParams, ['query'=> $queryParams]);
if (! $this->serverUrlHelper) {
return $path;
}
$serverUrlHelper = $this->serverUrlHelper;
return $serverUrlHelper($path);
}
}
I hope the code can help somebody.

Related

How to test array contains only objects with PHPUnit?

I'm looking for solution to test an array of objects with PHPUnit in my Laravel project.
This is my haystack array:
[
[
"id" => 10,
"name" => "Ten"
],
[
"id" => 5,
"name" => "Five"
]
]
And this is the needles array:
[
[
"id" => 5,
"name" => "Five"
],
[
"id" => 10,
"name" => "Ten"
]
]
The order of objects doesn't matter, also keys of objects doesn't matter. The only matter is we have two objects and all objects has exactly same keys and exactly same values.
What is the correct solution for this?
You can do this using the assertContainsEquals method like this:
$haystack = [
[
'id' => 10,
'name' => 'Ten'
],
[
'id' => 5,
'name' => 'Five'
]
];
$needles = [
[
'name' => 'Five',
'id' => 5
],
[
'id' => 10,
'name' => 'Ten'
]
];
foreach ($needles as $needle) {
$this->assertContainsEquals($needle, $haystack);
}
You could also a create your own assert method if you intend to perform the assertion more often:
public function assertContainsEqualsAll(array $needles, array $haystack): void
{
foreach ($needles as $needle) {
$this->assertContainsEquals($needle, $haystack);
}
}
Based on #Roj Vroemen's answer I implemented this solution for exact match asserting:
public function assertArrayContainsEqualsOnly(array $needles, array $haystack, string $context = ''): void
{
foreach ($needles as $index => $needle) {
$this->assertContainsEquals(
$needle,
$haystack,
($context ? $context . ': ' : '') . 'Object not found in array.'
);
unset($haystack[$index]);
}
$this->assertEmpty(
$haystack,
($context ? $context . ': ' : '') . 'Not exact match objects in array.'
);
}

Laravel Http Client 419 unknown status

For testing reasons, I want to make the following Post Request with the Laravel HTTP Client:
$test = Http::post(route('users.leads.store', ['user' => $user->id]), [
"company_name" => "TESTCOMPANY",
"zip" => "49685",
"city" => "BÜHREN",
"street" => "MÜHLENKAMP 3",
"contact_name" => "FABIANLUKASSEN",
"phone1" => "017691443785",
"email" => "FABIANLUKASSEN#TESTEN.DE",
"website" => "www.fabianlukassen.de",
"category" => "Hotel",
"closed_until" => now(),
"appointment_end" => now()->addDays(1),
"appointment_comment" => "HALLO ICH BIN FABIAN",
"additional_contacts" => "",
"phone2" => "",
"sub_category" => "",
"expert_status" => 0
]);
I know that the route is working just fine. However, with debugging in phpStorm, I can see that the $test variable contains a 419 error (unknown status). Does anyone know what's wrong?
(I'm using laravel 8)
I agree with #ElektaKode that the issue is likely due to lack of csrf token.
In order to disable CSRF middleware while testing,
switch off CSRF token for this route at /app/Http/Midddleware/VerifyCsrfToken.php, by updating:
protected $except = [ 'your-route-url' ];
Then you can use api authentication to follow it up.
The simplest way to use api authentication, follow this doc,
The other ways are either using Laravel passport or using jwt for api.(both will consume more time to set up, as you are using for testing using api authentication is your go to method.)
Usually in Laravel, 419 Page Expired error comes from CSRF middleware meaning there was a failure while validating CSRF token. Add your CSRF token to your test request or consider disabling CSRF middleware while testing.
Post Request with Laravels HTTP Client
$test = Http::post(route('users.leads.store', ['user' => $user->id]), [
"company_name" => "TESTCOMPANY",
"place_id" => null,
"street" => "MÜHLENKAMP 3",
"zip" => "49685",
"city" => "BÜHREN",
"title" => null,
"contact_name" => "FABIANLUKASSEN",
"additional_contacts" => null,
"phone1" => "+49 163 3006603",
"phone2" => null,
"email" => "FABIANLUKASSEN#TESTEN.DE",
"category" => "Hotel",
"sub_category" => null,
"website" => "www.fabianlukassen.de",
"status" => 1,
"expert_status" => 0,
"coordinates" => null,
"expert_id" => 1,
"agent_id" => null,
"blocked" => 0,
"important_note" => null,
]);
Route
Route::apiResource('users.leads', UserLeadController::class);
Store Method in the UserLeadController
public function store(User $user, CreateLeadRequest $request)
{
//TODO: Relocate validation to request class
if(!UserLeadController::isPhone("test", $request->phone1)) {
abort(400, "Keine gültige Telefonnummer!");
return;
}
if(!UserLeadController::isPhoneNumberUnique("test", $request->phone1)) {
abort(400, "Die Telefonnummer existiert bereits!");
return;
}
/**
* The logged in User
* #var User $agent
*/
$agent = Auth::user();
$phoneUtil = PhoneNumberUtil::getInstance();
$lead = new Lead();
$lead->fill($request->except(['appointment_end', 'appointment_comment']));
// Leads created by experts will be blocked
if ($user->id === $agent->id) {
$lead->blocked = true;
}
$numberProto = $phoneUtil->parse($lead->phone1, 'DE');
$lead->phone1 = $phoneUtil->format($numberProto, PhoneNumberFormat::INTERNATIONAL);
try {
$lead->save();
} catch (QueryException $e) {
//$message = 'Lead besteht bereits.';
//return Response::json(['errors' => $message], 422);
abort(422, "Lead besteht bereits!");
return;
}
if ($request->closed_until) {
$lead->closed_until = Carbon::create($request->closed_until);
$event_end = $request->appointment_end
? Carbon::parse($request->appointment_end)
: Carbon::parse($request->closed_until)->addMinutes(90);
$lead->calendarEvents()->save(new CalendarEvent([
'body' => $request->appointment_comment ?? "Wurde von {$this->roleDescriptor($agent->roles)}" . $agent->name . " angelegt.",
'type' => CalendarEventType::CALLCENTER_APPOINTMENT,
'event_begin' => $lead->closed_until,
'event_end' => $event_end,
]));
$lead->status = LeadState::APPOINTMENT;
$lead->expert_status = LeadExpertAcceptance::ACCEPTED;
} else {
$lead->status = LeadState::OPEN;
}
if (isset($request->agent)) {
$lead->agent_id = $request->agent;
}
try {
$user->leads()->save($lead);
$lead->comments()->save(new Comment([
'body' => "Wurde von {$this->roleDescriptor($agent->roles)}" . $agent->name . " angelegt.",
'user_id' => $agent->id,
'commentable_type' => 'lead',
'commentable_id' => $lead->id,
'reason' => 'CREATED',
'date' => now('Europe/Berlin'),
]));
if ($request->closed_until) {
$lead->comments()->save(new Comment([
'body' => "Termin wurde von {$this->roleDescriptor($agent->roles)}" . $agent->name . " vereinbart.",
'user_id' => $agent->id,
'commentable_type' => 'lead',
'commentable_id' => $lead->id,
'reason' => 'APPOINTMENT',
'date' => now('Europe/Berlin')->addMinute(),
]));
}
} catch (QueryException $e) {
//not sure if this works
$message = $e->getMessage();
abort(400, $message);
return;
}
if (empty($message)) {
return Response::json(['message' => 'Lead saved', 'lead' => new LeadSingleResource($lead)]);
} else {
return Response::json(compact('message'), 500);
}
}
//TODO: relocate function to rule object
protected static function isPhoneNumberUnique($attribute, $value) {
$withSpace = PhoneFormatter::formatInternational($value);
$withoutSpace = preg_replace('/ /', '', $withSpace);
$protos = [$withSpace, $withoutSpace]; // Necessary because legacy (25.06.2020).
$booleanTest = Company::query()->whereIn('phone', $protos)->doesntExist()
|| Lead::query()->whereIn('phone1', $protos)->orWhereIn('phone2', $protos)->doesntExist();
return $booleanTest;
}
//TODO: relocate function to rule object
protected static function isPhone($attribute, $value) {
if (!$value) {
return false;
}
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$test = $phoneUtil->isValidNumber($phoneUtil->parse($value, 'DE'));
return $test;
}
fillable variable in the Lead Model
protected $fillable = [
'company_name',
'place_id',
'street',
'zip',
'city',
'title',
'contact_name',
'additional_contacts',
'phone1',
'phone2',
'email',
'category',
'sub_category',
'website',
'status',
'expert_status',
'coordinates',
'expert_id',
'agent_id',
'blocked',
'important_note'
];
As mentioned before, I receive a 200 OK status. Also, in a Vue.js component, I have done the following axios post request, which also just works fine.
axios
.post(`/api/users/${this.user_id}/leads`, {
"company_name": this.companyName,
"zip": this.zipCode,
"city": this.city,
"street": this.streetAndHouseNumber,
"contact_name": this.contactPartner,
"phone1": this.contactPartnerPhoneNumber,
"email": this.contactPartnerEmail,
"website": this.website,
"category": this.category,
"closed_until": this.appointmentStart,
"appointment_end": this.appointmentEnd,
"appointment_comment": this.comment,
//not used but needed (don't know the purpose)
"additional_contacts": "",
"phone2": "",
"sub_category": "",
"expert_status":this.expert_status,
}).then(() => {
window.location.href = this.routeSuccess;
}).catch((error) => {
this.showErrorAlert = true;
this.errorAlertMessage = error.response.data.message;
});
}

Laravel translate values required_if does not work

I am using Laravel version 5.7 and I want to translate the value of required_if. I used below action but it does not work. what shall I do?
in RequestObject.php:
public function rules() {
$fee = '';
if ($this['params.fee_status'] == 'pay') {
$fee = 'required_if:params.fee_status,pay|numeric|min:100';
}
return [ 'params.fee' => $fee,];
}
in resources/lang/en/validation.php :
'values' => [
'params.fee_status' => [
'pay' => 'default'
],
],

hiding an index of array laravel

I want to ask, how to hidden a name in role login.
So I have a output in laravel like this:
{
"npp":"822345",
"nama":"Handra Pratama",
"bus_pergi":1,
"bus_pulang":4,
"hotel":null,
"kamar":"K1",
"teman_kamar":[
{
"nama":"Handra Pratama"
},
{
"nama":"Louis Vernando"
},
{
"nama":"Hallo Budi"
}
]
}
I want to hide role handra (because I'm login with handra username) in teman_kamar, and if i login role louis, i want to hide louis in teman_kamar, what should i do?
Your output is in JS, so you can use a filter function in JS. But if you want to do it in PHP here is an example that I ran and it works per your case, because you always have the name that you want to hide under the first name key.
<?php
$obj = [
"npp" => "822345",
"nama" => "Handra Pratama",
"bus_pergi" => 1,
"bus_pulang" => 4,
"hotel" => null,
"kamar" => "K1",
"teman_kamar" => [
[
"nama" => "Handra Pratama"
],
[
"nama" => "Louis Vernando"
],
[
"nama" => "Hallo Budi"
]
]
];
$obj['teman_kamar'] = array_filter($obj['teman_kamar'], function($val) use ($obj) {
return $val['nama'] !== $obj['nama'];
});
print_r($obj);

Installing user-management for Yii2.0

I've been trying to install user-management for Yii2.0, but getting ReflectionException while loading the page. I have attached the error page and directory structure below.
and the file path is as shown below.
I've searched a lot to find out the reason for this, but nothing worked out. can someone tell me what am I missing here to get it work. looks like the user-management installation documentation has some flaws. It is not clear enough to understand. Hope to get the steps to install. Thanks
Here is my console/web.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'gAry7SfUr0oOjNQDqItsobmGBcJajQoW',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
//'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'class' => 'app\webvimark\modules\user-management\components\UserConfig',
// Comment this if you don't want to record user logins
'on afterLogin' => function($event) {
\webvimark\modules\user-management\models\UserVisitLog::newVisitor($event->identity->id);
}
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
],
'modules'=>[
'user-management' => [
'class' => 'webvimark\modules\user-management\UserManagementModule',
// 'enableRegistration' => true,
// Here you can set your handler to change layout for any controller or action
// Tip: you can use this event in any module
'on beforeAction'=>function(yii\base\ActionEvent $event) {
if ( $event->action->uniqueId == 'user-management/auth/login' )
{
$event->action->controller->layout = 'loginLayout.php';
};
},
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
It seems to have a slight difference with the expected configuration for this extension.
Use this
'class' => 'webvimark\modules\UserManagement\components\UserConfig',
ie UserManagement instead of user-management is a configuration path and not a route

Resources