Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * 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;
 }
Exemplo n.º 3
0
 /**
  * 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;
 }
Exemplo n.º 4
0
 /**
  * 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;
 }
Exemplo n.º 5
0
 /**
  * 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;
 }
Exemplo n.º 6
0
/**
 * 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();
}