1
0
mirror of synced 2024-11-21 20:36:03 +03:00

load .env file in tests

This commit is contained in:
Pavel 2020-09-29 16:04:02 +03:00
parent feb1e4ad0d
commit 5edca2bfd2
6 changed files with 56 additions and 4 deletions

4
.env.dist Normal file
View File

@ -0,0 +1,4 @@
ENDPOINT=https://api.taobao.com/router/rest
APP_KEY=00000000
APP_SECRET=d784fa8b6d98d27699781bd9a7cf19f0
SESSION=test

1
.gitignore vendored
View File

@ -13,3 +13,4 @@ phpunit.xml
.project
.swp
/nbproject
.env

View File

@ -34,7 +34,8 @@
"squizlabs/php_codesniffer": "^3.5",
"guzzlehttp/guzzle": "^7.1",
"phpcompatibility/php-compatibility": "*",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0"
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"vlucas/phpdotenv": "^5.2"
},
"scripts": {
"phpunit": "./vendor/bin/phpunit -c phpunit.xml.dist",

View File

@ -18,7 +18,6 @@ use RetailCrm\Component\ServiceLocator;
use RetailCrm\Interfaces\AppDataInterface;
use RetailCrm\Interfaces\AuthenticatorInterface;
use RetailCrm\Interfaces\RequestFactoryInterface;
use RetailCrm\Interfaces\RequestTimestampProviderInterface;
use RetailCrm\Model\Request\BaseRequest;
use RetailCrm\Traits\ValidatorAwareTrait;
use Symfony\Component\Validator\Constraints as Assert;

View File

@ -49,9 +49,39 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
/**
* @return \RetailCrm\Interfaces\AppDataInterface
*/
protected function getAppData(): AppDataInterface
protected function getEnvAppData(): AppDataInterface
{
return new AppData(AppData::OVERSEAS_ENDPOINT, 'appKey', 'helloworld');
return $this->getAppData(
self::getenv('ENDPOINT', AppData::OVERSEAS_ENDPOINT),
self::getenv('APP_KEY', 'appKey'),
self::getenv('APP_SECRET', 'helloworld')
);
}
/**
* @param string $endpoint
* @param string $appKey
* @param string $appSecret
*
* @return \RetailCrm\Interfaces\AppDataInterface
*/
protected function getAppData(
string $endpoint = AppData::OVERSEAS_ENDPOINT,
string $appKey = 'appKey',
string $appSecret = 'helloworld'
): AppDataInterface{
return new AppData($endpoint, $appKey, $appSecret);
}
/**
* @return \RetailCrm\Interfaces\AuthenticatorInterface
*/
protected function getEnvAuthenticator(): AuthenticatorInterface
{
return $this->getAuthenticator(
self::getenv('APP_KEY', 'appKey'),
self::getenv('SESSION', 'helloworld')
);
}
/**
@ -100,4 +130,19 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
return $request;
}
/**
* @param string $variable
* @param mixed $default
*
* @return mixed|null
*/
protected static function getenv(string $variable, $default = null)
{
if (!array_key_exists($variable, $_ENV)) {
return $default;
}
return $_ENV[$variable];
}
}

View File

@ -8,3 +8,5 @@ if (function_exists('date_default_timezone_set')
$loader = include dirname(__DIR__) . '/vendor/autoload.php';
$loader->add('RetailCrm\\Test', __DIR__);
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();