Skip to content

Commit

Permalink
implement validation macro tests (#48570)
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 authored Sep 28, 2023
1 parent c834505 commit 69658a8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
40 changes: 40 additions & 0 deletions tests/Validation/ValidationMacroTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Tests\Validation;

use Illuminate\Validation\Rule;
use PHPUnit\Framework\TestCase;

class ValidationMacroTest extends TestCase
{
public function testMacroable()
{
// Define a phone validation macro
Rule::macro('phone', function () {
return 'regex:/^([0-9\s\-\+\(\)]*)$/';
});

$actualRule = Rule::phone();
$this->assertSame('regex:/^([0-9\s\-\+\(\)]*)$/', $actualRule);
}

public function testMacroArguments()
{
Rule::macro('maxLength', function (int $length) {
return "max:{$length}";
});

$actualRule = Rule::maxLength(10);
$this->assertSame('max:10', $actualRule);
}

public function testMacroDefaultArguments()
{
Rule::macro('maxLength', function ($length = 255) {
return "max:{$length}";
});

$actualRule = Rule::maxLength(); // No argument provided, should use default value
$this->assertSame('max:255', $actualRule);
}
}
19 changes: 0 additions & 19 deletions tests/Validation/ValidationRuleTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4486,7 +4486,7 @@ public function testValidateMimetypes()
{
$trans = $this->getIlluminateArrayTranslator();

$uploadedFile = [__DIR__.'/ValidationRuleTest.php', '', null, null, true];
$uploadedFile = [__DIR__.'/ValidationMacroTest.php', '', null, null, true];

$file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
$file->expects($this->any())->method('guessExtension')->willReturn('rtf');
Expand Down

0 comments on commit 69658a8

Please sign in to comment.