Scalr\Tests\Service\AwsTestCase::getServiceInterfaceMock PHP Method

getServiceInterfaceMock() public method

Gets an service interface mock object
public getServiceInterfaceMock ( string $serviceName, Closure | callback | string $callback = null ) : Scalr\Service\Aws\ServiceInterface
$serviceName string Service name (Elb, CloudWatch etc..)
$callback Closure | callback | string optional callback for QueryClientResponse mock
return Scalr\Service\Aws\ServiceInterface Returns service interface mock
    public function getServiceInterfaceMock($serviceName, $callback = null)
    {
        $me = $this;
        $serviceName = lcfirst($serviceName);
        $ucServiceName = ucfirst($serviceName);
        $serviceClass = AwsTestCase::AWS_NS . '\\' . $ucServiceName;
        $container = $this->getContainer();
        $awsStub = $this->getAwsMock();
        $serviceInterfaceStub = $this->getMock($serviceClass, array('getApiHandler', '__get'), array($awsStub));
        $serviceInterfaceStub->enableEntityManager();
        $aHandlers = array();
        $serviceInterfaceStub->expects($this->any())->method('__get')->will($this->returnCallback(function ($name) use(&$aHandlers, $serviceName, $serviceInterfaceStub) {
            if (in_array($name, $serviceInterfaceStub->getAllowedEntities())) {
                if (!isset($aHandlers[$serviceName][$name])) {
                    $className = sprintf(AwsTestCase::AWS_NS . '\\%s\\Handler\\%sHandler', ucfirst($serviceName), ucfirst($name));
                    $aHandlers[$serviceName][$name] = new $className($serviceInterfaceStub);
                }
                return $aHandlers[$serviceName][$name];
            } else {
                throw new \InvalidArgumentException(sprintf('Unknown handler "%s" for the Mock object "%s"', $name, get_class($serviceInterfaceStub)));
            }
        }));
        $awsStub->expects($this->any())->method('__get')->will($this->returnValue($serviceInterfaceStub));
        if (is_readable(SRCPATH . '/Scalr/Service/Aws/Client/QueryClient/' . ucfirst($serviceName) . 'QueryClient.php')) {
            $queryClientClass = AwsTestCase::AWS_NS . '\\Client\\QueryClient\\' . ucfirst($serviceName) . 'QueryClient';
        } else {
            $queryClientClass = AwsTestCase::AWS_NS . '\\Client\\QueryClient';
        }
        $queryClientStub = $this->getMock($queryClientClass, array('call'), array($container->initialized('environment') ? $container->awsAccessKeyId : 'fakeAccessKeyId', $container->initialized('environment') ? $container->awsSecretAccessKey : 'fakeAwsSecretAccessKey', $serviceClass::API_VERSION_CURRENT, $serviceInterfaceStub->getUrl()));
        if ($callback === null) {
            $callback = array($this, 'getQueryClientStandartCallResponseMock');
        } else {
            if (is_string($callback) && substr($callback, -4) == '.xml') {
                $mth = $callback;
                $callback = function () use($me, $awsStub, $mth) {
                    return $me->getQueryClientResponseMock($me->getFixtureFileContent($mth), null, $awsStub);
                };
            } else {
                if (!is_callable($callback)) {
                    throw new \InvalidArgumentException('Invalid callback');
                }
            }
        }
        $queryClientStub->expects($this->any())->method('call')->will($this->returnCallback($callback));
        $apiClass = $serviceClass . '\\V' . $serviceClass::API_VERSION_CURRENT . '\\' . $ucServiceName . 'Api';
        $elbApi = new $apiClass($serviceInterfaceStub, $queryClientStub);
        $serviceInterfaceStub->expects($this->any())->method('getApiHandler')->will($this->returnValue($elbApi));
        return $serviceInterfaceStub;
    }