GithubHelp home page GithubHelp logo

Mocking Responses about mailjet-apiv3-php HOT 15 CLOSED

mailjet avatar mailjet commented on August 15, 2024
Mocking Responses

from mailjet-apiv3-php.

Comments (15)

bretto36 avatar bretto36 commented on August 15, 2024

Just realised you ARE using guzzle - i'll keep you posted with how i go getting mocks in there

from mailjet-apiv3-php.

WeshGuillaume avatar WeshGuillaume commented on August 15, 2024

Hey @bretto36 ! Amazing! We are definitely going to feature your work in our documentation!

I don't really get what you are looking for here. You want the client to prevent the Http call right ?
you just need to pass a third parameter to the constructor. https://github.com/mailjet/mailjet-apiv3-php/blob/master/src/Mailjet/Client.php#L41 . false will return a response without actual data/status

Please let me know if you need any help on implementing this !

from mailjet-apiv3-php.

bretto36 avatar bretto36 commented on August 15, 2024

I can prevent the actual call (using the flag), but what i'm hoping to achieve is to simulate a response. EG I have an app where i want to test that the mail is sent and handles valid responses. I want to be able to see what it will do if i provide it with an error response and with a success response so i can ensure it fails gracefully and provides a meaningful error

from mailjet-apiv3-php.

Zhivko-Mailjet avatar Zhivko-Mailjet commented on August 15, 2024

@bretto36
We are about to release new version of our Send API v3.1. In there, we will have new mode called Sandbox mode. When set to true, the Send API processes the message as usual, validating the input payload but does not actually drop the message in the sending queue. When everything’s OK, it returns a 200 OK, using the success payload with fake IDs in it.
I believe this is similar to what you would like to achieve. The release of the new SendAPI is coming soon, stay tuned!

from mailjet-apiv3-php.

wysow avatar wysow commented on August 15, 2024

@Zhivko-Mailjet really great to hear that as this is exactly the feature I need right now ;), can you provide an approximate release date? (maybe an exact one... ;))

from mailjet-apiv3-php.

Zhivko-Mailjet avatar Zhivko-Mailjet commented on August 15, 2024

Hi @wysow , I can not give you exact date yet but the release of the new Send API is scheduled for January. Initially, it will start as private beta.

from mailjet-apiv3-php.

wysow avatar wysow commented on August 15, 2024

@Zhivko-Mailjet thanks for the feedback, really would like to be part of this beta if possible, what's the process to be in?

from mailjet-apiv3-php.

Zhivko-Mailjet avatar Zhivko-Mailjet commented on August 15, 2024

Hi @wysow,
I'm happy to hear you are interested in testing our new API. Please contact our support, selecting the API category, for details.

from mailjet-apiv3-php.

wysow avatar wysow commented on August 15, 2024

Done @Zhivko-Mailjet thanks.

from mailjet-apiv3-php.

tentacode avatar tentacode commented on August 15, 2024

@wysow @Zhivko-Mailjet is 3.1 out now ? Can't find anywhere in the doc for a sanbox flag (or the 3.1 changelog)

edit : ok looks like it's still in beta... guess we'll have to wait

from mailjet-apiv3-php.

Zhivko-Mailjet avatar Zhivko-Mailjet commented on August 15, 2024

@wysow @tentacode @bretto36
The new SendAPI beta is live. It has the Sandbox mode, which allows you to simulate the sending, verifyting the payload. See more details here:
https://dev.mailjet.com/beta/#sandbox-mode

from mailjet-apiv3-php.

dunglas avatar dunglas commented on August 15, 2024

Hi @Zhivko-Mailjet,

I've just tried this feature but even if I set the SandBoxMode flag, the mail is sent. The code I wrote:

    public function send(int $templateId, array $toUsers, array $vars = []): bool
    {
        $recipients = [];
        foreach ($toUsers as $user) {
            $recipients[] = [
                'Email' => $user->getEmail(),
                'Name' => \sprintf('%s %s', $user->getFirstname(), $user->getLastname()),
                'Vars' => $vars,
            ];
        }

        $body = [
            'FromEmail' => $this->fromEmail,
            'MJ-TemplateID' => $templateId,
            'MJ-TemplateLanguage' => true,
            'Recipients' => $recipients,
        ];

        if ($this->sandbox) {
            $body['SandboxMode'] = true;
        }

        $response = $this->mailjet->post(Resources::$Email, ['body' => $body]);

        if ($response->success()) {
            return true;
        }

        $this->logger->critical('Mailjet error: {error}', ['error' => $response->getReasonPhrase()]);

        return false;
    }

Am I doing something wrong?

from mailjet-apiv3-php.

Zhivko-Mailjet avatar Zhivko-Mailjet commented on August 15, 2024

Hi @dunglas

The Sandbox mode is new functionality, which works in our new SendAPI v3.1. In order to take advantage of it, you have to switch the API versions and adapt your code as per the new requirements. See sample code below:

use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'),true,['version' => 'v3.1']);
$body = [
    'Messages' => [
        [
            'From' => [
                'Email' => "[email protected]",
                'Name' => "Mailjet Pilot"
            ],
            'To' => [
                [
                    'Email' => "[email protected]",
                    'Name' => "passenger 1"
                ]
            ],
            'Subject' => "Your email flight plan!",
            'TextPart' => "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!",
            'HTMLPart' => "<h3>Dear passenger 1, welcome to Mailjet!</h3><br />May the delivery force be with you!"
        ]
    ],
    'SandboxMode' => true
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
$response->success() && var_dump($response->getData());

Additional information you can find in our guides:
https://dev.mailjet.com/beta/?php#send-api-v3-to-v3-1

from mailjet-apiv3-php.

dunglas avatar dunglas commented on August 15, 2024

Thanks, it works now. However there is an issue in the example in https://dev.mailjet.com/beta/?php#using-a-template:

\Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'),['version' => 'v3.1']); the 3rd parameter (true) is missing.

from mailjet-apiv3-php.

Zhivko-Mailjet avatar Zhivko-Mailjet commented on August 15, 2024

Thanks for reporting that. We have also spotted the error. It is now fixed.

from mailjet-apiv3-php.

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.