/** * Create a new mock of the curlbuilder and return * the given filename as content * * @access public * @param PHPUnit_Framework_TestCase $instance * @return mock */ public static function create($instance) { $reflection = new \ReflectionMethod($instance, $instance->getName()); $doc_block = $reflection->getDocComment(); $responsefile = self::parseDocBlock($doc_block, '@responsefile'); $responsecode = self::parseDocBlock($doc_block, '@responsecode'); $defaultheaders = array("X-Ratelimit-Limit" => "1000", "X-Ratelimit-Remaining" => "998", "X-Varnish" => "4059929980"); $skipmock = self::parseDocBlock($doc_block, '@skipmock'); if (empty($responsecode)) { $responsecode = [201]; } if (empty($responsefile)) { $responsefile = [$instance->getName()]; } // Setup Curlbuilder mock $curlbuilder = $instance->getMockBuilder("\\DirkGroenen\\Pinterest\\Utils\\CurlBuilder")->getMock(); $curlbuilder->expects($instance->any())->method('create')->will($instance->returnSelf()); // Build response file path $responseFilePath = __DIR__ . '/../responses/' . (new \ReflectionClass($instance))->getShortName() . '/' . $responsefile[0] . ".json"; if (file_exists($responseFilePath)) { $curlbuilder->expects($instance->once())->method('execute')->will($instance->returnValue(file_get_contents($responseFilePath))); } $curlbuilder->expects($instance->any())->method('getInfo')->will($instance->returnValue($responsecode[0])); return $curlbuilder; }
/** * Get data object builder * * @param string $className * @param array $arguments * @return object */ protected function getBuilder($className, array $arguments) { $objectFactory = $this->_testObject->getMock('Magento\\Framework\\Api\\ObjectFactory', [], [], '', false); if (!isset($arguments['objectFactory'])) { $arguments['objectFactory'] = $objectFactory; } $constructArguments = $this->getConstructArguments($className, $arguments); $reflectionClass = new \ReflectionClass($className); $builderObject = $reflectionClass->newInstanceArgs($constructArguments); $objectFactory->expects($this->_testObject->any())->method('populateWithArray')->will($this->_testObject->returnSelf()); $objectFactory->expects($this->_testObject->any())->method('populate')->will($this->_testObject->returnSelf()); $objectFactory->expects($this->_testObject->any())->method('create')->will($this->_testObject->returnCallback(function ($className, $arguments) { $reflectionClass = new \ReflectionClass($className); $constructorMethod = $reflectionClass->getConstructor(); $parameters = $constructorMethod->getParameters(); $args = []; foreach ($parameters as $parameter) { $parameterName = $parameter->getName(); if (isset($arguments[$parameterName])) { $args[] = $arguments[$parameterName]; } else { if ($parameter->isArray()) { $args[] = []; } elseif ($parameter->allowsNull()) { $args[] = null; } else { $mock = $this->_getMockWithoutConstructorCall($parameter->getClass()->getName()); $args[] = $mock; } } } return $reflectionClass->newInstanceArgs($args); })); return $builderObject; }
/** * Get a plain command-object mock * * @param string $sReturn * * @return \Testy\Util\Command */ public static function getPlainMock($sReturn = '') { $oMockBuilder = new \PHPUnit_Framework_MockObject_Generator(); $oCommandMock = $oMockBuilder->getMock('\\Testy\\Util\\Command'); $oCommandMock->expects(\PHPUnit_Framework_TestCase::any())->method('execute')->will(\PHPUnit_Framework_TestCase::returnSelf()); $oCommandMock->expects(\PHPUnit_Framework_TestCase::any())->method('reset')->will(\PHPUnit_Framework_TestCase::returnSelf()); $oCommandMock->expects(\PHPUnit_Framework_TestCase::any())->method('setCommand')->will(\PHPUnit_Framework_TestCase::returnSelf()); $oCommandMock->expects(\PHPUnit_Framework_TestCase::any())->method('get')->will(\PHPUnit_Framework_TestCase::returnValue($sReturn)); return $oCommandMock; }
/** * Get data object builder * * @param string $className * @param array $arguments * @return object */ protected function getBuilder($className, array $arguments) { $objectFactory = $this->_testObject->getMock('Magento\\Framework\\Api\\ObjectFactory', [], [], '', false); if (!isset($arguments['objectFactory'])) { $arguments['objectFactory'] = $objectFactory; } $constructArguments = $this->getConstructArguments($className, $arguments); $reflectionClass = new \ReflectionClass($className); $builderObject = $reflectionClass->newInstanceArgs($constructArguments); $objectFactory->expects($this->_testObject->any())->method('populateWithArray')->will($this->_testObject->returnSelf()); $objectFactory->expects($this->_testObject->any())->method('populate')->will($this->_testObject->returnSelf()); $objectFactory->expects($this->_testObject->any())->method('create')->will($this->_testObject->returnCallback(function ($className, $arguments) { $reflectionClass = new \ReflectionClass($className); return $reflectionClass->newInstanceArgs($arguments); })); return $builderObject; }
/** * Create a new mock of the curlbuilder and return * the given filename as content * * @access public * @param PHPUnit_Framework_TestCase $instance * @return mock */ public static function create($instance) { $reflection = new \ReflectionMethod($instance, $instance->getName()); $doc_block = $reflection->getDocComment(); $responsefile = self::parseDocBlock($doc_block, '@responsefile'); $responsecode = self::parseDocBlock($doc_block, '@responsecode'); if (empty($responsecode)) { $responsecode = [201]; } if (empty($responsefile)) { $responsefile = [$instance->getName()]; } // Setup Curlbuilder mock $curlbuilder = $instance->getMockBuilder("\\DirkGroenen\\Pinterest\\Utils\\CurlBuilder")->getMock(); $curlbuilder->expects($instance->once())->method('create')->will($instance->returnSelf()); $curlbuilder->expects($instance->once())->method('execute')->will($instance->returnValue(file_get_contents(__DIR__ . '/../responses/' . (new \ReflectionClass($instance))->getShortName() . '/' . $responsefile[0] . ".json"))); $curlbuilder->expects($instance->any())->method('getInfo')->will($instance->returnValue($responsecode[0])); return $curlbuilder; }
/** * Returns the current object. * * This method is useful when mocking a fluent interface. * * @return PHPUnit_Framework_MockObject_Stub_ReturnSelf * @since Method available since Release 3.6.0 */ function returnSelf() { return PHPUnit_Framework_TestCase::returnSelf(); }