Codeception add new JsonTypes - testing

I wonder if it's possible to define some custom JsonType for validation API responses through seeResponseMatchesJsonType method. I mean, let's suppose I have response with a structure:
[
'id' => 'integer',
'name' => 'string',
'address' => [
'street' => 'string',
'city' => 'string'
]
]
Obviously this structure has complex type address embedded and in the whole app such type might be used several times so I would like to write simply:
$I->seeResponseMatchesJsonType([
'id' => 'integer',
'name' => 'string',
'address' => 'addressType'
]);
Without rewriting this embedded structure all the time. How can I achieve it in Codeception?

Yes you can do this by using method addCustomFilter from \Codeception\Util\JsonType class.
/**
* Adds custom filter to JsonType list.
* You should specify a name and parameters of a filter.
*
* Example:
*
* ```php
* <?php
* JsonType::addCustomFilter('email', function($value) {
* return strpos('#', $value) !== false;
* });
* // => use it as 'string:email'
*
* // add custom function to matcher with `len($val)` syntax
* // parameter matching patterns should be valid regex and start with `/` char
* JsonType::addCustomFilter('/len\((.*?)\)/', function($value, $len) {
* return strlen($value) == $len;
* });
* // use it as 'string:len(5)'
* ?>
* ```
*
* #param $name
* #param callable $callable
*/
public static function addCustomFilter($name, callable $callable)
{
static::$customFilters[$name] = $callable;
}

Related

Object of class Faker\UniqueGenerator could not be converted to string

I am using laravel 9. I am trying to generate some fake users to boost my db using factoreis and seeders.
When i try to seed my db i get this error
Object of class Faker\UniqueGenerator could not be converted to string
at D:\PROJECTS\LARAVEL\todo-list\vendor\laravel\framework\src\Illuminate\Database\Connection.php:665
661▕ $value,
662▕ match (true) {
663▕ is_int($value) => PDO::PARAM_INT,
664▕ is_resource($value) => PDO::PARAM_LOB,
➜ 665▕ default => PDO::PARAM_STR
666▕ },
667▕ );
668▕ }
669▕ }
1 D:\PROJECTS\LARAVEL\todo-list\vendor\laravel\framework\src\Illuminate\Database\Connection.php:665
PDOStatement::bindValue(Object(Faker\UniqueGenerator))
2 D:\PROJECTS\LARAVEL\todo-list\vendor\laravel\framework\src\Illuminate\Database\Connection.php:540
Illuminate\Database\Connection::bindValues(Object(PDOStatement))
this my UserFactory
class UserFactory extends Factory
{
/**
* Define the model's default state.
*
* #return array<string, mixed>
*/
public function definition()
{
return [
'name' => fake()->name(),
'username' => fake()->unique(),
'company' => fake()->sentence(),
'position' => fake()->sentence(),
'bio' => fake()->realText($maxNbChars = 100),
'picture' => fake()->imageUrl(90, 90),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => Hash::make('password'), // password
'remember_token' => Str::random(10),
];
}
/**
* Indicate that the model's email address should be unverified.
*
* #return static
*/
public function unverified()
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}
}
After looking arround for a while I found that fake()->unique() does not return a string. So I tried to convert it to string but It also gives me a error saying Unknown format "toString" in Faker\Generator.php:731
[Problem solved]
I just had to edit my UserFactory username to this
'username' => fake()->unique()->text(16),

error in Laravel8: SQLSTATE[42S02]: Base table or view not found: 1146 Table

I have started a new project with Laravel 8.
I use the starter kit Laravel Breeze.
But I can't customize fields.
I have changed fields in the migration and Register Controller and User model.
here is my code:
migration file.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class TblUsers extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('tbl_users', function (Blueprint $table) {
$table->id();
$table->string('fullname');
$table->string('username');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('phone');
$table->string('organization_type');
$table->string('community_dev_auth_id');
$table->string('file_names');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('tbl_users');
}
}
register controller file.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class RegisteredUserController extends Controller
{
/**
* Display the registration view.
*
* #return \Illuminate\View\View
*/
public function create()
{
return view('auth.register');
}
/**
* Handle an incoming registration request.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\RedirectResponse
*
* #throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
{
$request->validate([
'fullname' => 'required|string|max:255',
'username' => 'required|string|max:255',
'email' => 'required|senter code heretring|email|max:255|unique:users',
'phone' => 'required|string|max:255',
'organization' => 'required|string|max:255',
'community' => 'required|string|max:255',
// 'phone' => 'required|string|max:255',
'password' => 'required|string|min:8',
]);
Auth::login($user = User::create([
'fullname' => $request->fullname,
'username' => $request->username,
'email' => $request->email,
'phone' => $request->phone,
'organization_type' => $request->organization,
'community_dev_auth_id' => $request->community,
'password' => Hash::make($request->password),
]));
event(new Registered($user));
return redirect(RouteServiceProvider::HOME);
}
}
user model file.
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'fullname',
'username',
'email',
'phone',
'organization',
'community',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
I have run this project, but it returns this error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'ambulance_dubai.users' doesn't exist (SQL: select count(*) as
aggregate from users where email = asdf#sdf.df)
Since you are using a different table name for the user model you have to define it in your model. By default, Laravel will look for the plural name
of a model(users) if your model doesn't have a table property.
Add this to the user model:
protected $table='tbl_user';
first, you will check user table was migrate, the user table not to be migrated
use this command
php artisan migrate
Open your User Model
and add $table
class User extends Authenticatable {
protected $table = 'users';
}
Another cause could be that the validation has a different name for the table. For example, having the table tbl_users in the validation could exist an error and have:
'required|unique:tbl_user,email'.
The letter "s" is missing and the error would be thrown.
`
return [
//
'name' => 'required',
'image' => 'required',
'email' => 'required|unique:users,email',
'username' => 'required|unique:users,username',
'password' => 'required|min:8',
];
`
Where you see users should correspond to your table name in the database.
This is under validation.

Unable to determine entity identifier for object of type "Namespace\Class"; no fields matching "id"

I am trying to use ORM in my laminas api tools. Everything is working, however, looks like the api-tools-hal is not able to recognise the fields.
This is part of my module.config.php
'router' => [
'routes' => [
'customer.rest.customer' => [
'type' => 'Segment',
'options' => [
'route' => '/customer[/:customer_id]',
'defaults' => [
'controller' => 'Customer\\V1\\Rest\\Customer\\Controller',
],
],
],
],
'api-tools-hal' => [
'metadata_map' => [
\Customer\V1\Rest\Customer\CustomerEntity::class => [
'entity_identifier_name' => 'id',
'route_name' => 'customer.rest.customer',
'route_identifier_name' => 'customer_id',
'hydrator' => \Laminas\Hydrator\ObjectPropertyHydrator::class,
],
],
]
My Customer\V1\Rest\Customer\CustomerEntity::class
use Doctrine\ORM\Mapping as ORM;
/**
* CustomerEntity
* #ORM\Entity
* #ORM\Table(uniqueConstraints={
* #ORM\UniqueConstraint(name="email", columns={"email"}),
* })
* #ORM\Entity(repositoryClass="Customer\V1\Rest\Customer\CustomerRepository")
* #ORM\Table(name = "customers")
*/
class CustomerEntity
{
/**
* The unique auto incremented primary key.
*
* #var int|null
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
ORM cli command orm:schema-tool:create does works.
But upon going to domain.com/customer it throws this error:
Unable to determine entity identifier for object of type
"Customer\V1\Rest\CustomerType\CustomerEntity"; no fields
matching "id"
When I remove the ORM annotation in the entity then it works.
What do I need to do in this case?
Actually, no, that is not stupid ;). And please, don't make your id public since your have no reason to do it.
Another way to fix this is to create a couple of getter/setter. In this case, I would guess that a getter is enought. So simply make a public function getId(): ?int to fix the error, no need for a setter as you only need it to resolve the argument of a route.
public function getId(): ?int
{
return $this->id;
}
Small tips: if you use PHPStorm, simply press Alt + Inser and then Getter to generate quickly this ;)
Ok, so I need to make my fields public.
protected $id; becomes public $id then it works.
This is just me being stupid. Learning is good.

updating yii form record with uploaded file it throw an error if i don't attach the file again

when i create a new record in my yii form with uploaded file, it's working fine, but when i update ihave to attach the file again or else it will give error
here is my controller file, please tell me what is my mistake
my uploaded file is an image, what i want is to change one field let's say the date and keep the rest as is including the uploaded file, but if don't attach the file again it will give an error
<?php
namespace app\controllers;
use Yii;
use app\models\JetskiDamageSettlementAgreement;
use app\models\JetskiDamageSettlementAgreementSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
/**
* JetskiDamageSettlementAgreementController implements the CRUD actions for JetskiDamageSettlementAgreement model.
*/
class JetskiDamageSettlementAgreementController extends Controller
{
/**
* {#inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all JetskiDamageSettlementAgreement models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new JetskiDamageSettlementAgreementSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single JetskiDamageSettlementAgreement model.
* #param integer $id
* #return mixed
* #throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new JetskiDamageSettlementAgreement model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new JetskiDamageSettlementAgreement();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
// get the instance of the uploaded file
$model->damage_image = UploadedFile::getInstance($model, 'damage_image');
$image_name = $model->customer_name.'.'.$model->damage_image->extension;
$image_path = 'attachments/' .$image_name;
$model->damage_image->saveAs($image_path);
$model->damage_image = $image_path;
$model->agreement_date = date ('y-m-d h:m:s');
$model->save();
return $this->redirect(['view', 'id' => $model->agreement_id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing JetskiDamageSettlementAgreement model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
* #throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->damage_image = UploadedFile::getInstance($model, 'damage_image');
$image_name = $model->customer_name.'.'.$model->damage_image->extension;
$image_path = 'attachments/' .$image_name;
$model->damage_image->saveAs($image_path);
$model->damage_image = $image_path;
$model->save();
return $this->redirect(['view', 'id' => $model->agreement_id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing JetskiDamageSettlementAgreement model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
* #throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the JetskiDamageSettlementAgreement model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return JetskiDamageSettlementAgreement the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = JetskiDamageSettlementAgreement::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
From what I understand you are getting the error because of your rules set to that model. In your model rules, this field for the file is set to be required for all scenarios.
One possible solution is to set the field to be required only on insert scenario and leave update scenario to be not required for the field. But this really depends on business logic that you need to satisfy.

How to override ImageFactory in Magento 2.3.3

i have my module which is overriding product in images in product list page (it is loading product images from custom attribute).
In Magento 2.2.x there was file vendor/magento/module-catalog/Block/Product/ImageBuilder.php which you can override and then use your own product image, in my case loaded from custom attribute.
class ImageBuilder extends \Magento\Catalog\Block\Product\ImageBuilder
{
public function create()
{
/** #var \Magento\Catalog\Helper\Image $helper */
$helper = $this->helperFactory->create()
->init($this->product, $this->imageId);
$template = $helper->getFrame()
? 'Magento_Catalog::product/image.phtml'
: 'Magento_Catalog::product/image_with_borders.phtml';
$imagesize = $helper->getResizedImageInfo();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($this->product->getId());
$url = $product->getData('MY_OWN_ATTRIBUTE');
if (trim($url) == '')
{
$url = $helper->getUrl();
}
$data = [
'data' => [
'template' => $template,
'image_url' => $url,
'width' => $helper->getWidth(),
'height' => $helper->getHeight(),
'label' => $helper->getLabel(),
'ratio' => $this->getRatio($helper),
'custom_attributes' => $this->getCustomAttributes(),
'resized_image_width' => !empty($imagesize[0]) ? $imagesize[0] : $helper->getWidth(),
'resized_image_height' => !empty($imagesize[1]) ? $imagesize[1] : $helper->getHeight()
],
];
return $this->imageFactory->create($data);
}
}
In Magento 2.3.3 this code was moved into vendor/magento/module-catalog/Block/Product/ImageFactory.php
Even if i copy whole file and overrride it under my module (without any changes), product list page wont load and i dont see any errors. It just look like this:
This is how look ImageFactory.php under my module:
<?php
namespace MY_COMPANY\MY_MODULE\Block\Product;
use Magento\Catalog\Block\Product\Image as ImageBlock;
use Magento\Catalog\Model\View\Asset\ImageFactory as AssetImageFactory;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\ConfigInterface;
use Magento\Catalog\Helper\Image as ImageHelper;
class ImageFactory extends \Magento\Catalog\Block\Product\ImageFactory
{
/**
* #var ConfigInterface
*/
private $presentationConfig;
/**
* #var AssetImageFactory
*/
private $viewAssetImageFactory;
/**
* #var ParamsBuilder
*/
private $imageParamsBuilder;
/**
* #var ObjectManagerInterface
*/
private $objectManager;
/**
* #var PlaceholderFactory
*/
private $viewAssetPlaceholderFactory;
/**
* #param ObjectManagerInterface $objectManager
* #param ConfigInterface $presentationConfig
* #param AssetImageFactory $viewAssetImageFactory
* #param PlaceholderFactory $viewAssetPlaceholderFactory
* #param ParamsBuilder $imageParamsBuilder
*/
public function __construct(
ObjectManagerInterface $objectManager,
ConfigInterface $presentationConfig,
AssetImageFactory $viewAssetImageFactory,
PlaceholderFactory $viewAssetPlaceholderFactory,
ParamsBuilder $imageParamsBuilder
) {
$this->objectManager = $objectManager;
$this->presentationConfig = $presentationConfig;
$this->viewAssetPlaceholderFactory = $viewAssetPlaceholderFactory;
$this->viewAssetImageFactory = $viewAssetImageFactory;
$this->imageParamsBuilder = $imageParamsBuilder;
}
public function create(Product $product, string $imageId, array $attributes = null): ImageBlock
{
$viewImageConfig = $this->presentationConfig->getViewConfig()->getMediaAttributes(
'Magento_Catalog',
ImageHelper::MEDIA_TYPE_CONFIG_NODE,
$imageId
);
$imageMiscParams = $this->imageParamsBuilder->build($viewImageConfig);
$originalFilePath = $product->getData($imageMiscParams['image_type']);
if ($originalFilePath === null || $originalFilePath === 'no_selection') {
$imageAsset = $this->viewAssetPlaceholderFactory->create(
[
'type' => $imageMiscParams['image_type']
]
);
} else {
$imageAsset = $this->viewAssetImageFactory->create(
[
'miscParams' => $imageMiscParams,
'filePath' => $originalFilePath,
]
);
}
$data = [
'data' => [
'template' => 'Magento_Catalog::product/image_with_borders.phtml',
'image_url' => $imageAsset->getUrl(),
'width' => $imageMiscParams['image_width'],
'height' => $imageMiscParams['image_height'],
'label' => $this->getLabel($product, $imageMiscParams['image_type']),
'ratio' => $this->getRatio($imageMiscParams['image_width'], $imageMiscParams['image_height']),
'custom_attributes' => $this->getStringCustomAttributes($attributes),
'class' => $this->getClass($attributes),
'product_id' => $product->getId()
],
];
return $this->objectManager->create(ImageBlock::class, $data);
}
}
Of course i have defined override in di.xml.
Question is: how can i override ImageFactory.php in my module ?
thanks
just asking: have You deleted content of generated?
like rm -rf ./generated/*
and cleared cache (especially config)
like:
bin/magento c:c
https://devdocs.magento.com/guides/v2.3/howdoi/php/php_clear-dirs.html