Best Mockery code snippet using CompositeExpectation.shouldReceive
ElasticsearchGrammarTest.php
Source:ElasticsearchGrammarTest.php  
...23    {24        parent::setUp();25        /** @var CatNamespace|m\CompositeExpectation $catNamespace */26        $catNamespace = m::mock(CatNamespace::class);27        $catNamespace->shouldReceive('indices')->andReturn([]);28        /** @var IndicesNamespace|m\CompositeExpectation $indicesNamespace */29        $indicesNamespace = m::mock(IndicesNamespace::class);30        $indicesNamespace->shouldReceive('existsAlias')->andReturnFalse();31        /** @var Client|m\CompositeExpectation $client */32        $client = m::mock(Client::class);33        $client->shouldReceive('cat')->andReturn($catNamespace);34        $client->shouldReceive('indices')->andReturn($indicesNamespace);35        /** @var Connection|m\CompositeExpectation $connection */36        $connection = m::mock(Connection::class)->makePartial()->shouldAllowMockingProtectedMethods();37        $connection->shouldReceive('createConnection')->andReturn($client);38        Carbon::setTestNow(39            Carbon::create(2019, 7, 2, 12)40        );41        $this->blueprint = new Blueprint('indices');42        $this->connection = $connection;43        $this->grammar = new ElasticsearchGrammar();44    }45    /**46     * It returns a closure that will create an index.47     * @test48     * @covers \DesignMyNight\Elasticsearch\Database\Schema\Grammars\ElasticsearchGrammar::compileCreate49     */50    public function it_returns_a_closure_that_will_create_an_index()51    {52        $alias = 'indices_dev';53        $index = '2019_07_02_120000_indices_dev';54        $mapping = [55            'mappings' => [56                'properties' => [57                    'title' => [58                        'type' => 'text',59                        'fields' => [60                            'raw' => [61                                'type' => 'keyword'62                            ]63                        ]64                    ],65                    'date' => [66                        'type' => 'date'67                    ]68                ]69            ]70        ];71        $blueprint = clone($this->blueprint);72        $blueprint->text('title')->fields(function (Blueprint $mapping): void {73            $mapping->keyword('raw');74        });75        $blueprint->date('date');76        /** @var IndicesNamespace|m\CompositeExpectation $indicesNamespace */77        $indicesNamespace = m::mock(IndicesNamespace::class);78        $indicesNamespace->shouldReceive('create')->once()->with(['index' => $index, 'body' => $mapping]);79        $indicesNamespace->shouldReceive('existsAlias')->once()->with(['name' => $alias])->andReturnFalse();80        $indicesNamespace->shouldReceive('putAlias')->once()->with(['index' => $index, 'name' => $alias]);81        $this->connection->shouldReceive('indices')->andReturn($indicesNamespace);82        $this->connection->shouldReceive('createAlias')->once()->with($index, $alias)->passthru();83        $this->connection->shouldReceive('createIndex')->once()->with($index, $mapping)->passthru();84        $executable = $this->grammar->compileCreate(new Blueprint(''), new Fluent(), $this->connection);85        $this->assertInstanceOf(Closure::class, $executable);86        $executable($blueprint, $this->connection);87    }88    /**89     * It returns a closure that will drop an index.90     * @test91     * @covers \DesignMyNight\Elasticsearch\Database\Schema\Grammars\ElasticsearchGrammar::compileDrop92     */93    public function it_returns_a_closure_that_will_drop_an_index()94    {95        $index = '2019_06_03_120000_indices_dev';96        /** @var CatNamespace|m\CompositeExpectation $catNamespace */97        $catNamespace = m::mock(CatNamespace::class);98        $catNamespace->shouldReceive('indices')->andReturn([99            ['index' => $index]100        ]);101        /** @var IndicesNamespace|m\CompositeExpectation $indicesNamespace */102        $indicesNamespace = m::mock(IndicesNamespace::class);103        $indicesNamespace->shouldReceive('delete')->once()->with(['index' => $index]);104        $this->connection->shouldReceive('cat')->andReturn($catNamespace);105        $this->connection->shouldReceive('indices')->andReturn($indicesNamespace);106        $this->connection->shouldReceive('dropIndex')->once()->with($index)->passthru();107        $executable = $this->grammar->compileDrop(new Blueprint(''), new Fluent(), $this->connection);108        $this->assertInstanceOf(Closure::class, $executable);109        $executable($this->blueprint, $this->connection);110    }111    /**112     * It returns a closure that will drop an index if it exists.113     * @test114     * @covers       \DesignMyNight\Elasticsearch\Database\Schema\Grammars\ElasticsearchGrammar::compileDropIfExists115     * @dataProvider compile_drop_if_exists_data_provider116     */117    public function it_returns_a_closure_that_will_drop_an_index_if_it_exists($table, $times)118    {119        $index = '2019_06_03_120000_indices_dev';120        $this->blueprint = new Blueprint($table);121        /** @var CatNamespace|m\CompositeExpectation $catNamespace */122        $catNamespace = m::mock(CatNamespace::class);123        $catNamespace->shouldReceive('indices')->andReturn([124            ['index' => $index]125        ]);126        /** @var IndicesNamespace|m\CompositeExpectation $indicesNamespace */127        $indicesNamespace = m::mock(IndicesNamespace::class);128        $indicesNamespace->shouldReceive('delete')->times($times)->with(['index' => $index]);129        $this->connection->shouldReceive('indices')->andReturn($indicesNamespace);130        $this->connection->shouldReceive('cat')->once()->andReturn($catNamespace);131        $this->connection->shouldReceive('dropIndex')->times($times)->with($index)->passthru();132        $executable = $this->grammar->compileDropIfExists(new Blueprint(''), new Fluent(), $this->connection);133        $this->assertInstanceOf(Closure::class, $executable);134        $executable($this->blueprint, $this->connection);135    }136    /**137     * compileDropIfExists data provider.138     */139    public function compile_drop_if_exists_data_provider(): array140    {141        return [142            'it exists' => ['indices', 1],143            'it does not exists' => ['books', 0]144        ];145    }146    /**147     * It returns a closure that will update an index mapping.148     * @test149     * @covers \DesignMyNight\Elasticsearch\Database\Schema\Grammars\ElasticsearchGrammar::compileUpdate150     */151    public function it_returns_a_closure_that_will_update_an_index_mapping()152    {153        $this->blueprint->text('title');154        $this->blueprint->date('date');155        $this->blueprint->keyword('status');156        $this->connection->shouldReceive('updateIndex')->once()->with('indices_dev', 'index', [157            'index' => [158                'properties' => [159                    'title' => [160                        'type' => 'text'161                    ],162                    'date' => [163                        'type' => 'date'164                    ],165                    'status' => [166                        'type' => 'keyword'167                    ]168                ]169            ]170        ]);...UnitSeleniumTestCase.php
Source:UnitSeleniumTestCase.php  
...23 * Or better:24 *25 *     $this->inject(26 *         $this->mock('overload:' . DesiredCapabilities::class)27 *             ->shouldReceive('create')28 *             ->once()29 *     );30 *31 * Or even better:32 *33 *     $this->inject(WebDriverBy::class)34 *         ->shouldReceive('create')35 *         ->once()36 *         ->withAnyArgs()37 *         ->andReturn(Mockery::slef());38 *39 * How cool is that?!40 */41abstract class UnitSeleniumTestCase extends SeleniumTestCase42{43    use MockeryPHPUnitIntegration;44    /**45     * An array of reusable dependency test doubles.46     *47     * @var MockInterface[]48     */49    protected $doubles = [];50    /**51     * Test setup.52     */53    public function setUp()54    {55        // Mock all calls to sleep() during unit tests56        PHPMockery::mock('Sepehr\PHPUnitSelenium', 'sleep')57            ->zeroOrMoreTimes()58            ->andReturn(0);59        parent::setUp();60    }61    /**62     * Manages test doubles.63     *64     * @param string $doubleId65     * @param \Closure|null $closure66     * @param string $doubleType67     *68     * @return MockInterface69     * @throws \Exception70     */71    protected function double($doubleId, $closure = null, $doubleType = 'mock')72    {73        if (key_exists($doubleId, $this->doubles)) {74            return $closure75                ? $closure($this->doubles[$doubleId])76                : $this->doubles[$doubleId];77        }78        try {79            return $this->doubles[$doubleId] = $closure80                ? Mockery::$doubleType($doubleId, $closure)81                : Mockery::$doubleType($doubleId);82        } catch (\Exception $e) {83            throw new \Exception(84                "Could not find/create a $doubleType with identifier: $doubleId\nMessage: {$e->getMessage()}"85            );86        }87    }88    /**89     * Mock interface.90     *91     * @param string $mockId92     * @param \Closure|null $closure93     *94     * @return MockInterface95     * @throws \Exception96     */97    protected function mock($mockId, $closure = null)98    {99        return $this->double($mockId, $closure, 'mock');100    }101    /**102     * Spy interface.103     *104     * @param string $spyId105     * @param \Closure|null $closure106     *107     * @return MockInterface108     * @throws \Exception109     */110    protected function spy($spyId, $closure = null)111    {112        return $this->double($spyId, $closure, 'spy');113    }114    /**115     * Genius test double injector; she knows how to inject!116     *117     * @param MockInterface|CompositeExpectation|string $double118     * @param string $doubleType119     *120     * @return MockInterface121     */122    protected function inject($double, $doubleType = 'mock')123    {124        $double = $this->normalizeDouble($double, $doubleType);125        $injector = $this->getDependencyInjector($double);126        // Each dependency might have its own logic for127        // injection, so the separate methods...128        return $this->$injector($double);129    }130    /**131     * Injects a mock.132     *133     * @param MockInterface|CompositeExpectation|string $double134     *135     * @return MockInterface136     */137    protected function injectMock($double)138    {139        return $this->inject($double, 'mock');140    }141    /**142     * Injects a spy.143     *144     * @param MockInterface|CompositeExpectation|string $double145     *146     * @return MockInterface147     */148    protected function injectSpy($double)149    {150        return $this->inject($double, 'spy');151    }152    /**153     * Injects a RemoteWebDriver double into the SeleniumTestCase.154     *155     * @param MockInterface|CompositeExpectation|string $double156     *157     * @return RemoteWebDriver158     */159    protected function injectWebDriver($double = RemoteWebDriver::class)160    {161        // Default behavior for mock doubles162        $double = $this->normalizeDouble($double)->shouldReceive('quit')->byDefault();163        return $this->injectDependency($double, 'setWebDriver');164    }165    /**166     * Injects a DesiredCapabilities double into the SeleniumTestCase.167     *168     * @param MockInterface|CompositeExpectation|string $double169     *170     * @return DesiredCapabilities171     */172    protected function injectDesiredCapabilities($double = DesiredCapabilities::class)173    {174        return $this->injectDependency($double, 'setDesiredCapabilities');175    }176    /**...Mock.php
Source:Mock.php  
...19});20it('allows access to the underlying mockery mock', function () {21    $mock = mock(Http::class);22    expect($mock->expect())->toBeInstanceOf(MockInterface::class);23    expect($mock->shouldReceive('get'))->toBeInstanceOf(CompositeExpectation::class);24});25it('passes the arguments to the underlying mock correctly', function () {26    $mock = mock(Http::class);27    $mock->shouldReceive('get')28        ->atLeast()29        ->once()30        ->andReturn('foo');31    expect($mock->get())->toBe('foo');32});...shouldReceive
Using AI Code Generation
1$mock->shouldReceive('foo')->once()->with('bar')->andReturn('baz');2$mock->shouldNotReceive('foo')->once()->with('bar')->andReturn('baz');3$mock->shouldReceive('foo')->once()->with('bar')->andReturn('baz');4$mock->shouldNotReceive('foo')->once()->with('bar')->andReturn('baz');5$mock->shouldReceive('foo')->once()->with('bar')->andReturn('baz');6$mock->shouldNotReceive('foo')->once()->with('bar')->andReturn('baz');7$mock->shouldReceive('foo')->once()->with('bar')->andReturn('baz');8$mock->shouldNotReceive('foo')->once()->with('bar')->andReturn('baz');9$mock->shouldReceive('foo')->once()->with('bar')->andReturn('baz');10$mock->shouldNotReceive('foo')->once()->with('bar')->andReturn('baz');11$mock->shouldReceive('foo')->once()->with('bar')->andReturn('baz');12$mock->shouldNotReceive('foo')->once()->with('bar')->andReturn('baz');13$mock->shouldReceive('foo')->once()->with('bar')->andReturn('baz');14$mock->shouldNotReceive('foo')->once()->with('bar')->andReturn('baz');15$mock->shouldReceive('foo')->onceshouldReceive
Using AI Code Generation
1$mock = $this->getMock('SomeClass', array('someMethod'));2$mock->expects($this->once())3->method('someMethod')4->with($this->equalTo('something'))5->will($this->returnValue('something'));6$mock->someMethod('something');7$mock = Mockery::mock('SomeClass');8$mock->shouldReceive('someMethod')9->once()10->with('something')11->andReturn('something');12$mock->someMethod('something');13$mock = Mockery::mock('SomeClass');14$mock->shouldReceive('someMethod')15->once()16->with('something')17->andReturn('something');18$mock->someMethod('something');19$mock = Mockery::mock('SomeClass');20$mock->shouldReceive('someMethod')21->once()22->with('something')23->andReturn('something');24$mock->someMethod('something');25$mock = Mockery::mock('SomeClass');26$mock->shouldReceive('someMethod')27->once()28->with('something')29->andReturn('something');30$mock->someMethod('something');31$mock = Mockery::mock('SomeClass');32$mock->shouldReceive('someMethod')33->once()34->with('something')35->andReturn('something');36$mock->someMethod('something');37$mock = Mockery::mock('SomeClass');38$mock->shouldReceive('someMethod')39->once()40->with('something')41->andReturn('something');42$mock->someMethod('something');43$mock = Mockery::mock('SomeClass');44$mock->shouldReceive('someMethod')45->once()46->with('something')47->andReturn('something');48$mock->someMethod('something');49$mock = Mockery::mock('SomeshouldReceive
Using AI Code Generation
1$mock->shouldReceive('foo')->once();2$mock->shouldReceive('bar')->twice();3$mock->shouldReceive('baz')->atLeast()->once();4$mock->shouldReceive('baz')->atLeast()->twice();5$mock->shouldReceive('baz')->atLeast()->times(3);6$mock->shouldReceive('baz')->atMost()->once();7$mock->shouldReceive('baz')->atMost()->twice();8$mock->shouldReceive('baz')->atMost()->times(3);9$mock->shouldReceive('baz')->never();10$mock->shouldReceive('baz')->ordered();11$mock->shouldReceive('baz')->zeroOrMoreTimes();12$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered();13$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('default');14$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('second');15$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('third');16$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('fourth');17$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('fifth');18$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('sixth');19$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('seventh');20$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('eighth');21$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('ninth');22$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('tenth');23$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('eleventh');24$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('twelfth');25$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('thirteenth');26$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('fourteenth');27$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('fifteenth');28$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('sixteenth');29$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('seventeenth');30$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('eighteenth');31$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('nineteenth');32$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('twentieth');33$mock->shouldReceive('baz')->zeroOrMoreTimes()->ordered('twenty-firstshouldReceive
Using AI Code Generation
1$mock->shouldReceive('foo')->with('bar')->once();2$mock->shouldNotReceive('foo')->with('bar')->once();3$mock->shouldHaveReceived('foo')->with('bar')->once();4$mock->shouldNotHaveReceived('foo')->with('bar')->once();5$mock->shouldReceive('foo')->with('bar')->once();6$mock->shouldNotReceive('foo')->with('bar')->once();7$mock->shouldHaveReceived('foo')->with('bar')->once();8$mock->shouldNotHaveReceived('foo')->with('bar')->once();9$mock->shouldReceive('foo')->with('bar')->once();10$mock->shouldNotReceive('foo')->with('bar')->once();11$mock->shouldHaveReceived('foo')->with('bar')->once();12$mock->shouldNotHaveReceived('foo')->with('bar')->once();13$mock->shouldReceive('foo')->with('bar')->once();shouldReceive
Using AI Code Generation
1{2    function shouldReceive($method, $args = array())3    {4        $this->expected[] = array($method, $args);5        return $this;6    }7}8{9    function shouldReceive($method, $args = array())10    {11        $this->expected[] = array($method, $args);12        return $this;13    }14}15{16    function shouldReceive($method, $args = array())17    {18        $this->expected[] = array($method, $args);19        return $this;20    }21}22{23    function shouldReceive($method, $args = array())24    {25        $this->expected[] = array($method, $args);26        return $this;27    }28}29{30    function shouldReceive($method, $args = array())31    {32        $this->expected[] = array($method, $args);33        return $this;34    }35}36{37    function shouldReceive($method, $args = array())38    {39        $this->expected[] = array($method, $args);40        return $this;41    }42}43{44    function shouldReceive($method, $args = array())45    {46        $this->expected[] = array($method, $args);47        return $this;48    }49}50{shouldReceive
Using AI Code Generation
1$mock = $this->getMock('SomeClass');2$mock->expects($this->once())3     ->method('someMethod')4     ->with($this->equalTo('something'));5$mock = $this->getMock('SomeClass');6$mock->expects($this->once())7     ->method('someMethod')8     ->with($this->equalTo('something'));9$mock = $this->getMock('SomeClass');10$mock->expects($this->once())11     ->method('someMethod')12     ->with($this->equalTo('something'));13$mock = $this->getMock('SomeClass');14$mock->expects($this->once())15     ->method('someMethod')16     ->with($this->equalTo('something'));17$mock = $this->getMock('SomeClass');18$mock->expects($this->once())19     ->method('someMethod')20     ->with($this->equalTo('something'));21$mock = $this->getMock('SomeClass');22$mock->expects($this->once())23     ->method('someMethod')24     ->with($this->equalTo('something'));shouldReceive
Using AI Code Generation
1$mock = Mockery::mock('MyClass');2$mock->shouldReceive(array('method1', 'method2'))->andReturn('foo');3$mock = Mockery::mock('MyClass');4$mock->shouldReceive(array('method1', 'method2'))->andReturn('foo');5$mock = Mockery::mock('MyClass');6$mock->shouldReceive(array('method1', 'method2'))->andReturn('foo');7$mock = Mockery::mock('MyClass');8$mock->shouldReceive(array('method1', 'method2'))->andReturn('foo');9$mock = Mockery::mock('MyClass');10$mock->shouldReceive(array('method1', 'method2'))->andReturn('foo');11$mock = Mockery::mock('MyClass');12$mock->shouldReceive(array('method1', 'method2'))->andReturn('foo');13$mock = Mockery::mock('MyClass');14$mock->shouldReceive(array('method1', 'method2'))->andReturn('foo');15$mock = Mockery::mock('MyClass');16$mock->shouldReceive(array('method1', 'method2'))->andReturn('foo');17$mock = Mockery::mock('MyClass');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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with shouldReceive on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!
