drfrvbq736383 2015-05-23 22:38
浏览 234
已采纳

如何在Laravel 5中测试自定义验证规则?

I created a custom validation rule in Laravel, extending it in the register() method of a service provider, and I'd like to test it, but don't know how.

I took a look at Laravel framework's validation tests, but I couldn't understand the purpose of the getTranslator() and getRealTranslator() methods.

Could someone give me a hint on how to test Laravel's custom validation rules?

EDIT

That's what I did:

Created a ValidatorServiceProvider as follows:

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ValidatorServiceProvider extends ServiceProvider {

    /**
    * Bootstrap the application services.
    *
    * @return void
    */
    public function boot()
    {
        $this->app['validator']->extend('greater_than', function($attr, $val, $params)
        {
            return false;
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
    }
}

Also added it to the providers array and issued composer dump-autoload -o.

Added the following to tests\CustomValidationRulesTest.php:

<?php

use Mockery as m;
use Illuminate\Validation\Validator;

class CustomValidationRulesTest extends TestCase {

    public function tearDown()
    {
        m::close();
    }

    public function testValidateGreaterThan()
    {
        $trans = $this->getTranslator();

        $rules = [
            'field2' => 'greater_than:field1'
        ];

        $data = [
            'field1' => 1,
            'field2' => 2
        ];

        $v = new Validator($trans, $data, $rules);
        $this->assertTrue($v->passes());
    }

    protected function getTranslator()
    {
        return m::mock('Symfony\Component\Translation\TranslatorInterface');
    }

    protected function getRealTranslator()
    {
        $trans = new Symfony\Component\Translation\Translator('en', new Symfony\Component\Translation\MessageSelector);
        $trans->addLoader('array', new Symfony\Component\Translation\Loader\ArrayLoader);
        return $trans;
    }

}

Running PHPUnit gives me the following:

PHPUnit 4.6.6 by Sebastian Bergmann and contributors.

Configuration read from /home/ubuntu/workspace/phpunit.xml

E.

Time: 248 ms, Memory: 14.75Mb

There was 1 error:

1) CustomValidationRulesTest::testValidateGreaterThan
BadMethodCallException: Method [validateGreaterThan] does not exist.

/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:2615
/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:372
/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:372
/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:325
/home/ubuntu/workspace/tests/CustomValidationRulesTest.php:27

What I'm doing wrong?

Thanks!

  • 写回答

2条回答 默认 最新

  • duanlian1978 2015-07-30 04:58
    关注

    You need to understand a bit more the Container or IoC and how the Validator is registered there.

    Laravel registers an instance of Illuminate\Validation\Factory as validator. So if you check the \Illuminate\Support\Facades\Validator, you find that it resolves to the Factory. When you extend the validator, you are in fact adding an extension in the Factory. Now, calling Validator::make() calls the Factory which has the extensions and it creates the Validator with the extensions, while instantiating a new Illuminate\Validation\Validator won't be able to resolve the extentions from the Factory.

    You should not instantiate a new Validator, but use app('validator')->make().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀