How to post data in from back-end testcase to controller - laravel

I am new to php and I am currently trying to make a testcase for an add function I wrote for adding records in the table "project_point" of my database. In this testcase I want to post some test data to that add function and check if the data is set correctly.
Project Point Add function
public function addProjectPoint (Request $request) {
$point = new ProjectPoint();
$location = new Point($request->markerLat, $request->markerLong);
$point->project_id = $request->project_id;
$point->location = $location;
$point->area = $request->area;
$point->name = $request->name;
$point->information = $request->information;
$point->category = $request->category;
$point->save();
}
My test case
public function testCreateProjectPoint()
{
$this->post('admin/projectpoint/create', [
'project_id' => 1,
'markerLat' => 5.287020206451416,
'markerLong' => 51.68828138589033,
'area' => null,
'name' => 'TestCaseProjectPoint',
'information' => 'This is a automated test ProjectPoint, please delete this point!',
'category' => 'bezienswaardigheid'
]);
$this->assertDatabaseHas('interest_point', [
'project_id' => 1,
'location' => new Point(5.287020206451416, 51.68828138589033),
'area' => null,
'name' => 'TestCaseProjectPoint',
'information' => 'This is a automated test ProjectPoint, please delete this point!',
'category' => 'bezienswaardigheid'
]);
/*
$test = factory(ProjectPoint::class)->create();
$this->post('admin/projectpoint/create', $test);
$this->assertDatabaseHas('project_point', $test);
*/
}
ProjectPoint model
class ProjectPoint extends Model
{
use SpatialTrait;
protected $table = 'interest_point';
protected $fillable = ['project_id', 'name', 'information', 'category' ];
protected $spatialFields = [
'location',
'area'
];
public $timestamps = false;
public function project()
{
return $this->belongsTo('App\Models\Project', 'project_id');
}
}
The output of the test is:
Failed asserting that a row in the table [interest_point] matches the attributes {
"project_id": 1,
"location": {
"type": "Point",
"coordinates": [
51.68828138589033,
5.287020206451416
]
},
"area": null,
"name": "TestCaseProjectPoint",
"information": "This is a automated test ProjectPoint, please delete this point!",
"category": "bezienswaardigheid"
}.
But I expect to see the test case succeed and when checking the database no records have been added to the database

Have you tried still putting location and area in your fillables?

Related

how to change query so it only shows name instead whole array (laravel 8)

I got a query that gets the data into a collection, problem is that it shows foreign id but i want it to display what i have given in the url parameters.
columnsGiven is the parameter from url. contains column names with child: "language.name". so column=active,title,language.name
For example i get this:
"name": "george",
"active": 1,
"language": 1,
and this is what i want:
"name": "george",
"active": 1,
"language": "Dutch",
this is my code:
public function index(Request $request){
$columnsGiven = explode(',', $request->columns);
$tableName = $request->table_name; //example: 'support_guide_translations'
$modelName = $request->model_name; //example: "App\Models\SupportGuideTranslation";
if($request->search){
$query = $modelName::search($request->search);
} else{
$query = $modelName::query();
if($request->sort){
$sort = explode('?', $request->sort);
$query->orderBy($sort[0], $sort[1]);
}
}
foreach ($request->query() as $key => $value) {
// dd($value);
if(!$value == ""){
if(Schema::hasColumn($tableName, $key)){
$query->where($key, $value);
}
if(in_array($key, $columnsGiven)){
dd('true');
}
// $searchWord = Str::contains('account123',$request->search);
}
}
$guides = $query->get();
return GuideResource::collection($guides);
}
}
this is GuideResource, it sends data to vue by making it json first. Not allowed to make changes here, it has to be done in the function index. :
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'active' => $this->active,
'language' => $this->language,
'support_guide' => $this->support_guide,
'support_guide_group' => $this->support_guide_group,
];
}
"language": {
"id": 1,
"name": "Dutch",
"code": "NL",
"created_at": "2021-06-14T10:10:32.000000Z",
"updated_at": "2021-06-14T10:10:32.000000Z"
},
I believe "language" has a relationship with another model.
Isn't this what you are looking for?
return [
// your other attributes...
'language' => $this->language->relationshipName->name,
];
where relationshipName is the method name in the Language model.

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'
],
...
]
];

Shopify API in PHP using GuzzleHttp array of objects

I am currently attempting to update a bunch of products via the Shopify API, however, when I am sending the request, the product is created, however, it appears to be ignoring things where it is an array or arrays (for example, images or variants).
This is my Shopify Helper class that I am using for all requests.
class Shopify {
protected $api_key;
protected $password;
protected $url;
protected $host;
protected $secret;
protected $client;
public function __construct() {
$this->api_key = env('SHOPIFY_API_KEY');
$this->password = env('SHOPIFY_API_PASSWORD');
$this->secret = env('SHOPIFY_API_SHARED_SECRET');
$this->host = env('SHOPIFY_API_HOST');
$this->url = "https://{$this->api_key}:{$this->password}#{$this->host}";
$this->client = new Client();
}
public function __call($method, $args)
{
$method = strtoupper($method);
$allowedMethods = ['POST','GET','PUT','DELETE'];
if(!in_array($method,$allowedMethods)){
throw new InvalidMethodRequestException();
}
return $this->request($method,trim($args[0]),$args[1] ?? []);
}
protected function request(string $method, string $uri, array $payload)
{
$response = $this->client->request(
$method,
"{$this->url}{$uri}",
[
'form_params' => $payload
]
);
return json_decode($response->getBody());
}
}
Here is an example of me using this client to create a product with a variant:
$shopify = new Shopify();
$result = $shopify->post('/admin/api/2020-10/products.json', [
'product' => [
'title' => $product->title,
'body_html' => $product->body_text,
"variants" => [
[ "sku" => $product->sku, "price" => 20.00 ]
]
]
]);
As I mentioned above, the product is created in Shopify, but is missing anything where the data is an array of arrays. Could this be todo with the way GuzzleHttp encodes the data? How can get this data in a format that Shopify needs?
Your code looks good. The variants output maybe need some conversion to json format.
As you can see here, the expected post request:
"variants": [
{
"option1": "First",
"price": "10.00",
"sku": "123"
},
I would try to dump the post variable before the request and see what is wrong.
I made some successfull code with laravel in the past using Facades, and worked. But it took me some time to make it as shopify needs.
PS: I'm using tokens here https://www.shopify.com/partners/blog/17056443-how-to-generate-a-shopify-api-token
See:
//my endpoint, in your case '/admin/api/2020-10/products.json
$endpoint = config('endpoint');
//registered as a token.
$token = config('token');
$postFields = 'product' => [
'title' => $product->title,
'body_html' => $product->body_text,
"variants" => [
[ "sku" => $product->sku, "price" => 20.00 ]
]
];
//in case of any problems, you can uncomment this line and inspect your request.
//json_encode will help with this
//dd(json_encode($postFields));
return Http::withHeaders([
"content-type" => "application/json",
"Authorization" => "Bearer " . $token
])->post($endpoint, $postFields)->json();

Extend Laravel package

I've searched around and couldn't find a definitive answer for this...
I have a package DevDojo Chatter and would like to extend it using my application. I understand I'd have to override the functions so that a composer update doesn't overwrite my changes.
How do I go about doing this?
UPDATE
public function store(Request $request)
{
$request->request->add(['body_content' => strip_tags($request->body)]);
$validator = Validator::make($request->all(), [
'title' => 'required|min:5|max:255',
'body_content' => 'required|min:10',
'chatter_category_id' => 'required',
]);
Event::fire(new ChatterBeforeNewDiscussion($request, $validator));
if (function_exists('chatter_before_new_discussion')) {
chatter_before_new_discussion($request, $validator);
}
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
$user_id = Auth::user()->id;
if (config('chatter.security.limit_time_between_posts')) {
if ($this->notEnoughTimeBetweenDiscussion()) {
$minute_copy = (config('chatter.security.time_between_posts') == 1) ? ' minute' : ' minutes';
$chatter_alert = [
'chatter_alert_type' => 'danger',
'chatter_alert' => 'In order to prevent spam, please allow at least '.config('chatter.security.time_between_posts').$minute_copy.' in between submitting content.',
];
return redirect('/'.config('chatter.routes.home'))->with($chatter_alert)->withInput();
}
}
// *** Let's gaurantee that we always have a generic slug *** //
$slug = str_slug($request->title, '-');
$discussion_exists = Models::discussion()->where('slug', '=', $slug)->first();
$incrementer = 1;
$new_slug = $slug;
while (isset($discussion_exists->id)) {
$new_slug = $slug.'-'.$incrementer;
$discussion_exists = Models::discussion()->where('slug', '=', $new_slug)->first();
$incrementer += 1;
}
if ($slug != $new_slug) {
$slug = $new_slug;
}
$new_discussion = [
'title' => $request->title,
'chatter_category_id' => $request->chatter_category_id,
'user_id' => $user_id,
'slug' => $slug,
'color' => $request->color,
];
$category = Models::category()->find($request->chatter_category_id);
if (!isset($category->slug)) {
$category = Models::category()->first();
}
$discussion = Models::discussion()->create($new_discussion);
$new_post = [
'chatter_discussion_id' => $discussion->id,
'user_id' => $user_id,
'body' => $request->body,
];
if (config('chatter.editor') == 'simplemde'):
$new_post['markdown'] = 1;
endif;
// add the user to automatically be notified when new posts are submitted
$discussion->users()->attach($user_id);
$post = Models::post()->create($new_post);
if ($post->id) {
Event::fire(new ChatterAfterNewDiscussion($request));
if (function_exists('chatter_after_new_discussion')) {
chatter_after_new_discussion($request);
}
if($discussion->status === 1) {
$chatter_alert = [
'chatter_alert_type' => 'success',
'chatter_alert' => 'Successfully created a new '.config('chatter.titles.discussion').'.',
];
return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$slug)->with($chatter_alert);
} else {
$chatter_alert = [
'chatter_alert_type' => 'info',
'chatter_alert' => 'You post has been submitted for approval.',
];
return redirect()->back()->with($chatter_alert);
}
} else {
$chatter_alert = [
'chatter_alert_type' => 'danger',
'chatter_alert' => 'Whoops :( There seems to be a problem creating your '.config('chatter.titles.discussion').'.',
];
return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$slug)->with($chatter_alert);
}
}
There's a store function within the vendor package that i'd like to modify/override. I want to be able to modify some of the function or perhaps part of it if needed. Please someone point me in the right direction.
If you mean modify class implementation in your application you can change the way class is resolved:
app()->bind(PackageClass:class, YourCustomClass::class);
and now you can create this custom class like so:
class YourCustomClass extends PackageClass
{
public function packageClassYouWantToChange()
{
// here you can modify behavior
}
}
I would advise you to read more about binding.
Of course a lot depends on how class is created, if it is created using new operator you might need to change multiple classes but if it's injected it should be enough to change this single class.

Resources