Best Mockery code snippet using ExpectationDirector
TestBootstrap.php
Source:TestBootstrap.php
1<?php2namespace Instante\Tests;3use Mockery;4use Mockery\Expectation;5use Mockery\ExpectationDirector;6use Mockery\Mock;7use Nette\DI\Container;8use Nette\InvalidStateException;9use Tester\Environment;10use Nette\Loaders\RobotLoader;11use Nette\Caching\Storages\FileStorage;12use Nette\Configurator;13class TestBootstrap14{15 /** @var string tests directory */16 public static $testsDir;17 /** @var string temp directory for tests (defaults to $testsDir/temp) */18 public static $tempDir;19 /** @var string project root directory (defaults to $testsDir/..) */20 public static $rootDir;21 /** @var string nette application directory (defaults to $rootDir/app) */22 public static $appDir;23 /** @var string nette composer directory (defaults to $rootDir/vendor) */24 public static $vendorDir;25 /** @var string nette config directory (defaults to $appDir/config) */26 public static $configDir;27 protected static $prepared = FALSE;28 /** static class, cannot be instantiated */29 private function __construct() { }30 /**31 * Prepares common test environment, not dependent on testing Nette application:32 *33 * - sets fixed request time and timezone34 * - purges _GET, _POST, _ENV35 * - calls Tester\Environment::setup()36 * - ensures tests temp directory exists37 * - validates Mockery expectations on shutdown38 *39 * @param string $tempDir40 */41 public static function prepareTestEnvironment($tempDir = NULL)42 {43 if ($tempDir !== NULL) {44 static::$tempDir = $tempDir;45 }46 static::checkPreparedOnce();47 static::unifyConfiguration();48 Environment::setup();49 static::prepareTempDir();50 static::prepareMockery();51 }52 /**53 * Prepares environment for unit tests in Nette application:54 *55 * - calls prepareTestEnvironment() to prepare basics56 * - auto-configures project paths relatively from tests directory57 * - loads Composer autoloader and Nette RobotLoader58 *59 * @param string $testsDir60 */61 public static function prepareUnitTest($testsDir = NULL)62 {63 static::preparePaths($testsDir);64 require_once static::$vendorDir . '/autoload.php';65 static::prepareTestEnvironment();66 static::prepareRobotLoader();67 }68 /**69 * Prepares environment for integration tests in Nette application - loads70 * DI container with app configuration.71 *72 * @param string $testsDir73 * @return Container;74 */75 public static function prepareIntegrationTest($testsDir = NULL)76 {77 static::prepareUnitTest($testsDir);78 if (static::$configDir === NULL) {79 static::$configDir = static::$appDir . '/config';80 }81 $configurator = static::createConfigurator();82 static::configureConfigurator($configurator, static::$configDir);83 return $configurator->createContainer();84 }85 protected static function configureConfigurator(Configurator $configurator, $configDir)86 {87 $configurator->addParameters([88 'appDir' => static::$appDir,89 'paths' => [90 'root' => static::$rootDir,91 'log' => static::$rootDir . '/log',92 ],93 ]);94 $configurator->addConfig("$configDir/default.neon");95 if (file_exists("$configDir/local.neon")) {96 $configurator->addConfig("$configDir/local.neon");97 }98 $configurator->addConfig(['parameters' => ['database' => ['dbname' => '%database.dbname_test%']]]);99 }100 protected static function createConfigurator()101 {102 $configurator = new Configurator;103 foreach ($configurator->defaultExtensions as $name => $class) { // remove extensions from not installed packages104 if (is_array($class)) {105 $class = $class[0];106 }107 if (!class_exists($class)) {108 unset($configurator->defaultExtensions[$name]);109 }110 }111 $configurator->setTempDirectory(static::$tempDir);112 return $configurator;113 }114 protected static function prepareRobotLoader()115 {116 if (class_exists(RobotLoader::class)) {117 $loader = new RobotLoader;118 if (class_exists(FileStorage::class)) {119 $loader->setCacheStorage(new FileStorage(static::$tempDir));120 }121 static::addRobotLoaderPaths($loader);122 $loader->register();123 }124 }125 protected static function addRobotLoaderPaths(RobotLoader $loader)126 {127 $loader128 ->addDirectory(static::$appDir)129 ->addDirectory(static::$testsDir);130 }131 protected static function prepareTempDir()132 {133 @mkdir(static::$tempDir . '/cache', 0777, TRUE); // @ - dir may already exist134 }135 protected static function checkPreparedOnce()136 {137 if (static::$prepared) {138 throw new InvalidStateException('Test environment already prepared');139 }140 static::$prepared = TRUE;141 }142 protected static function preparePaths($testsDir = NULL)143 {144 if ($testsDir !== NULL) {145 static::$testsDir = $testsDir;146 }147 if (static::$testsDir === NULL) {148 throw new InvalidStateException(__CLASS__ . '::$testsDir has to be set');149 }150 if (static::$rootDir === NULL) {151 static::$rootDir = static::$testsDir . '/..';152 }153 if (static::$vendorDir === NULL) {154 static::$vendorDir = static::$rootDir . '/vendor';155 }156 if (static::$appDir === NULL) {157 static::$appDir = static::$rootDir . '/app';158 }159 if (static::$tempDir === NULL) {160 static::$tempDir = static::$testsDir . '/temp';161 }162 }163 protected static function unifyConfiguration()164 {165 date_default_timezone_set('Europe/Prague');166 $_SERVER['REQUEST_TIME'] = 1234567890;167 $_ENV = $_GET = $_POST = [];168 }169 protected static function prepareMockery()170 {171 register_shutdown_function(function () {172 static::ignoreAssertionsWhenExpectations();173 Mockery::close();174 });175 }176 /**177 * Do not check assertions were executed when there are constrainted mocks178 */179 protected static function ignoreAssertionsWhenExpectations()180 {181 /** @var Mock $mock */182 foreach (\Mockery::getContainer()->getMocks() as $mock) {183 /** @var ExpectationDirector $expectationDirector */184 foreach ($mock->mockery_getExpectations() as $expectationDirector) {185 $expectations = $expectationDirector->getExpectations();186 if (method_exists($expectationDirector, 'getDefaultExpectations')) {187 //mockery <=0.9.5 compatibility188 $expectations = array_merge($expectations, $expectationDirector->getDefaultExpectations());189 }190 /** @var Expectation $expectation */191 foreach ($expectations as $expectation) {192 if ($expectation->isCallCountConstrained()) {193 Environment::$checkAssertions = FALSE;194 return;195 }196 }197 }...
ExpectationDirectorTest.php
Source:ExpectationDirectorTest.php
1<?php2namespace tests\Mockery;3use Mockery as m;4use Mockery\ExpectationDirector;5use Mockery\MockInterface;6class ExpectationDirectorTest extends \PHPUnit_Framework_TestCase7{8/**9* @var mixed10*/11protected $_name = null;12/**13* @var \Mockery\MockInterface14*/15protected $_mock;16/**17* @var \Mockery\ExpectationDirector18*/19protected $expectationDirector;20public function setUp()21{22 parent::setUp();23 $this->_name = null;24 $this->_mock = m::mock(\Mockery\MockInterface::class);25 $this->expectationDirector = new \Mockery\ExpectationDirector($this->_name, $this->_mock);26}27public function testAddExpectation0()28{29 $expectation = m::mock(\Mockery\Expectation::class);30 // TODO: Your mock expectations here31 $actual = $this->expectationDirector->addExpectation($expectation);32 $expected = null; // TODO: Expected value here33 $this->assertEquals($expected, $actual);34}35public function testCall0()36{37 $args = [];38 // TODO: Your mock expectations here39 // Traversed conditions...
MockInterface.php
Source:MockInterface.php
...134 /**135 * Return the expectations director for the given method136 *137 * @var string $method138 * @return \Mockery\ExpectationDirector|null139 */140 public function mockery_setExpectationsFor($method, \Mockery\ExpectationDirector $director);141 142 /**143 * Return the expectations director for the given method144 *145 * @var string $method146 * @return \Mockery\ExpectationDirector|null147 */148 public function mockery_getExpectationsFor($method);149 150 /**151 * Find an expectation matching the given method and arguments152 *153 * @var string $method154 * @var array $args155 * @return \Mockery\Expectation|null156 */157 public function mockery_findExpectation($method, array $args);158 159 /**160 * Return the container for this mock...
ExpectationDirector
Using AI Code Generation
1$director = new \Mockery\ExpectationDirector($mock);2$director = new \Mockery\ExpectationDirector($mock);3$director = new \Mockery\ExpectationDirector($mock);4$director = new \Mockery\ExpectationDirector($mock);5$director = new \Mockery\ExpectationDirector($mock);6$director = new \Mockery\ExpectationDirector($mock);7$director = new \Mockery\ExpectationDirector($mock);8$director = new \Mockery\ExpectationDirector($mock);9$director = new \Mockery\ExpectationDirector($mock);10$director = new \Mockery\ExpectationDirector($mock);11$director = new \Mockery\ExpectationDirector($mock);12$director = new \Mockery\ExpectationDirector($mock);13$director = new \Mockery\ExpectationDirector($mock);14$director = new \Mockery\ExpectationDirector($mock);15$director = new \Mockery\ExpectationDirector($mock);
ExpectationDirector
Using AI Code Generation
1$director = new \Mockery\ExpectationDirector($mock);2$director->shouldReceive('foo')->once()->andReturn('bar');3$director = new \Mockery\ExpectationDirector($mock);4$director->shouldReceive('foo')->once()->andReturn('baz');5Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_1::foo(). Either the method was unexpected or its arguments matched no expected argument list for this method6$mock = Mockery::mock();7Mockery::shouldReceive('foo')->once()->andReturn('bar');8$mock = Mockery::mock();9Mockery::shouldReceive('foo')->once()->andReturn('baz');10$mock = Mockery::mock();11Mockery::shouldReceive('foo')->once()->andReturn('bar');12$mock = Mockery::mock();13Mockery::shouldReceive('foo')->once()->andReturn('baz');14Mockery::close();15$mock = Mockery::mock();16Mockery::shouldReceive('foo')->once()->andReturn('bar');17Mockery::close();18$mock = Mockery::mock();19Mockery::shouldReceive('foo')->once()->andReturn('baz');20Mockery::close();21$mock = Mockery::mock();22Mockery::shouldReceive('foo')->once()->andReturn('bar');23Mockery::close();24$mock = Mockery::mock();
ExpectationDirector
Using AI Code Generation
1$mock = Mockery::mock('ExpectationDirector');2$mock->shouldReceive('test')->andReturn('test');3echo $mock->test();4$mock = Mockery::mock('ExpectationDirector');5$mock->shouldReceive('test')->andReturn('test');6echo $mock->test();7$mock = Mockery::mock('ExpectationDirector');8$mock->shouldReceive('test')->andReturn('test');9echo $mock->test();10$mock = Mockery::mock('ExpectationDirector');11$mock->shouldReceive('test')->andReturn('test');12echo $mock->test();13$mock = Mockery::mock('ExpectationDirector');14$mock->shouldReceive('test')->andReturn('test');15echo $mock->test();16$mock = Mockery::mock('ExpectationDirector');17$mock->shouldReceive('test')->andReturn('test');18echo $mock->test();19$mock = Mockery::mock('ExpectationDirector');20$mock->shouldReceive('test')->andReturn('test');21echo $mock->test();22$mock = Mockery::mock('ExpectationDirector');23$mock->shouldReceive('test')->andReturn('test');24echo $mock->test();25$mock = Mockery::mock('ExpectationDirector');26$mock->shouldReceive('test')->andReturn('test');27echo $mock->test();28$mock = Mockery::mock('ExpectationDirector');29$mock->shouldReceive('test')->andReturn('test');30echo $mock->test();31$mock = Mockery::mock('ExpectationDirector');32$mock->shouldReceive('test
ExpectationDirector
Using AI Code Generation
1$director = new Mockery\ExpectationDirector($mock);2$director->shouldReceive('foo')->once()->andReturn('bar');3$director->shouldReceive('foo')->once()->andReturn('baz');4$director = new Mockery\ExpectationDirector($mock);5$director->shouldReceive('foo')->once()->andReturn('bar');6$director->shouldReceive('foo')->once()->andReturn('baz');7$director = new Mockery\ExpectationDirector($mock);8$director->shouldReceive('foo')->once()->andReturn('bar');9$director->shouldReceive('foo')->once()->andReturn('baz');10$director = new Mockery\ExpectationDirector($mock);11$director->shouldReceive('foo')->once()->andReturn('bar');12$director->shouldReceive('foo')->once()->andReturn('baz');13$director = new Mockery\ExpectationDirector($mock);14$director->shouldReceive('foo')->once()->andReturn('bar');15$director->shouldReceive('foo')->once()->andReturn('baz');16$director = new Mockery\ExpectationDirector($mock);17$director->shouldReceive('foo')->once()->andReturn('bar');18$director->shouldReceive('foo')->once()->andReturn('baz');19$director = new Mockery\ExpectationDirector($mock);20$director->shouldReceive('foo')->once()->andReturn('bar');21$director->shouldReceive('foo')->once()->andReturn('baz');22$director = new Mockery\ExpectationDirector($mock);23$director->shouldReceive('foo')->once()->andReturn('bar');24$director->shouldReceive('foo')->once()->andReturn('baz');25$director = new Mockery\ExpectationDirector($mock);26$director->shouldReceive('foo')->once()->andReturn('bar');27$director->shouldReceive('foo')->once
ExpectationDirector
Using AI Code Generation
1use Mockery\Adapter\Phpunit\MockeryTestCase;2use Mockery\MockInterface;3use Mockery as m;4use Mockery\ExpectationDirector;5{6 public function testMockeryExpectationDirector()7 {8 $mock = m::mock('MyClass');9 $mock->shouldReceive('foo')->once();10 $mock->shouldReceive('bar')->once();11 $mock->shouldReceive('baz')->once();12 $mock->shouldReceive('qux')->once();13 $mock->shouldReceive('quux')->once();14 $mock->shouldReceive('corge')->once();15 $mock->shouldReceive('grault')->once();16 $mock->shouldReceive('garply')->once();17 $mock->shouldReceive('waldo')->once();18 $mock->shouldReceive('fred')->once();19 $mock->shouldReceive('plugh')->once();20 $mock->shouldReceive('xyzzy')->once();
ExpectationDirector
Using AI Code Generation
1$director = new \Mockery\ExpectationDirector($mock);2$director->shouldReceive('someMethod')->once()->andReturn('some value');3$director = new \Mockery\ExpectationDirector($mock);4$director->shouldReceive('someMethod')->once()->andReturn('some value');5$director = new \Mockery\ExpectationDirector($mock);6$director->shouldReceive('someMethod')->once()->andReturn('some value');7$director = new \Mockery\ExpectationDirector($mock);8$director->shouldReceive('someMethod')->once()->andReturn('some value');9$director = new \Mockery\ExpectationDirector($mock);10$director->shouldReceive('someMethod')->once()->andReturn('some value');11$director = new \Mockery\ExpectationDirector($mock);12$director->shouldReceive('someMethod')->once()->andReturn('some value');13$director = new \Mockery\ExpectationDirector($mock);14$director->shouldReceive('someMethod')->once()->andReturn('some value');15$director = new \Mockery\ExpectationDirector($mock);16$director->shouldReceive('someMethod')->once()->andReturn('some value');17$director = new \Mockery\ExpectationDirector($mock);18$director->shouldReceive('someMethod')->once()->andReturn('some value');19$director = new \Mockery\ExpectationDirector($mock);20$director->shouldReceive('someMethod')->once()->andReturn('some value');21$director = new \Mockery\ExpectationDirector($mock);
ExpectationDirector
Using AI Code Generation
1$director = new Mockery\ExpectationDirector($mock);2$director->shouldReceive('foo')->andReturn('bar');3$director->shouldReceive('bar')->andReturn('foo');4$mock->shouldReceive('foo')->andReturn('bar');5$mock->shouldReceive('bar')->andReturn('foo');6$director = new Mockery\ExpectationDirector($mock);7$director->shouldReceive('foo')->andReturn('bar');8$director->shouldReceive('bar')->andReturn('foo');9$mock->shouldReceive('foo')->andReturn('bar');10$mock->shouldReceive('bar')->andReturn('foo');11$director = new Mockery\ExpectationDirector($mock);12$director->shouldReceive('foo')->andReturn('bar');13$director->shouldReceive('bar')->andReturn('foo');14$mock->shouldReceive('foo')->andReturn('bar');15$mock->shouldReceive('bar')->andReturn('foo');16$director = new Mockery\ExpectationDirector($mock);17$director->shouldReceive('foo')->andReturn('bar');18$director->shouldReceive('bar')->andReturn('foo');19$mock->shouldReceive('foo')->andReturn('bar');20$mock->shouldReceive('bar')->andReturn('foo');21$director = new Mockery\ExpectationDirector($mock);22$director->shouldReceive('foo')->andReturn('bar');23$director->shouldReceive('bar')->andReturn('foo');24$mock->shouldReceive('foo')->andReturn('bar');25$mock->shouldReceive('bar')->andReturn('foo');26$director = new Mockery\ExpectationDirector($mock);27$director->shouldReceive('foo')->andReturn('bar');28$director->shouldReceive('bar')->andReturn('foo');29$mock->shouldReceive('foo')->andReturn('bar');30$mock->shouldReceive('bar')->andReturn('foo');
ExpectationDirector
Using AI Code Generation
1{2 private $mock;3 public function setUp()4 {5 $this->mock = Mockery::mock('SomeClass');6 }7 public function testMockery()8 {9 $this->mock->shouldReceive('someMethod')->once()->andReturn('foo');10 $this->assertEquals('foo', $this->mock->someMethod());11 }12 public function tearDown()13 {14 Mockery::close();15 }16}17{18 private $mock;19 public function setUp()20 {21 $this->mock = Mockery::mock('SomeClass');22 }23 public function testMockery()24 {25 $this->mock->shouldReceive('someMethod')->once()->andReturn('foo');26 $this->assertEquals('foo', $this->mock->someMethod());27 }28 public function tearDown()29 {30 Mockery::close();31 }32}33{34 private $mock;35 public function setUp()36 {37 $this->mock = Mockery::mock('SomeClass');38 }39 public function testMockery()40 {41 $this->mock->shouldReceive('someMethod')->once()->andReturn('foo');42 $this->assertEquals('foo', $this->mock->someMethod());43 }44 public function tearDown()45 {46 Mockery::close();47 }48}49{50 private $mock;51 public function setUp()52 {53 $this->mock = Mockery::mock('SomeClass');54 }55 public function testMockery()56 {57 $this->mock->shouldReceive('someMethod')->once()->andReturn('foo');58 $this->assertEquals('foo', $this->mock->someMethod());59 }60 public function tearDown()61 {62 Mockery::close();63 }64}
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.
Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.
Test now for FreeGet 100 minutes of automation test minutes FREE!!