Aug 20, 2023
0
Level 1
I have problem with phpunit for test api my requests not work
I have problem with phpunit for test api my requests work in postman call but thay dont work when I run in test I locate marge in TypeRequests::passedValidation but in controller there aren't that data
<?php
namespace App\Http\Requests;
use App\Http\Requests\TypeRequests\General;
use App\Models\Customer;
use App\Models\Enums\DnsRecordsType;
use App\Models\Enums\ZoneStatus;
use App\Models\Record;
use App\Rules\CheckTypeRule;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\App;
use Illuminate\Validation\Rule;
class CreateRecordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
protected function prepareForValidation()
{
$this->merge([
'zone' => $this->zone . '.',
'name' => $this->name . '.',
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
$DnsRecordType = data_get(DnsRecordsType::cases(), '*.name');
return [
"customer_id" => "required|ulid|exists:customers,customer_id",
"zone" => ["required",
"string",
Rule::exists("zones", "name")
->where(fn(Builder $query) => $query->where("status", ZoneStatus::success->name)
->where("deleted_at", null)
->whereIn("customer_id", Customer::where("customer_id", $this->customer_id)->pluck("id")))
],
"type" => ["required", Rule::in($DnsRecordType)],
"name" => ["required", "string"],
"ttl" => ["required", "integer"],
"disabled" => ["required", "boolean"]
];
}
protected function passedValidation(): void
{
App::make("App\Http\Requests\TypeRequests\".$this->type);
App::make(General::class);
}
}
and this is my test
<?php
namespace Tests\Feature;
use App\Models\Customer;
use App\Models\Enums\DnsRecordsType;
use App\Models\Record;
use App\Models\Zone;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Arr;
use Tests\TestCase;
class RecordTest extends TestCase
{
/**
* A basic feature test example.
*/
public function test_create_A_record(): void
{
$customer = Customer::first();
$data = [
"customer_id" => $customer->customer_id,
"zone" => "routerz.cloud",
"name" => "a.routerz.cloud",
"type" => "A",
"ttl" => 60,
"disabled" => false,
"text" => "127.0.0.200"
];
$response = $this->post("/api/v1/zones/record", $data, [
"Accept" => "application/json"
]
);dump($data,$response->content());
$response->assertStatus(201);
}
public function test_create_AAAA_record(): void
{
$customer = Customer::first();
$data = [
"customer_id" => $customer->customer_id,
"zone" => "routerz.cloud",
"name" => "aaaa.routerz.cloud",
"type" => "AAAA",
"ttl" => 60,
"disabled" => false,
"text" => "3001:0da8:75a3:0000:0000:8a2e:0370:7334"
];
$response = $this->post("/api/v1/zones/record", $data, [
"Accept" => "application/json"
]
);dump($data,$response->content());
$response->assertStatus(201);
}
public function test_list_record(): void
{
$customer = Customer::first();
$response = $this->get(
"/api/v1/zones/record?customer_id=$customer->customer_id&zone=routerz.cloud",
[
"Accept" => "application/json"
]
);
$response->assertStatus(200);
}
// public function test_update_record(): void
// {
// sleep(5);
// $customer = Customer::first();
// $record = Record::with(["content", "zone"])->whereHas("zone", function (Builder $q) {
// $q->where("name", "routerz.cloud");
// })->where("type", "A")->where("name", "aaa.routerz.cloud")->first();
// $response = $this->put(
// "/api/v1/zones/record",
// [
// "customer_id" => $customer->customer_id,
// "zone" => "routerz.cloud",
// "name" => "aaa.routerz.cloud",
// "type" => "A",
// "ttl" => 600,
// "content_id" => $record->content()->first()->id,
// "text" => "125.0.0.8",
// ],
// [
// "Accept" => "application/json"
// ]
// );
// $response->assertStatus(200);
// }
// public function test_delete_record(): void
// {
// sleep(5);
// $customer = Customer::first();
// $record = Record::with(["content", "zone"])->whereHas("zone", function (Builder $q) {
// $q->where("name", "routerz.cloud");
// })->where("type", "A")->where("name", "aaa.routerz.cloud")->first();
// $response = $this->delete("/api/v1/zones",
// [
// "customer_id" => $customer->customer_id,
// "zone" => "routerz.cloud",
// "name" => "aaa.routerz.cloud",
// "type" => "A",
// "content_id" => $record->content()->first()->id,
// ]);
// $response->assertStatus(200);
// }
public function test_create_SRV_record(): void
{
$customer = Customer::first();
$data = [
"customer_id" => $customer->customer_id,
"zone" => "routerz.cloud",
"name" => "srv.routerz.cloud",
"type" => "SRV",
"ttl" => 60,
"disabled" => false,
"text" => "a.routerz.cloud",
"port" => 30000,
"priority" => 50,
"weight" => 10,
];
$response = $this->post("/api/v1/zones/record", $data, [
"Accept" => "application/json",
"Content-Type" => "application/json",
]
);dump($data,$response->content());
$response->assertStatus(201);
}
public function test_create_CNAME_record(): void
{
$customer = Customer::first();
$data = [
"customer_id" => $customer->customer_id,
"zone" => "routerz.cloud",
"name" => "cname.routerz.cloud",
"type" => "CNAME",
"ttl" => 60,
"disabled" => false,
"text" => "sas.routerz.cloud",
];
$response = $this->post("/api/v1/zones/record", $data, [
"Accept" => "application/json"
]
);dump($data,$response->content());
$response->assertStatus(201);
}
public function test_create_NS_record(): void
{
$customer = Customer::first();
$data = [
"customer_id" => $customer->customer_id,
"zone" => "routerz.cloud",
"name" => "routerz.cloud",
"type" => "NS",
"ttl" => 60,
"disabled" => false,
"text" => "ns10.routerz.cloud",
];
$response = $this->post("/api/v1/zones/record", $data, [
"Accept" => "application/json"
]
);
dump($data,$response->content());
$response->assertStatus(201);
}
public function test_create_MX_record(): void
{
$customer = Customer::first();
$data = [
"customer_id" => $customer->customer_id,
"zone" => "routerz.cloud",
"name" => "mx.routerz.cloud",
"type" => "MX",
"ttl" => 60,
"disabled" => false,
"text" => "mailhost1.routerz.cloud",
"priority" => 10,
];
$response = $this->post("/api/v1/zones/record", $data, [
"Accept" => "application/json"
]
);
dump($data,$response->content());
$response->assertStatus(201);
}
public function test_create_TXT_record(): void
{
$customer = Customer::first();
$data = [
"customer_id" => $customer->customer_id,
"zone" => "routerz.cloud",
"name" => "txt.routerz.cloud",
"type" => "TXT",
"ttl" => 60,
"disabled" => false,
"text" => "\"HI\"",
];
$response = $this->post("/api/v1/zones/record", $data, [
"Accept" => "application/json"
]
);
dump($data,$response->content());
$response->assertStatus(201);
}
public function test_create_CAA_record(): void
{
$customer = Customer::first();
$data = [
"customer_id" => $customer->customer_id,
"zone" => "routerz.cloud",
"name" => "caa.routerz.cloud",
"type" => "CAA",
"ttl" => 60,
"disabled" => false,
"text" => "\"a.routerz.cloud\"",
"flags" => 0,
"tag" => "issue",
];
$response = $this->post("/api/v1/zones/record", $data, [
"Accept" => "application/json"
]
);dump($data,$response->content());
$response->assertStatus(201);
}
public function test_create_TLSA_record(): void
{
$customer = Customer::first();
$data = [
"customer_id" => $customer->customer_id,
"zone" => "routerz.cloud",
"name" => "tlsa.routerz.cloud",
"type" => "TLSA",
"ttl" => 60,
"disabled" => false,
"text" => "asdfvfdbgbnshagadf",
"license" => 2,
"choicer" => 1,
"match" => 2,
];
$response = $this->post("/api/v1/zones/record", $data, [
"Accept" => "application/json"
]
);dump($data,$response->content());
$response->assertStatus(201);
}
}
and these are TypeRequests that dont work
<?php
namespace App\Http\Requests\TypeRequests;
use Illuminate\Foundation\Http\FormRequest;
class SRV extends FormRequest
{
protected function prepareForValidation()
{
$this->merge([
'text' => $this->text . '.',
]);
}
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
return [
"text" => "required|regex:/^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)+\.$/i",
"port" => "required|integer|min:0|max:65535",
"priority" => "required|integer",
"weight" => "required|integer",
];
}
protected function passedValidation(): void
{
$this->merge([
'json_content' => [
"priority" => $this->priority,
"weight" => $this->weight,
"port" => $this->port,
"text" => $this->text,
],
'text' => $this->priority . " " . $this->weight . " " . $this->port . " " . $this->text,
]);
}
}
Please or to participate in this conversation.