I am storing my configs in env.php so i need to use dependency injection in my test class to access the configs. so i would like to find out how to inject the config class 'Magento\\Framework\\App\\DeploymentConfig' in my test class.
I tried using the constructor and the objectManager and i cant seem to get it to work
first attempt
{
//$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$config = $objectManager->get('Magento\Framework\App\DeploymentConfig');
$test_config = $config->get('tests');
// create our http client (Guzzle)
$this->client = new Client(['base_uri' => $test_config['base_url']]);
//set headers
$this->headers = [
'Authorization' => 'Bearer ' . $test_config['token'],
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];
}
second attempt
public function __construct(
\Magento\Framework\App\DeploymentConfig $config
) {
$this->test_config = $config->get('tests');
}
public function Setup()
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$config = $objectManager->getObject('Magento\Framework\App\DeploymentConfig');
$test_config = $config->get('tests');
// create our http client (Guzzle)
$this->client = new Client(['base_uri' => $test_config['base_url']]);
//set headers
$this->headers = [
'Authorization' => 'Bearer ' . $test_config['token'],
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];
}
I think you are trying to call an API in your test case.
PHP unit test case will not call actual API instead, you need to pass the return value of that API call in willReturn
method.
Please have a look at the below-given examples.
In a similar scenario, I've used the below code.
$this->scopeConfig = $this->getMockBuilder(Magento\Framework\App\Config\ScopeConfigInterface::class)
->setMethods(['getValue'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->scopeConfig->expects($this->any())
->method('getValue')->willReturn('your config value');
In case your code has multiple instances of getValue
call, please use the below-given code.
$scopeConfigInterface = $this->createMock(Magento\Framework\App\Config\ScopeConfigInterface::class);
$valueMap = [
['<SECTION NAME>/<GROUP NAME>/<FIELD NAME>', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null, '<CONFIG VALUE>']
];
$scopeConfigInterface->method('getValue')->willReturnMap($valueMap);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.