Skip to content

Instantly share code, notes, and snippets.

@sixlive
Created Apr 25, 2019
Embed
What would you like to do?
<?php
namespace App\Http\Requests;
use App\Concerns\TransformsToMonetra;
use Illuminate\Foundation\Http\FormRequest;
class SaleRequest extends FormRequest
{
use TransformsToMonetra;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'ttid' => 'required_without:token|string',
'amount' => 'required',
'token' => 'required_without:ttid|string',
'allow_partial_authorization' => 'sometimes|required|boolean',
'card_present' => 'sometimes|required|boolean',
];
}
protected function getValidatorInstance()
{
return parent::getValidatorInstance()->after(function ($validator) {
if ($this->ecomm === true && $this->moto === true) {
$message = 'moto and ecomm can not both be set to true';
$validator->errors()->add('ecomm', $message);
$validator->errors()->add('moto', $message);
}
})->after(function ($validator) {
if (!empty($this->ttid) && !empty($this->token)) {
$message = 'ttid and token can not be sent together';
$validator->errors()->add('token', $message);
$validator->errors()->add('ttid', $message);
}
});
}
}
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Http\Request;
use App\Http\Requests\SaleRequest;
use Illuminate\Validation\ValidationException;
class SaleRequestTest extends TestCase
{
/** @test */
public function it_transforms_to_monetra()
{
$request = new SaleRequest([
'ttid' => '1234',
'amount' => '3.50',
'token' => '123456',
'allow_partial_authorization' => true,
'card_present' => true,
]);
$this->assertEquals([
'ttid' => '1234',
'amount' => '3.50',
'token' => '123456',
'nsf' => 'Y',
'cardpresent' => 'Y',
], $request->toMonetra());
}
/** @test */
public function it_authorizes_requests()
{
$this->assertTrue((new SaleRequest)->authorize());
}
/** @test */
public function it_requires_either_a_ttid_or_a_token()
{
try {
$this->app[SaleRequest::class];
} catch (ValidationException $e) {
$this->assertArrayHasKey('ttid', $e->errors());
$this->assertArrayHasKey('token', $e->errors());
}
}
/** @test */
public function ttid_and_token_cant_both_be_sent()
{
$this->app['request'] = new Request([
'ttid' => '1234',
'token' => '12345',
'amount' => '3.50',
]);
try {
$this->app[SaleRequest::class];
} catch (ValidationException $e) {
$this->assertArrayHasKey('token', $e->errors());
$this->assertArrayHasKey('ttid', $e->errors());
}
}
/** @test */
public function ecomm_and_moto_cant_both_be_true()
{
$this->app['request'] = new Request([
'amount' => '3.50',
'ttid' => '1234',
'moto' => true,
'ecomm' => true,
]);
try {
$this->app[SaleRequest::class];
} catch (ValidationException $e) {
$this->assertArrayHasKey('moto', $e->errors());
$this->assertArrayHasKey('ecomm', $e->errors());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment