From eb8c282bca4589cf57bac251d3f7f72e9e923076 Mon Sep 17 00:00:00 2001 From: Sebastian Sellmeier Date: Sun, 22 Dec 2019 01:51:47 +0000 Subject: [PATCH] Upgrade to Domrobot v3 and adjust pipeline, Fix codestyle (#11) * Upgrade Domrobot-Client from INWX to v3 * Bump versions to latest supported PHP and Symfony-Versions * Fix deprecations * Improve tests * Switch to Github Actions --- .coveralls.yml | 1 - .github/workflows/main.yml | 45 +++++ .travis.yml | 46 ----- README.md | 18 +- composer.json | 10 +- src/DependencyInjection/Configuration.php | 28 +-- src/DependencyInjection/InwxApiExtension.php | 17 +- src/Domrobot.php | 73 ++----- .../UnsupportedEnvironmentException.php | 11 -- src/Resources/config/services.yml | 8 +- .../DependencyInjection/ConfigurationTest.php | 7 +- .../InwxApiExtensionTest.php | 3 - tests/DependencyInjection/fixtures/config.yml | 11 +- tests/DomrobotTest.php | 185 +++++++++++++----- 14 files changed, 245 insertions(+), 218 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml delete mode 100644 src/Exception/UnsupportedEnvironmentException.php diff --git a/.coveralls.yml b/.coveralls.yml index 3750671..f6e9ef4 100644 --- a/.coveralls.yml +++ b/.coveralls.yml @@ -1,2 +1 @@ coverage_clover: build/logs/clover.xml -service_name: travis-pro diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..1ae3c59 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,45 @@ +name: INWX-API-Bundle +on: ["push", "pull_request"] +jobs: + run: + runs-on: ubuntu-latest + strategy: + matrix: + php-versions: ['7.2', '7.3', '7.4'] + symfony-versions: ['3.4.*', '4.2.*', '4.3.*', '4.4.*', '5.0.*'] + name: PHP ${{ matrix.php-versions }} with Symfony ${{ matrix.symfony-versions }} + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Setup PHP + uses: shivammathur/setup-php@v1 + with: + coverage: xdebug + extensions: mbstring + ini-values: memory_limit=1G + php-version: ${{ matrix.php-versions }} + - name: Get Composer Cache Directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + - name: Validate composer.json and composer.lock + run: composer validate + - name: Install Symfony + run: composer require --dev --no-update symfony/framework-bundle:${{ matrix.symfony-versions }} + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest + - uses: actions/cache@v1 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: iab-php${{ matrix.php-versions }}-sf${{ matrix.symfony-versions }} + restore-keys: iab-php${{ matrix.php-versions }}-sf${{ matrix.symfony-versions }} + - name: Run linter + run: ./vendor/bin/php-cs-fixer fix --diff --dry-run --no-interaction -v + - name: Run tests + run: ./vendor/bin/phpunit -c phpunit.dist.xml + env: + INWX_API_USERNAME: ${{ secrets.INWX_API_USERNAME }} + INWX_API_PASSWORD: ${{ secrets.INWX_API_PASSWORD }} + - name: Send to coveralls + run: ./vendor/bin/php-coveralls -v + env: + COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a2fb1aa..0000000 --- a/.travis.yml +++ /dev/null @@ -1,46 +0,0 @@ -language: php -sudo: false - -cache: - directories: - - "$HOME/.composer/cache" - -matrix: - fast_finish: true - include: - - php: 7.1 - env: SYMFONY_VERSION=3.4.* - - php: 7.1 - env: SYMFONY_VERSION=4.1.* - - php: 7.1 - env: SYMFONY_VERSION=4.2.* - - php: 7.2 - env: SYMFONY_VERSION=3.4.* - - php: 7.2 - env: SYMFONY_VERSION=4.1.* - - php: 7.2 - env: SYMFONY_VERSION=4.2.* - - php: 7.3 - env: SYMFONY_VERSION=3.4.* - - php: 7.3 - env: SYMFONY_VERSION=4.1.* - - php: 7.3 - env: SYMFONY_VERSION=4.2.* - -before_install: -- echo "memory_limit=1G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; - -install: -- if [ "$SYMFONY_VERSION" != "" ]; then composer require --dev --no-update symfony/framework-bundle:${SYMFONY_VERSION}; fi -- if [ $(phpenv version-name) = "7.1" ]; then composer require --dev --no-update phpunit/phpunit:~7 matthiasnoback/symfony-dependency-injection-test:~3; fi -- composer install --no-interaction - -before_script: -- mkdir -p build/logs/ - -script: -- ./vendor/bin/phpunit -c phpunit.dist.xml -- ./vendor/bin/php-cs-fixer fix --diff --dry-run --no-interaction -v - -after_success: -- travis_retry php vendor/bin/php-coveralls diff --git a/README.md b/README.md index fd22fb2..0bd6f72 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ requiring the `sebtm/inwx-api-bundle` package in your project's `composer.json`: ```json { "require": { - "sebtm/inwx-api-bundle": "~0.3" + "sebtm/inwx-api-bundle": "~1.0" } } ``` @@ -25,14 +25,12 @@ and adding an instance of `SebTM\INWX\InwxApiBundle` to your application's kerne ```php class AppKernel extends Kernel { - public function registerBundles() + public function registerBundles(): void { return [ - ... new \SebTM\INWX\InwxApiBundle(), ]; } - ... } ``` (This is NOT needed for Symfony-Flex while using recipes!) @@ -44,14 +42,15 @@ The sample configuration which can be placed in `app/config/config.yml` file. ```yaml inwx_api: - environment: "production" + debug: false + environment: "development" + json: true + language: "en" username: "username" password: "password" - language: "en" - debug: false ``` -Supported environments: "production", "test" +Supported environments: "production", "development" Supported languages: see documentation of INWX PHP-Client ## Usage @@ -64,6 +63,9 @@ Service | Instance Of inwx_api | SebTM\INWX\Domrobot ``` +It provides an additional function called "loginWrapper()" (BC >=1.0.0: login will not overwritten anymore!) for using +the login data from configuration. + ## Links * [INWX PHP-Client on Github](https://github.com/inwx/php-client) * [INWX PHP-Client Documentation](https://www.inwx.de/en/help/apidoc) diff --git a/composer.json b/composer.json index 99cca75..0462251 100644 --- a/composer.json +++ b/composer.json @@ -12,11 +12,11 @@ } ], "require": { - "php": ">=7.1", - "inwx/domrobot": "^2.4", - "symfony/config": "^3.4|^4.1|^4.2", - "symfony/http-kernel": "^3.4|^4.1|^4.2", - "symfony/yaml": "^3.4|^4.1|^4.2" + "php": ">=7.2", + "inwx/domrobot": "^3.0", + "symfony/config": "^3.4|^4.2|^4.3|^4.4|^5.0", + "symfony/http-kernel": "^3.4|^4.2|^4.3|^4.4|^5.0", + "symfony/yaml": "^3.4|^4.2|^4.3|^4.4|^5.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "~2", diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 7f92169..723f435 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -7,24 +7,30 @@ class Configuration implements ConfigurationInterface { - /** - * @return TreeBuilder - */ public function getConfigTreeBuilder(): TreeBuilder { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('inwx_api'); + $treeBuilder = new TreeBuilder('inwx_api'); - $rootNode + $treeBuilder + ->getRootNode() ->children() + ->booleanNode('debug') + ->defaultFalse() + ->end() ->enumNode('environment') ->values( array( 'production', - 'test', + 'development', ) ) - ->defaultValue('test') + ->defaultValue('development') + ->end() + ->booleanNode('json') + ->defaultTrue() + ->end() + ->scalarNode('language') + ->defaultValue('en') ->end() ->scalarNode('username') ->isRequired() @@ -32,12 +38,6 @@ public function getConfigTreeBuilder(): TreeBuilder ->scalarNode('password') ->isRequired() ->end() - ->scalarNode('language') - ->defaultValue('en') - ->end() - ->booleanNode('debug') - ->defaultFalse() - ->end() ->end(); return $treeBuilder; diff --git a/src/DependencyInjection/InwxApiExtension.php b/src/DependencyInjection/InwxApiExtension.php index ea5ac89..772644e 100644 --- a/src/DependencyInjection/InwxApiExtension.php +++ b/src/DependencyInjection/InwxApiExtension.php @@ -10,12 +10,9 @@ class InwxApiExtension extends Extension { /** - * @param array $configs - * @param ContainerBuilder $container - * * @throws \Exception Error occurred while parsing "services.yml" */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); @@ -25,11 +22,13 @@ public function load(array $configs, ContainerBuilder $container) $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/')); $loader->load('services.yml'); + $options = \array_filter($config, static function (string $key) { + return 'username' !== $key && 'password' !== $key; + }, ARRAY_FILTER_USE_KEY); + $definition = $container->getDefinition('inwx_api'); - $definition->replaceArgument(0, $config['environment']); - $definition->replaceArgument(1, $config['username']); - $definition->replaceArgument(2, $config['password']); - $definition->replaceArgument(3, $config['language']); - $definition->replaceArgument(4, $config['debug']); + $definition->replaceArgument(0, $config['username']); + $definition->replaceArgument(1, $config['password']); + $definition->replaceArgument(2, $options); } } diff --git a/src/Domrobot.php b/src/Domrobot.php index e5094cc..662ff22 100644 --- a/src/Domrobot.php +++ b/src/Domrobot.php @@ -4,15 +4,9 @@ use INWX\Domrobot as BaseDomrobot; use SebTM\INWX\Exception\LoginUnsuccessfulException; -use SebTM\INWX\Exception\UnsupportedEnvironmentException; class Domrobot extends BaseDomrobot { - /** - * @var string - */ - protected $environment; - /** * @var string */ @@ -23,71 +17,36 @@ class Domrobot extends BaseDomrobot */ protected $password; - /** - * @param string $environment - * @param string $username - * @param string $password - * @param string $language - * @param bool $debug - * - * @throws UnsupportedEnvironmentException - */ - public function __construct($environment, $username, $password, $language = 'en', $debug = false) + public function __construct(string $username, string $password, array $options = array()) { - $this->environment = $environment; + parent::__construct(); + $this->username = $username; $this->password = $password; - parent::__construct($this->getApiUrl()); - - $this->setLanguage($language); - $this->setDebug($debug); - } - - /** - * @throws UnsupportedEnvironmentException - * - * @return string - */ - public function getApiUrl(): string - { - switch ($this->environment) { - case 'production': - return 'https://api.domrobot.com/xmlrpc/'; + if (\array_key_exists('debug', $options) && true === $options['debug']) { + $this->setDebug(true); + } - case 'test': - return 'https://api.ote.domrobot.com/xmlrpc/'; + if (\array_key_exists('environment', $options) && 'production' === $options['environment']) { + $this->useLive(); + } - default: - throw new UnsupportedEnvironmentException(); + if (\array_key_exists('json', $options) && false === $options['json']) { + $this->useXml(); } - } - /** - * @return string - */ - public function getEnvironment(): string - { - return $this->environment; + if (\array_key_exists('language', $options)) { + $this->setLanguage($options['language']); + } } /** - * @param null|string $username Should not be set to use values from DI - must have method signature compatible - * @param null|string $password Should not be set to use values from DI - must have method signature compatible - * @param null|string $sharedSecret - * * @throws LoginUnsuccessfulException - * - * @return bool */ - public function login($username = null, $password = null, $sharedSecret = null): bool + public function loginWrapper(): bool { - if (null === $username && null === $password) { - $username = $this->username; - $password = $this->password; - } - - $result = parent::login($username, $password, $sharedSecret); + $result = $this->login($this->username, $this->password); if (1000 === $result['code']) { return true; diff --git a/src/Exception/UnsupportedEnvironmentException.php b/src/Exception/UnsupportedEnvironmentException.php deleted file mode 100644 index 4a0e3cb..0000000 --- a/src/Exception/UnsupportedEnvironmentException.php +++ /dev/null @@ -1,11 +0,0 @@ - 'test', + 'debug' => true, + 'environment' => 'production', + 'json' => false, + 'language' => 'de', 'username' => 'test', 'password' => 'test', - 'language' => 'en', - 'debug' => true, ); $this->assertProcessedConfigurationEquals( diff --git a/tests/DependencyInjection/InwxApiExtensionTest.php b/tests/DependencyInjection/InwxApiExtensionTest.php index 15dc4a1..e64ec09 100644 --- a/tests/DependencyInjection/InwxApiExtensionTest.php +++ b/tests/DependencyInjection/InwxApiExtensionTest.php @@ -34,9 +34,6 @@ protected function getContainerExtensions(): array ); } - /** - * @return array - */ protected function getMinimalConfiguration(): array { return array( diff --git a/tests/DependencyInjection/fixtures/config.yml b/tests/DependencyInjection/fixtures/config.yml index 5b6b4b1..5a66de6 100644 --- a/tests/DependencyInjection/fixtures/config.yml +++ b/tests/DependencyInjection/fixtures/config.yml @@ -1,6 +1,7 @@ inwx_api: - environment: test - username: test - password: test - language: en - debug: true + debug: true + environment: production + json: false + language: de + username: test + password: test diff --git a/tests/DomrobotTest.php b/tests/DomrobotTest.php index 84f5848..a4219b0 100644 --- a/tests/DomrobotTest.php +++ b/tests/DomrobotTest.php @@ -5,7 +5,6 @@ use PHPUnit\Framework\TestCase; use SebTM\INWX\Domrobot; use SebTM\INWX\Exception\LoginUnsuccessfulException; -use SebTM\INWX\Exception\UnsupportedEnvironmentException; /** * @covers \SebTM\INWX\Domrobot @@ -13,67 +12,160 @@ class DomrobotTest extends TestCase { /** - * @covers \SebTM\INWX\Domrobot::getApiUrl() - * - * @throws UnsupportedEnvironmentException + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::getDebug */ - public function testGetApiUrlProduction(): void + public function testDebugDefault(): void { - $domrobot = new Domrobot('production', 'test', 'test'); + $domrobot = new Domrobot('test', 'test'); - $this->assertSame( - 'https://api.domrobot.com/xmlrpc/', - $domrobot->getApiUrl() - ); + $this->assertFalse($domrobot->getDebug()); } /** - * @covers \SebTM\INWX\Domrobot::__construct() - * @covers \SebTM\INWX\Domrobot::getApiUrl() - * @covers \SebTM\INWX\Domrobot::getDebug() - * @covers \SebTM\INWX\Domrobot::getEnvironment() - * @covers \SebTM\INWX\Domrobot::getLanguage() - * @covers \SebTM\INWX\Domrobot::setDebug() - * @covers \SebTM\INWX\Domrobot::setLanguage() - * - * @throws UnsupportedEnvironmentException + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::getDebug + */ + public function testDebugFalse(): void + { + $domrobot = new Domrobot('test', 'test', array('debug' => false)); + + $this->assertFalse($domrobot->getDebug()); + } + + /** + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::getDebug + * @covers \SebTM\INWX\Domrobot::setDebug + */ + public function testDebugTrue(): void + { + $domrobot = new Domrobot('test', 'test', array('debug' => true)); + + $this->assertTrue($domrobot->getDebug()); + } + + /** + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::isLive + * @covers \SebTM\INWX\Domrobot::isOte + */ + public function testEnvironmentDefault(): void + { + $domrobot = new Domrobot('test', 'test'); + + $this->assertFalse($domrobot->isLive()); + $this->assertTrue($domrobot->isOte()); + } + + /** + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::isLive + * @covers \SebTM\INWX\Domrobot::isOte + */ + public function testEnvironmentDevelopment(): void + { + $domrobot = new Domrobot('test', 'test', array('environment' => 'development')); + + $this->assertFalse($domrobot->isLive()); + $this->assertTrue($domrobot->isOte()); + } + + /** + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::isLive + * @covers \SebTM\INWX\Domrobot::useLive + */ + public function testEnvironmentProduction(): void + { + $domrobot = new Domrobot('test', 'test', array('environment' => 'production')); + + $this->assertTrue($domrobot->isLive()); + $this->assertFalse($domrobot->isOte()); + } + + /** + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::isJson + * @covers \SebTM\INWX\Domrobot::isXml */ - public function testInitialization(): void + public function testJsonDefault(): void { - $debug = true; - $environment = 'test'; - $language = 'en'; - - $domrobot = new Domrobot($environment, 'test', 'test', $language, $debug); - - $this->assertSame($debug, $domrobot->getDebug()); - $this->assertSame($environment, $domrobot->getEnvironment()); - $this->assertSame($language, $domrobot->getLanguage()); - $this->assertSame( - 'https://api.ote.domrobot.com/xmlrpc/', - $domrobot->getApiUrl() - ); + $domrobot = new Domrobot('test', 'test'); + + $this->assertTrue($domrobot->isJson()); + $this->assertFalse($domrobot->isXml()); + } + + /** + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::isJson + * @covers \SebTM\INWX\Domrobot::isXml + * @covers \SebTM\INWX\Domrobot::useXml + */ + public function testJsonFalse(): void + { + $domrobot = new Domrobot('test', 'test', array('json' => false)); + + $this->assertFalse($domrobot->isJson()); + $this->assertTrue($domrobot->isXml()); + } + + /** + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::isJson + * @covers \SebTM\INWX\Domrobot::isXml + */ + public function testJsonTrue(): void + { + $domrobot = new Domrobot('test', 'test', array('json' => true)); + + $this->assertTrue($domrobot->isJson()); + $this->assertFalse($domrobot->isXml()); + } + + /** + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::getLanguage + */ + public function testLanguageDefault(): void + { + $expectedLanguage = 'en'; + $domrobot = new Domrobot('test', 'test'); + + $this->assertSame($expectedLanguage, $domrobot->getLanguage()); + } + + /** + * @covers \SebTM\INWX\Domrobot::__construct + * @covers \SebTM\INWX\Domrobot::getLanguage + * @covers \SebTM\INWX\Domrobot::setLanguage + */ + public function testLanguageNotDefault(): void + { + $expectedLanguage = 'de'; + $domrobot = new Domrobot('test', 'test', array('language' => $expectedLanguage)); + + $this->assertSame($expectedLanguage, $domrobot->getLanguage()); } /** - * @covers \SebTM\INWX\Domrobot::login() + * @covers \SebTM\INWX\Domrobot::loginWrapper() * * @throws LoginUnsuccessfulException - * @throws UnsupportedEnvironmentException */ public function testLoginUnsuccessful(): void { $this->expectException(LoginUnsuccessfulException::class); - $domrobot = new Domrobot('test', 'test', 'test'); - $domrobot->login(); + $domrobot = new Domrobot('test', 'test'); + $domrobot->loginWrapper(); } /** - * @covers \SebTM\INWX\Domrobot::login() + * @covers \SebTM\INWX\Domrobot::loginWrapper() * * @throws LoginUnsuccessfulException - * @throws UnsupportedEnvironmentException */ public function testLoginSuccessful(): void { @@ -84,21 +176,8 @@ public function testLoginSuccessful(): void $this->markTestSkipped('Secret variables for successful login-test not available!'); } - $domrobot = new Domrobot('test', $username, $password); - $result = $domrobot->login(); + $result = (new Domrobot($username, $password))->loginWrapper(); $this->assertTrue($result); } - - /** - * @covers \SebTM\INWX\Domrobot::__construct() - * - * @throws UnsupportedEnvironmentException - */ - public function testUnsupportedEnvironment(): void - { - $this->expectException(UnsupportedEnvironmentException::class); - - new Domrobot('unsupported', 'test', 'test'); - } }