Coder Social home page Coder Social logo

Comments (7)

Sergej-Tihonov avatar Sergej-Tihonov commented on July 30, 2024 2

@FzSalehi , @sinamiandashti
I had the same problem today. Here my solution for it. Not pretty, but does the work. 😜

Some explanation:

You can not get the captcha value, because it is hashed after the creation and only the hash is obtained.
See: https://github.com/mewebstudio/captcha/blob/master/src/Captcha.php#L343

The comparison is done via hashing and comparing the provided value with existing hash.
See: https://github.com/mewebstudio/captcha/blob/master/src/Captcha.php#L478

The good thing is, that the author used dependency injection and all the properties and methods are public or protected, so you can easily override necessary parts and replace the base class. 👍

Here my simple Fake Class solution:

<?php

namespace Tests;

use Mews\Captcha\Captcha;

class CaptchaFake extends Captcha
{
    public static string $expectedValue = '';

    public static function fake(string $value = ''): void
    {
        self::$expectedValue = $value;

        app()->bind('captcha', function ($app) {
            return new CaptchaFake(
                $app['Illuminate\Filesystem\Filesystem'],
                $app['Illuminate\Contracts\Config\Repository'],
                $app['Intervention\Image\ImageManager'],
                $app['Illuminate\Session\Store'],
                $app['Illuminate\Hashing\BcryptHasher'],
                $app['Illuminate\Support\Str']
            );
        });
    }

    public function check(string $value): bool
    {
        return self::$expectedValue === $value;
    }
}

I have added a static expected value property, so I can easily simulate success and failure.
Just call the fake method at the beginning of your test method:

CaptchaFake::fake('123456');

I did not wanted to mock a lot so I just copied the creation of the original Captcha Class from the provider.
See: https://github.com/mewebstudio/captcha/blob/master/src/CaptchaServiceProvider.php#L73

If you use the check_api method, you can fake it as well.
https://github.com/mewebstudio/captcha/blob/master/src/Captcha.php#L505

Hope it helps you as well. Enjoy 😉

from captcha.

ronaldaraujo avatar ronaldaraujo commented on July 30, 2024 1

Oohh man, @Sergej-Tihonov. Thank so much. It's work!

from captcha.

rezasrk avatar rezasrk commented on July 30, 2024 1

Hi
You can use it like below

If used API

Captcha::shouldReceive('check_api')->andReturn(true);

If did not use API

Captcha::shouldReceive('check')->andReturn(true);

@sinamiandashti
@FzSalehi

from captcha.

sinamiandashti avatar sinamiandashti commented on July 30, 2024

same question

from captcha.

FzSalehi avatar FzSalehi commented on July 30, 2024

@sinamiandashti
thank you for your help .. I have also a solution which is not so pretty too 😜 but it is simpler

you can modify laravel request validations .. right ?

so just put a simple condition like so:

// in some controller
public function store(Request $request)
{
    //here we decide which inputs required
    $requiredInputs = $this->getInputRequired(); 


    //then do the validation ...
    $validatedData = $request->validate($requiredInputs );


    // The blog post is valid...
}

private function getInputRequired(){
    // this is golden condition
    if(env('debug')){

       return [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ];

    return [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
        'captcha' => 'required',
    ];
    }

}

in this way .. you will never need to pass captcha input from your test requests.

But anyways I suggest to @mewebstudio to do something about the testing environment.

I think @Sergej-Tihonov 's solution could be implemented in this package.

from captcha.

m3di avatar m3di commented on July 30, 2024

expectedValue

it's a very bad practice, never mix test and production code.

from captcha.

githubcodehome avatar githubcodehome commented on July 30, 2024

Hi You can use it like below

If used API

Captcha::shouldReceive('check_api')->andReturn(true);

If did not use API

Captcha::shouldReceive('check')->andReturn(true);

@sinamiandashti @FzSalehi

thank you, this work for me, I use FormRequest validation and it's working

from captcha.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.