How to use ReturnPromise class

Best Prophecy code snippet using ReturnPromise

CartEligibilityValidatorSpec.php

Source:CartEligibilityValidatorSpec.php Github

copy

Full Screen

...3namespace spec\Sylius\ShopApiPlugin\Validator\Cart;4use ArrayIterator;5use Doctrine\Common\Collections\ArrayCollection;6use PhpSpec\ObjectBehavior;7use Prophecy\Promise\ReturnPromise;8use Sylius\Component\Core\Model\OrderInterface;9use Sylius\Component\Core\Model\OrderItemInterface;10use Sylius\Component\Core\Model\ProductInterface;11use Sylius\Component\Core\Model\ProductVariantInterface;12use Sylius\Component\Core\Repository\OrderRepositoryInterface;13use Sylius\ShopApiPlugin\Request\Checkout\CompleteOrderRequest;14use Sylius\ShopApiPlugin\Validator\Constraints\CartEligibility;15use Symfony\Component\Validator\Context\ExecutionContextInterface;16use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;17final class CartEligibilityValidatorSpec extends ObjectBehavior18{19 function let(20 OrderRepositoryInterface $repository,21 ExecutionContextInterface $context22 ): void {23 $this->beConstructedWith($repository);24 $this->initialize($context);25 }26 function it_add_no_violation_if_cart_has_eligible_item(27 OrderRepositoryInterface $repository,28 OrderInterface $order,29 OrderItemInterface $orderItem,30 ProductVariantInterface $productVariant,31 ProductInterface $product,32 ArrayCollection $collection,33 ArrayIterator $arrayIterator,34 ExecutionContextInterface $context,35 CompleteOrderRequest $completeOrderRequest36 ): void {37 $completeOrderRequest->getToken()->willReturn('CART_TOKEN');38 $repository->findOneBy(['tokenValue' => 'CART_TOKEN', 'state' => OrderInterface::STATE_CART])->willReturn($order);39 $arrayIterator->valid()->will(new ReturnPromise(array_merge(array_fill(0, count([$orderItem]), true), [false])));40 $arrayIterator->current()->will(new ReturnPromise([$orderItem]));41 $arrayIterator->count()->willReturn(count([$orderItem]));42 $arrayIterator->next()->shouldBeCalled();43 $arrayIterator->rewind()->shouldBeCalled();44 $arrayIterator->key()->willReturn(0);45 $order->getItems()->willReturn($collection);46 $collection->getIterator()->willReturn($arrayIterator);47 $orderItem->getVariant()->willReturn($productVariant);48 $productVariant->getProduct()->willReturn($product);49 $productVariant->isEnabled()->willReturn(true);50 $product->isEnabled()->willReturn(true);51 $context->buildViolation('sylius.shop_api.checkout.cart_item.non_eligible')->shouldNotBeCalled();52 $context->buildViolation('sylius.shop_api.checkout.cart_item_variant.non_eligible')->shouldNotBeCalled();53 $this->validate($completeOrderRequest, new CartEligibility());54 }55 function it_add_violation_if_cart_has_non_eligible_item_variant(56 OrderRepositoryInterface $repository,57 OrderInterface $order,58 OrderItemInterface $orderItem,59 ProductVariantInterface $productVariant,60 ProductInterface $product,61 ArrayCollection $collection,62 ArrayIterator $arrayIterator,63 ExecutionContextInterface $context,64 ConstraintViolationBuilderInterface $builder,65 CompleteOrderRequest $completeOrderRequest66 ): void {67 $completeOrderRequest->getToken()->willReturn('CART_TOKEN');68 $repository->findOneBy(['tokenValue' => 'CART_TOKEN', 'state' => OrderInterface::STATE_CART])->willReturn($order);69 $arrayIterator->valid()->will(new ReturnPromise(array_merge(array_fill(0, count([$orderItem]), true), [false])));70 $arrayIterator->current()->will(new ReturnPromise([$orderItem]));71 $arrayIterator->count()->willReturn(count([$orderItem]));72 $arrayIterator->next()->shouldNotBeCalled();73 $arrayIterator->rewind()->shouldBeCalled();74 $arrayIterator->key()->willReturn(0);75 $order->getItems()->willReturn($collection);76 $collection->getIterator()->willReturn($arrayIterator);77 $orderItem->getVariant()->willReturn($productVariant);78 $productVariant->isEnabled()->willReturn(false);79 $context->buildViolation('sylius.shop_api.checkout.cart_item_variant.non_eligible')->willReturn($builder);80 $builder->atPath('items[0].product.variants[0].code')->willReturn($builder);81 $builder->addViolation()->shouldBeCalled();82 $this->validate($completeOrderRequest, new CartEligibility());83 }84 function it_add_violation_if_cart_has_non_eligible_item(85 OrderRepositoryInterface $repository,86 OrderInterface $order,87 OrderItemInterface $orderItem,88 ProductVariantInterface $productVariant,89 ProductInterface $product,90 ArrayCollection $collection,91 ArrayIterator $arrayIterator,92 ExecutionContextInterface $context,93 ConstraintViolationBuilderInterface $builder,94 CompleteOrderRequest $completeOrderRequest95 ): void {96 $completeOrderRequest->getToken()->willReturn('CART_TOKEN');97 $repository->findOneBy(['tokenValue' => 'CART_TOKEN', 'state' => OrderInterface::STATE_CART])->willReturn($order);98 $arrayIterator->valid()->will(new ReturnPromise(array_merge(array_fill(0, count([$orderItem]), true), [false])));99 $arrayIterator->current()->will(new ReturnPromise([$orderItem]));100 $arrayIterator->count()->willReturn(count([$orderItem]));101 $arrayIterator->next()->shouldNotBeCalled();102 $arrayIterator->rewind()->shouldBeCalled();103 $arrayIterator->key()->willReturn(0);104 $order->getItems()->willReturn($collection);105 $collection->getIterator()->willReturn($arrayIterator);106 $orderItem->getVariant()->willReturn($productVariant);107 $productVariant->getProduct()->willReturn($product);108 $productVariant->isEnabled()->willReturn(true);109 $product->isEnabled()->willReturn(false);110 $context->buildViolation('sylius.shop_api.checkout.cart_item_variant.non_eligible')->shouldNotBeCalled();111 $context->buildViolation('sylius.shop_api.checkout.cart_item.non_eligible')->willReturn($builder);112 $builder->atPath('items[0].product.code')->willReturn($builder);113 $builder->addViolation()->shouldBeCalled();...

Full Screen

Full Screen

LifecycleTrait.php

Source:LifecycleTrait.php Github

copy

Full Screen

1<?php2namespace Frontastic\Common;3use GuzzleHttp\Promise\FulfilledPromise;4use GuzzleHttp\Promise\PromiseInterface;5/**6 * Trait LifecycleTrait7 *8 * @package Frontastic\Common9 */10trait LifecycleTrait11{12 /**13 * @var array14 */15 private $listeners = [];16 /**17 * @var array18 */19 private $events = [];20 /**21 * @param mixed $listener22 */23 protected function addListener($listener): void24 {25 $this->listeners[] = $listener;26 }27 /**28 * @param array $events29 */30 protected function addEvents(array $events): void31 {32 $this->events = $events;33 }34 /**35 * Get *dangerous* inner client36 *37 * This method exists to enable you to use features which are not yet part38 * of the abstraction layer.39 *40 * Be aware that any usage of this method might seriously hurt backwards41 * compatibility and the future abstractions might differ a lot from the42 * vendor provided abstraction.43 *44 * Use this with care for features necessary in your customer and talk with45 * Frontastic about provising an abstraction.46 *47 * @return mixed48 */49 public function getDangerousInnerClient()50 {51 return $this->getAggregate()->getDangerousInnerClient();52 }53 /**54 * @param string $method55 * @param array $arguments56 * @return mixed57 */58 protected function dispatch(string $method, array $arguments)59 {60 $arguments = $this->before($method, $arguments);61 list($resultPromise, $returnPromise) = $this->call($method, $arguments);62 $resultPromise = $this->after($resultPromise, $method);63 return $returnPromise ? $resultPromise : $resultPromise->wait();64 }65 /**66 * @param string $method67 * @param array $arguments68 * @return array69 */70 protected function before(string $method, array $arguments): array71 {72 $beforeEvent = 'before' . ucfirst($method);73 foreach ($this->listeners as $listener) {74 if (is_callable([$listener, $beforeEvent])) {75 $newArguments = call_user_func_array(76 [$listener, $beforeEvent],77 array_merge([$this->getAggregateForListeners()], $arguments)78 );79 if (is_array($newArguments)) {80 $arguments = $newArguments;81 }82 }83 }84 return $arguments;85 }86 /**87 * @param string $method88 * @param array $arguments89 * @return array90 */91 protected function call(string $method, array $arguments): array92 {93 $rawResult = call_user_func_array([$this->getAggregate(), $method], $arguments);94 if ($rawResult instanceof PromiseInterface) {95 $resultPromise = $rawResult;96 $returnPromise = true;97 } else {98 $resultPromise = new FulfilledPromise($rawResult);99 $returnPromise = false;100 }101 return array($resultPromise, $returnPromise);102 }103 /**104 * @param $resultPromise105 * @param string $method106 * @return mixed107 */108 protected function after(PromiseInterface $resultPromise, string $method)109 {110 $resultPromise = $resultPromise->then(function ($result) use ($method) {111 $afterEvent = 'after' . ucfirst($method);112 foreach ($this->listeners as $listener) {113 if (is_callable([$listener, $afterEvent])) {114 $returnValue = $listener->$afterEvent($this->getAggregateForListeners(), $result);115 // If a listerner changes the return value, for example116 // replacing the default return object with an enriched custom117 // object we use this as a return value for now. The return118 // type hints ensure this will stay valid.119 if ($returnValue) {120 $result = $returnValue;121 }122 }123 }124 return $result;125 });126 return $resultPromise;127 }128 /**129 * Used for BC purposes in {@link LegacyLifecycleEventDecorator}.130 */131 protected function getAggregateForRawResult(): object132 {133 return $this->getAggregate();134 }135 /**136 * Used for BC purposes in {@link LegacyLifecycleEventDecorator}.137 */138 protected function getAggregateForListeners(): object139 {140 return $this->getAggregate();141 }142 /**143 * @return object144 */145 abstract protected function getAggregate(): object;146}...

Full Screen

Full Screen

Async.php

Source:Async.php Github

copy

Full Screen

1<?php2namespace Ajency\ServiceComm\Comm;3use Log;4/**5 * Class for asynchronous communication to an external service6 *7 * @package ServiceComm8 * @author Shashank Shetye Saudagar9 **/10class Async11{12 /**13 * Do an async call to a provider14 * @param string topic - Identifier for the message you are publishing15 * @param array payload - payload of the message to be sent16 * @param string provider - provider for the service to be used17 * @return void | guzzle promise18 * @author Shashank Shetye Saudagar19 **/20 public static function call($topic,$payload,$provider = null, $returnPromise = true)21 {22 getEnvHelper();23 if(is_null($provider)){24 $provider = config('service_comm.async_provider');25 }26 Log::notice('Publishing to ' . $topic . ' with ' . $provider);27 switch ($provider) {28 case 'sns':29 return self::snsCall($topic,$payload,$returnPromise);30 default:31 Log::error('Provider '.$provider.' Not supported');32 break;33 }34 }35 /**36 * Publish to AWS SNS asynchronously37 * @param string topic - Identifier for the message you are publishing38 * @param array payload - payload of the message to be sent39 * @return guzzle promise40 * @author Shashank Shetye Saudagar41 **/42 protected static function snsCall($topic, $payload, $returnPromise)43 {44 $sns = SNS::createInstance();45 if($returnPromise)46 return $sns->publishAsync($topic,$payload);47 else 48 return $sns->publish($topic,$payload);49 }50} // END class ...

Full Screen

Full Screen

ReturnPromise

Using AI Code Generation

copy

Full Screen

1$prophecy = new Prophecy\Prophet();2$prophecy->prophesize('ReturnPromise');3$prophecy->will(new ReturnPromise(array(1, 2, 3)));4$prophecy->reveal();5$prophecy = new Prophecy\Prophet();6$prophecy->prophesize('ReturnPromise');7$prophecy->will(new ReturnPromise(array(1, 2, 3)));8$prophecy->reveal();9$prophecy = new Prophecy\Prophet();10$prophecy->prophesize('ReturnPromise');11$prophecy->will(new ReturnPromise(array(1, 2, 3)));12$prophecy->reveal();13$prophecy = new Prophecy\Prophet();14$prophecy->prophesize('ReturnPromise');15$prophecy->will(new ReturnPromise(array(1, 2, 3)));16$prophecy->reveal();17$prophecy = new Prophecy\Prophet();18$prophecy->prophesize('ReturnPromise');19$prophecy->will(new ReturnPromise(array(1, 2, 3)));20$prophecy->reveal();21$prophecy = new Prophecy\Prophet();22$prophecy->prophesize('ReturnPromise');23$prophecy->will(new ReturnPromise(array(1, 2, 3)));24$prophecy->reveal();25$prophecy = new Prophecy\Prophet();26$prophecy->prophesize('ReturnPromise');27$prophecy->will(new ReturnPromise(array(1, 2, 3)));28$prophecy->reveal();29$prophecy = new Prophecy\Prophet();30$prophecy->prophesize('ReturnPromise');31$prophecy->will(new ReturnPromise(array(1, 2, 3)));

Full Screen

Full Screen

ReturnPromise

Using AI Code Generation

copy

Full Screen

1require_once 'ReturnPromise.php';2{3 public function test()4 {5 $mock = $this->prophesize('Test');6 $mock->test()->will(new ReturnPromise(array(1, 2, 3)));7 $mock->test()->shouldBeCalledTimes(3);8 $mock->test()->willReturn(1);9 $mock->test()->shouldBeCalledTimes(3);10 $mock->test()->willReturn(2);11 $mock->test()->shouldBeCalledTimes(3);12 $mock->test()->willReturn(3);13 $mock->test()->shouldBeCalledTimes(3);14 return $mock;15 }16}17require_once 'ReturnPromise.php';18{19 public function test()20 {21 $mock = $this->prophesize('Test');22 $mock->test()->will(new ReturnPromise(array(1, 2, 3)));23 $mock->test()->shouldBeCalledTimes(3);24 $mock->test()->willReturn(1);25 $mock->test()->shouldBeCalledTimes(3);26 $mock->test()->willReturn(2);27 $mock->test()->shouldBeCalledTimes(3);28 $mock->test()->willReturn(3);29 $mock->test()->shouldBeCalledTimes(3);30 return $mock;31 }32}33require_once 'ReturnPromise.php';34{35 public function test()36 {37 $mock = $this->prophesize('Test');38 $mock->test()->will(new ReturnPromise(array(1, 2, 3)));39 $mock->test()->shouldBeCalledTimes(3);40 $mock->test()->willReturn(1);41 $mock->test()->shouldBeCalledTimes(3);42 $mock->test()->willReturn(2);43 $mock->test()->shouldBeCalledTimes(3);44 $mock->test()->willReturn(3);45 $mock->test()->shouldBeCalledTimes(3);46 return $mock;47 }48}49require_once 'ReturnPromise.php';50{51 public function test()52 {53 $mock = $this->prophesize('Test

Full Screen

Full Screen

ReturnPromise

Using AI Code Generation

copy

Full Screen

1$prophet = new Prophet();2$prophecy = $prophet->prophesize();3$prophecy->willExtend(ReturnPromise::class);4$prophecy->method('returnPromise')->willReturn('This is the return value of returnPromise() method');5$object = $prophecy->reveal();6echo $object->returnPromise();7$prophet = new Prophet();8$prophecy = $prophet->prophesize();9$prophecy->willExtend(ReturnPromise::class);10$prophecy->method('returnPromise')->willReturn('This is the return value of returnPromise() method');11$object = $prophecy->reveal();12echo $object->returnPromise();13This is the return value of returnPromise() method14This is the return value of returnPromise() method15This is the return value of returnPromise() method

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Prophecy automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in ReturnPromise

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful