ShortifyPunit\ShortifyPunit::when PHP Method

when() public static method

Examples: Chain Stubbing ShortifyPunit::when($mock)->first_method()->second_method(1)->returns(1); ShortifyPunit::when($mock)->first_method()->second_method(2)->returns(2); ShortifyPunit::when($mock)->first_method(1)->second_method(1)->returns(3); ShortifyPunit::when($mock)->first_method(2)->second_method(2)->third_method()->returns(4); echo $mock->first_method()->second_method(1); // prints '1' echo $mock->first_method()->second_method(2); // prints '2' echo $mock->first_method(1)->second_method(1); // prints '3' echo $mock->first_method(2)->second_method(2)->third_method(); // prints '4'
public static when ( ShortifyPunit\Mock\MockInterface $mock ) : ShortifyPunit\Stub\WhenChainCase
$mock ShortifyPunit\Mock\MockInterface
return ShortifyPunit\Stub\WhenChainCase
    public static function when($mock)
    {
        if (!$mock instanceof MockInterface) {
            throw self::generateException('when() must get a mocked instance as parameter');
        }
        return new WhenChainCase($mock);
    }

Usage Example

 public function testOnKernelController()
 {
     $request = ShortifyPunit::mock('Symfony\\Component\\HttpFoundation\\Request');
     $requestStack = ShortifyPunit::mock('Symfony\\Component\\HttpFoundation\\RequestStack');
     $sandboxResponseManager = ShortifyPunit::mock('danrevah\\SandboxBundle\\Managers\\SandboxResponseManager');
     $event = ShortifyPunit::mock('Symfony\\Component\\HttpKernel\\Event\\FilterControllerEvent');
     $parameterBag = ShortifyPunit::mock('Symfony\\Component\\HttpFoundation\\ParameterBag');
     ShortifyPunit::when($request)->getContent()->returns('');
     $request->query = $parameterBag;
     $request->request = $parameterBag;
     ShortifyPunit::when($requestStack)->getCurrentRequest()->returns($request);
     ShortifyPunit::when($event)->getController()->returns([0, 1]);
     ShortifyPunit::when($event)->setController(anything())->returns(1);
     $sandboxListener = new SandboxListener($requestStack, $sandboxResponseManager);
     $sandboxListener->onKernelController($event);
     $this->assertTrue(ShortifyPunit::verify($event)->setController(anything())->atLeastOnce());
     $response = [false, 0, 0, 0];
     ShortifyPunit::when($sandboxResponseManager)->getResponseController(anything(), anything(), anything(), anything(), anything())->returns($response);
     $event2 = ShortifyPunit::mock('Symfony\\Component\\HttpKernel\\Event\\FilterControllerEvent');
     ShortifyPunit::when($event2)->setController(anything())->returns('');
     ShortifyPunit::when($event2)->getController(anything())->returns([0, 1]);
     $sandboxListener = new SandboxListener($requestStack, $sandboxResponseManager);
     $sandboxListener->onKernelController($event2);
     $this->assertTrue(ShortifyPunit::verify($event2)->setController(anything())->neverCalled());
 }
All Usage Examples Of ShortifyPunit\ShortifyPunit::when