New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lumen module not loading Service providers #2607

Closed
NeokamiCode opened this Issue Dec 4, 2015 · 4 comments

Comments

Projects
None yet
2 participants
@ghost

ghost commented Dec 4, 2015

Hi,

I am trying to load a validation service provider that extends the validator facade but none of the extensions are found when the tests are run. The bootstrap/app.php is registering the provider and the logic works just fine out of Codeception.

Any ideas?
Thanks!

@janhenkgerritsen janhenkgerritsen self-assigned this Dec 5, 2015

@janhenkgerritsen

This comment has been minimized.

Show comment
Hide comment
@janhenkgerritsen

janhenkgerritsen Dec 7, 2015

Contributor

Can you provide some code samples of what you are doing? And what versions of Lumen and Codeception are you using?

Contributor

janhenkgerritsen commented Dec 7, 2015

Can you provide some code samples of what you are doing? And what versions of Lumen and Codeception are you using?

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Dec 7, 2015

Lumen is version 5.1.6 and Codeception 2.1.4
So under app/Providers there is this class

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ValidationServiceProvider extends ServiceProvider {

    public function register() {}

    public function boot() {
        \Validator::extend('alpha_dash_spaces', function($attribute, $value)
        {
            return (bool) preg_match( '/^[a-z0-9 .\-]+$/i', $value );
        });

        \Validator::extend('is_array_of_strings', function($attribute, $values, $parameters)
        {
            $checkStrings = function($carry, $string){
                return $carry && is_string($string) !== false;
            };
            if(!is_array($values)) return false;
            return  array_reduce($values,$checkStrings,true);
        });
    }
}

The problem is that the provider seems to not to be registered so the extension methods are not available and a BadMethodCallException is thrown while running the test.

On the app.php file under app/bootstrap there is this line to register the provider:

$app->register('App\Providers\ValidationServiceProvider');

ghost commented Dec 7, 2015

Lumen is version 5.1.6 and Codeception 2.1.4
So under app/Providers there is this class

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ValidationServiceProvider extends ServiceProvider {

    public function register() {}

    public function boot() {
        \Validator::extend('alpha_dash_spaces', function($attribute, $value)
        {
            return (bool) preg_match( '/^[a-z0-9 .\-]+$/i', $value );
        });

        \Validator::extend('is_array_of_strings', function($attribute, $values, $parameters)
        {
            $checkStrings = function($carry, $string){
                return $carry && is_string($string) !== false;
            };
            if(!is_array($values)) return false;
            return  array_reduce($values,$checkStrings,true);
        });
    }
}

The problem is that the provider seems to not to be registered so the extension methods are not available and a BadMethodCallException is thrown while running the test.

On the app.php file under app/bootstrap there is this line to register the provider:

$app->register('App\Providers\ValidationServiceProvider');
@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Dec 7, 2015

The following happened:
It threw the exception when I was running the test like that

vendor/bin/codecept run tests/api/ProvidersCest.php:test_it_configures_provider

But I don't have the described problem if I run it without specifying a test function. So just

vendor/bin/codecept run tests/api/ProvidersCest.php

ghost commented Dec 7, 2015

The following happened:
It threw the exception when I was running the test like that

vendor/bin/codecept run tests/api/ProvidersCest.php:test_it_configures_provider

But I don't have the described problem if I run it without specifying a test function. So just

vendor/bin/codecept run tests/api/ProvidersCest.php
@janhenkgerritsen

This comment has been minimized.

Show comment
Hide comment
@janhenkgerritsen

janhenkgerritsen Dec 9, 2015

Contributor

I cannot replicate your issue. But how do you use your validator? I tested it with the following code after adding your validator definition to the AppServiceProvider::register() method:

$data = ['test' => 'abcde%'];
$rules = ['test' => 'alpha_dash_spaces'];
$validator = \Validator::make($data, $rules);
$validator->fails(); // or $validator->passes()

Everything worked as expected when doing it like this.

Contributor

janhenkgerritsen commented Dec 9, 2015

I cannot replicate your issue. But how do you use your validator? I tested it with the following code after adding your validator definition to the AppServiceProvider::register() method:

$data = ['test' => 'abcde%'];
$rules = ['test' => 'alpha_dash_spaces'];
$validator = \Validator::make($data, $rules);
$validator->fails(); // or $validator->passes()

Everything worked as expected when doing it like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment