diff --git a/.gitattributes b/.gitattributes index 5a646aae4..6eda2ae79 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,11 +1,11 @@ -* text=auto - -/doc export-ignore -/test export-ignore -/.gitattributes export-ignore -/.gitignore export-ignore -/.styleci.yml export-ignore -/.travis.yml export-ignore -/phpunit.xml.dist export-ignore -/README.md export-ignore -/UPGRADE.md export-ignore +/doc export-ignore +/test export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.styleci.yml export-ignore +/.travis.yml export-ignore +/phpcs.xml export-ignore +/phpstan.neon export-ignore +/phpunit.xml.dist export-ignore +/README.md export-ignore +/UPGRADE.md export-ignore diff --git a/.gitignore b/.gitignore index 3399b9df8..49706e86a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ +.idea vendor/ composer.lock -.idea +.phpunit.result.cache +.phpcs-cache \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index b0cd492f4..f7c63edf9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,25 +1,17 @@ language: php -sudo: false - php: - - 5.6 - - 7.0 - - 7.1 - 7.2 - 7.3 + - 7.4snapshot -matrix: - # test latest PHP stable version with lowest dependencies and phpstan static analysis - include: - - php: 7.1 - env: COMPOSER_FLAGS="--prefer-lowest" STATIC_ANALYSIS=yes - -before_script: - - travis_retry composer self-update - - travis_retry composer update --no-interaction ${COMPOSER_FLAGS} - - if [ "$STATIC_ANALYSIS" != "" ]; then curl -L https://github.com/phpstan/phpstan/releases/download/0.8/phpstan.phar -o phpstan.phar; fi; +install: +- composer install --no-progress script: - - vendor/bin/phpunit --verbose --coverage-text - - if [ "$STATIC_ANALYSIS" != "" ]; then php phpstan.phar analyse --level=4 lib; fi; +- ./vendor/bin/phpcs +- ./vendor/bin/phpstan analyse -c phpstan.neon +- ./vendor/bin/phpunit --coverage-clover=coverage.xml + +after_success: +- bash <(curl -s https://codecov.io/bash) diff --git a/etc/docker/dev/php/Dockerfile b/Dockerfile similarity index 100% rename from etc/docker/dev/php/Dockerfile rename to Dockerfile diff --git a/Makefile b/Makefile index 89c01d8d7..f95047afa 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,4 @@ up: ## Up the containers test: up ## Test API docker-compose exec php composer install - docker-compose exec php /app/vendor/phpunit/phpunit/phpunit --no-configuration /app/test \ No newline at end of file + docker-compose exec php /app/vendor/phpunit/phpunit/phpunit --no-configuration /app/test diff --git a/README.md b/README.md index 103e7d971..2060012f0 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ A PHP wrapper to be used with [Gitlab's API](https://github.com/gitlabhq/gitlabhq/tree/master/doc/api). ============== -[![Build Status](https://travis-ci.org/m4tthumphrey/php-gitlab-api.svg?branch=master)](https://travis-ci.org/m4tthumphrey/php-gitlab-api) -[![StyleCI](https://styleci.io/repos/6816335/shield?branch=master)](https://styleci.io/repos/6816335) -[![Total Downloads](https://poser.pugx.org/m4tthumphrey/php-gitlab-api/downloads?format=flat-square)](https://packagist.org/packages/m4tthumphrey/php-gitlab-api) -[![Latest Stable Version](https://poser.pugx.org/m4tthumphrey/php-gitlab-api/version?format=flat-square)](https://packagist.org/packages/m4tthumphrey/php-gitlab-api) -[![Latest Unstable Version](https://poser.pugx.org/m4tthumphrey/php-gitlab-api/v/unstable?format=flat-square)](//packagist.org/packages/m4tthumphrey/php-gitlab-api) +[![Build Status](https://travis-ci.org/Jeroeny/gitlab-api.svg?branch=master)](https://travis-ci.org/jeroeny/gitlab-api) +[![StyleCI](https://styleci.io/repos/217726394/shield?branch=master)](https://github.styleci.io/repos/217726394) +[![codecov](https://codecov.io/gh/Jeroeny/gitlab-api/branch/master/graph/badge.svg)](https://codecov.io/gh/Jeroeny/gitlab-api) -Based on [php-github-api](https://github.com/m4tthumphrey/php-github-api) and code from [KnpLabs](https://github.com/KnpLabs/php-github-api). + +Forked from [php-gitlab-api](https://github.com/jeroeny/gitlab-api) and based on [php-github-api](https://github.com/m4tthumphrey/php-github-api) and code from [KnpLabs](https://github.com/KnpLabs/php-github-api). Installation ------------ @@ -15,24 +14,11 @@ Installation Via [composer](https://getcomposer.org) ```bash -composer require m4tthumphrey/php-gitlab-api php-http/guzzle6-adapter:^1.0 +composer require jeroeny/gitlab-api ``` -Why `php-http/guzzle6-adapter`? We are decoupled from any HTTP messaging client with help by [HTTPlug](http://httplug.io). - You can visit [HTTPlug for library users](http://docs.php-http.org/en/latest/httplug/users.html) to get more information about installing HTTPlug related packages. -Versioning ----------- - -Depending on your Gitlab server version, you must choose the right version of this library. -Please refer to the following table to pick the right one. - -|Version|Gitlab API Version|Gitlab Version| -|-------|------------------|--------------| -|9.x | V4 | >= 9.0 | -|8.x | V3 | < 9.5 | - General API Usage ----------------- @@ -98,7 +84,7 @@ $issue = $project->createIssue('This does not work.', array( $issue->close(); ``` -You get the idea! Take a look around ([API methods](https://github.com/m4tthumphrey/php-gitlab-api/tree/master/lib/Gitlab/Api), +You get the idea! Take a look around ([API methods](https://github.com/jeroeny/gitlab-api/tree/master/lib/Gitlab/Api), [models](https://github.com/m4tthumphrey/php-gitlab-api/tree/master/lib/Gitlab/Model)) and please feel free to report any bugs. Framework Integrations @@ -111,5 +97,4 @@ If you have integrated GitLab into a popular PHP framework, let us know! Contributing ------------ -There are many parts of Gitlab that I have not added to this as it was originally created for personal use, hence the -lack of tests. Feel free to fork and add new functionality and tests, I'll gladly accept decent pull requests. +Feel free to fork and add new functionality and/or tests, I'll gladly accept decent pull requests. diff --git a/composer.json b/composer.json index fefa479cc..e17e48c2c 100644 --- a/composer.json +++ b/composer.json @@ -1,50 +1,63 @@ { - "name": "m4tthumphrey/php-gitlab-api", - "type": "library", - "description": "GitLab API client", - "homepage": "https://github.com/m4tthumphrey/php-gitlab-api", - "keywords": ["gitlab", "api"], - "license": "MIT", - "authors": [ - { - "name": "Matt Humphrey", - "homepage": "http://m4tt.io" - }, - { - "name": "KnpLabs Team", - "homepage": "http://knplabs.com" - }, - { - "name": "Thibault Duplessis", - "email": "thibault.duplessis@gmail.com", - "homepage": "http://ornicar.github.com" - } - ], - "require": { - "php": "^5.6 || ^7.0", - "ext-xml": "*", - "php-http/client-common": "^1.6", - "php-http/client-implementation": "^1.0", - "php-http/discovery": "^1.2", - "php-http/httplug": "^1.1", - "php-http/multipart-stream-builder": "^1.0", - "symfony/options-resolver": "^2.6 || ^3.0 || ^4.0" - }, - "require-dev": { - "guzzlehttp/psr7": "^1.2", - "php-http/guzzle6-adapter": "^1.0", - "php-http/mock-client": "^1.0", - "phpunit/phpunit": "^5.7.27 || ^6.5" - }, - "autoload": { - "psr-4": { "Gitlab\\": "lib/Gitlab/" } - }, - "autoload-dev": { - "psr-4": { "Gitlab\\Tests\\": "test/Gitlab/Tests/" } - }, - "extra": { - "branch-alias": { - "dev-master": "9.14-dev" - } - } + "name": "jeroeny/gitlab-api", + "type": "library", + "description": "GitLab API client", + "homepage": "https://github.com/jeroeny/gitlab-api", + "keywords": [ + "gitlab", + "api" + ], + "license": "MIT", + "authors": [ + { + "name": "Matt Humphrey", + "homepage": "http://m4tt.io" + }, + { + "name": "KnpLabs Team", + "homepage": "http://knplabs.com" + }, + { + "name": "Thibault Duplessis", + "email": "thibault.duplessis@gmail.com", + "homepage": "http://ornicar.github.com" + } + ], + "require": { + "php": "^7.2", + "ext-json": "*", + "ext-xml": "*", + "php-http/client-common": "^2.0", + "php-http/discovery": "^1.7", + "php-http/httplug": "^2.0", + "php-http/message": "^1.8", + "php-http/multipart-stream-builder": "^1.1", + "symfony/options-resolver": "^4.3" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5", + "doctrine/coding-standard": "^6.0", + "guzzlehttp/psr7": "^1.6", + "moxio/php-codesniffer-sniffs": "^2.3", + "php-http/guzzle6-adapter": "^2.0", + "php-http/mock-client": "^1.3", + "phpstan/phpstan": "^0.11", + "phpunit/phpunit": "^8.4", + "slevomat/coding-standard": "^5.0", + "squizlabs/php_codesniffer": "^3.5", + "zendframework/zend-diactoros": "^2.1" + }, + "autoload": { + "psr-4": { + "Gitlab\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Gitlab\\Tests\\": "tests/unit" + } + }, + "config": { + "sort-packages": true + } } diff --git a/docker-compose.yml b/docker-compose.yml index 3f8c30c9a..286062d28 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,8 @@ version: "3" services: php: - build: etc/docker/dev/php + build: . volumes: - .:/app working_dir: /app - command: tail -f /dev/null \ No newline at end of file + command: tail -f /dev/null diff --git a/lib/Gitlab/Api/ApiInterface.php b/lib/Gitlab/Api/ApiInterface.php deleted file mode 100644 index 40c3a5659..000000000 --- a/lib/Gitlab/Api/ApiInterface.php +++ /dev/null @@ -1,11 +0,0 @@ -getGroupSearchResolver(); - - return $this->get('groups', $resolver->resolve($parameters)); - } - - /** - * @param int $id - * @return mixed - */ - public function show($id) - { - return $this->get('groups/'.$this->encodePath($id)); - } - - /** - * @param string $name - * @param string $path - * @param string $description - * @param string $visibility - * @param bool $lfs_enabled - * @param bool $request_access_enabled - * @param int $parent_id - * @param int $shared_runners_minutes_limit - * @return mixed - */ - public function create($name, $path, $description = null, $visibility = 'private', $lfs_enabled = null, $request_access_enabled = null, $parent_id = null, $shared_runners_minutes_limit = null) - { - $params = array( - 'name' => $name, - 'path' => $path, - 'description' => $description, - 'visibility' => $visibility, - 'lfs_enabled' => $lfs_enabled, - 'request_access_enabled' => $request_access_enabled, - 'parent_id' => $parent_id, - 'shared_runners_minutes_limit' => $shared_runners_minutes_limit, - ); - - return $this->post('groups', array_filter($params, 'strlen')); - } - - /** - * @param int $id - * @param array $params - * @return mixed - */ - public function update($id, array $params) - { - return $this->put('groups/'.$this->encodePath($id), $params); - } - - /** - * @param int $group_id - * @return mixed - */ - public function remove($group_id) - { - return $this->delete('groups/'.$this->encodePath($group_id)); - } - - /** - * @param int $group_id - * @param int $project_id - * @return mixed - */ - public function transfer($group_id, $project_id) - { - return $this->post('groups/'.$this->encodePath($group_id).'/projects/'.$this->encodePath($project_id)); - } - - /** - * @param integer $id - * @param array $parameters - * @return mixed - */ - public function allMembers($id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - $resolver->setDefined('query'); - - return $this->get('groups/'.$this->encodePath($id).'/members/all', $resolver->resolve($parameters)); - } - - /** - * @param int $id - * @param array $parameters ( - * - * @var string $query A query string to search for members. - * ) - * - * @return mixed - */ - public function members($id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - $resolver->setDefined('query'); - - return $this->get('groups/'.$this->encodePath($id).'/members', $resolver->resolve($parameters)); - } - - /** - * @param int $group_id - * @param int $user_id - * @param int $access_level - * @return mixed - */ - public function addMember($group_id, $user_id, $access_level) - { - return $this->post('groups/'.$this->encodePath($group_id).'/members', array( - 'user_id' => $user_id, - 'access_level' => $access_level - )); - } - - /** - * @param int $group_id - * @param int $user_id - * @param int $access_level - * @return mixed - */ - public function saveMember($group_id, $user_id, $access_level) - { - return $this->put('groups/'.$this->encodePath($group_id).'/members/'.$this->encodePath($user_id), array( - 'access_level' => $access_level - )); - } - - /** - * @param int $group_id - * @param int $user_id - * @return mixed - */ - public function removeMember($group_id, $user_id) - { - return $this->delete('groups/'.$this->encodePath($group_id).'/members/'.$this->encodePath($user_id)); - } - - /** - * @param $id - * @param array $parameters ( - * - * @var bool $archived Limit by archived status. - * @var string $visibility Limit by visibility public, internal, or private. - * @var string $order_by Return projects ordered by id, name, path, created_at, updated_at, or last_activity_at fields. - * Default is created_at. - * @var string $sort Return projects sorted in asc or desc order. Default is desc. - * @var string $search Return list of authorized projects matching the search criteria. - * @var bool $simple Return only the ID, URL, name, and path of each project. - * @var bool $owned Limit by projects owned by the current user. - * @var bool $starred Limit by projects starred by the current user. - * @var bool $with_issues_enabled Limit by projects with issues feature enabled. Default is false. - * @var bool $with_merge_requests_enabled Limit by projects with merge requests feature enabled. Default is false. - * @var bool $with_shared Include projects shared to this group. Default is true. - * @var bool $include_subgroups Include projects in subgroups of this group. Default is false. - * @var bool $with_custom_attributes Include custom attributes in response (admins only). - * ) - * - * @return mixed - */ - public function projects($id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - $booleanNormalizer = function (Options $resolver, $value) { - return $value ? 'true' : 'false'; - }; - - $resolver->setDefined('archived') - ->setAllowedTypes('archived', 'bool') - ->setNormalizer('archived', $booleanNormalizer) - ; - $resolver->setDefined('visibility') - ->setAllowedValues('visibility', ['public', 'internal', 'private']) - ; - $resolver->setDefined('order_by') - ->setAllowedValues('order_by', ['id', 'name', 'path', 'created_at', 'updated_at', 'last_activity_at']) - ; - $resolver->setDefined('sort') - ->setAllowedValues('sort', ['asc', 'desc']) - ; - $resolver->setDefined('search'); - $resolver->setDefined('simple') - ->setAllowedTypes('simple', 'bool') - ->setNormalizer('simple', $booleanNormalizer) - ; - $resolver->setDefined('owned') - ->setAllowedTypes('owned', 'bool') - ->setNormalizer('owned', $booleanNormalizer) - ; - $resolver->setDefined('starred') - ->setAllowedTypes('starred', 'bool') - ->setNormalizer('starred', $booleanNormalizer) - ; - $resolver->setDefined('with_issues_enabled') - ->setAllowedTypes('with_issues_enabled', 'bool') - ->setNormalizer('with_issues_enabled', $booleanNormalizer) - ; - $resolver->setDefined('with_merge_requests_enabled') - ->setAllowedTypes('with_merge_requests_enabled', 'bool') - ->setNormalizer('with_merge_requests_enabled', $booleanNormalizer) - ; - $resolver->setDefined('with_shared') - ->setAllowedTypes('with_shared', 'bool') - ->setNormalizer('with_shared', $booleanNormalizer) - ; - $resolver->setDefined('include_subgroups') - ->setAllowedTypes('include_subgroups', 'bool') - ->setNormalizer('include_subgroups', $booleanNormalizer) - ; - $resolver->setDefined('with_custom_attributes') - ->setAllowedTypes('with_custom_attributes', 'bool') - ->setNormalizer('with_custom_attributes', $booleanNormalizer) - ; - - return $this->get('groups/'.$this->encodePath($id).'/projects', $resolver->resolve($parameters)); - } - - /** - * @param int $group_id - * @param array $parameters ( - * - * @var int[] $skip_groups Skip the group IDs passes. - * @var bool $all_available Show all the groups you have access to. - * @var string $search Return list of authorized groups matching the search criteria. - * @var string $order_by Order groups by name or path. Default is name. - * @var string $sort Order groups in asc or desc order. Default is asc. - * @var bool $statistics Include group statistics (admins only). - * @var bool $owned Limit by groups owned by the current user. - * ) - * @return mixed - */ - public function subgroups($group_id, array $parameters = []) - { - $resolver = $this->getGroupSearchResolver(); - - return $this->get('groups/'.$this->encodePath($group_id).'/subgroups', $resolver->resolve($parameters)); - } - - /** - * @param int $group_id - * @param array $parameters - * @return mixed - */ - public function labels($group_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - - return $this->get('groups/'.$this->encodePath($group_id). '/labels', $resolver->resolve($parameters)); - } - - /** - * @param int $group_id - * @param array $params - * @return mixed - */ - public function addLabel($group_id, array $params) - { - return $this->post('groups/'.$this->encodePath($group_id). '/labels', $params); - } - - /** - * @param int $group_id - * @param array $params - * @return mixed - */ - public function updateLabel($group_id, array $params) - { - return $this->put('groups/'.$this->encodePath($group_id). '/labels', $params); - } - - /** - * @param int $group_id - * @param string $name - * @return mixed - */ - public function removeLabel($group_id, $name) - { - return $this->delete('groups/'.$this->encodePath($group_id). '/labels', array( - 'name' => $name - )); - } - - /** - * @param int $group_id - * @param array $parameters - * @return mixed - */ - public function variables($group_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - - return $this->get($this->getGroupPath($group_id, 'variables'), $resolver->resolve($parameters)); - } - - /** - * @param int $group_id - * @param string $key - * @return mixed - */ - public function variable($group_id, $key) - { - return $this->get($this->getGroupPath($group_id, 'variables/'.$this->encodePath($key))); - } - - /** - * @param int $group_id - * @param string $key - * @param string $value - * @param bool $protected - * @return mixed - */ - public function addVariable($group_id, $key, $value, $protected = null) - { - $payload = array( - 'key' => $key, - 'value' => $value, - ); - - if ($protected) { - $payload['protected'] = $protected; - } - - return $this->post($this->getGroupPath($group_id, 'variables'), $payload); - } - - /** - * @param int $group_id - * @param string $key - * @param string $value - * @param bool $protected - * @return mixed - */ - public function updateVariable($group_id, $key, $value, $protected = null) - { - $payload = array( - 'value' => $value, - ); - - if ($protected) { - $payload['protected'] = $protected; - } - - return $this->put($this->getGroupPath($group_id, 'variables/'.$this->encodePath($key)), $payload); - } - - /** - * @param int $group_id - * @param string $key - * @return mixed - */ - public function removeVariable($group_id, $key) - { - return $this->delete($this->getGroupPath($group_id, 'variables/'.$this->encodePath($key))); - } - - private function getGroupSearchResolver() - { - $resolver = $this->createOptionsResolver(); - $booleanNormalizer = function (Options $resolver, $value) { - return $value ? 'true' : 'false'; - }; - - $resolver->setDefined('skip_groups') - ->setAllowedTypes('skip_groups', 'array') - ->setAllowedValues('skip_groups', function (array $value) { - return count($value) == count(array_filter($value, 'is_int')); - }) - ; - $resolver->setDefined('all_available') - ->setAllowedTypes('all_available', 'bool') - ->setNormalizer('all_available', $booleanNormalizer) - ; - $resolver->setDefined('search'); - $resolver->setDefined('order_by') - ->setAllowedValues('order_by', ['name', 'path']) - ; - $resolver->setDefined('sort') - ->setAllowedValues('sort', ['asc', 'desc']) - ; - $resolver->setDefined('statistics') - ->setAllowedTypes('statistics', 'bool') - ->setNormalizer('statistics', $booleanNormalizer) - ; - $resolver->setDefined('owned') - ->setAllowedTypes('owned', 'bool') - ->setNormalizer('owned', $booleanNormalizer) - ; - - return $resolver; - } -} diff --git a/lib/Gitlab/Api/GroupsBoards.php b/lib/Gitlab/Api/GroupsBoards.php deleted file mode 100644 index e2df06dad..000000000 --- a/lib/Gitlab/Api/GroupsBoards.php +++ /dev/null @@ -1,123 +0,0 @@ -createOptionsResolver(); - - $path = $group_id === null ? 'boards' : $this->getGroupPath($group_id, 'boards'); - - return $this->get($path, $resolver->resolve($parameters)); - } - - /** - * @param int $group_id - * @param int $board_id - * @return mixed - */ - public function show($group_id, $board_id) - { - return $this->get($this->getGroupPath($group_id, 'boards/'.$this->encodePath($board_id))); - } - - /** - * @param int $group_id - * @param array $params - * @return mixed - */ - public function create($group_id, array $params) - { - return $this->post($this->getGroupPath($group_id, 'boards'), $params); - } - - /** - * @param int $group_id - * @param int $board_id - * @param array $params - * @return mixed - */ - public function update($group_id, $board_id, array $params) - { - return $this->put($this->getGroupPath($group_id, 'boards/'.$this->encodePath($board_id)), $params); - } - - /** - * @param int $group_id - * @param int $board_id - * @return mixed - */ - public function remove($group_id, $board_id) - { - return $this->delete($this->getGroupPath($group_id, 'boards/'.$this->encodePath($board_id))); - } - - /** - * @param int $group_id - * @param int $board_id - * @return mixed - */ - public function allLists($group_id, $board_id) - { - return $this->get($this->getGroupPath($group_id, 'boards/'.$this->encodePath($board_id).'/lists')); - } - - /** - * @param int $group_id - * @param int $board_id - * @param int $list_id - * @return mixed - */ - public function showList($group_id, $board_id, $list_id) - { - return $this->get($this->getGroupPath($group_id, 'boards/'.$this->encodePath($board_id).'/lists/'.$this->encodePath($list_id))); - } - - /** - * @param int $group_id - * @param int $board_id - * @param int $label_id - * @return mixed - */ - public function createList($group_id, $board_id, $label_id) - { - $params = array( - 'label_id' => $label_id - ); - - return $this->post($this->getGroupPath($group_id, 'boards/'.$this->encodePath($board_id).'/lists'), $params); - } - - /** - * @param int $group_id - * @param int $board_id - * @param int $list_id - * @param int $position - * @return mixed - */ - public function updateList($group_id, $board_id, $list_id, $position) - { - $params = array( - 'position' => $position - ); - - return $this->put($this->getGroupPath($group_id, 'boards/'.$this->encodePath($board_id).'/lists/'.$this->encodePath($list_id)), $params); - } - - /** - * @param int $group_id - * @param int $board_id - * @param int $list_id - * @return mixed - */ - public function deleteList($group_id, $board_id, $list_id) - { - return $this->delete($this->getGroupPath($group_id, 'boards/'.$this->encodePath($board_id).'/lists/'.$this->encodePath($list_id))); - } -} diff --git a/lib/Gitlab/Api/GroupsMilestones.php b/lib/Gitlab/Api/GroupsMilestones.php deleted file mode 100644 index 7c32fd87a..000000000 --- a/lib/Gitlab/Api/GroupsMilestones.php +++ /dev/null @@ -1,93 +0,0 @@ -createOptionsResolver(); - $resolver->setDefined('iids') - ->setAllowedTypes('iids', 'array') - ->setAllowedValues('iids', function (array $value) { - return count($value) == count(array_filter($value, 'is_int')); - }) - ; - $resolver->setDefined('state') - ->setAllowedValues('state', ['active', 'closed']) - ; - $resolver->setDefined('search'); - - return $this->get($this->getGroupPath($group_id, 'milestones'), $resolver->resolve($parameters)); - } - - /** - * @param int $group_id - * @param int $milestone_id - * @return mixed - */ - public function show($group_id, $milestone_id) - { - return $this->get($this->getGroupPath($group_id, 'milestones/'.$this->encodePath($milestone_id))); - } - - /** - * @param int $group_id - * @param array $params - * @return mixed - */ - public function create($group_id, array $params) - { - return $this->post($this->getGroupPath($group_id, 'milestones'), $params); - } - - /** - * @param int $group_id - * @param int $milestone_id - * @param array $params - * @return mixed - */ - public function update($group_id, $milestone_id, array $params) - { - return $this->put($this->getGroupPath($group_id, 'milestones/'.$this->encodePath($milestone_id)), $params); - } - - /** - * @param int $group_id - * @param int $milestone_id - * @return mixed - */ - public function remove($group_id, $milestone_id) - { - return $this->delete($this->getGroupPath($group_id, 'milestones/'.$this->encodePath($milestone_id))); - } - - /** - * @param int $group_id - * @param int $milestone_id - * @return mixed - */ - public function issues($group_id, $milestone_id) - { - return $this->get($this->getGroupPath($group_id, 'milestones/'.$this->encodePath($milestone_id).'/issues')); - } - - /** - * @param int $group_id - * @param int $milestone_id - * @return mixed - */ - public function mergeRequests($group_id, $milestone_id) - { - return $this->get($this->getGroupPath($group_id, 'milestones/'.$this->encodePath($milestone_id).'/merge_requests')); - } -} diff --git a/lib/Gitlab/Api/IssueBoards.php b/lib/Gitlab/Api/IssueBoards.php deleted file mode 100644 index 0bdf1455c..000000000 --- a/lib/Gitlab/Api/IssueBoards.php +++ /dev/null @@ -1,123 +0,0 @@ -createOptionsResolver(); - - $path = $project_id === null ? 'boards' : $this->getProjectPath($project_id, 'boards'); - - return $this->get($path, $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param int $board_id - * @return mixed - */ - public function show($project_id, $board_id) - { - return $this->get($this->getProjectPath($project_id, 'boards/'.$this->encodePath($board_id))); - } - - /** - * @param int $project_id - * @param array $params - * @return mixed - */ - public function create($project_id, array $params) - { - return $this->post($this->getProjectPath($project_id, 'boards'), $params); - } - - /** - * @param int $project_id - * @param int $board_id - * @param array $params - * @return mixed - */ - public function update($project_id, $board_id, array $params) - { - return $this->put($this->getProjectPath($project_id, 'boards/'.$this->encodePath($board_id)), $params); - } - - /** - * @param int $project_id - * @param int $board_id - * @return mixed - */ - public function remove($project_id, $board_id) - { - return $this->delete($this->getProjectPath($project_id, 'boards/'.$this->encodePath($board_id))); - } - - /** - * @param int $project_id - * @param int $board_id - * @return mixed - */ - public function allLists($project_id, $board_id) - { - return $this->get($this->getProjectPath($project_id, 'boards/'.$this->encodePath($board_id).'/lists')); - } - - /** - * @param int $project_id - * @param int $board_id - * @param int $list_id - * @return mixed - */ - public function showList($project_id, $board_id, $list_id) - { - return $this->get($this->getProjectPath($project_id, 'boards/'.$this->encodePath($board_id).'/lists/'.$this->encodePath($list_id))); - } - - /** - * @param int $project_id - * @param int $board_id - * @param int $label_id - * @return mixed - */ - public function createList($project_id, $board_id, $label_id) - { - $params = array( - 'label_id' => $label_id - ); - - return $this->post($this->getProjectPath($project_id, 'boards/'.$this->encodePath($board_id).'/lists'), $params); - } - - /** - * @param int $project_id - * @param int $board_id - * @param int $list_id - * @param int $position - * @return mixed - */ - public function updateList($project_id, $board_id, $list_id, $position) - { - $params = array( - 'position' => $position - ); - - return $this->put($this->getProjectPath($project_id, 'boards/'.$this->encodePath($board_id).'/lists/'.$this->encodePath($list_id)), $params); - } - - /** - * @param int $project_id - * @param int $board_id - * @param int $list_id - * @return mixed - */ - public function deleteList($project_id, $board_id, $list_id) - { - return $this->delete($this->getProjectPath($project_id, 'boards/'.$this->encodePath($board_id).'/lists/'.$this->encodePath($list_id))); - } -} diff --git a/lib/Gitlab/Api/IssueLinks.php b/lib/Gitlab/Api/IssueLinks.php deleted file mode 100644 index 937709afd..000000000 --- a/lib/Gitlab/Api/IssueLinks.php +++ /dev/null @@ -1,40 +0,0 @@ -get($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid)).'/links'); - } - - /** - * @param int $source_project_id - * @param int $source_issue_iid - * @param int $target_project_id - * @param int $target_issue_iid - * @return mixed - */ - public function create($source_project_id, $source_issue_iid, $target_project_id, $target_issue_iid) - { - return $this->post($this->getProjectPath($source_project_id, 'issues/'.$this->encodePath($source_issue_iid).'/links'), array( - 'target_project_id' => $target_project_id, - 'target_issue_iid' => $target_issue_iid - )); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param int $issue_link_id - * @return mixed - */ - public function remove($project_id, $issue_iid, $issue_link_id) - { - return $this->delete($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid)).'/links/'.$this->encodePath($issue_link_id)); - } -} diff --git a/lib/Gitlab/Api/Issues.php b/lib/Gitlab/Api/Issues.php deleted file mode 100644 index 953d6336a..000000000 --- a/lib/Gitlab/Api/Issues.php +++ /dev/null @@ -1,331 +0,0 @@ -createOptionsResolver(); - - $resolver->setDefined('state') - ->setAllowedValues('state', ['opened', 'closed']) - ; - $resolver->setDefined('labels'); - $resolver->setDefined('milestone'); - $resolver->setDefined('iids') - ->setAllowedTypes('iids', 'array') - ->setAllowedValues('iids', function (array $value) { - return count($value) == count(array_filter($value, 'is_int')); - }) - ; - $resolver->setDefined('scope') - ->setAllowedValues('scope', ['created-by-me', 'assigned-to-me', 'all']) - ; - $resolver->setDefined('order_by') - ->setAllowedValues('order_by', ['created_at', 'updated_at']) - ; - $resolver->setDefined('sort') - ->setAllowedValues('sort', ['asc', 'desc']) - ; - $resolver->setDefined('search'); - $resolver->setDefined('assignee_id') - ->setAllowedTypes('assignee_id', 'integer') - ; - - $path = $project_id === null ? 'issues' : $this->getProjectPath($project_id, 'issues'); - - return $this->get($path, $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @return mixed - */ - public function show($project_id, $issue_iid) - { - return $this->get($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid))); - } - - /** - * @param int $project_id - * @param array $params - * @return mixed - */ - public function create($project_id, array $params) - { - return $this->post($this->getProjectPath($project_id, 'issues'), $params); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param array $params - * @return mixed - */ - public function update($project_id, $issue_iid, array $params) - { - return $this->put($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid)), $params); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param int $to_project_id - * @return mixed - */ - public function move($project_id, $issue_iid, $to_project_id) - { - return $this->post($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid)).'/move', array( - 'to_project_id' => $to_project_id - )); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @return mixed - */ - public function remove($project_id, $issue_iid) - { - return $this->delete($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid))); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @return mixed - */ - public function showComments($project_id, $issue_iid) - { - return $this->get($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid)).'/notes'); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param int $note_id - * @return mixed - */ - public function showComment($project_id, $issue_iid, $note_id) - { - return $this->get($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid)).'/notes/'.$this->encodePath($note_id)); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param string|array $body - * @return mixed - */ - public function addComment($project_id, $issue_iid, $body) - { - // backwards compatibility - if (is_array($body)) { - $params = $body; - } else { - $params = array('body' => $body); - } - - return $this->post($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/notes'), $params); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param int $note_id - * @param string $body - * @return mixed - */ - public function updateComment($project_id, $issue_iid, $note_id, $body) - { - return $this->put($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/notes/'.$this->encodePath($note_id)), array( - 'body' => $body - )); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param int $note_id - * @return mixed - */ - public function removeComment($project_id, $issue_iid, $note_id) - { - return $this->delete($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/notes/'.$this->encodePath($note_id))); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @return mixed - */ - public function showDiscussions($project_id, $issue_iid) - { - return $this->get($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid)).'/discussions'); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param string $discussion_id - * @return mixed - */ - public function showDiscussion($project_id, $issue_iid, $discussion_id) - { - return $this->get($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid)).'/discussions/'.$this->encodePath($discussion_id)); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param string|array $body - * @return mixed - */ - public function addDiscussion($project_id, $issue_iid, $body) - { - // backwards compatibility - if (is_array($body)) { - $params = $body; - } else { - $params = array('body' => $body); - } - - return $this->post($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/discussions'), $params); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param string $discussion_id - * @param string|array $body - * @return mixed - */ - public function addDiscussionNote($project_id, $issue_iid, $discussion_id, $body) - { - // backwards compatibility - if (is_array($body)) { - $params = $body; - } else { - $params = array('body' => $body); - } - - return $this->post($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/discussions/'.$this->encodePath($discussion_id).'/notes'), $params); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param string $discussion_id - * @param int $note_id - * @param string $body - * @return mixed - */ - public function updateDiscussionNote($project_id, $issue_iid, $discussion_id, $note_id, $body) - { - return $this->put($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/discussions/'.$this->encodePath($discussion_id).'/notes/'.$this->encodePath($note_id)), array( - 'body' => $body - )); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param string $discussion_id - * @param int $note_id - * @return mixed - */ - public function removeDiscussionNote($project_id, $issue_iid, $discussion_id, $note_id) - { - return $this->delete($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/discussions/'.$this->encodePath($discussion_id).'/notes/'.$this->encodePath($note_id))); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param string $duration - * @return mixed - */ - public function setTimeEstimate($project_id, $issue_iid, $duration) - { - return $this->post($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/time_estimate'), array('duration' => $duration)); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @return mixed - */ - public function resetTimeEstimate($project_id, $issue_iid) - { - return $this->post($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/reset_time_estimate')); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @param string $duration - * @return mixed - */ - public function addSpentTime($project_id, $issue_iid, $duration) - { - return $this->post($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/add_spent_time'), array('duration' => $duration)); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @return mixed - */ - public function resetSpentTime($project_id, $issue_iid) - { - return $this->post($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/reset_spent_time')); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @return mixed - */ - public function getTimeStats($project_id, $issue_iid) - { - return $this->get($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid) .'/time_stats')); - } - - /** - * @param int $project_id - * @param int $issue_iid - * - * @return mixed - */ - public function awardEmoji($project_id, $issue_iid) - { - return $this->get($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid).'/award_emoji')); - } - - /** - * @param int $project_id - * @param int $issue_iid - * @return mixed - */ - public function closedByMergeRequests($project_id, $issue_iid) - { - return $this->get($this->getProjectPath($project_id, 'issues/'.$this->encodePath($issue_iid)).'/closed_by'); - } -} diff --git a/lib/Gitlab/Api/Jobs.php b/lib/Gitlab/Api/Jobs.php deleted file mode 100644 index 0cbe46d6a..000000000 --- a/lib/Gitlab/Api/Jobs.php +++ /dev/null @@ -1,192 +0,0 @@ -createOptionsResolver(); - - return $this->get("projects/".$this->encodePath($project_id)."/jobs", $resolver->resolve($parameters)); - } - - /** - * @param int|string $project_id - * @param int $pipeline_id - * @param array $parameters ( - * - * @var string|string[] $scope The scope of jobs to show, one or array of: created, pending, running, failed, - * success, canceled, skipped, manual; showing all jobs if none provided. - * ) - * - * @return mixed - */ - public function pipelineJobs($project_id, $pipeline_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - - return $this->get( - $this->getProjectPath($project_id, 'pipelines/').$this->encodePath($pipeline_id)."/jobs", - $resolver->resolve($parameters) - ); - } - - /** - * @param int|string $project_id - * @param int $job_id - * @return mixed - */ - public function show($project_id, $job_id) - { - return $this->get("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)); - } - - /** - * @param int|string $project_id - * @param int $job_id - * @return StreamInterface - */ - public function artifacts($project_id, $job_id) - { - return $this->getAsResponse("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/artifacts")->getBody(); - } - - /** - * @param int|string $project_id - * @param string $ref_name - * @param string $job_name - * @return StreamInterface - */ - public function artifactsByRefName($project_id, $ref_name, $job_name) - { - return $this->getAsResponse("projects/".$this->encodePath($project_id)."/jobs/artifacts/".$this->encodePath($ref_name)."/download", array( - 'job' => $this->encodePath($job_name) - ))->getBody(); - } - - /** - * @param int|string $project_id - * @param string $ref_name - * @param string $job_name - * @param string $artifact_path - * @return StreamInterface - */ - public function artifactByRefName($project_id, $ref_name, $job_name, $artifact_path) - { - return $this->getAsResponse("projects/".$this->encodePath($project_id)."/jobs/artifacts/".$this->encodePath($ref_name)."/raw/".$this->encodePath($artifact_path), array( - 'job' => $this->encodePath($job_name) - ))->getBody(); - } - - /** - * @param int|string $project_id - * @param int $job_id - * @return string - */ - public function trace($project_id, $job_id) - { - return $this->get("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/trace"); - } - - /** - * @param int|string $project_id - * @param int $job_id - * @return mixed - */ - public function cancel($project_id, $job_id) - { - return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/cancel"); - } - - /** - * @param int|string $project_id - * @param int $job_id - * @return mixed - */ - public function retry($project_id, $job_id) - { - return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/retry"); - } - - /** - * @param int|string $project_id - * @param int $job_id - * @return mixed - */ - public function erase($project_id, $job_id) - { - return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/erase"); - } - - /** - * @param int|string $project_id - * @param int $job_id - * @return mixed - */ - public function keepArtifacts($project_id, $job_id) - { - return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/artifacts/keep"); - } - - /** - * @param int|string $project_id - * @param int $job_id - * @return mixed - */ - public function play($project_id, $job_id) - { - return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/play"); - } - - /** - * {@inheritdoc} - */ - protected function createOptionsResolver() - { - $allowedScopeValues = [ - self::SCOPE_CANCELED, - self::SCOPE_CREATED, - self::SCOPE_FAILED, - self::SCOPE_MANUAL, - self::SCOPE_PENDING, - self::SCOPE_RUNNING, - self::SCOPE_SKIPPED, - self::SCOPE_SUCCESS, - ]; - - $resolver = parent::createOptionsResolver(); - $resolver->setDefined('scope') - ->setAllowedTypes('scope', ['string', 'array']) - ->setAllowedValues('scope', $allowedScopeValues) - ->addAllowedValues('scope', function ($value) use ($allowedScopeValues) { - return is_array($value) && empty(array_diff($value, $allowedScopeValues)); - }) - ->setNormalizer('scope', function (OptionsResolver $resolver, $value) { - return (array) $value; - }) - ; - - return $resolver; - } -} diff --git a/lib/Gitlab/Api/Keys.php b/lib/Gitlab/Api/Keys.php deleted file mode 100644 index 162c33855..000000000 --- a/lib/Gitlab/Api/Keys.php +++ /dev/null @@ -1,13 +0,0 @@ -get('keys/'.$this->encodePath($id)); - } -} diff --git a/lib/Gitlab/Api/MergeRequests.php b/lib/Gitlab/Api/MergeRequests.php deleted file mode 100644 index a0b74edfe..000000000 --- a/lib/Gitlab/Api/MergeRequests.php +++ /dev/null @@ -1,376 +0,0 @@ -createOptionsResolver(); - $datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) { - return $value->format('c'); - }; - $resolver->setDefined('iids') - ->setAllowedTypes('iids', 'array') - ->setAllowedValues('iids', function (array $value) { - return count($value) == count(array_filter($value, 'is_int')); - }) - ; - $resolver->setDefined('state') - ->setAllowedValues('state', ['all', 'opened', 'merged', 'closed']) - ; - $resolver->setDefined('order_by') - ->setAllowedValues('order_by', ['created_at', 'updated_at']) - ; - $resolver->setDefined('sort') - ->setAllowedValues('sort', ['asc', 'desc']) - ; - $resolver->setDefined('milestone'); - $resolver->setDefined('view') - ->setAllowedValues('view', ['simple']) - ; - $resolver->setDefined('labels'); - $resolver->setDefined('created_after') - ->setAllowedTypes('created_after', \DateTimeInterface::class) - ->setNormalizer('created_after', $datetimeNormalizer) - ; - $resolver->setDefined('created_before') - ->setAllowedTypes('created_before', \DateTimeInterface::class) - ->setNormalizer('created_before', $datetimeNormalizer) - ; - - $resolver->setDefined('updated_after') - ->setAllowedTypes('updated_after', \DateTimeInterface::class) - ->setNormalizer('updated_after', $datetimeNormalizer) - ; - $resolver->setDefined('updated_before') - ->setAllowedTypes('updated_before', \DateTimeInterface::class) - ->setNormalizer('updated_before', $datetimeNormalizer) - ; - - $resolver->setDefined('scope') - ->setAllowedValues('scope', ['created_by_me', 'assigned_to_me', 'all']) - ; - $resolver->setDefined('author_id') - ->setAllowedTypes('author_id', 'integer'); - - $resolver->setDefined('assignee_id') - ->setAllowedTypes('assignee_id', 'integer'); - - $resolver->setDefined('search'); - $resolver->setDefined('source_branch'); - $resolver->setDefined('target_branch'); - - return $this->get($this->getProjectPath($project_id, 'merge_requests'), $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param int $mr_id - * @return mixed - */ - public function show($project_id, $mr_id) - { - return $this->get($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_id))); - } - - /** - * @param int $project_id - * @param string $source - * @param string $target - * @param string $title - * @param int $assignee - * @param int $target_project_id - * @param string $description - * @return mixed - */ - public function create($project_id, $source, $target, $title, $assignee = null, $target_project_id = null, $description = null) - { - return $this->post($this->getProjectPath($project_id, 'merge_requests'), array( - 'source_branch' => $source, - 'target_branch' => $target, - 'title' => $title, - 'assignee_id' => $assignee, - 'target_project_id' => $target_project_id, - 'description' => $description - )); - } - - /** - * @param int $project_id - * @param int $mr_id - * @param array $params - * @return mixed - */ - public function update($project_id, $mr_id, array $params) - { - return $this->put($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_id)), $params); - } - - /** - * @param int $project_id - * @param int $mr_id - * @param string $message - * @return mixed - */ - public function merge($project_id, $mr_id, $message = null) - { - if (is_array($message)) { - $params = $message; - } else { - $params = array('merge_commit_message' => $message); - } - - return $this->put($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_id).'/merge'), $params); - } - - /** - * @param int $project_id - * @param int $mr_id - * - * @return mixed - */ - public function showNotes($project_id, $mr_id) - { - return $this->get($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_id).'/notes')); - } - - /** - * @param int $project_id - * @param int $mr_id - * @param string $note - * @return mixed - */ - public function addNote($project_id, $mr_id, $note) - { - return $this->post($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_id).'/notes'), array( - 'body' => $note - )); - } - - /** - * @param int $projectId - * @param int $mrId - * @param int $noteId - * @return mixed - */ - public function removeNote($projectId, $mrId, $noteId) - { - return $this->delete($this->getProjectPath($projectId, 'merge_requests/'.$this->encodePath($mrId).'/notes/'.$this->encodePath($noteId))); - } - - /** - * @param int $project_id - * @param int $mr_id - * @return mixed - */ - public function showComments($project_id, $mr_id) - { - @trigger_error(sprintf('The %s() method is deprecated since version 9.1 and will be removed in 10.0. Use the showNotes() method instead.', __METHOD__), E_USER_DEPRECATED); - - return $this->showNotes($project_id, $mr_id); - } - - /** - * @param int $project_id - * @param int $mr_id - * @param string $note - * @return mixed - */ - public function addComment($project_id, $mr_id, $note) - { - @trigger_error(sprintf('The %s() method is deprecated since version 9.1 and will be removed in 10.0. Use the addNote() method instead.', __METHOD__), E_USER_DEPRECATED); - - return $this->addNote($project_id, $mr_id, $note); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @return mixed - */ - public function showDiscussions($project_id, $mr_iid) - { - return $this->get($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid)).'/discussions'); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @param string $discussion_id - * @return mixed - */ - public function showDiscussion($project_id, $mr_iid, $discussion_id) - { - return $this->get($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid)).'/discussions/'.$this->encodePath($discussion_id)); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @param array $params - * @return mixed - */ - public function addDiscussion($project_id, $mr_iid, array $params) - { - return $this->post($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid).'/discussions'), $params); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @param string $discussion_id - * @param bool $resolved - * @return mixed - */ - public function resolveDiscussion($project_id, $mr_iid, $discussion_id, $resolved = true) - { - return $this->put($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid).'/discussions/'.$this->encodePath($discussion_id)), array( - 'resolved' => $resolved - )); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @param string $discussion_id - * @param string|array $body - * @return mixed - */ - public function addDiscussionNote($project_id, $mr_iid, $discussion_id, $body) - { - // backwards compatibility - if (is_array($body)) { - $params = $body; - } else { - $params = array('body' => $body); - } - - return $this->post($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid).'/discussions/'.$this->encodePath($discussion_id).'/notes'), $params); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @param string $discussion_id - * @param int $note_id - * @param array $params - * @return mixed - */ - public function updateDiscussionNote($project_id, $mr_iid, $discussion_id, $note_id, array $params) - { - return $this->put($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid).'/discussions/'.$this->encodePath($discussion_id).'/notes/'.$this->encodePath($note_id)), $params); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @param string $discussion_id - * @param int $note_id - * @return mixed - */ - public function removeDiscussionNote($project_id, $mr_iid, $discussion_id, $note_id) - { - return $this->delete($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid).'/discussions/'.$this->encodePath($discussion_id).'/notes/'.$this->encodePath($note_id))); - } - - /** - * @param int $project_id - * @param int $mr_id - * @return mixed - */ - public function changes($project_id, $mr_id) - { - return $this->get($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_id).'/changes')); - } - - /** - * @param int $project_id - * @param int $mr_id - * @return mixed - */ - public function commits($project_id, $mr_id) - { - return $this->get($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_id).'/commits')); - } - - /** - * @param int $project_id - * @param int $mr_id - * @return mixed - */ - public function closesIssues($project_id, $mr_id) - { - return $this->get($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_id).'/closes_issues')); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @return mixed - */ - public function approvals($project_id, $mr_iid) - { - return $this->get($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid).'/approvals')); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @return mixed - */ - public function approve($project_id, $mr_iid) - { - return $this->post($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid).'/approve')); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @return mixed - */ - public function unapprove($project_id, $mr_iid) - { - return $this->post($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid).'/unapprove')); - } - - /** - * @param int $project_id - * @param int $mr_iid - * @return mixed - */ - public function awardEmoji($project_id, $mr_iid) - { - return $this->get($this->getProjectPath($project_id, 'merge_requests/'.$this->encodePath($mr_iid).'/award_emoji')); - } -} diff --git a/lib/Gitlab/Api/Projects.php b/lib/Gitlab/Api/Projects.php deleted file mode 100644 index 073a09c16..000000000 --- a/lib/Gitlab/Api/Projects.php +++ /dev/null @@ -1,894 +0,0 @@ -createOptionsResolver(); - $booleanNormalizer = function (Options $resolver, $value) { - return $value ? 'true' : 'false'; - }; - $resolver->setDefined('archived') - ->setAllowedTypes('archived', 'bool') - ->setNormalizer('archived', $booleanNormalizer) - ; - $resolver->setDefined('visibility') - ->setAllowedValues('visibility', ['public', 'internal', 'private']) - ; - $resolver->setDefined('order_by') - ->setAllowedValues('order_by', ['id', 'name', 'path', 'created_at', 'updated_at', 'last_activity_at']) - ; - $resolver->setDefined('sort') - ->setAllowedValues('sort', ['asc', 'desc']) - ; - $resolver->setDefined('search'); - $resolver->setDefined('simple') - ->setAllowedTypes('simple', 'bool') - ->setNormalizer('simple', $booleanNormalizer) - ; - $resolver->setDefined('owned') - ->setAllowedTypes('owned', 'bool') - ->setNormalizer('owned', $booleanNormalizer) - ; - $resolver->setDefined('membership') - ->setAllowedTypes('membership', 'bool') - ->setNormalizer('membership', $booleanNormalizer) - ; - $resolver->setDefined('starred') - ->setAllowedTypes('starred', 'bool') - ->setNormalizer('starred', $booleanNormalizer) - ; - $resolver->setDefined('statistics') - ->setAllowedTypes('statistics', 'bool') - ->setNormalizer('statistics', $booleanNormalizer) - ; - $resolver->setDefined('with_issues_enabled') - ->setAllowedTypes('with_issues_enabled', 'bool') - ->setNormalizer('with_issues_enabled', $booleanNormalizer) - ; - $resolver->setDefined('with_merge_requests_enabled') - ->setAllowedTypes('with_merge_requests_enabled', 'bool') - ->setNormalizer('with_merge_requests_enabled', $booleanNormalizer) - ; - $resolver->setDefined('min_access_level') - ->setAllowedValues('min_access_level', [null, 10, 20, 30, 40, 50]) - ; - - return $this->get('projects', $resolver->resolve($parameters)); - } - - /** - * @param int|string $project_id - * @param array $parameters { - * - * @var bool $statistics Include project statistics. - * @var bool $with_custom_attributes Include project custom attributes. - * } - * @return mixed - */ - public function show($project_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - $booleanNormalizer = function (Options $resolver, $value) { - return $value ? true : false; - }; - $resolver->setDefined('statistics') - ->setAllowedTypes('statistics', 'bool') - ->setNormalizer('statistics', $booleanNormalizer) - ; - $resolver->setDefined('with_custom_attributes') - ->setAllowedTypes('with_custom_attributes', 'bool') - ->setNormalizer('with_custom_attributes', $booleanNormalizer) - ; - return $this->get('projects/'.$this->encodePath($project_id), $resolver->resolve($parameters)); - } - - /** - * @param string $name - * @param array $params - * @return mixed - */ - public function create($name, array $params = array()) - { - $params['name'] = $name; - - return $this->post('projects', $params); - } - - /** - * @param int $user_id - * @param string $name - * @param array $params - * @return mixed - */ - public function createForUser($user_id, $name, array $params = array()) - { - $params['name'] = $name; - - return $this->post('projects/user/'.$this->encodePath($user_id), $params); - } - - /** - * @param int $project_id - * @param array $params - * @return mixed - */ - public function update($project_id, array $params) - { - return $this->put('projects/'.$this->encodePath($project_id), $params); - } - - /** - * @param int $project_id - * @return mixed - */ - public function remove($project_id) - { - return $this->delete('projects/'.$this->encodePath($project_id)); - } - - /** - * @param int $project_id - * @return mixed - */ - public function archive($project_id) - { - return $this->post("projects/".$this->encodePath($project_id)."/archive"); - } - - /** - * @param int $project_id - * @return mixed - */ - public function unarchive($project_id) - { - return $this->post("projects/".$this->encodePath($project_id)."/unarchive"); - } - - /** - * @param int $project_id - * @param array $parameters ( - * - * @var string $scope The scope of pipelines, one of: running, pending, finished, branches, tags. - * @var string $status The status of pipelines, one of: running, pending, success, failed, canceled, skipped. - * @var string $ref The ref of pipelines. - * @var string $sha The sha of pipelines. - * @var bool $yaml_errors Returns pipelines with invalid configurations. - * @var string $name The name of the user who triggered pipelines. - * @var string $username The username of the user who triggered pipelines. - * @var string $order_by Order pipelines by id, status, ref, or user_id (default: id). - * @var string $order Sort pipelines in asc or desc order (default: desc). - * ) - * @return mixed - */ - public function pipelines($project_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - $booleanNormalizer = function (Options $resolver, $value) { - return $value ? 'true' : 'false'; - }; - - $resolver->setDefined('scope') - ->setAllowedValues('scope', ['running', 'pending', 'finished', 'branches', 'tags']) - ; - $resolver->setDefined('status') - ->setAllowedValues('status', ['running', 'pending', 'success', 'failed', 'canceled', 'skipped']) - ; - $resolver->setDefined('ref'); - $resolver->setDefined('sha'); - $resolver->setDefined('yaml_errors') - ->setAllowedTypes('yaml_errors', 'bool') - ->setNormalizer('yaml_errors', $booleanNormalizer) - ; - $resolver->setDefined('name'); - $resolver->setDefined('username'); - $resolver->setDefined('order_by') - ->setAllowedValues('order_by', ['id', 'status', 'ref', 'user_id']) - ; - $resolver->setDefined('sort') - ->setAllowedValues('sort', ['asc', 'desc']) - ; - - return $this->get($this->getProjectPath($project_id, 'pipelines'), $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param int $pipeline_id - * @return mixed - */ - public function pipeline($project_id, $pipeline_id) - { - return $this->get($this->getProjectPath($project_id, 'pipelines/'.$this->encodePath($pipeline_id))); - } - - /** - * @param int $project_id - * @param string $commit_ref - * @return mixed - */ - public function createPipeline($project_id, $commit_ref) - { - return $this->post($this->getProjectPath($project_id, 'pipeline'), array( - 'ref' => $commit_ref)); - } - - /** - * @param int $project_id - * @param int $pipeline_id - * @return mixed - */ - public function retryPipeline($project_id, $pipeline_id) - { - return $this->post($this->getProjectPath($project_id, 'pipelines/'.$this->encodePath($pipeline_id)).'/retry'); - } - - /** - * @param int $project_id - * @param int $pipeline_id - * @return mixed - */ - public function cancelPipeline($project_id, $pipeline_id) - { - return $this->post($this->getProjectPath($project_id, 'pipelines/'.$this->encodePath($pipeline_id)).'/cancel'); - } - - /** - * @param $project_id - * @param $pipeline_id - * @return mixed - */ - public function deletePipeline($project_id, $pipeline_id) - { - return $this->delete($this->getProjectPath($project_id, 'pipelines/'.$this->encodePath($pipeline_id))); - } - - /** - * @param integer $project_id - * @param array $parameters - * @return mixed - */ - public function allMembers($project_id, $parameters = []) - { - $resolver = $this->createOptionsResolver(); - $resolver->setDefined('query'); - - return $this->get('projects/'.$this->encodePath($project_id).'/members/all', $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param array $parameters ( - * - * @var string $query The query you want to search members for. - * ) - * - * @throws MissingOptionsException If a required option is not provided - * - * @return mixed - */ - public function members($project_id, $parameters = []) - { - if (!is_array($parameters)) { - @trigger_error("Deprecated: String parameter of the members() function is deprecated.", E_USER_NOTICE); - $username_query = $parameters; - $parameters = array(); - if (!empty($username_query)) { - $parameters['query'] = $username_query; - } - } - - $resolver = $this->createOptionsResolver(); - - $resolver->setDefined('query') - ->setAllowedTypes('query', 'string') - ; - - return $this->get($this->getProjectPath($project_id, 'members'), $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param int $user_id - * @return mixed - */ - public function member($project_id, $user_id) - { - return $this->get($this->getProjectPath($project_id, 'members/'.$this->encodePath($user_id))); - } - - /** - * @param int $project_id - * @param int $user_id - * @param int $access_level - * @return mixed - */ - public function addMember($project_id, $user_id, $access_level) - { - return $this->post($this->getProjectPath($project_id, 'members'), array( - 'user_id' => $user_id, - 'access_level' => $access_level - )); - } - - /** - * @param int $project_id - * @param int $user_id - * @param int $access_level - * @return mixed - */ - public function saveMember($project_id, $user_id, $access_level) - { - return $this->put($this->getProjectPath($project_id, 'members/'.urldecode($user_id)), array( - 'access_level' => $access_level - )); - } - - /** - * @param int $project_id - * @param int $user_id - * @return mixed - */ - public function removeMember($project_id, $user_id) - { - return $this->delete($this->getProjectPath($project_id, 'members/'.urldecode($user_id))); - } - - /** - * @param int $project_id - * @param array $parameters - * @return mixed - */ - public function hooks($project_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - - return $this->get($this->getProjectPath($project_id, 'hooks'), $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param int $hook_id - * @return mixed - */ - public function hook($project_id, $hook_id) - { - return $this->get($this->getProjectPath($project_id, 'hooks/'.$this->encodePath($hook_id))); - } - - /** - * Get project issues. - * - * See https://docs.gitlab.com/ee/api/issues.html#list-project-issues for more info. - * - * @param int $project_id - * Project id. - * @param array $parameters - * Url parameters. For example: issue state (opened / closed). - * - * @return array - * List of project issues. - */ - public function issues($project_id, array $parameters = []) - { - return $this->get($this->getProjectPath($project_id, 'issues'), $parameters); - } - - /** - * Get projects board list. - * - * See https://docs.gitlab.com/ee/api/boards.html for more info. - * - * @param int $project_id - * Project id. - * - * @return array - * List of project boards. - */ - public function boards($project_id) - { - return $this->get($this->getProjectPath($project_id, 'boards')); - } - - /** - * @param int $project_id - * @param string $url - * @param array $params - * @return mixed - */ - public function addHook($project_id, $url, array $params = array()) - { - if (empty($params)) { - $params = array('push_events' => true); - } - - $params['url'] = $url; - - return $this->post($this->getProjectPath($project_id, 'hooks'), $params); - } - - /** - * @param int $project_id - * @param int $hook_id - * @param array $params - * @return mixed - */ - public function updateHook($project_id, $hook_id, array $params) - { - return $this->put($this->getProjectPath($project_id, 'hooks/'.$this->encodePath($hook_id)), $params); - } - - /** - * @param int $project_id - * @param int $hook_id - * @return mixed - */ - public function removeHook($project_id, $hook_id) - { - return $this->delete($this->getProjectPath($project_id, 'hooks/'.$this->encodePath($hook_id))); - } - - /** - * @param int $project_id - * @param mixed $namespace - * @return mixed - */ - public function transfer($project_id, $namespace) - { - return $this->put($this->getProjectPath($project_id, 'transfer'), ['namespace' => $namespace]); - } - - /** - * @param int $project_id - * @return mixed - */ - public function deployKeys($project_id) - { - return $this->get($this->getProjectPath($project_id, 'deploy_keys')); - } - - /** - * @param int $project_id - * @param int $key_id - * @return mixed - */ - public function deployKey($project_id, $key_id) - { - return $this->get($this->getProjectPath($project_id, 'deploy_keys/'.$this->encodePath($key_id))); - } - - /** - * @param int $project_id - * @param string $title - * @param string $key - * @param bool $canPush - * @return mixed - */ - public function addDeployKey($project_id, $title, $key, $canPush = false) - { - return $this->post($this->getProjectPath($project_id, 'deploy_keys'), array( - 'title' => $title, - 'key' => $key, - 'can_push' => $canPush - )); - } - - /** - * @param int $project_id - * @param int $key_id - * @return mixed - */ - public function deleteDeployKey($project_id, $key_id) - { - return $this->delete($this->getProjectPath($project_id, 'deploy_keys/'.$this->encodePath($key_id))); - } - - /** - * @param int $project_id - * @param int $key_id - * @return mixed - */ - public function enableDeployKey($project_id, $key_id) - { - return $this->post($this->getProjectPath($project_id, 'deploy_keys/'.$this->encodePath($key_id).'/enable')); - } - - /** - * @param int $project_id - * @param array $parameters ( - * - * @var string $action Include only events of a particular action type. - * @var string $target_type Include only events of a particular target type. - * @var \DateTimeInterface $before Include only events created before a particular date. - * @var \DateTimeInterface $after Include only events created after a particular date. - * @var string $sort Sort events in asc or desc order by created_at. Default is desc. - * ) - * - * @return mixed - */ - public function events($project_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - $datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) { - return $value->format('Y-m-d'); - }; - - $resolver->setDefined('action') - ->setAllowedValues('action', ['created', 'updated', 'closed', 'reopened', 'pushed', 'commented', 'merged', 'joined', 'left', 'destroyed', 'expired']) - ; - $resolver->setDefined('target_type') - ->setAllowedValues('target_type', ['issue', 'milestone', 'merge_request', 'note', 'project', 'snippet', 'user']) - ; - $resolver->setDefined('before') - ->setAllowedTypes('before', \DateTimeInterface::class) - ->setNormalizer('before', $datetimeNormalizer); - $resolver->setDefined('after') - ->setAllowedTypes('after', \DateTimeInterface::class) - ->setNormalizer('after', $datetimeNormalizer) - ; - $resolver->setDefined('sort') - ->setAllowedValues('sort', ['asc', 'desc']) - ; - - return $this->get($this->getProjectPath($project_id, 'events'), $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param array $parameters - * @return mixed - */ - public function labels($project_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - - return $this->get($this->getProjectPath($project_id, 'labels'), $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param array $params - * @return mixed - */ - public function addLabel($project_id, array $params) - { - return $this->post($this->getProjectPath($project_id, 'labels'), $params); - } - - /** - * @param int $project_id - * @param array $params - * @return mixed - */ - public function updateLabel($project_id, array $params) - { - return $this->put($this->getProjectPath($project_id, 'labels'), $params); - } - - /** - * @param int $project_id - * @param string $name - * @return mixed - */ - public function removeLabel($project_id, $name) - { - return $this->delete($this->getProjectPath($project_id, 'labels'), array( - 'name' => $name - )); - } - - /** - * Get languages used in a project with percentage value. - * - * @param int $project_id - * @return mixed - */ - public function languages($project_id) - { - return $this->get($this->getProjectPath($project_id, 'languages')); - } - - /** - * @param int $project_id - * @param array $params ( - * - * @var string $namespace The ID or path of the namespace that the project will be forked to - * @var string $path The path of the forked project (optional) - * @var string $name The name of the forked project (optional) - * ) - * @return mixed - */ - public function fork($project_id, array $parameters = []) - { - $resolver = new OptionsResolver(); - $resolver->setDefined(['namespace', 'path', 'name']); - - $resolved = $resolver->resolve($parameters); - - return $this->post($this->getProjectPath($project_id, 'fork'), $resolved); - } - - /** - * @param int $project_id - * @param int $forked_project_id - * @return mixed - */ - public function createForkRelation($project_id, $forked_project_id) - { - return $this->post($this->getProjectPath($project_id, 'fork/'.$this->encodePath($forked_project_id))); - } - - /** - * @param int $project_id - * @return mixed - */ - public function removeForkRelation($project_id) - { - return $this->delete($this->getProjectPath($project_id, 'fork')); - } - - /** - * @param int $project_id - * @param string $service_name - * @param array $params - * @return mixed - */ - public function setService($project_id, $service_name, array $params = array()) - { - return $this->put($this->getProjectPath($project_id, 'services/'.$this->encodePath($service_name)), $params); - } - - /** - * @param int $project_id - * @param string $service_name - * @return mixed - */ - public function removeService($project_id, $service_name) - { - return $this->delete($this->getProjectPath($project_id, 'services/'.$this->encodePath($service_name))); - } - - /** - * @param int $project_id - * @param array $parameters - * @return mixed - */ - public function variables($project_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - - return $this->get($this->getProjectPath($project_id, 'variables'), $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param string $key - * @return mixed - */ - public function variable($project_id, $key) - { - return $this->get($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key))); - } - - /** - * @param int $project_id - * @param string $key - * @param string $value - * @param bool $protected - * @param string $environment_scope - * @return mixed - */ - public function addVariable($project_id, $key, $value, $protected = null, $environment_scope = null) - { - $payload = array( - 'key' => $key, - 'value' => $value, - ); - - if ($protected) { - $payload['protected'] = $protected; - } - - if ($environment_scope) { - $payload['environment_scope'] = $environment_scope; - } - - return $this->post($this->getProjectPath($project_id, 'variables'), $payload); - } - - /** - * @param int $project_id - * @param string $key - * @param string $value - * @param bool $protected - * @param string $environment_scope - * @return mixed - */ - public function updateVariable($project_id, $key, $value, $protected = null, $environment_scope = null) - { - $payload = array( - 'value' => $value, - ); - - if ($protected) { - $payload['protected'] = $protected; - } - - if ($environment_scope) { - $payload['environment_scope'] = $environment_scope; - } - - return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), $payload); - } - - /** - * @param int $project_id - * @param string $key - * @return mixed - */ - public function removeVariable($project_id, $key) - { - return $this->delete($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key))); - } - - /** - * @param int $project_id - * @param string $file - * @return mixed - */ - public function uploadFile($project_id, $file) - { - return $this->post($this->getProjectPath($project_id, 'uploads'), array(), array(), array('file' => $file)); - } - - /** - * @param int $project_id - * @param array $parameters - * @return mixed - */ - public function deployments($project_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - - return $this->get($this->getProjectPath($project_id, 'deployments'), $resolver->resolve($parameters)); - } - - /** - * @param int $project_id - * @param int $deployment_id - * @return mixed - */ - public function deployment($project_id, $deployment_id) - { - return $this->get($this->getProjectPath($project_id, 'deployments/'.$this->encodePath($deployment_id))); - } - - /** - * @param mixed $project_id - * @param array $parameters - * @return mixed - */ - public function addShare($project_id, array $parameters = []) - { - $resolver = $this->createOptionsResolver(); - - $datetimeNormalizer = function (OptionsResolver $optionsResolver, \DateTimeInterface $value) { - return $value->format('Y-m-d'); - }; - - $resolver->setRequired('group_id') - ->setAllowedTypes('group_id', 'int'); - - $resolver->setRequired('group_access') - ->setAllowedTypes('group_access', 'int') - ->setAllowedValues('group_access', [0,10,20,30,40,50]); - - $resolver->setDefined('expires_at') - ->setAllowedTypes('expires_at', \DateTimeInterface::class) - ->setNormalizer('expires_at', $datetimeNormalizer) - ; - - return $this->post($this->getProjectPath($project_id, 'share'), $resolver->resolve($parameters)); - } - - /** - * @param mixed $project_id - * @param int $group_id - * @return mixed - */ - public function removeShare($project_id, $group_id) - { - return $this->delete($this->getProjectPath($project_id, 'share/' . $group_id)); - } - - /** - * @param int $project_id - * @return mixed - */ - public function badges($project_id) - { - return $this->get($this->getProjectPath($project_id, 'badges')); - } - - /** - * @param int $project_id - * @param string $badge_id - * @return mixed - */ - public function badge($project_id, $badge_id) - { - return $this->get($this->getProjectPath($project_id, 'badges/' . $this->encodePath($badge_id))); - } - - /** - * @param int $project_id - * @param array $params - * @return mixed - */ - public function addBadge($project_id, array $params = array()) - { - return $this->post($this->getProjectPath($project_id, 'badges'), $params); - } - - /** - * @param int $project_id - * @param string $badge_id - * @return mixed - */ - public function removeBadge($project_id, $badge_id) - { - return $this->delete($this->getProjectPath($project_id, 'badges/' . $this->encodePath($badge_id))); - } - - /** - * @param int $project_id - * @param string $badge_id - * @param array $params - * @return mixed - */ - public function updateBadge($project_id, $badge_id, array $params = array()) - { - return $this->put($this->getProjectPath($project_id, 'badges/' . $this->encodePath($badge_id))); - } - - - /** - * @param int $project_id - * @param array $params - * @return mixed - */ - public function addProtectedBranch($project_id, array $params = []) - { - return $this->post($this->getProjectPath($project_id, 'protected_branches'), $params); - } -} diff --git a/lib/Gitlab/Api/Schedules.php b/lib/Gitlab/Api/Schedules.php deleted file mode 100644 index f08938f60..000000000 --- a/lib/Gitlab/Api/Schedules.php +++ /dev/null @@ -1,56 +0,0 @@ -post($this->getProjectPath($project_id, 'pipeline_schedules'), $params); - } - - /** - * @param int $project_id - * @param int $schedule_id - * @return mixed - */ - public function show($project_id, $schedule_id) - { - return $this->get($this->getProjectPath($project_id, 'pipeline_schedules/'.$this->encodePath($schedule_id))); - } - - /** - * @param int $project_id - * @return mixed - */ - public function showAll($project_id) - { - return $this->get($this->getProjectPath($project_id, 'pipeline_schedules')); - } - - /** - * @param int $project_id - * @param int $schedule_id - * @param array $params - * @return mixed - */ - public function update($project_id, $schedule_id, array $params) - { - return $this->put($this->getProjectPath($project_id, 'pipeline_schedules/'.$this->encodePath($schedule_id)), $params); - } - - /** - * @param int $project_id - * @param int $schedule_id - * @return mixed - */ - public function remove($project_id, $schedule_id) - { - return $this->delete($this->getProjectPath($project_id, 'pipeline_schedules/'.$this->encodePath($schedule_id))); - } -} diff --git a/lib/Gitlab/Api/Snippets.php b/lib/Gitlab/Api/Snippets.php deleted file mode 100644 index 4f4aa19d7..000000000 --- a/lib/Gitlab/Api/Snippets.php +++ /dev/null @@ -1,83 +0,0 @@ -get($this->getProjectPath($project_id, 'snippets')); - } - - /** - * @param int $project_id - * @param int $snippet_id - * @return mixed - */ - public function show($project_id, $snippet_id) - { - return $this->get($this->getProjectPath($project_id, 'snippets/'.$this->encodePath($snippet_id))); - } - - /** - * @param int $project_id - * @param string $title - * @param string $filename - * @param string $code - * @param string $visibility - * @return mixed - */ - public function create($project_id, $title, $filename, $code, $visibility) - { - return $this->post($this->getProjectPath($project_id, 'snippets'), array( - 'title' => $title, - 'file_name' => $filename, - 'code' => $code, - 'visibility' => $visibility - )); - } - - /** - * @param int $project_id - * @param int $snippet_id - * @param array $params - * @return mixed - */ - public function update($project_id, $snippet_id, array $params) - { - return $this->put($this->getProjectPath($project_id, 'snippets/'.$this->encodePath($snippet_id)), $params); - } - - /** - * @param int $project_id - * @param int $snippet_id - * @return string - */ - public function content($project_id, $snippet_id) - { - return $this->get($this->getProjectPath($project_id, 'snippets/'.$this->encodePath($snippet_id).'/raw')); - } - - /** - * @param int $project_id - * @param int $snippet_id - * @return mixed - */ - public function remove($project_id, $snippet_id) - { - return $this->delete($this->getProjectPath($project_id, 'snippets/'.$this->encodePath($snippet_id))); - } - - /** - * @param int $project_id - * @param int $snippet_id - * - * @return mixed - */ - public function awardEmoji($project_id, $snippet_id) - { - return $this->get($this->getProjectPath($project_id, 'snippets/'.$this->encodePath($snippet_id).'/award_emoji')); - } -} diff --git a/lib/Gitlab/Api/SystemHooks.php b/lib/Gitlab/Api/SystemHooks.php deleted file mode 100644 index b21fd0908..000000000 --- a/lib/Gitlab/Api/SystemHooks.php +++ /dev/null @@ -1,41 +0,0 @@ -get('hooks'); - } - - /** - * @param string $url - * @return mixed - */ - public function create($url) - { - return $this->post('hooks', array( - 'url' => $url - )); - } - - /** - * @param int $id - * @return mixed - */ - public function test($id) - { - return $this->get('hooks/'.$this->encodePath($id)); - } - - /** - * @param int $id - * @return mixed - */ - public function remove($id) - { - return $this->delete('hooks/'.$this->encodePath($id)); - } -} diff --git a/lib/Gitlab/Api/Tags.php b/lib/Gitlab/Api/Tags.php deleted file mode 100644 index 7349f09ea..000000000 --- a/lib/Gitlab/Api/Tags.php +++ /dev/null @@ -1,65 +0,0 @@ -get($this->getProjectPath($project_id, 'repository/tags')); - } - - /** - * @param int $project_id - * @param string $tag_name - * @return mixed - */ - public function show($project_id, $tag_name) - { - return $this->get($this->getProjectPath($project_id, 'repository/tags/'.$this->encodePath($tag_name))); - } - - /** - * @param int $project_id - * @param array $params - * @return mixed - */ - public function create($project_id, array $params = array()) - { - return $this->post($this->getProjectPath($project_id, 'repository/tags'), $params); - } - - /** - * @param int $project_id - * @param string $tag_name - * @return mixed - */ - public function remove($project_id, $tag_name) - { - return $this->delete($this->getProjectPath($project_id, 'repository/tags/'.$this->encodePath($tag_name))); - } - - /** - * @param int $project_id - * @param string $tag_name - * @param array $params - * @return mixed - */ - public function createRelease($project_id, $tag_name, array $params = array()) - { - return $this->post($this->getProjectPath($project_id, 'repository/tags/'.$this->encodePath($tag_name).'/release'), $params); - } - - /** - * @param int $project_id - * @param string $tag_name - * @param array $params - * @return mixed - */ - public function updateRelease($project_id, $tag_name, array $params = array()) - { - return $this->put($this->getProjectPath($project_id, 'repository/tags/'.$this->encodePath($tag_name).'/release'), $params); - } -} diff --git a/lib/Gitlab/Api/Users.php b/lib/Gitlab/Api/Users.php deleted file mode 100644 index 009aac1bc..000000000 --- a/lib/Gitlab/Api/Users.php +++ /dev/null @@ -1,351 +0,0 @@ -createOptionsResolver(); - $datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) { - return $value->format('c'); - }; - - $resolver->setDefined('search'); - $resolver->setDefined('username'); - $resolver->setDefined('external') - ->setAllowedTypes('external', 'bool') - ; - $resolver->setDefined('extern_uid'); - $resolver->setDefined('provider'); - $resolver->setDefined('created_before') - ->setAllowedTypes('created_before', \DateTimeInterface::class) - ->setNormalizer('created_before', $datetimeNormalizer) - ; - $resolver->setDefined('created_after') - ->setAllowedTypes('created_after', \DateTimeInterface::class) - ->setNormalizer('created_after', $datetimeNormalizer) - ; - $resolver->setDefined('active') - ->setAllowedTypes('active', 'bool') - ->setAllowedValues('active', true) - ; - $resolver->setDefined('blocked') - ->setAllowedTypes('blocked', 'bool') - ->setAllowedValues('blocked', true) - ; - - - return $this->get('users', $resolver->resolve($parameters)); - } - - /** - * @param int $id - * @return mixed - */ - public function show($id) - { - return $this->get('users/'.$this->encodePath($id)); - } - - /** - * @param int $id - * @return mixed - */ - public function usersProjects($id) - { - return $this->get('users/'.$this->encodePath($id).'/projects'); - } - - /** - * @return mixed - */ - public function user() - { - return $this->get('user'); - } - - /** - * @param string $email - * @param string $password - * @param array $params - * @return mixed - */ - public function create($email, $password, array $params = array()) - { - $params['email'] = $email; - $params['password'] = $password; - - return $this->post('users', $params); - } - - /** - * @param int $id - * @param array $params - * @param array $files - * @return mixed - */ - public function update($id, array $params, array $files = array()) - { - return $this->put('users/'.$this->encodePath($id), $params, array(), $files); - } - - /** - * @param int $id - * @return mixed - */ - public function remove($id) - { - return $this->delete('users/'.$this->encodePath($id)); - } - - /** - * @param int $id - * @return mixed - */ - public function block($id) - { - return $this->post('users/'.$this->encodePath($id).'/block'); - } - - /** - * @param int $id - * @return mixed - */ - public function unblock($id) - { - return $this->post('users/'.$this->encodePath($id).'/unblock'); - } - - /** - * @param string $emailOrUsername - * @param string $password - * @return mixed - */ - public function session($emailOrUsername, $password) - { - return $this->post('session', array( - 'login' => $emailOrUsername, - 'email' => $emailOrUsername, - 'password' => $password - )); - } - - /** - * @param string $email - * @param string $password - * @return mixed - */ - public function login($email, $password) - { - return $this->session($email, $password); - } - - /** - * @return mixed - */ - public function me() - { - return $this->get('user'); - } - - /** - * @return mixed - */ - public function keys() - { - return $this->get('user/keys'); - } - - /** - * @param int $id - * @return mixed - */ - public function key($id) - { - return $this->get('user/keys/'.$this->encodePath($id)); - } - - /** - * @param string $title - * @param string $key - * @return mixed - */ - public function createKey($title, $key) - { - return $this->post('user/keys', array( - 'title' => $title, - 'key' => $key - )); - } - - /** - * @param int $id - * @return mixed - */ - public function removeKey($id) - { - return $this->delete('user/keys/'.$this->encodePath($id)); - } - - /** - * @param int $user_id - * @return mixed - */ - public function userKeys($user_id) - { - return $this->get('users/'.$this->encodePath($user_id).'/keys'); - } - - /* - * @param int $user_id - * @param int $key_id - * @return mixed - */ - public function userKey($user_id, $key_id) - { - return $this->get('users/'.$this->encodePath($user_id).'/keys/'.$this->encodePath($key_id)); - } - - /** - * @param int $user_id - * @param string $title - * @param string $key - * @return mixed - */ - public function createKeyForUser($user_id, $title, $key) - { - return $this->post('users/'.$this->encodePath($user_id).'/keys', array( - 'title' => $title, - 'key' => $key - )); - } - - /** - * @param int $user_id - * @param int $key_id - * @return mixed - */ - public function removeUserKey($user_id, $key_id) - { - return $this->delete('users/'.$this->encodePath($user_id).'/keys/'.$this->encodePath($key_id)); - } - - /** - * @return mixed - */ - public function emails() - { - return $this->get('user/emails'); - } - - /** - * @param $id - * @return mixed - */ - public function email($id) - { - return $this->get('user/emails/'.$this->encodePath($id)); - } - - /** - * @param int $user_id - * @return mixed - */ - public function userEmails($user_id) - { - return $this->get('users/'.$this->encodePath($user_id).'/emails'); - } - - /** - * @param int $user_id - * @param string $email - * @param bool $skip_confirmation - * @return mixed - */ - public function createEmailForUser($user_id, $email, $skip_confirmation = false) - { - return $this->post('users/'.$this->encodePath($user_id).'/emails', array( - 'email' => $email, - 'skip_confirmation' => $skip_confirmation, - )); - } - - /** - * @param int $user_id - * @param int $email_id - * @return mixed - */ - public function removeUserEmail($user_id, $email_id) - { - return $this->delete('users/'.$this->encodePath($user_id).'/emails/'.$this->encodePath($email_id)); - } - - /** - * @param int $user_id - * @param array $params - * @return mixed - */ - public function userImpersonationTokens($user_id, array $params = []) - { - $resolver = $this->createOptionsResolver(); - - $resolver->setDefined('state') - ->setAllowedValues('state', ['all', 'active', 'inactive']) - ; - - return $this->get('users/'.$this->encodePath($user_id).'/impersonation_tokens', $resolver->resolve($params)); - } - - /** - * @param int $user_id - * @param int $impersonation_token_id - * @return mixed - */ - public function userImpersonationToken($user_id, $impersonation_token_id) - { - return $this->get('users/'.$this->encodePath($user_id).'/impersonation_tokens/'.$this->encodePath($impersonation_token_id)); - } - - /** - * @param int $user_id - * @param string $name - * @param array $scopes - * @param null $expires_at - * @return mixed - */ - public function createImpersonationToken($user_id, $name, array $scopes, $expires_at = null) - { - return $this->post('users/'.$this->encodePath($user_id).'/impersonation_tokens', array( - 'name' => $name, - 'scopes' => $scopes, - 'expires_at' => $expires_at - )); - } - - /** - * @param int $user_id - * @param int $impersonation_token_id - * @return mixed - */ - public function removeImpersonationToken($user_id, $impersonation_token_id) - { - return $this->delete('users/'.$this->encodePath($user_id).'/impersonation_tokens/'.$this->encodePath($impersonation_token_id)); - } -} diff --git a/lib/Gitlab/Api/Version.php b/lib/Gitlab/Api/Version.php deleted file mode 100644 index 59a58104b..000000000 --- a/lib/Gitlab/Api/Version.php +++ /dev/null @@ -1,9 +0,0 @@ -get('version'); - } -} diff --git a/lib/Gitlab/Client.php b/lib/Gitlab/Client.php deleted file mode 100644 index e89d775bf..000000000 --- a/lib/Gitlab/Client.php +++ /dev/null @@ -1,463 +0,0 @@ - - * - * @property-read \Gitlab\Api\Groups $groups - * @property-read \Gitlab\Api\Issues $issues - * @property-read \Gitlab\Api\Jobs $jobs - * @property-read \Gitlab\Api\MergeRequests $merge_requests - * @property-read \Gitlab\Api\MergeRequests $mr - * @property-read \Gitlab\Api\Milestones $milestones - * @property-read \Gitlab\Api\Milestones $ms - * @property-read \Gitlab\Api\ProjectNamespaces $namespaces - * @property-read \Gitlab\Api\ProjectNamespaces $ns - * @property-read \Gitlab\Api\Projects $projects - * @property-read \Gitlab\Api\Repositories $repositories - * @property-read \Gitlab\Api\Repositories $repo - * @property-read \Gitlab\Api\Snippets $snippets - * @property-read \Gitlab\Api\SystemHooks $hooks - * @property-read \Gitlab\Api\SystemHooks $system_hooks - * @property-read \Gitlab\Api\Users $users - * @property-read \Gitlab\Api\Keys $keys - * @property-read \Gitlab\Api\Tags $tags - * @property-read \Gitlab\Api\Version $version - */ -class Client -{ - /** - * Constant for authentication method. Indicates the default, but deprecated - * login with username and token in URL. - */ - const AUTH_URL_TOKEN = 'url_token'; - - /** - * Constant for authentication method. Indicates the new login method with - * with username and token via HTTP Authentication. - */ - const AUTH_HTTP_TOKEN = 'http_token'; - - /** - * Constant for authentication method. Indicates the OAuth method with a key - * obtain using Gitlab's OAuth provider. - */ - const AUTH_OAUTH_TOKEN = 'oauth_token'; - - /** - * @var History - */ - private $responseHistory; - - /** - * @var Builder - */ - private $httpClientBuilder; - - /** - * Instantiate a new Gitlab client - * - * @param Builder $httpClientBuilder - */ - public function __construct(Builder $httpClientBuilder = null) - { - $this->responseHistory = new History(); - $this->httpClientBuilder = $httpClientBuilder ?: new Builder(); - - $this->httpClientBuilder->addPlugin(new GitlabExceptionThrower()); - $this->httpClientBuilder->addPlugin(new HistoryPlugin($this->responseHistory)); - $this->httpClientBuilder->addPlugin(new HeaderDefaultsPlugin([ - 'User-Agent' => 'php-gitlab-api (http://github.com/m4tthumphrey/php-gitlab-api)', - ])); - $this->httpClientBuilder->addPlugin(new RedirectPlugin()); - $this->httpClientBuilder->addPlugin(new ApiVersion()); - - $this->setUrl('https://gitlab.com'); - } - - /** - * Create a Gitlab\Client using an url. - * - * @param string $url - * - * @return Client - */ - public static function create($url) - { - $client = new self(); - $client->setUrl($url); - - return $client; - } - - /** - * Create a Gitlab\Client using an HttpClient. - * - * @param HttpClient $httpClient - * - * @return Client - */ - public static function createWithHttpClient(HttpClient $httpClient) - { - $builder = new Builder($httpClient); - - return new self($builder); - } - - /** - * @return Api\DeployKeys - */ - public function deployKeys() - { - return new Api\DeployKeys($this); - } - - /** - * @return Api\Groups - */ - public function groups() - { - return new Api\Groups($this); - } - - /** - * @return Api\GroupsMilestones - */ - public function groupsMilestones() - { - return new Api\GroupsMilestones($this); - } - - /** - * @return Api\Issues - */ - public function issues() - { - return new Api\Issues($this); - } - - /** - * @return Api\IssueBoards - */ - public function issueBoards() - { - return new Api\IssueBoards($this); - } - - /** - * @return Api\GroupsBoards - */ - public function groupsBoards() - { - return new Api\GroupsBoards($this); - } - - - /** - * @return Api\IssueLinks - */ - public function issueLinks() - { - return new Api\IssueLinks($this); - } - - /** - * @return Api\Jobs - */ - public function jobs() - { - return new Api\Jobs($this); - } - - /** - * @return Api\MergeRequests - */ - public function mergeRequests() - { - return new Api\MergeRequests($this); - } - - /** - * @return Api\Milestones - */ - public function milestones() - { - return new Api\Milestones($this); - } - - /** - * @return Api\ProjectNamespaces - */ - public function namespaces() - { - return new Api\ProjectNamespaces($this); - } - - /** - * @return Api\Projects - */ - public function projects() - { - return new Api\Projects($this); - } - - /** - * @return Api\Repositories - */ - public function repositories() - { - return new Api\Repositories($this); - } - - /** - * @return Api\RepositoryFiles - */ - public function repositoryFiles() - { - return new Api\RepositoryFiles($this); - } - - /** - * @return Api\Snippets - */ - public function snippets() - { - return new Api\Snippets($this); - } - - /** - * @return Api\SystemHooks - */ - public function systemHooks() - { - return new Api\SystemHooks($this); - } - - /** - * @return Api\Users - */ - public function users() - { - return new Api\Users($this); - } - - /** - * @return Api\Keys - */ - public function keys() - { - return new Api\Keys($this); - } - - /** - * @return Api\Tags - */ - public function tags() - { - return new Api\Tags($this); - } - - /** - * @return Api\Version - */ - public function version() - { - return new Api\Version($this); - } - - /** - * @return Api\Deployments - */ - public function deployments() - { - return new Api\Deployments($this); - } - - /** - * @return Api\Environments - */ - public function environments() - { - return new Api\Environments($this); - } - - /** - * @return Api\Schedules - */ - public function schedules() - { - return new Api\Schedules($this); - } - - /** - * @return Api\IssuesStatistics - */ - public function issuesStatistics() - { - return new Api\IssuesStatistics($this); - } - - /** - * @param string $name - * - * @return AbstractApi|mixed - * @throws InvalidArgumentException - */ - public function api($name) - { - switch ($name) { - - case 'deploy_keys': - return $this->deployKeys(); - - case 'groups': - return $this->groups(); - - case 'groupsMilestones': - return $this->groupsMilestones(); - - case 'issues': - return $this->issues(); - - case 'board': - case 'issue_boards': - return $this->issueBoards(); - - case 'group_boards': - return $this->groupsBoards(); - - case 'issue_links': - return $this->issueLinks(); - - case 'jobs': - return $this->jobs(); - - case 'mr': - case 'merge_requests': - return $this->mergeRequests(); - - case 'milestones': - case 'ms': - return $this->milestones(); - - case 'namespaces': - case 'ns': - return $this->namespaces(); - - case 'projects': - return $this->projects(); - - case 'repo': - case 'repositories': - return $this->repositories(); - - case 'repositoryFiles': - return $this->repositoryFiles(); - - case 'snippets': - return $this->snippets(); - - case 'hooks': - case 'system_hooks': - return $this->systemHooks(); - - case 'users': - return $this->users(); - - case 'keys': - return $this->keys(); - - case 'tags': - return $this->tags(); - - case 'version': - return $this->version(); - - case 'environments': - return $this->environments(); - - case 'deployments': - return $this->deployments(); - - case 'schedules': - return $this->schedules(); - - case 'issues_statistics': - return $this->issuesStatistics(); - - - default: - throw new InvalidArgumentException('Invalid endpoint: "'.$name.'"'); - } - } - - /** - * Authenticate a user for all next requests - * - * @param string $token Gitlab private token - * @param string $authMethod One of the AUTH_* class constants - * @param string $sudo - * @return $this - */ - public function authenticate($token, $authMethod = self::AUTH_URL_TOKEN, $sudo = null) - { - $this->httpClientBuilder->removePlugin(Authentication::class); - $this->httpClientBuilder->addPlugin(new Authentication($authMethod, $token, $sudo)); - - return $this; - } - - /** - * @param string $url - * - * @return $this - */ - public function setUrl($url) - { - $this->httpClientBuilder->removePlugin(AddHostPlugin::class); - $this->httpClientBuilder->addPlugin(new AddHostPlugin(UriFactoryDiscovery::find()->createUri($url))); - - return $this; - } - - /** - * @param string $api - * @return AbstractApi - */ - public function __get($api) - { - return $this->api($api); - } - - /** - * @return HttpMethodsClient - */ - public function getHttpClient() - { - return $this->httpClientBuilder->getHttpClient(); - } - - /** - * @return History - */ - public function getResponseHistory() - { - return $this->responseHistory; - } -} diff --git a/lib/Gitlab/Exception/ErrorException.php b/lib/Gitlab/Exception/ErrorException.php deleted file mode 100644 index 4bca15dc2..000000000 --- a/lib/Gitlab/Exception/ErrorException.php +++ /dev/null @@ -1,10 +0,0 @@ - - */ -class ErrorException extends \ErrorException implements ExceptionInterface -{ -} diff --git a/lib/Gitlab/Exception/ExceptionInterface.php b/lib/Gitlab/Exception/ExceptionInterface.php deleted file mode 100644 index 81403dcf8..000000000 --- a/lib/Gitlab/Exception/ExceptionInterface.php +++ /dev/null @@ -1,12 +0,0 @@ - - */ -interface ExceptionInterface extends Exception -{ -} diff --git a/lib/Gitlab/Exception/InvalidArgumentException.php b/lib/Gitlab/Exception/InvalidArgumentException.php deleted file mode 100644 index f430ab9e5..000000000 --- a/lib/Gitlab/Exception/InvalidArgumentException.php +++ /dev/null @@ -1,10 +0,0 @@ - - */ -class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface -{ -} diff --git a/lib/Gitlab/Exception/MissingArgumentException.php b/lib/Gitlab/Exception/MissingArgumentException.php deleted file mode 100644 index 93a5451e5..000000000 --- a/lib/Gitlab/Exception/MissingArgumentException.php +++ /dev/null @@ -1,18 +0,0 @@ - - */ -class MissingArgumentException extends ErrorException implements ExceptionInterface -{ - public function __construct($required, $code = 0, $previous = null) - { - if (is_string($required)) { - $required = array($required); - } - - parent::__construct(sprintf('One or more of required ("%s") parameters is missing!', implode('", "', $required)), $code, $previous); - } -} diff --git a/lib/Gitlab/Exception/RuntimeException.php b/lib/Gitlab/Exception/RuntimeException.php deleted file mode 100644 index 0a12b500c..000000000 --- a/lib/Gitlab/Exception/RuntimeException.php +++ /dev/null @@ -1,10 +0,0 @@ - - */ -class RuntimeException extends \RuntimeException implements ExceptionInterface -{ -} diff --git a/lib/Gitlab/Exception/ValidationFailedException.php b/lib/Gitlab/Exception/ValidationFailedException.php deleted file mode 100644 index 066b9f855..000000000 --- a/lib/Gitlab/Exception/ValidationFailedException.php +++ /dev/null @@ -1,10 +0,0 @@ - - */ -class ValidationFailedException extends ErrorException implements ExceptionInterface -{ -} diff --git a/lib/Gitlab/HttpClient/Message/QueryStringBuilder.php b/lib/Gitlab/HttpClient/Message/QueryStringBuilder.php deleted file mode 100644 index beaefb4a3..000000000 --- a/lib/Gitlab/HttpClient/Message/QueryStringBuilder.php +++ /dev/null @@ -1,80 +0,0 @@ -hydrate($data); - } - - /** - * @param Project $project - * @param Client $client - */ - public function __construct(Project $project, Client $client = null) - { - $this->setClient($client); - $this->setData('project', $project); - } -} diff --git a/lib/Gitlab/Model/File.php b/lib/Gitlab/Model/File.php deleted file mode 100644 index b2edac127..000000000 --- a/lib/Gitlab/Model/File.php +++ /dev/null @@ -1,47 +0,0 @@ -hydrate($data); - } - - /** - * @param Project $project - * @param string $file_path - * @param Client $client - */ - public function __construct(Project $project, $file_path = null, Client $client = null) - { - $this->setClient($client); - $this->setData('project', $project); - $this->setData('file_path', $file_path); - } -} diff --git a/lib/Gitlab/Model/Hook.php b/lib/Gitlab/Model/Hook.php deleted file mode 100644 index d276982e2..000000000 --- a/lib/Gitlab/Model/Hook.php +++ /dev/null @@ -1,76 +0,0 @@ -hydrate($data); - } - - /** - * @param Client $client - * @param string $url - * @return Hook - */ - public static function create(Client $client, $url) - { - $data = $client->systemHooks()->create($url); - - return static::fromArray($client, $data); - } - - /** - * @param int $id - * @param Client $client - */ - public function __construct($id, Client $client = null) - { - $this->setClient($client); - $this->setData('id', $id); - } - - /** - * @return bool - */ - public function test() - { - $this->client->systemHooks()->test($this->id); - - return true; - } - - /** - * @return bool - */ - public function delete() - { - $this->client->systemHooks()->remove($this->id); - - return true; - } -} diff --git a/lib/Gitlab/Model/IssueLink.php b/lib/Gitlab/Model/IssueLink.php deleted file mode 100644 index c72f83a7f..000000000 --- a/lib/Gitlab/Model/IssueLink.php +++ /dev/null @@ -1,46 +0,0 @@ -hydrate($data); - } - - /** - * @param Issue $issue - * @param int|null $issue_link_id - * @param Client|null $client - */ - public function __construct(Issue $issue, $issue_link_id = null, Client $client = null) - { - $this->setClient($client); - $this->setData('issue', $issue); - $this->setData('issue_link_id', $issue_link_id); - } -} diff --git a/lib/Gitlab/Model/Key.php b/lib/Gitlab/Model/Key.php deleted file mode 100644 index 5e6aebfa5..000000000 --- a/lib/Gitlab/Model/Key.php +++ /dev/null @@ -1,44 +0,0 @@ -hydrate($data); - } - - /** - * @param Client $client - */ - public function __construct(Client $client = null) - { - $this->setClient($client); - } -} diff --git a/lib/Gitlab/Model/Label.php b/lib/Gitlab/Model/Label.php deleted file mode 100644 index 711f09d8a..000000000 --- a/lib/Gitlab/Model/Label.php +++ /dev/null @@ -1,45 +0,0 @@ -hydrate($data); - } - - /** - * @param Project $project - * @param Client $client - */ - public function __construct(Project $project, Client $client = null) - { - $this->setClient($client); - $this->setData('project', $project); - } -} diff --git a/lib/Gitlab/Model/Node.php b/lib/Gitlab/Model/Node.php deleted file mode 100644 index bb9451687..000000000 --- a/lib/Gitlab/Model/Node.php +++ /dev/null @@ -1,53 +0,0 @@ -hydrate($data); - } - - /** - * @param Project $project - * @param int $id - * @param Client $client - */ - public function __construct(Project $project, $id = null, Client $client) - { - $this->setClient($client); - $this->setData('project', $project); - $this->setData('id', $id); - } -} diff --git a/lib/Gitlab/Model/Noteable.php b/lib/Gitlab/Model/Noteable.php deleted file mode 100644 index 11e2cb650..000000000 --- a/lib/Gitlab/Model/Noteable.php +++ /dev/null @@ -1,36 +0,0 @@ -hydrate($data); - } - - /** - * @param Project $project - * @param int $id - * @param Client $client - */ - public function __construct(Project $project, $id = null, Client $client = null) - { - $this->setClient($client); - $this->setData('project', $project); - $this->setData('id', $id); - } -} diff --git a/lib/Gitlab/Model/Release.php b/lib/Gitlab/Model/Release.php deleted file mode 100644 index 6044c871a..000000000 --- a/lib/Gitlab/Model/Release.php +++ /dev/null @@ -1,43 +0,0 @@ -hydrate($data); - } - - /** - * @param Project $project - * @param int $id - * @param Client $client - */ - public function __construct(Client $client = null) - { - $this->setClient($client); - } -} diff --git a/lib/Gitlab/ResultPagerInterface.php b/lib/Gitlab/ResultPagerInterface.php deleted file mode 100644 index 0078580c1..000000000 --- a/lib/Gitlab/ResultPagerInterface.php +++ /dev/null @@ -1,68 +0,0 @@ - + + + + + + + + + + + src + tests + + + + + + + + 0 + + + + + + + + + + + + + + + ./src/* + + + */tests/* + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 000000000..361c31dc2 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,11 @@ +parameters: + level: max + paths: + - src + ignoreErrors: + - '#Cannot call method [a-zA-Z0-9-_]+\(\) on Gitlab\\Client\|null#' + - '#call_user_func_array expects callable#' + - '#Parameter \#1 \$response of static method Gitlab\\HttpClient\\Message\\ResponseMediator\:\:getPagination#' + - '#Method [a-zA-Z0-9-_::()\\]+ should return [a-zA-Z0-9-_::()\\]+ but returns Gitlab\\Model\\Model#' + - '#callback of function array_filter expects callable#' + - '#Http\\Message\\MultipartStream\\MultipartStreamBuilder\:\:addResource\(\) expects #' \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7351de836..a86a5bad4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,25 +1,19 @@ - - - - ./test/Gitlab/ - - + - - - ./lib/Gitlab/ - - + + + ./tests/unit + + + + + + ./lib/Gitlab/ + + diff --git a/src/Api/Api.php b/src/Api/Api.php new file mode 100644 index 000000000..b8f5c6edb --- /dev/null +++ b/src/Api/Api.php @@ -0,0 +1,12 @@ + - * @author Matt Humphrey - * @author Radu Topala - */ -abstract class AbstractApi implements ApiInterface +use const FILEINFO_MIME_TYPE; +use function basename; +use function class_exists; +use function count; +use function fopen; +use function rawurlencode; +use function str_replace; + +abstract class ApiBase implements Api { - /** - * The client - * - * @var Client - */ + /** @var Client */ protected $client; - /** - * @var StreamFactory - */ + /** @var StreamFactoryInterface */ private $streamFactory; - /** - * @param Client $client - * @param StreamFactory|null $streamFactory - */ - public function __construct(Client $client, StreamFactory $streamFactory = null) + public function __construct(Client $client, ?StreamFactoryInterface $streamFactory = null) { - $this->client = $client; - $this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find(); + $this->client = $client; + $this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory(); } - /** - * @return $this - * @codeCoverageIgnore - */ - public function configure() + public function configure(): self { return $this; } @@ -54,12 +44,10 @@ public function configure() /** * Performs a GET query and returns the response as a PSR-7 response object. * - * @param string $path - * @param array $parameters - * @param array $requestHeaders - * @return ResponseInterface + * @param mixed[] $parameters + * @param mixed[] $requestHeaders */ - protected function getAsResponse($path, array $parameters = array(), $requestHeaders = array()) + protected function getAsResponse(string $path, array $parameters = [], array $requestHeaders = []): ResponseInterface { $path = $this->preparePath($path, $parameters); @@ -67,32 +55,33 @@ protected function getAsResponse($path, array $parameters = array(), $requestHea } /** - * @param string $path - * @param array $parameters - * @param array $requestHeaders + * @param mixed[] $parameters + * @param mixed[] $requestHeaders + * * @return mixed */ - protected function get($path, array $parameters = array(), $requestHeaders = array()) + protected function get(string $path, array $parameters = [], array $requestHeaders = []) { return ResponseMediator::getContent($this->getAsResponse($path, $parameters, $requestHeaders)); } /** - * @param string $path - * @param array $parameters - * @param array $requestHeaders - * @param array $files + * @param mixed[] $parameters + * @param mixed[] $requestHeaders + * @param mixed[] $files + * * @return mixed */ - protected function post($path, array $parameters = array(), $requestHeaders = array(), array $files = array()) + protected function post(string $path, array $parameters = [], array $requestHeaders = [], array $files = []) { $path = $this->preparePath($path); $body = null; - if (empty($files) && !empty($parameters)) { - $body = $this->prepareBody($parameters); + if ($files === [] && $parameters !== []) { + $body = $this->prepareBody($parameters); $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; - } elseif (!empty($files)) { + } + if ($files !== []) { $builder = new MultipartStreamBuilder($this->streamFactory); foreach ($parameters as $name => $value) { @@ -108,8 +97,8 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar ]); } - $body = $builder->build(); - $requestHeaders['Content-Type'] = 'multipart/form-data; boundary='.$builder->getBoundary(); + $body = $builder->build(); + $requestHeaders['Content-Type'] = 'multipart/form-data; boundary=' . $builder->getBoundary(); } $response = $this->client->getHttpClient()->post($path, $requestHeaders, $body); @@ -118,20 +107,22 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar } /** - * @param string $path - * @param array $parameters - * @param array $requestHeaders + * @param mixed[] $parameters + * @param mixed[] $requestHeaders + * @param mixed[] $files + * * @return mixed */ - protected function put($path, array $parameters = array(), $requestHeaders = array(), array $files = array()) + protected function put(string $path, array $parameters = [], array $requestHeaders = [], array $files = []) { $path = $this->preparePath($path); $body = null; - if (empty($files) && !empty($parameters)) { - $body = $this->prepareBody($parameters); + if ($files === [] && $parameters !== []) { + $body = $this->prepareBody($parameters); $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; - } elseif (!empty($files)) { + } + if ($files !== []) { $builder = new MultipartStreamBuilder($this->streamFactory); foreach ($parameters as $name => $value) { @@ -147,8 +138,8 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr ]); } - $body = $builder->build(); - $requestHeaders['Content-Type'] = 'multipart/form-data; boundary='.$builder->getBoundary(); + $body = $builder->build(); + $requestHeaders['Content-Type'] = 'multipart/form-data; boundary=' . $builder->getBoundary(); } $response = $this->client->getHttpClient()->put($path, $requestHeaders, $body); @@ -157,12 +148,12 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr } /** - * @param string $path - * @param array $parameters - * @param array $requestHeaders + * @param mixed[] $parameters + * @param mixed[] $requestHeaders + * * @return mixed */ - protected function delete($path, array $parameters = array(), $requestHeaders = array()) + protected function delete(string $path, array $parameters = [], array $requestHeaders = []) { $path = $this->preparePath($path, $parameters); @@ -171,31 +162,17 @@ protected function delete($path, array $parameters = array(), $requestHeaders = return ResponseMediator::getContent($response); } - /** - * @param int $id - * @param string $path - * @return string - */ - protected function getProjectPath($id, $path) + protected function getProjectPath(int $id, string $path): string { - return 'projects/'.$this->encodePath($id).'/'.$path; + return 'projects/' . $this->encodePath((string)$id) . '/' . $path; } - /** - * @param int $id - * @param string $path - * @return string - */ - protected function getGroupPath($id, $path) + protected function getGroupPath(int $id, string $path): string { - return 'groups/'.$this->encodePath($id).'/'.$path; + return 'groups/' . $this->encodePath((string)$id) . '/' . $path; } - /** - * @param string $path - * @return string - */ - protected function encodePath($path) + protected function encodePath(string $path): string { $path = rawurlencode($path); @@ -204,61 +181,53 @@ protected function encodePath($path) /** * Create a new OptionsResolver with page and per_page options. - * - * @return OptionsResolver */ - protected function createOptionsResolver() + protected function createOptionsResolver(): OptionsResolver { $resolver = new OptionsResolver(); $resolver->setDefined('page') ->setAllowedTypes('page', 'int') - ->setAllowedValues('page', function ($value) { + ->setAllowedValues('page', static function ($value) { return $value > 0; - }) - ; + }); $resolver->setDefined('per_page') ->setAllowedTypes('per_page', 'int') - ->setAllowedValues('per_page', function ($value) { + ->setAllowedValues('per_page', static function ($value) { return $value > 0 && $value <= 100; - }) - ; + }); return $resolver; } /** - * @param array $parameters - * @return StreamInterface + * @param mixed[] $parameters */ - private function prepareBody(array $parameters = []) + private function prepareBody(array $parameters = []): StreamInterface { $raw = QueryStringBuilder::build($parameters); - $stream = $this->streamFactory->createStream($raw); - return $stream; + return $this->streamFactory->createStream($raw); } - private function preparePath($path, array $parameters = []) + /** + * @param mixed[] $parameters + */ + private function preparePath(string $path, array $parameters = []): string { if (count($parameters) > 0) { - $path .= '?'.QueryStringBuilder::build($parameters); + $path .= '?' . QueryStringBuilder::build($parameters); } return $path; } - /** - * @param $file - * - * @return string - */ - private function guessContentType($file) + private function guessContentType(string $file): string { - if (!class_exists(\finfo::class, false)) { + if (! class_exists(finfo::class, false)) { return 'application/octet-stream'; } - $finfo = new \finfo(FILEINFO_MIME_TYPE); + $finfo = new finfo(FILEINFO_MIME_TYPE); - return $finfo->file($file); + return $finfo->file($file) ?: ''; } } diff --git a/lib/Gitlab/Api/DeployKeys.php b/src/Api/DeployKeys.php similarity index 64% rename from lib/Gitlab/Api/DeployKeys.php rename to src/Api/DeployKeys.php index 32e0a82cb..c8db6054b 100644 --- a/lib/Gitlab/Api/DeployKeys.php +++ b/src/Api/DeployKeys.php @@ -1,9 +1,13 @@ -createOptionsResolver(); + return $this->get($this->getProjectPath($project_id, 'deployments'), $resolver->resolve($parameters)); } /** - * @param int $project_id - * @param string $deployment_id * @return mixed */ - public function show($project_id, $deployment_id) + public function show(int $project_id, int $deployment_id) { return $this->get($this->getProjectPath($project_id, 'deployments/' . $deployment_id)); } diff --git a/lib/Gitlab/Api/Environments.php b/src/Api/Environments.php similarity index 64% rename from lib/Gitlab/Api/Environments.php rename to src/Api/Environments.php index 28e40f511..862aa289b 100644 --- a/lib/Gitlab/Api/Environments.php +++ b/src/Api/Environments.php @@ -1,30 +1,35 @@ -createOptionsResolver(); + return $this->get($this->getProjectPath($project_id, 'environments'), $resolver->resolve($parameters)); } /** - * @param int $project_id - * @param array $parameters ( + * @param mixed[] $parameters ( * * @var string $name The name of the environment * @var string $external_url Place to link to for this environment * ) + * * @return mixed */ - public function create($project_id, array $parameters = array()) + public function create(int $project_id, array $parameters = []) { $resolver = new OptionsResolver(); $resolver->setDefined('name') @@ -37,22 +42,18 @@ public function create($project_id, array $parameters = array()) } /** - * @param int $project_id - * @param string $environment_id * @return mixed */ - public function remove($project_id, $environment_id) + public function remove(int $project_id, int $environment_id) { return $this->delete($this->getProjectPath($project_id, 'environments/' . $environment_id)); } /** - * @param int $project_id - * @param string $environment_id * @return mixed */ - public function stop($project_id, $environment_id) + public function stop(int $project_id, int $environment_id) { - return $this->post($this->getProjectPath($project_id, 'environments/'.$this->encodePath($environment_id).'/stop')); + return $this->post($this->getProjectPath($project_id, 'environments/' . $this->encodePath((string)$environment_id) . '/stop')); } } diff --git a/src/Api/Groups.php b/src/Api/Groups.php new file mode 100644 index 000000000..af2b03f6a --- /dev/null +++ b/src/Api/Groups.php @@ -0,0 +1,368 @@ +getGroupSearchResolver(); + + return $this->get('groups', $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function show(int $id) + { + return $this->get('groups/' . $this->encodePath((string)(string)$id)); + } + + /** + * @return mixed + */ + public function create( + string $name, + string $path, + ?string $description = null, + string $visibility = 'private', + ?bool $lfs_enabled = null, + ?bool $request_access_enabled = null, + ?int $parent_id = null, + ?int $shared_runners_minutes_limit = null + ) { + $params = [ + 'name' => $name, + 'path' => $path, + 'description' => $description, + 'visibility' => $visibility, + 'lfs_enabled' => $lfs_enabled, + 'request_access_enabled' => $request_access_enabled, + 'parent_id' => $parent_id, + 'shared_runners_minutes_limit' => $shared_runners_minutes_limit, + ]; + + return $this->post('groups', array_filter($params, 'strlen')); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function update(int $id, array $params) + { + return $this->put('groups/' . $this->encodePath((string)$id), $params); + } + + /** + * @return mixed + */ + public function remove(int $group_id) + { + return $this->delete('groups/' . $this->encodePath((string)$group_id)); + } + + /** + * @return mixed + */ + public function transfer(int $group_id, int $project_id) + { + return $this->post('groups/' . $this->encodePath((string)$group_id) . '/projects/' . $this->encodePath((string)$project_id)); + } + + /** + * @param string[] $parameters + * + * @return mixed + */ + public function allMembers(int $id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + $resolver->setDefined('query'); + + return $this->get('groups/' . $this->encodePath((string)$id) . '/members/all', $resolver->resolve($parameters)); + } + + /** + * @param string[] $parameters + * + * @return mixed + */ + public function members(int $id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + $resolver->setDefined('query'); + + return $this->get('groups/' . $this->encodePath((string)$id) . '/members', $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function addMember(int $group_id, int $user_id, int $access_level) + { + return $this->post('groups/' . $this->encodePath((string)$group_id) . '/members', [ + 'user_id' => $user_id, + 'access_level' => $access_level, + ]); + } + + /** + * @return mixed + */ + public function saveMember(int $group_id, int $user_id, int $access_level) + { + return $this->put('groups/' . $this->encodePath((string)$group_id) . '/members/' . $this->encodePath((string)$user_id), ['access_level' => $access_level]); + } + + /** + * @return mixed + */ + public function removeMember(int $group_id, int $user_id) + { + return $this->delete('groups/' . $this->encodePath((string)$group_id) . '/members/' . $this->encodePath((string)$user_id)); + } + + /** + * @param mixed[] $parameters + * archived Limit by archived status. + * visibility Limit by visibility public, internal, or private. + * order_by Return projects ordered by id, name, path, created_at, updated_at, or last_activity_at fields. + * Default is created_at. + * sort Return projects sorted in asc or desc order. Default is desc. + * search Return list of authorized projects matching the search criteria. + * simple Return only the ID, URL, name, and path of each project. + * owned Limit by projects owned by the current user. + * starred Limit by projects starred by the current user. + * with_issues_enabled Limit by projects with issues feature enabled. Default is false. + * with_merge_requests_enabled Limit by projects with merge requests feature enabled. Default is false. + * with_shared Include projects shared to this group. Default is true. + * include_subgroups Include projects in subgroups of this group. Default is false. + * with_custom_attributes Include custom attributes in response (admins only). + * + * @return mixed + */ + public function projects(int $id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + $booleanNormalizer = static function (Options $resolver, $value): string { + return $value ? 'true' : 'false'; + }; + + $resolver + ->setDefined('archived') + ->setAllowedTypes('archived', 'bool') + ->setNormalizer('archived', $booleanNormalizer); + $resolver + ->setDefined('visibility') + ->setAllowedValues('visibility', ['public', 'internal', 'private']); + $resolver + ->setDefined('order_by') + ->setAllowedValues('order_by', ['id', 'name', 'path', 'created_at', 'updated_at', 'last_activity_at']); + $resolver + ->setDefined('sort') + ->setAllowedValues('sort', ['asc', 'desc']); + $resolver->setDefined('search'); + $resolver + ->setDefined('simple') + ->setAllowedTypes('simple', 'bool') + ->setNormalizer('simple', $booleanNormalizer); + $resolver + ->setDefined('owned') + ->setAllowedTypes('owned', 'bool') + ->setNormalizer('owned', $booleanNormalizer); + $resolver + ->setDefined('starred') + ->setAllowedTypes('starred', 'bool') + ->setNormalizer('starred', $booleanNormalizer); + $resolver + ->setDefined('with_issues_enabled') + ->setAllowedTypes('with_issues_enabled', 'bool') + ->setNormalizer('with_issues_enabled', $booleanNormalizer); + $resolver + ->setDefined('with_merge_requests_enabled') + ->setAllowedTypes('with_merge_requests_enabled', 'bool') + ->setNormalizer('with_merge_requests_enabled', $booleanNormalizer); + $resolver + ->setDefined('with_shared') + ->setAllowedTypes('with_shared', 'bool') + ->setNormalizer('with_shared', $booleanNormalizer); + $resolver + ->setDefined('include_subgroups') + ->setAllowedTypes('include_subgroups', 'bool') + ->setNormalizer('include_subgroups', $booleanNormalizer); + $resolver + ->setDefined('with_custom_attributes') + ->setAllowedTypes('with_custom_attributes', 'bool') + ->setNormalizer('with_custom_attributes', $booleanNormalizer); + + return $this->get('groups/' . $this->encodePath((string)$id) . '/projects', $resolver->resolve($parameters)); + } + + /** + * @param mixed[] $parameters all_available Show all the groups you have access to. + * search Return list of authorized groups matching the search criteria. + * order_by Order groups by name or path. Default is name. + * sort Order groups in asc or desc order. Default is asc. + * statistics Include group statistics (admins only). + * owned Limit by groups owned by the current user. + * skip_groups Skip the group IDs passes. + * + * @return mixed + */ + public function subgroups(int $group_id, array $parameters = []) + { + $resolver = $this->getGroupSearchResolver(); + + return $this->get('groups/' . $this->encodePath((string)$group_id) . '/subgroups', $resolver->resolve($parameters)); + } + + /** + * @param mixed[] $parameters + * + * @return mixed + */ + public function labels(int $group_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + + return $this->get('groups/' . $this->encodePath((string)$group_id) . '/labels', $resolver->resolve($parameters)); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function addLabel(int $group_id, array $params) + { + return $this->post('groups/' . $this->encodePath((string)$group_id) . '/labels', $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function updateLabel(int $group_id, array $params) + { + return $this->put('groups/' . $this->encodePath((string)$group_id) . '/labels', $params); + } + + /** + * @return mixed + */ + public function removeLabel(int $group_id, string $name) + { + return $this->delete('groups/' . $this->encodePath((string)$group_id) . '/labels', ['name' => $name]); + } + + /** + * @param mixed[] $parameters + * + * @return mixed + */ + public function variables(int $group_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + + return $this->get($this->getGroupPath($group_id, 'variables'), $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function variable(int $group_id, string $key) + { + return $this->get($this->getGroupPath($group_id, 'variables/' . $this->encodePath((string)$key))); + } + + /** + * @return mixed + */ + public function addVariable(int $group_id, string $key, string $value, ?bool $protected = null) + { + $payload = [ + 'key' => $key, + 'value' => $value, + ]; + + if ($protected) { + $payload['protected'] = $protected; + } + + return $this->post($this->getGroupPath($group_id, 'variables'), $payload); + } + + /** + * @return mixed + */ + public function updateVariable(int $group_id, string $key, string $value, ?bool $protected = null) + { + $payload = ['value' => $value]; + + if ($protected) { + $payload['protected'] = $protected; + } + + return $this->put($this->getGroupPath($group_id, 'variables/' . $this->encodePath((string)$key)), $payload); + } + + /** + * @return mixed + */ + public function removeVariable(int $group_id, string $key) + { + return $this->delete($this->getGroupPath($group_id, 'variables/' . $this->encodePath((string)$key))); + } + + private function getGroupSearchResolver(): OptionsResolver + { + $resolver = $this->createOptionsResolver(); + $booleanNormalizer = static function (Options $resolver, $value) { + return $value ? 'true' : 'false'; + }; + + $resolver->setDefined('skip_groups') + ->setAllowedTypes('skip_groups', 'array') + ->setAllowedValues('skip_groups', static function (array $value) { + return count($value) === count(array_filter($value, 'is_int')); + }); + $resolver->setDefined('all_available') + ->setAllowedTypes('all_available', 'bool') + ->setNormalizer('all_available', $booleanNormalizer); + $resolver->setDefined('search'); + $resolver->setDefined('order_by') + ->setAllowedValues('order_by', ['name', 'path']); + $resolver->setDefined('sort') + ->setAllowedValues('sort', ['asc', 'desc']); + $resolver->setDefined('statistics') + ->setAllowedTypes('statistics', 'bool') + ->setNormalizer('statistics', $booleanNormalizer); + $resolver->setDefined('owned') + ->setAllowedTypes('owned', 'bool') + ->setNormalizer('owned', $booleanNormalizer); + + return $resolver; + } +} diff --git a/src/Api/GroupsBoards.php b/src/Api/GroupsBoards.php new file mode 100644 index 000000000..f8190a044 --- /dev/null +++ b/src/Api/GroupsBoards.php @@ -0,0 +1,102 @@ +createOptionsResolver(); + + $path = $group_id === null ? 'boards' : $this->getGroupPath($group_id, 'boards'); + + return $this->get($path, $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function show(int $group_id, int $board_id) + { + return $this->get($this->getGroupPath($group_id, 'boards/' . $this->encodePath((string)$board_id))); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function create(int $group_id, array $params) + { + return $this->post($this->getGroupPath($group_id, 'boards'), $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function update(int $group_id, int $board_id, array $params) + { + return $this->put($this->getGroupPath($group_id, 'boards/' . $this->encodePath((string)$board_id)), $params); + } + + /** + * @return mixed + */ + public function remove(int $group_id, int $board_id) + { + return $this->delete($this->getGroupPath($group_id, 'boards/' . $this->encodePath((string)$board_id))); + } + + /** + * @return mixed + */ + public function allLists(int $group_id, int $board_id) + { + return $this->get($this->getGroupPath($group_id, 'boards/' . $this->encodePath((string)$board_id) . '/lists')); + } + + /** + * @return mixed + */ + public function showList(int $group_id, int $board_id, int $list_id) + { + return $this->get($this->getGroupPath($group_id, 'boards/' . $this->encodePath((string)$board_id) . '/lists/' . $this->encodePath((string)$list_id))); + } + + /** + * @return mixed + */ + public function createList(int $group_id, int $board_id, int $label_id) + { + $params = ['label_id' => $label_id]; + + return $this->post($this->getGroupPath($group_id, 'boards/' . $this->encodePath((string)$board_id) . '/lists'), $params); + } + + /** + * @return mixed + */ + public function updateList(int $group_id, int $board_id, int $list_id, int $position) + { + $params = ['position' => $position]; + + return $this->put($this->getGroupPath($group_id, 'boards/' . $this->encodePath((string)$board_id) . '/lists/' . $this->encodePath((string)$list_id)), $params); + } + + /** + * @return mixed + */ + public function deleteList(int $group_id, int $board_id, int $list_id) + { + return $this->delete($this->getGroupPath($group_id, 'boards/' . $this->encodePath((string)$board_id) . '/lists/' . $this->encodePath((string)$list_id))); + } +} diff --git a/src/Api/GroupsMilestones.php b/src/Api/GroupsMilestones.php new file mode 100644 index 000000000..0d6cde796 --- /dev/null +++ b/src/Api/GroupsMilestones.php @@ -0,0 +1,88 @@ +createOptionsResolver(); + $resolver->setDefined('iids') + ->setAllowedTypes('iids', 'array') + ->setAllowedValues('iids', static function (array $value) { + return count($value) === count(array_filter($value, 'is_int')); + }); + $resolver->setDefined('state') + ->setAllowedValues('state', ['active', 'closed']); + $resolver->setDefined('search'); + + return $this->get($this->getGroupPath($group_id, 'milestones'), $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function show(int $group_id, int $milestone_id) + { + return $this->get($this->getGroupPath($group_id, 'milestones/' . $this->encodePath((string)$milestone_id))); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function create(int $group_id, array $params) + { + return $this->post($this->getGroupPath($group_id, 'milestones'), $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function update(int $group_id, int $milestone_id, array $params) + { + return $this->put($this->getGroupPath($group_id, 'milestones/' . $this->encodePath((string)$milestone_id)), $params); + } + + /** + * @return mixed + */ + public function remove(int $group_id, int $milestone_id) + { + return $this->delete($this->getGroupPath($group_id, 'milestones/' . $this->encodePath((string)$milestone_id))); + } + + /** + * @return mixed + */ + public function issues(int $group_id, int $milestone_id) + { + return $this->get($this->getGroupPath($group_id, 'milestones/' . $this->encodePath((string)$milestone_id) . '/issues')); + } + + /** + * @return mixed + */ + public function mergeRequests(int $group_id, int $milestone_id) + { + return $this->get($this->getGroupPath($group_id, 'milestones/' . $this->encodePath((string)$milestone_id) . '/merge_requests')); + } +} diff --git a/src/Api/IssueBoards.php b/src/Api/IssueBoards.php new file mode 100644 index 000000000..61daa266c --- /dev/null +++ b/src/Api/IssueBoards.php @@ -0,0 +1,102 @@ +createOptionsResolver(); + + $path = $project_id === null ? 'boards' : $this->getProjectPath($project_id, 'boards'); + + return $this->get($path, $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function show(int $project_id, int $board_id) + { + return $this->get($this->getProjectPath($project_id, 'boards/' . $this->encodePath((string)$board_id))); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function create(int $project_id, array $params) + { + return $this->post($this->getProjectPath($project_id, 'boards'), $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function update(int $project_id, int $board_id, array $params) + { + return $this->put($this->getProjectPath($project_id, 'boards/' . $this->encodePath((string)$board_id)), $params); + } + + /** + * @return mixed + */ + public function remove(int $project_id, int $board_id) + { + return $this->delete($this->getProjectPath($project_id, 'boards/' . $this->encodePath((string)$board_id))); + } + + /** + * @return mixed + */ + public function allLists(int $project_id, int $board_id) + { + return $this->get($this->getProjectPath($project_id, 'boards/' . $this->encodePath((string)$board_id) . '/lists')); + } + + /** + * @return mixed + */ + public function showList(int $project_id, int $board_id, int $list_id) + { + return $this->get($this->getProjectPath($project_id, 'boards/' . $this->encodePath((string)$board_id) . '/lists/' . $this->encodePath((string)$list_id))); + } + + /** + * @return mixed + */ + public function createList(int $project_id, int $board_id, int $label_id) + { + $params = ['label_id' => $label_id]; + + return $this->post($this->getProjectPath($project_id, 'boards/' . $this->encodePath((string)$board_id) . '/lists'), $params); + } + + /** + * @return mixed + */ + public function updateList(int $project_id, int $board_id, int $list_id, int $position) + { + $params = ['position' => $position]; + + return $this->put($this->getProjectPath($project_id, 'boards/' . $this->encodePath((string)$board_id) . '/lists/' . $this->encodePath((string)$list_id)), $params); + } + + /** + * @return mixed + */ + public function deleteList(int $project_id, int $board_id, int $list_id) + { + return $this->delete($this->getProjectPath($project_id, 'boards/' . $this->encodePath((string)$board_id) . '/lists/' . $this->encodePath((string)$list_id))); + } +} diff --git a/src/Api/IssueLinks.php b/src/Api/IssueLinks.php new file mode 100644 index 000000000..571ee7fac --- /dev/null +++ b/src/Api/IssueLinks.php @@ -0,0 +1,35 @@ +get($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid)) . '/links'); + } + + /** + * @return mixed + */ + public function create(int $source_project_id, int $source_issue_iid, int $target_project_id, int $target_issue_iid) + { + return $this->post($this->getProjectPath($source_project_id, 'issues/' . $this->encodePath((string)$source_issue_iid) . '/links'), [ + 'target_project_id' => $target_project_id, + 'target_issue_iid' => $target_issue_iid, + ]); + } + + /** + * @return mixed + */ + public function remove(int $project_id, int $issue_iid, int $issue_link_id) + { + return $this->delete($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid)) . '/links/' . $this->encodePath((string)$issue_link_id)); + } +} diff --git a/src/Api/Issues.php b/src/Api/Issues.php new file mode 100644 index 000000000..54ae3a999 --- /dev/null +++ b/src/Api/Issues.php @@ -0,0 +1,271 @@ +createOptionsResolver(); + + $resolver->setDefined('state') + ->setAllowedValues('state', ['opened', 'closed']); + $resolver->setDefined('labels'); + $resolver->setDefined('milestone'); + $resolver->setDefined('iids') + ->setAllowedTypes('iids', 'array') + ->setAllowedValues('iids', static function (array $value) { + return count($value) === count(array_filter($value, 'is_int')); + }); + $resolver->setDefined('scope') + ->setAllowedValues('scope', ['created-by-me', 'assigned-to-me', 'all']); + $resolver->setDefined('order_by') + ->setAllowedValues('order_by', ['created_at', 'updated_at']); + $resolver->setDefined('sort') + ->setAllowedValues('sort', ['asc', 'desc']); + $resolver->setDefined('search'); + $resolver->setDefined('assignee_id') + ->setAllowedTypes('assignee_id', 'integer'); + + $path = $project_id === null ? 'issues' : $this->getProjectPath($project_id, 'issues'); + + return $this->get($path, $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function show(int $project_id, int $issue_iid) + { + return $this->get($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid))); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function create(int $project_id, array $params) + { + return $this->post($this->getProjectPath($project_id, 'issues'), $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function update(int $project_id, int $issue_iid, array $params) + { + return $this->put($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid)), $params); + } + + /** + * @return mixed + */ + public function move(int $project_id, int $issue_iid, int $to_project_id) + { + return $this->post($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid)) . '/move', ['to_project_id' => $to_project_id]); + } + + /** + * @return mixed + */ + public function remove(int $project_id, int $issue_iid) + { + return $this->delete($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid))); + } + + /** + * @return mixed + */ + public function showComments(int $project_id, int $issue_iid) + { + return $this->get($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid)) . '/notes'); + } + + /** + * @return mixed + */ + public function showComment(int $project_id, int $issue_iid, int $note_id) + { + return $this->get($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid)) . '/notes/' . $this->encodePath((string)$note_id)); + } + + /** + * @param string|mixed[] $body + * + * @return mixed + */ + public function addComment(int $project_id, int $issue_iid, $body) + { + // backwards compatibility + if (is_array($body)) { + $params = $body; + } else { + $params = ['body' => $body]; + } + + return $this->post($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/notes'), $params); + } + + /** + * @return mixed + */ + public function updateComment(int $project_id, int $issue_iid, int $note_id, string $body) + { + return $this->put($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/notes/' . $this->encodePath((string)$note_id)), ['body' => $body]); + } + + /** + * @return mixed + */ + public function removeComment(int $project_id, int $issue_iid, int $note_id) + { + return $this->delete($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/notes/' . $this->encodePath((string)$note_id))); + } + + /** + * @return mixed + */ + public function showDiscussions(int $project_id, int $issue_iid) + { + return $this->get($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid)) . '/discussions'); + } + + /** + * @return mixed + */ + public function showDiscussion(int $project_id, int $issue_iid, string $discussion_id) + { + return $this->get($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid)) . '/discussions/' . $this->encodePath((string)$discussion_id)); + } + + /** + * @param string|mixed[] $body + * + * @return mixed + */ + public function addDiscussion(int $project_id, int $issue_iid, $body) + { + // backwards compatibility + if (is_array($body)) { + $params = $body; + } else { + $params = ['body' => $body]; + } + + return $this->post($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/discussions'), $params); + } + + /** + * @param string|mixed[] $body + * + * @return mixed + */ + public function addDiscussionNote(int $project_id, int $issue_iid, string $discussion_id, $body) + { + // backwards compatibility + if (is_array($body)) { + $params = $body; + } else { + $params = ['body' => $body]; + } + + return $this->post($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/discussions/' . $this->encodePath((string)$discussion_id) . '/notes'), $params); + } + + /** + * @return mixed + */ + public function updateDiscussionNote(int $project_id, int $issue_iid, string $discussion_id, int $note_id, string $body) + { + return $this->put($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/discussions/' . $this->encodePath((string)$discussion_id) . '/notes/' . $this->encodePath((string)$note_id)), ['body' => $body]); + } + + /** + * @return mixed + */ + public function removeDiscussionNote(int $project_id, int $issue_iid, string $discussion_id, int $note_id) + { + return $this->delete($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/discussions/' . $this->encodePath((string)$discussion_id) . '/notes/' . $this->encodePath((string)$note_id))); + } + + /** + * @return mixed + */ + public function setTimeEstimate(int $project_id, int $issue_iid, string $duration) + { + return $this->post($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/time_estimate'), ['duration' => $duration]); + } + + /** + * @return mixed + */ + public function resetTimeEstimate(int $project_id, int $issue_iid) + { + return $this->post($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/reset_time_estimate')); + } + + /** + * @return mixed + */ + public function addSpentTime(int $project_id, int $issue_iid, string $duration) + { + return $this->post($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/add_spent_time'), ['duration' => $duration]); + } + + /** + * @return mixed + */ + public function resetSpentTime(int $project_id, int $issue_iid) + { + return $this->post($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/reset_spent_time')); + } + + /** + * @return mixed + */ + public function getTimeStats(int $project_id, int $issue_iid) + { + return $this->get($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/time_stats')); + } + + /** + * @return mixed + */ + public function awardEmoji(int $project_id, int $issue_iid) + { + return $this->get($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid) . '/award_emoji')); + } + + /** + * @return mixed + */ + public function closedByMergeRequests(int $project_id, int $issue_iid) + { + return $this->get($this->getProjectPath($project_id, 'issues/' . $this->encodePath((string)$issue_iid)) . '/closed_by'); + } +} diff --git a/lib/Gitlab/Api/IssuesStatistics.php b/src/Api/IssuesStatistics.php similarity index 68% rename from lib/Gitlab/Api/IssuesStatistics.php rename to src/Api/IssuesStatistics.php index e66c4e459..46324e2b3 100644 --- a/lib/Gitlab/Api/IssuesStatistics.php +++ b/src/Api/IssuesStatistics.php @@ -1,26 +1,46 @@ -get('issues_statistics', $this->createOptionsResolver()->resolve($parameters)); } - public function project($project_id, $parameters) + /** + * @param mixed[] $parameters + * + * @return mixed + */ + public function project(int $project_id, array $parameters) { return $this->get($this->getProjectPath($project_id, 'issues_statistics'), $this->createOptionsResolver()->resolve($parameters)); } - public function group($group_id, $parameters) + /** + * @param mixed[] $parameters + * + * @return mixed + */ + public function group(int $group_id, array $parameters) { return $this->get($this->getGroupPath($group_id, 'issues_statistics'), $this->createOptionsResolver()->resolve($parameters)); } - protected function createOptionsResolver() + protected function createOptionsResolver(): OptionsResolver { $resolver = new OptionsResolver(); @@ -51,27 +71,27 @@ protected function createOptionsResolver() $resolver->setDefined('search') ->setAllowedTypes('search', 'string'); - $datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) { + $datetimeNormalizer = static function (Options $resolver, DateTimeInterface $value) { return $value->format('c'); }; $resolver->setDefined('created_after') - ->setAllowedTypes('created_after', \DateTimeInterface::class) + ->setAllowedTypes('created_after', DateTimeInterface::class) ->setNormalizer('created_after', $datetimeNormalizer); $resolver->setDefined('created_before') - ->setAllowedTypes('created_before', \DateTimeInterface::class) + ->setAllowedTypes('created_before', DateTimeInterface::class) ->setNormalizer('created_before', $datetimeNormalizer); $resolver->setDefined('updated_after') - ->setAllowedTypes('updated_after', \DateTimeInterface::class) + ->setAllowedTypes('updated_after', DateTimeInterface::class) ->setNormalizer('updated_after', $datetimeNormalizer); $resolver->setDefined('updated_before') - ->setAllowedTypes('updated_before', \DateTimeInterface::class) + ->setAllowedTypes('updated_before', DateTimeInterface::class) ->setNormalizer('updated_before', $datetimeNormalizer); - $booleanNormalizer = function (Options $resolver, $value) { + $booleanNormalizer = static function (Options $resolver, $value): string { return $value ? 'true' : 'false'; }; diff --git a/src/Api/Jobs.php b/src/Api/Jobs.php new file mode 100644 index 000000000..383300c5f --- /dev/null +++ b/src/Api/Jobs.php @@ -0,0 +1,179 @@ +createOptionsResolver(); + + return $this->get('projects/' . $this->encodePath((string)$project_id) . '/jobs', $resolver->resolve($parameters)); + } + + /** + * @param string[] $parameters Containing: scope The scope of jobs to show, one or array of: created, pending, running, failed, + * success, canceled, skipped, manual; showing all jobs if none provided. + * + * @return mixed + */ + public function pipelineJobs(int $project_id, int $pipeline_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + + return $this->get( + $this->getProjectPath($project_id, 'pipelines/') . $this->encodePath((string)$pipeline_id) . '/jobs', + $resolver->resolve($parameters) + ); + } + + /** + * @param int|string $project_id + * + * @return mixed + */ + public function show($project_id, int $job_id) + { + return $this->get('projects/' . $this->encodePath((string)$project_id) . '/jobs/' . $this->encodePath((string)$job_id)); + } + + /** + * @param int|string $project_id + */ + public function artifacts($project_id, int $job_id): StreamInterface + { + return $this->getAsResponse('projects/' . $this->encodePath((string)$project_id) . '/jobs/' . $this->encodePath((string)$job_id) . '/artifacts')->getBody(); + } + + /** + * @param int|string $project_id + */ + public function artifactsByRefName($project_id, string $ref_name, string $job_name): StreamInterface + { + return $this->getAsResponse('projects/' . $this->encodePath((string)$project_id) . '/jobs/artifacts/' . $this->encodePath((string)$ref_name) . '/download', [ + 'job' => $this->encodePath((string)$job_name), + ])->getBody(); + } + + public function artifactByRefName( + int $project_id, + string $ref_name, + string $job_name, + string $artifact_path + ): StreamInterface { + return $this->getAsResponse('projects/' . $this->encodePath((string)$project_id) . '/jobs/artifacts/' . $this->encodePath((string)$ref_name) . '/raw/' . $this->encodePath((string)$artifact_path), [ + 'job' => $this->encodePath((string)$job_name), + ])->getBody(); + } + + /** + * @param int|string $project_id + */ + public function trace($project_id, int $job_id): string + { + return $this->get('projects/' . $this->encodePath((string)$project_id) . '/jobs/' . $this->encodePath((string)$job_id) . '/trace'); + } + + /** + * @param int|string $project_id + * + * @return mixed + */ + public function cancel($project_id, int $job_id) + { + return $this->post('projects/' . $this->encodePath((string)$project_id) . '/jobs/' . $this->encodePath((string)$job_id) . '/cancel'); + } + + /** + * @param int|string $project_id + * + * @return mixed + */ + public function retry($project_id, int $job_id) + { + return $this->post('projects/' . $this->encodePath((string)$project_id) . '/jobs/' . $this->encodePath((string)$job_id) . '/retry'); + } + + /** + * @param int|string $project_id + * + * @return mixed + */ + public function erase($project_id, int $job_id) + { + return $this->post('projects/' . $this->encodePath((string)$project_id) . '/jobs/' . $this->encodePath((string)$job_id) . '/erase'); + } + + /** + * @param int|string $project_id + * + * @return mixed + */ + public function keepArtifacts($project_id, int $job_id) + { + return $this->post('projects/' . $this->encodePath((string)$project_id) . '/jobs/' . $this->encodePath((string)$job_id) . '/artifacts/keep'); + } + + /** + * @param int|string $project_id + * + * @return mixed + */ + public function play($project_id, int $job_id) + { + return $this->post('projects/' . $this->encodePath((string)$project_id) . '/jobs/' . $this->encodePath((string)$job_id) . '/play'); + } + + /** + * {@inheritdoc} + */ + protected function createOptionsResolver(): OptionsResolver + { + $allowedScopeValues = [ + self::SCOPE_CANCELED, + self::SCOPE_CREATED, + self::SCOPE_FAILED, + self::SCOPE_MANUAL, + self::SCOPE_PENDING, + self::SCOPE_RUNNING, + self::SCOPE_SKIPPED, + self::SCOPE_SUCCESS, + ]; + + $resolver = parent::createOptionsResolver(); + $resolver->setDefined('scope') + ->setAllowedTypes('scope', ['string', 'array']) + ->setAllowedValues('scope', $allowedScopeValues) + ->addAllowedValues('scope', static function ($value) use ($allowedScopeValues) { + return is_array($value) && empty(array_diff($value, $allowedScopeValues)); + }) + ->setNormalizer('scope', static function (OptionsResolver $resolver, $value) { + return (array)$value; + }); + + return $resolver; + } +} diff --git a/src/Api/Keys.php b/src/Api/Keys.php new file mode 100644 index 000000000..b8796c0b5 --- /dev/null +++ b/src/Api/Keys.php @@ -0,0 +1,16 @@ +get('keys/' . $this->encodePath((string)$id)); + } +} diff --git a/src/Api/MergeRequests.php b/src/Api/MergeRequests.php new file mode 100644 index 000000000..1144e593f --- /dev/null +++ b/src/Api/MergeRequests.php @@ -0,0 +1,314 @@ +createOptionsResolver(); + $datetimeNormalizer = static function (Options $resolver, DateTimeInterface $value) { + return $value->format('c'); + }; + $resolver->setDefined('iids') + ->setAllowedTypes('iids', 'array') + ->setAllowedValues('iids', static function (array $value) { + return count($value) === count(array_filter($value, 'is_int')); + }); + $resolver->setDefined('state') + ->setAllowedValues('state', ['all', 'opened', 'merged', 'closed']); + $resolver->setDefined('order_by') + ->setAllowedValues('order_by', ['created_at', 'updated_at']); + $resolver->setDefined('sort') + ->setAllowedValues('sort', ['asc', 'desc']); + $resolver->setDefined('milestone'); + $resolver->setDefined('view') + ->setAllowedValues('view', ['simple']); + $resolver->setDefined('labels'); + $resolver->setDefined('created_after') + ->setAllowedTypes('created_after', DateTimeInterface::class) + ->setNormalizer('created_after', $datetimeNormalizer); + $resolver->setDefined('created_before') + ->setAllowedTypes('created_before', DateTimeInterface::class) + ->setNormalizer('created_before', $datetimeNormalizer); + + $resolver->setDefined('updated_after') + ->setAllowedTypes('updated_after', DateTimeInterface::class) + ->setNormalizer('updated_after', $datetimeNormalizer); + $resolver->setDefined('updated_before') + ->setAllowedTypes('updated_before', DateTimeInterface::class) + ->setNormalizer('updated_before', $datetimeNormalizer); + + $resolver->setDefined('scope') + ->setAllowedValues('scope', ['created_by_me', 'assigned_to_me', 'all']); + $resolver->setDefined('author_id') + ->setAllowedTypes('author_id', 'integer'); + + $resolver->setDefined('assignee_id') + ->setAllowedTypes('assignee_id', 'integer'); + + $resolver->setDefined('search'); + $resolver->setDefined('source_branch'); + $resolver->setDefined('target_branch'); + + return $this->get($this->getProjectPath($project_id, 'merge_requests'), $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function show(int $project_id, int $mr_id) + { + return $this->get($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_id))); + } + + /** + * @return mixed + */ + public function create(int $project_id, string $source, string $target, string $title, ?int $assignee = null, ?int $target_project_id = null, ?string $description = null) + { + return $this->post($this->getProjectPath($project_id, 'merge_requests'), [ + 'source_branch' => $source, + 'target_branch' => $target, + 'title' => $title, + 'assignee_id' => $assignee, + 'target_project_id' => $target_project_id, + 'description' => $description, + ]); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function update(int $project_id, int $mr_id, array $params) + { + return $this->put($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_id)), $params); + } + + /** + * @param string|mixed[]|null $message + * + * @return mixed + */ + public function merge(int $project_id, int $mr_id, $message = null) + { + if (is_array($message)) { + $params = $message; + } else { + $params = ['merge_commit_message' => $message]; + } + + return $this->put($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_id) . '/merge'), $params); + } + + /** + * @return mixed + */ + public function showNotes(int $project_id, int $mr_id) + { + return $this->get($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_id) . '/notes')); + } + + /** + * @return mixed + */ + public function addNote(int $project_id, int $mr_id, string $note) + { + return $this->post($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_id) . '/notes'), ['body' => $note]); + } + + /** + * @return mixed + */ + public function removeNote(int $projectId, int $mrId, int $noteId) + { + return $this->delete($this->getProjectPath($projectId, 'merge_requests/' . $this->encodePath((string)$mrId) . '/notes/' . $this->encodePath((string)$noteId))); + } + + /** + * @return mixed + */ + public function showComments(int $project_id, int $mr_id) + { + @trigger_error(sprintf('The %s() method is deprecated since version 9.1 and will be removed in 10.0. Use the showNotes() method instead.', __METHOD__), E_USER_DEPRECATED); + + return $this->showNotes($project_id, $mr_id); + } + + /** + * @return mixed + */ + public function addComment(int $project_id, int $mr_id, string $note) + { + @trigger_error(sprintf('The %s() method is deprecated since version 9.1 and will be removed in 10.0. Use the addNote() method instead.', __METHOD__), E_USER_DEPRECATED); + + return $this->addNote($project_id, $mr_id, $note); + } + + /** + * @return mixed + */ + public function showDiscussions(int $project_id, int $mr_iid) + { + return $this->get($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid)) . '/discussions'); + } + + /** + * @return mixed + */ + public function showDiscussion(int $project_id, int $mr_iid, string $discussion_id) + { + return $this->get($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid)) . '/discussions/' . $this->encodePath((string)$discussion_id)); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function addDiscussion(int $project_id, int $mr_iid, array $params) + { + return $this->post($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid) . '/discussions'), $params); + } + + /** + * @return mixed + */ + public function resolveDiscussion(int $project_id, int $mr_iid, string $discussion_id, bool $resolved = true) + { + return $this->put($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid) . '/discussions/' . $this->encodePath((string)$discussion_id)), ['resolved' => $resolved]); + } + + /** + * @param string|string[] $body + * + * @return mixed + */ + public function addDiscussionNote(int $project_id, int $mr_iid, string $discussion_id, $body) + { + // backwards compatibility + if (is_array($body)) { + $params = $body; + } else { + $params = ['body' => $body]; + } + + return $this->post($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid) . '/discussions/' . $this->encodePath((string)$discussion_id) . '/notes'), $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function updateDiscussionNote(int $project_id, int $mr_iid, string $discussion_id, int $note_id, array $params) + { + return $this->put($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid) . '/discussions/' . $this->encodePath((string)$discussion_id) . '/notes/' . $this->encodePath((string)$note_id)), $params); + } + + /** + * @return mixed + */ + public function removeDiscussionNote(int $project_id, int $mr_iid, string $discussion_id, int $note_id) + { + return $this->delete($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid) . '/discussions/' . $this->encodePath((string)$discussion_id) . '/notes/' . $this->encodePath((string)$note_id))); + } + + /** + * @return mixed + */ + public function changes(int $project_id, int $mr_id) + { + return $this->get($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_id) . '/changes')); + } + + /** + * @return mixed + */ + public function commits(int $project_id, int $mr_id) + { + return $this->get($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_id) . '/commits')); + } + + /** + * @return mixed + */ + public function closesIssues(int $project_id, int $mr_id) + { + return $this->get($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_id) . '/closes_issues')); + } + + /** + * @return mixed + */ + public function approvals(int $project_id, int $mr_iid) + { + return $this->get($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid) . '/approvals')); + } + + /** + * @return mixed + */ + public function approve(int $project_id, int $mr_iid) + { + return $this->post($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid) . '/approve')); + } + + /** + * @return mixed + */ + public function unapprove(int $project_id, int $mr_iid) + { + return $this->post($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid) . '/unapprove')); + } + + /** + * @return mixed + */ + public function awardEmoji(int $project_id, int $mr_iid) + { + return $this->get($this->getProjectPath($project_id, 'merge_requests/' . $this->encodePath((string)$mr_iid) . '/award_emoji')); + } +} diff --git a/lib/Gitlab/Api/Milestones.php b/src/Api/Milestones.php similarity index 50% rename from lib/Gitlab/Api/Milestones.php rename to src/Api/Milestones.php index 964af7057..dee04577e 100644 --- a/lib/Gitlab/Api/Milestones.php +++ b/src/Api/Milestones.php @@ -1,10 +1,16 @@ -createOptionsResolver(); $resolver->setDefined('iids') ->setAllowedTypes('iids', 'array') - ->setAllowedValues('iids', function (array $value) { - return count($value) == count(array_filter($value, 'is_int')); - }) - ; + ->setAllowedValues('iids', static function (array $value) { + return count($value) === count(array_filter($value, 'is_int')); + }); $resolver->setDefined('state') - ->setAllowedValues('state', ['active', 'closed']) - ; + ->setAllowedValues('state', ['active', 'closed']); $resolver->setDefined('search'); return $this->get($this->getProjectPath($project_id, 'milestones'), $resolver->resolve($parameters)); } /** - * @param int $project_id - * @param int $milestone_id * @return mixed */ - public function show($project_id, $milestone_id) + public function show(int $project_id, int $milestone_id) { - return $this->get($this->getProjectPath($project_id, 'milestones/'.$this->encodePath($milestone_id))); + return $this->get($this->getProjectPath($project_id, 'milestones/' . $this->encodePath((string)$milestone_id))); } /** - * @param int $project_id - * @param array $params + * @param mixed[] $params + * * @return mixed */ - public function create($project_id, array $params) + public function create(int $project_id, array $params) { return $this->post($this->getProjectPath($project_id, 'milestones'), $params); } /** - * @param int $project_id - * @param int $milestone_id - * @param array $params + * @param mixed[] $params + * * @return mixed */ - public function update($project_id, $milestone_id, array $params) + public function update(int $project_id, int $milestone_id, array $params) { - return $this->put($this->getProjectPath($project_id, 'milestones/'.$this->encodePath($milestone_id)), $params); + return $this->put($this->getProjectPath($project_id, 'milestones/' . $this->encodePath((string)$milestone_id)), $params); } /** - * @param int $project_id - * @param int $milestone_id * @return mixed */ - public function remove($project_id, $milestone_id) + public function remove(int $project_id, int $milestone_id) { - return $this->delete($this->getProjectPath($project_id, 'milestones/'.$this->encodePath($milestone_id))); + return $this->delete($this->getProjectPath($project_id, 'milestones/' . $this->encodePath((string)$milestone_id))); } /** - * @param int $project_id - * @param int $milestone_id * @return mixed */ - public function issues($project_id, $milestone_id) + public function issues(int $project_id, int $milestone_id) { - return $this->get($this->getProjectPath($project_id, 'milestones/'.$this->encodePath($milestone_id).'/issues')); + return $this->get($this->getProjectPath($project_id, 'milestones/' . $this->encodePath((string)$milestone_id) . '/issues')); } } diff --git a/lib/Gitlab/Api/ProjectNamespaces.php b/src/Api/ProjectNamespaces.php similarity index 63% rename from lib/Gitlab/Api/ProjectNamespaces.php rename to src/Api/ProjectNamespaces.php index a9643db6f..503a40e7a 100644 --- a/lib/Gitlab/Api/ProjectNamespaces.php +++ b/src/Api/ProjectNamespaces.php @@ -1,9 +1,13 @@ -get('namespaces/'.$this->encodePath($namespace_id)); + return $this->get('namespaces/' . $this->encodePath((string)$namespace_id)); } } diff --git a/src/Api/Projects.php b/src/Api/Projects.php new file mode 100644 index 000000000..fadb42d48 --- /dev/null +++ b/src/Api/Projects.php @@ -0,0 +1,785 @@ +createOptionsResolver(); + $booleanNormalizer = static function (Options $resolver, $value) { + return $value ? 'true' : 'false'; + }; + $resolver->setDefined('archived') + ->setAllowedTypes('archived', 'bool') + ->setNormalizer('archived', $booleanNormalizer); + $resolver->setDefined('visibility') + ->setAllowedValues('visibility', ['public', 'internal', 'private']); + $resolver->setDefined('order_by') + ->setAllowedValues('order_by', ['id', 'name', 'path', 'created_at', 'updated_at', 'last_activity_at']); + $resolver->setDefined('sort') + ->setAllowedValues('sort', ['asc', 'desc']); + $resolver->setDefined('search'); + $resolver->setDefined('simple') + ->setAllowedTypes('simple', 'bool') + ->setNormalizer('simple', $booleanNormalizer); + $resolver->setDefined('owned') + ->setAllowedTypes('owned', 'bool') + ->setNormalizer('owned', $booleanNormalizer); + $resolver->setDefined('membership') + ->setAllowedTypes('membership', 'bool') + ->setNormalizer('membership', $booleanNormalizer); + $resolver->setDefined('starred') + ->setAllowedTypes('starred', 'bool') + ->setNormalizer('starred', $booleanNormalizer); + $resolver->setDefined('statistics') + ->setAllowedTypes('statistics', 'bool') + ->setNormalizer('statistics', $booleanNormalizer); + $resolver->setDefined('with_issues_enabled') + ->setAllowedTypes('with_issues_enabled', 'bool') + ->setNormalizer('with_issues_enabled', $booleanNormalizer); + $resolver->setDefined('with_merge_requests_enabled') + ->setAllowedTypes('with_merge_requests_enabled', 'bool') + ->setNormalizer('with_merge_requests_enabled', $booleanNormalizer); + $resolver->setDefined('min_access_level') + ->setAllowedValues('min_access_level', [null, 10, 20, 30, 40, 50]); + + return $this->get('projects', $resolver->resolve($parameters)); + } + + /** + * @param int|string $project_id + * @param mixed[] $parameters { + * + * @return mixed + * + * @var bool $statistics Include project statistics. + * @var bool $with_custom_attributes Include project custom attributes. + * } + */ + public function show($project_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + $booleanNormalizer = static function (Options $resolver, $value): bool { + return (bool)$value; + }; + $resolver + ->setDefined('statistics') + ->setAllowedTypes('statistics', 'bool') + ->setNormalizer('statistics', $booleanNormalizer); + $resolver + ->setDefined('with_custom_attributes') + ->setAllowedTypes('with_custom_attributes', 'bool') + ->setNormalizer('with_custom_attributes', $booleanNormalizer); + + return $this->get('projects/' . $this->encodePath((string)$project_id), $resolver->resolve($parameters)); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function create(string $name, array $params = []) + { + $params['name'] = $name; + + return $this->post('projects', $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function createForUser(int $user_id, string $name, array $params = []) + { + $params['name'] = $name; + + return $this->post('projects/user/' . $this->encodePath((string)$user_id), $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function update(int $project_id, array $params) + { + return $this->put('projects/' . $this->encodePath((string)$project_id), $params); + } + + /** + * @return mixed + */ + public function remove(int $project_id) + { + return $this->delete('projects/' . $this->encodePath((string)$project_id)); + } + + /** + * @return mixed + */ + public function archive(int $project_id) + { + return $this->post('projects/' . $this->encodePath((string)$project_id) . '/archive'); + } + + /** + * @return mixed + */ + public function unarchive(int $project_id) + { + return $this->post('projects/' . $this->encodePath((string)$project_id) . '/unarchive'); + } + + /** + * @param mixed[] $parameters + * scope The scope of pipelines, one of: running, pending, finished, branches, tags. + * status The status of pipelines, one of: running, pending, success, failed, canceled, skipped. + * ref The ref of pipelines. + * sha The sha of pipelines. + * yaml_errors Returns pipelines with invalid configurations. + * name The name of the user who triggered pipelines. + * username The username of the user who triggered pipelines. + * order_by Order pipelines by id, status, ref, or user_id (default: id). + * order Sort pipelines in asc or desc order (default: desc). + * + * @return mixed + * ) + */ + public function pipelines(int $project_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + $booleanNormalizer = static function (Options $resolver, $value) { + return $value ? 'true' : 'false'; + }; + + $resolver->setDefined('scope') + ->setAllowedValues('scope', ['running', 'pending', 'finished', 'branches', 'tags']); + $resolver->setDefined('status') + ->setAllowedValues('status', ['running', 'pending', 'success', 'failed', 'canceled', 'skipped']); + $resolver->setDefined('ref'); + $resolver->setDefined('sha'); + $resolver->setDefined('yaml_errors') + ->setAllowedTypes('yaml_errors', 'bool') + ->setNormalizer('yaml_errors', $booleanNormalizer); + $resolver->setDefined('name'); + $resolver->setDefined('username'); + $resolver->setDefined('order_by') + ->setAllowedValues('order_by', ['id', 'status', 'ref', 'user_id']); + $resolver->setDefined('sort') + ->setAllowedValues('sort', ['asc', 'desc']); + + return $this->get($this->getProjectPath($project_id, 'pipelines'), $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function pipeline(int $project_id, int $pipeline_id) + { + return $this->get($this->getProjectPath($project_id, 'pipelines/' . $this->encodePath((string)$pipeline_id))); + } + + /** + * @return mixed + */ + public function createPipeline(int $project_id, string $commit_ref) + { + return $this->post($this->getProjectPath($project_id, 'pipeline'), ['ref' => $commit_ref]); + } + + /** + * @return mixed + */ + public function retryPipeline(int $project_id, int $pipeline_id) + { + return $this->post($this->getProjectPath($project_id, 'pipelines/' . $this->encodePath((string)$pipeline_id)) . '/retry'); + } + + /** + * @return mixed + */ + public function cancelPipeline(int $project_id, int $pipeline_id) + { + return $this->post($this->getProjectPath($project_id, 'pipelines/' . $this->encodePath((string)$pipeline_id)) . '/cancel'); + } + + /** + * @return mixed + */ + public function deletePipeline(int $project_id, int $pipeline_id) + { + return $this->delete($this->getProjectPath($project_id, 'pipelines/' . $this->encodePath((string)$pipeline_id))); + } + + /** + * @param mixed[] $parameters + * + * @return mixed + */ + public function allMembers(int $project_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + $resolver->setDefined('query'); + + return $this->get('projects/' . $this->encodePath((string)$project_id) . '/members/all', $resolver->resolve($parameters)); + } + + /** + * @param mixed[] $parameters Contains: query The query you want to search members for. + * + * @return mixed + */ + public function members(int $project_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + + $resolver->setDefined('query') + ->setAllowedTypes('query', 'string'); + + return $this->get($this->getProjectPath($project_id, 'members'), $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function member(int $project_id, int $user_id) + { + return $this->get($this->getProjectPath($project_id, 'members/' . $this->encodePath((string)$user_id))); + } + + /** + * @return mixed + */ + public function addMember(int $project_id, int $user_id, int $access_level) + { + return $this->post($this->getProjectPath($project_id, 'members'), [ + 'user_id' => $user_id, + 'access_level' => $access_level, + ]); + } + + /** + * @return mixed + */ + public function saveMember(int $project_id, int $user_id, int $access_level) + { + return $this->put($this->getProjectPath($project_id, 'members/' . urldecode((string)$user_id)), ['access_level' => $access_level]); + } + + /** + * @return mixed + */ + public function removeMember(int $project_id, int $user_id) + { + return $this->delete($this->getProjectPath($project_id, 'members/' . urldecode((string)$user_id))); + } + + /** + * @param mixed[] $parameters + * + * @return mixed + */ + public function hooks(int $project_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + + return $this->get($this->getProjectPath($project_id, 'hooks'), $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function hook(int $project_id, int $hook_id) + { + return $this->get($this->getProjectPath($project_id, 'hooks/' . $this->encodePath((string)$hook_id))); + } + + /** + * Get project issues. + * + * See https://docs.gitlab.com/ee/api/issues.html#list-project-issues for more info. + * + * @param int $project_id + * Project id. + * @param mixed[] $parameters + * Url parameters. For example: issue state (opened / closed). + * + * @return mixed[] + * List of project issues. + */ + public function issues(int $project_id, array $parameters = []): array + { + return $this->get($this->getProjectPath($project_id, 'issues'), $parameters); + } + + /** + * Get projects board list. + * + * See https://docs.gitlab.com/ee/api/boards.html for more info. + * + * @param int $project_id + * Project id. + * + * @return mixed[] + * List of project boards. + */ + public function boards(int $project_id): array + { + return $this->get($this->getProjectPath($project_id, 'boards')); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function addHook(int $project_id, string $url, array $params = []) + { + if (empty($params)) { + $params = ['push_events' => true]; + } + + $params['url'] = $url; + + return $this->post($this->getProjectPath($project_id, 'hooks'), $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function updateHook(int $project_id, int $hook_id, array $params) + { + return $this->put($this->getProjectPath($project_id, 'hooks/' . $this->encodePath((string)$hook_id)), $params); + } + + /** + * @return mixed + */ + public function removeHook(int $project_id, int $hook_id) + { + return $this->delete($this->getProjectPath($project_id, 'hooks/' . $this->encodePath((string)$hook_id))); + } + + /** + * @param mixed $namespace + * + * @return mixed + */ + public function transfer(int $project_id, $namespace) + { + return $this->put($this->getProjectPath($project_id, 'transfer'), ['namespace' => $namespace]); + } + + /** + * @return mixed + */ + public function deployKeys(int $project_id) + { + return $this->get($this->getProjectPath($project_id, 'deploy_keys')); + } + + /** + * @return mixed + */ + public function deployKey(int $project_id, int $key_id) + { + return $this->get($this->getProjectPath($project_id, 'deploy_keys/' . $this->encodePath((string)$key_id))); + } + + /** + * @return mixed + */ + public function addDeployKey(int $project_id, string $title, string $key, bool $canPush = false) + { + return $this->post($this->getProjectPath($project_id, 'deploy_keys'), [ + 'title' => $title, + 'key' => $key, + 'can_push' => $canPush, + ]); + } + + /** + * @return mixed + */ + public function deleteDeployKey(int $project_id, int $key_id) + { + return $this->delete($this->getProjectPath($project_id, 'deploy_keys/' . $this->encodePath((string)$key_id))); + } + + /** + * @return mixed + */ + public function enableDeployKey(int $project_id, int $key_id) + { + return $this->post($this->getProjectPath($project_id, 'deploy_keys/' . $this->encodePath((string)$key_id) . '/enable')); + } + + /** + * @param mixed[] $parameters + * target_type Include only events of a particular target type. + * before Include only events created before a particular date. + * after Include only events created after a particular date. + * sort Sort events in asc or desc order by created_at. Default is desc. + * action Include only events of a particular action type. + * + * @return mixed + */ + public function events(int $project_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + $datetimeNormalizer = static function (Options $resolver, DateTimeInterface $value) { + return $value->format('Y-m-d'); + }; + + $resolver->setDefined('action') + ->setAllowedValues('action', ['created', 'updated', 'closed', 'reopened', 'pushed', 'commented', 'merged', 'joined', 'left', 'destroyed', 'expired']); + $resolver->setDefined('target_type') + ->setAllowedValues('target_type', ['issue', 'milestone', 'merge_request', 'note', 'project', 'snippet', 'user']); + $resolver->setDefined('before') + ->setAllowedTypes('before', DateTimeInterface::class) + ->setNormalizer('before', $datetimeNormalizer); + $resolver->setDefined('after') + ->setAllowedTypes('after', DateTimeInterface::class) + ->setNormalizer('after', $datetimeNormalizer); + $resolver->setDefined('sort') + ->setAllowedValues('sort', ['asc', 'desc']); + + return $this->get($this->getProjectPath($project_id, 'events'), $resolver->resolve($parameters)); + } + + /** + * @param mixed[] $parameters + * + * @return mixed + */ + public function labels(int $project_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + + return $this->get($this->getProjectPath($project_id, 'labels'), $resolver->resolve($parameters)); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function addLabel(int $project_id, array $params) + { + return $this->post($this->getProjectPath($project_id, 'labels'), $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function updateLabel(int $project_id, array $params) + { + return $this->put($this->getProjectPath($project_id, 'labels'), $params); + } + + /** + * @return mixed + */ + public function removeLabel(int $project_id, string $name) + { + return $this->delete($this->getProjectPath($project_id, 'labels'), ['name' => $name]); + } + + /** + * Get languages used in a project with percentage value. + * + * @return mixed + */ + public function languages(int $project_id) + { + return $this->get($this->getProjectPath($project_id, 'languages')); + } + + /** + * @param mixed[] $parameters + * path The path of the forked project (optional) + * name The name of the forked project (optional) + * namespace The ID or path of the namespace that the project will be forked to + * + * @return mixed + */ + public function fork(int $project_id, array $parameters = []) + { + $resolver = new OptionsResolver(); + $resolver->setDefined(['namespace', 'path', 'name']); + + $resolved = $resolver->resolve($parameters); + + return $this->post($this->getProjectPath($project_id, 'fork'), $resolved); + } + + /** + * @return mixed + */ + public function createForkRelation(int $project_id, int $forked_project_id) + { + return $this->post($this->getProjectPath($project_id, 'fork/' . $this->encodePath((string)$forked_project_id))); + } + + /** + * @return mixed + */ + public function removeForkRelation(int $project_id) + { + return $this->delete($this->getProjectPath($project_id, 'fork')); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function setService(int $project_id, string $service_name, array $params = []) + { + return $this->put($this->getProjectPath($project_id, 'services/' . $this->encodePath((string)$service_name)), $params); + } + + /** + * @return mixed + */ + public function removeService(int $project_id, string $service_name) + { + return $this->delete($this->getProjectPath($project_id, 'services/' . $this->encodePath((string)$service_name))); + } + + /** + * @param mixed[] $parameters + * + * @return mixed + */ + public function variables(int $project_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + + return $this->get($this->getProjectPath($project_id, 'variables'), $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function variable(int $project_id, string $key) + { + return $this->get($this->getProjectPath($project_id, 'variables/' . $this->encodePath((string)$key))); + } + + /** + * @return mixed + */ + public function addVariable( + int $project_id, + string $key, + string $value, + ?bool $protected = null, + ?string $environment_scope = null + ) { + $payload = [ + 'key' => $key, + 'value' => $value, + ]; + + if ($protected) { + $payload['protected'] = $protected; + } + + if ($environment_scope) { + $payload['environment_scope'] = $environment_scope; + } + + return $this->post($this->getProjectPath($project_id, 'variables'), $payload); + } + + /** + * @return mixed + */ + public function updateVariable( + int $project_id, + string $key, + string $value, + ?bool $protected = null, + ?string $environment_scope = null + ) { + $payload = ['value' => $value]; + + if ($protected) { + $payload['protected'] = $protected; + } + + if ($environment_scope) { + $payload['environment_scope'] = $environment_scope; + } + + return $this->put($this->getProjectPath($project_id, 'variables/' . $this->encodePath($key)), $payload); + } + + /** + * @return mixed + */ + public function removeVariable(int $project_id, string $key) + { + return $this->delete($this->getProjectPath($project_id, 'variables/' . $this->encodePath($key))); + } + + /** + * @return mixed + */ + public function uploadFile(int $project_id, string $file) + { + return $this->post($this->getProjectPath($project_id, 'uploads'), [], [], ['file' => $file]); + } + + /** + * @param mixed[] $parameters + * + * @return mixed + */ + public function deployments(int $project_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + + return $this->get($this->getProjectPath($project_id, 'deployments'), $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function deployment(int $project_id, int $deployment_id) + { + return $this->get($this->getProjectPath($project_id, 'deployments/' . $this->encodePath((string)$deployment_id))); + } + + /** + * @param mixed[] $parameters + * + * @return mixed + */ + public function addShare(int $project_id, array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + + $datetimeNormalizer = static function (OptionsResolver $optionsResolver, DateTimeInterface $value) { + return $value->format('Y-m-d'); + }; + + $resolver->setRequired('group_id') + ->setAllowedTypes('group_id', 'int'); + + $resolver->setRequired('group_access') + ->setAllowedTypes('group_access', 'int') + ->setAllowedValues('group_access', [0, 10, 20, 30, 40, 50]); + + $resolver->setDefined('expires_at') + ->setAllowedTypes('expires_at', DateTimeInterface::class) + ->setNormalizer('expires_at', $datetimeNormalizer); + + return $this->post($this->getProjectPath($project_id, 'share'), $resolver->resolve($parameters)); + } + + /** + * @param mixed $project_id + * + * @return mixed + */ + public function removeShare($project_id, int $group_id) + { + return $this->delete($this->getProjectPath($project_id, 'share/' . $group_id)); + } + + /** + * @return mixed + */ + public function badges(int $project_id) + { + return $this->get($this->getProjectPath($project_id, 'badges')); + } + + /** + * @return mixed + */ + public function badge(int $project_id, int $badge_id) + { + return $this->get($this->getProjectPath($project_id, 'badges/' . $this->encodePath((string)$badge_id))); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function addBadge(int $project_id, array $params = []) + { + return $this->post($this->getProjectPath($project_id, 'badges'), $params); + } + + /** + * @return mixed + */ + public function removeBadge(int $project_id, int $badge_id) + { + return $this->delete($this->getProjectPath($project_id, 'badges/' . $this->encodePath((string)$badge_id))); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function updateBadge(int $project_id, int $badge_id, array $params = []) + { + return $this->put($this->getProjectPath($project_id, 'badges/' . $this->encodePath((string)$badge_id))); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function addProtectedBranch(int $project_id, array $params = []) + { + return $this->post($this->getProjectPath($project_id, 'protected_branches'), $params); + } +} diff --git a/lib/Gitlab/Api/Repositories.php b/src/Api/Repositories.php similarity index 55% rename from lib/Gitlab/Api/Repositories.php rename to src/Api/Repositories.php index a52369c42..7ca060153 100644 --- a/lib/Gitlab/Api/Repositories.php +++ b/src/Api/Repositories.php @@ -1,22 +1,31 @@ -createOptionsResolver(); $resolver->setDefined('search') @@ -26,70 +35,59 @@ public function branches($project_id, array $parameters = []) } /** - * @param int $project_id - * @param int $branch_id + * @param string|int $branch_id + * * @return mixed */ - public function branch($project_id, $branch_id) + public function branch(int $project_id, $branch_id) { - return $this->get($this->getProjectPath($project_id, 'repository/branches/'.$this->encodePath($branch_id))); + return $this->get($this->getProjectPath($project_id, 'repository/branches/' . $this->encodePath((string)$branch_id))); } /** - * @param int $project_id - * @param string $branch - * @param string $ref * @return mixed */ - public function createBranch($project_id, $branch, $ref) + public function createBranch(int $project_id, string $branch, string $ref) { - return $this->post($this->getProjectPath($project_id, 'repository/branches'), array( + return $this->post($this->getProjectPath($project_id, 'repository/branches'), [ 'branch' => $branch, - 'ref' => $ref - )); + 'ref' => $ref, + ]); } /** - * @param int $project_id - * @param string $branch * @return mixed */ - public function deleteBranch($project_id, $branch) + public function deleteBranch(int $project_id, string $branch) { - return $this->delete($this->getProjectPath($project_id, 'repository/branches/'.$this->encodePath($branch))); + return $this->delete($this->getProjectPath($project_id, 'repository/branches/' . $this->encodePath($branch))); } /** - * @param int $project_id - * @param string $branch_name - * @param bool $devPush - * @param bool $devMerge * @return mixed */ - public function protectBranch($project_id, $branch_name, $devPush = false, $devMerge = false) + public function protectBranch(int $project_id, string $branch_name, bool $devPush = false, bool $devMerge = false) { - return $this->put($this->getProjectPath($project_id, 'repository/branches/'.$this->encodePath($branch_name).'/protect'), array( + return $this->put($this->getProjectPath($project_id, 'repository/branches/' . $this->encodePath($branch_name) . '/protect'), [ 'developers_can_push' => $devPush, - 'developers_can_merge' => $devMerge - )); + 'developers_can_merge' => $devMerge, + ]); } /** - * @param int $project_id - * @param string $branch_name * @return mixed */ - public function unprotectBranch($project_id, $branch_name) + public function unprotectBranch(int $project_id, string $branch_name) { - return $this->put($this->getProjectPath($project_id, 'repository/branches/'.$this->encodePath($branch_name).'/unprotect')); + return $this->put($this->getProjectPath($project_id, 'repository/branches/' . $this->encodePath($branch_name) . '/unprotect')); } /** - * @param int $project_id - * @param array $parameters + * @param mixed[] $parameters + * * @return mixed */ - public function tags($project_id, array $parameters = []) + public function tags(int $project_id, array $parameters = []) { $resolver = $this->createOptionsResolver(); @@ -97,64 +95,53 @@ public function tags($project_id, array $parameters = []) } /** - * @param int $project_id - * @param string $name - * @param string $ref - * @param string $message * @return mixed */ - public function createTag($project_id, $name, $ref, $message = null) + public function createTag(int $project_id, string $name, string $ref, ?string $message = null) { - return $this->post($this->getProjectPath($project_id, 'repository/tags'), array( + return $this->post($this->getProjectPath($project_id, 'repository/tags'), [ 'tag_name' => $name, 'ref' => $ref, - 'message' => $message - )); + 'message' => $message, + ]); } /** - * @param int $project_id - * @param string $tag_name - * @param string $description * @return mixed */ - public function createRelease($project_id, $tag_name, $description) + public function createRelease(int $project_id, string $tag_name, string $description) { - return $this->post($this->getProjectPath($project_id, 'repository/tags/' . $this->encodePath($tag_name) . '/release'), array( + return $this->post($this->getProjectPath($project_id, 'repository/tags/' . $this->encodePath($tag_name) . '/release'), [ 'id' => $project_id, 'tag_name' => $tag_name, - 'description' => $description - )); + 'description' => $description, + ]); } /** - * @param int $project_id - * @param string $tag_name - * @param string $description * @return mixed */ - public function updateRelease($project_id, $tag_name, $description) + public function updateRelease(int $project_id, string $tag_name, string $description) { - return $this->put($this->getProjectPath($project_id, 'repository/tags/' . $this->encodePath($tag_name) . '/release'), array( + return $this->put($this->getProjectPath($project_id, 'repository/tags/' . $this->encodePath($tag_name) . '/release'), [ 'id' => $project_id, 'tag_name' => $tag_name, - 'description' => $description - )); + 'description' => $description, + ]); } /** - * @param int $project_id * @return mixed */ - public function releases($project_id) + public function releases(int $project_id) { $resolver = $this->createOptionsResolver(); + return $this->get($this->getProjectPath($project_id, 'releases')); } /** - * @param int $project_id - * @param array $parameters ( + * @param mixed[] $parameters ( * * @var string $ref_name The name of a repository branch or tag or if not given the default branch. * @var \DateTimeInterface $since Only commits after or on this date will be returned. @@ -163,23 +150,21 @@ public function releases($project_id) * * @return mixed */ - public function commits($project_id, array $parameters = []) + public function commits(int $project_id, array $parameters = []) { - $resolver = $this->createOptionsResolver(); - $datetimeNormalizer = function (Options $options, \DateTimeInterface $value) { + $resolver = $this->createOptionsResolver(); + $datetimeNormalizer = static function (Options $options, DateTimeInterface $value) { return $value->format('c'); }; $resolver->setDefined('path'); $resolver->setDefined('ref_name'); $resolver->setDefined('since') - ->setAllowedTypes('since', \DateTimeInterface::class) - ->setNormalizer('since', $datetimeNormalizer) - ; + ->setAllowedTypes('since', DateTimeInterface::class) + ->setNormalizer('since', $datetimeNormalizer); $resolver->setDefined('until') - ->setAllowedTypes('until', \DateTimeInterface::class) - ->setNormalizer('until', $datetimeNormalizer) - ; + ->setAllowedTypes('until', DateTimeInterface::class) + ->setNormalizer('until', $datetimeNormalizer); $resolver->setDefined('all'); $resolver->setDefined('with_stats'); @@ -187,39 +172,35 @@ public function commits($project_id, array $parameters = []) } /** - * @param int $project_id - * @param $sha * @return mixed */ - public function commit($project_id, $sha) + public function commit(int $project_id, string $sha) { - return $this->get($this->getProjectPath($project_id, 'repository/commits/'.$this->encodePath($sha))); + return $this->get($this->getProjectPath($project_id, 'repository/commits/' . $this->encodePath((string)$sha))); } /** - * @param int $project_id - * @param $sha - * @param array $parameters + * @param mixed[] $parameters + * * @return mixed */ - public function commitRefs($project_id, $sha, array $parameters = []) + public function commitRefs(int $project_id, string $sha, array $parameters = []) { $resolver = $this->createOptionsResolver(); return $this->get( - $this->getProjectPath($project_id, 'repository/commits/' . $this->encodePath($sha) . '/refs'), + $this->getProjectPath($project_id, 'repository/commits/' . $this->encodePath((string)$sha) . '/refs'), $resolver->resolve($parameters) ); } /** - * @param int $project_id - * @param array $parameters ( + * @param mixed[] $parameters ( * * @var string $branch Name of the branch to commit into. To create a new branch, also provide start_branch. * @var string $commit_message Commit message. * @var string $start_branch Name of the branch to start the new commit from. - * @var array $actions ( + * @var mixed[] $actions ( * * @var string $action he action to perform, create, delete, move, update. * @var string $file_path Full path to the file. @@ -233,42 +214,36 @@ public function commitRefs($project_id, $sha, array $parameters = []) * * @return mixed */ - public function createCommit($project_id, array $parameters = []) + public function createCommit(int $project_id, array $parameters = []) { $resolver = new OptionsResolver(); $resolver->setDefined('branch') - ->setRequired('branch') - ; + ->setRequired('branch'); $resolver->setDefined('commit_message') - ->setRequired('commit_message') - ; + ->setRequired('commit_message'); $resolver->setDefined('start_branch'); $resolver->setDefined('actions') ->setRequired('actions') ->setAllowedTypes('actions', 'array') - ->setAllowedValues('actions', function (array $actions) { - return !empty($actions); + ->setAllowedValues('actions', static function (array $actions) { + return ! empty($actions); }) - ->setNormalizer('actions', function (Options $resolver, array $actions) { + ->setNormalizer('actions', static function (Options $resolver, array $actions) { $actionsOptionsResolver = new OptionsResolver(); $actionsOptionsResolver->setDefined('action') ->setRequired('action') - ->setAllowedValues('action', ['create', 'delete', 'move', 'update']) - ; + ->setAllowedValues('action', ['create', 'delete', 'move', 'update']); $actionsOptionsResolver->setDefined('file_path') - ->setRequired('file_path') - ; + ->setRequired('file_path'); $actionsOptionsResolver->setDefined('previous_path'); $actionsOptionsResolver->setDefined('content'); $actionsOptionsResolver->setDefined('encoding') - ->setAllowedValues('encoding', ['test', 'base64']) - ; + ->setAllowedValues('encoding', ['test', 'base64']); - return array_map(function ($action) use ($actionsOptionsResolver) { + return array_map(static function ($action) use ($actionsOptionsResolver) { return $actionsOptionsResolver->resolve($action); }, $actions); - }) - ; + }); $resolver->setDefined('author_email'); $resolver->setDefined('author_name'); @@ -276,102 +251,87 @@ public function createCommit($project_id, array $parameters = []) } /** - * @param int $project_id - * @param string $sha - * @param array $parameters + * @param mixed[] $parameters + * * @return mixed */ - public function commitComments($project_id, $sha, array $parameters = []) + public function commitComments(int $project_id, string $sha, array $parameters = []) { $resolver = $this->createOptionsResolver(); return $this->get( - $this->getProjectPath($project_id, 'repository/commits/'.$this->encodePath($sha).'/comments'), + $this->getProjectPath($project_id, 'repository/commits/' . $this->encodePath($sha) . '/comments'), $resolver->resolve($parameters) ); } /** - * @param int $project_id - * @param string $sha - * @param string $note - * @param array $params + * @param mixed[] $params + * * @return mixed */ - public function createCommitComment($project_id, $sha, $note, array $params = array()) + public function createCommitComment(int $project_id, string $sha, string $note, array $params = []) { $params['note'] = $note; - return $this->post($this->getProjectPath($project_id, 'repository/commits/'.$this->encodePath($sha).'/comments'), $params); + return $this->post($this->getProjectPath($project_id, 'repository/commits/' . $this->encodePath($sha) . '/comments'), $params); } /** - * @param int $project_id - * @param string $sha - * @param array $params + * @param mixed[] $params + * * @return mixed */ - public function getCommitBuildStatus($project_id, $sha, array $params = array()) + public function getCommitBuildStatus(int $project_id, string $sha, array $params = []) { - return $this->get($this->getProjectPath($project_id, 'repository/commits/'.$this->encodePath($sha).'/statuses'), $params); + return $this->get($this->getProjectPath($project_id, 'repository/commits/' . $this->encodePath($sha) . '/statuses'), $params); } /** - * @param int $project_id - * @param string $sha - * @param string $state - * @param array $params + * @param mixed[] $params + * * @return mixed */ - public function postCommitBuildStatus($project_id, $sha, $state, array $params = array()) + public function postCommitBuildStatus(int $project_id, string $sha, string $state, array $params = []) { $params['state'] = $state; - return $this->post($this->getProjectPath($project_id, 'statuses/'.$this->encodePath($sha)), $params); + return $this->post($this->getProjectPath($project_id, 'statuses/' . $this->encodePath($sha)), $params); } /** - * @param int $project_id - * @param string $fromShaOrMaster - * @param string $toShaOrMaster - * @param bool $straight * @return mixed */ - public function compare($project_id, $fromShaOrMaster, $toShaOrMaster, $straight = false) + public function compare(int $project_id, string $fromShaOrMaster, string $toShaOrMaster, bool $straight = false) { return $this->get($this->getProjectPath( $project_id, - 'repository/compare?from='.$this->encodePath($fromShaOrMaster).'&to='.$this->encodePath($toShaOrMaster).'&straight='.$this->encodePath($straight ? 'true' : 'false') + 'repository/compare?from=' . $this->encodePath($fromShaOrMaster) . '&to=' . $this->encodePath($toShaOrMaster) . '&straight=' . $this->encodePath($straight ? 'true' : 'false') )); } /** - * @param int $project_id - * @param string $sha - * @return string + * @return string[]|string */ - public function diff($project_id, $sha) + public function diff(int $project_id, string $sha) { - return $this->get($this->getProjectPath($project_id, 'repository/commits/'.$this->encodePath($sha).'/diff')); + return $this->get($this->getProjectPath($project_id, 'repository/commits/' . $this->encodePath($sha) . '/diff')); } /** - * @param int $project_id - * @param array $params + * @param mixed[] $params + * * @return mixed */ - public function tree($project_id, array $params = array()) + public function tree(int $project_id, array $params = []) { return $this->get($this->getProjectPath($project_id, 'repository/tree'), $params); } /** - * @param int $project_id - * @param string $sha - * @param string $filepath * @return mixed */ - public function blob($project_id, $sha, $filepath) + public function blob(int $project_id, string $sha, string $filepath) { @trigger_error(sprintf('The %s() method is deprecated since version 9.2 and will be removed in 10.0. Use the %s::getRawFile() method instead.', __METHOD__, RepositoryFiles::class), E_USER_DEPRECATED); @@ -379,12 +339,9 @@ public function blob($project_id, $sha, $filepath) } /** - * @param int $project_id - * @param string $file_path - * @param string $ref * @return mixed */ - public function getFile($project_id, $file_path, $ref) + public function getFile(int $project_id, string $file_path, string $ref) { @trigger_error(sprintf('The %s() method is deprecated since version 9.2 and will be removed in 10.0. Use the %s::getFile() method instead.', __METHOD__, RepositoryFiles::class), E_USER_DEPRECATED); @@ -392,17 +349,9 @@ public function getFile($project_id, $file_path, $ref) } /** - * @param int $project_id - * @param string $file_path - * @param string $content - * @param string $branch - * @param string $commit_message - * @param string $encoding - * @param string $author_email - * @param string $author_name * @return mixed */ - public function createFile($project_id, $file_path, $content, $branch, $commit_message, $encoding = null, $author_email = null, $author_name = null) + public function createFile(int $project_id, string $file_path, string $content, string $branch, string $commit_message, ?string $encoding = null, ?string $author_email = null, ?string $author_name = null) { @trigger_error(sprintf('The %s() method is deprecated since version 9.2 and will be removed in 10.0. Use the %s::createFile() method instead.', __METHOD__, RepositoryFiles::class), E_USER_DEPRECATED); @@ -418,17 +367,9 @@ public function createFile($project_id, $file_path, $content, $branch, $commit_m } /** - * @param int $project_id - * @param string $file_path - * @param string $content - * @param string $branch - * @param string $commit_message - * @param string $encoding - * @param string $author_email - * @param string $author_name * @return mixed */ - public function updateFile($project_id, $file_path, $content, $branch, $commit_message, $encoding = null, $author_email = null, $author_name = null) + public function updateFile(int $project_id, string $file_path, string $content, string $branch, string $commit_message, ?string $encoding = null, ?string $author_email = null, ?string $author_name = null) { @trigger_error(sprintf('The %s() method is deprecated since version 9.2 and will be removed in 10.0. Use the %s::updateFile() method instead.', __METHOD__, RepositoryFiles::class), E_USER_DEPRECATED); @@ -444,15 +385,9 @@ public function updateFile($project_id, $file_path, $content, $branch, $commit_m } /** - * @param int $project_id - * @param string $file_path - * @param string $branch - * @param string $commit_message - * @param string $author_email - * @param string $author_name * @return mixed */ - public function deleteFile($project_id, $file_path, $branch, $commit_message, $author_email = null, $author_name = null) + public function deleteFile(int $project_id, string $file_path, string $branch, string $commit_message, ?string $author_email = null, ?string $author_name = null) { @trigger_error(sprintf('The %s() method is deprecated since version 9.2 and will be removed in 10.0. Use the %s::deleteFile() method instead.', __METHOD__, RepositoryFiles::class), E_USER_DEPRECATED); @@ -466,40 +401,39 @@ public function deleteFile($project_id, $file_path, $branch, $commit_message, $a } /** - * @param int $project_id * @return mixed */ - public function contributors($project_id) + public function contributors(int $project_id) { return $this->get($this->getProjectPath($project_id, 'repository/contributors')); } /** - * @param int $project_id - * @param array $params - * @param string $format Options: "tar.gz", "zip", "tar.bz2" and "tar" + * @param mixed[] $params + * @param string $format Options: "tar.gz", "zip", "tar.bz2" and "tar" + * * @return mixed */ - public function archive($project_id, $params = array(), $format = 'tar.gz') + public function archive(int $project_id, array $params = [], string $format = 'tar.gz') { - return $this->get($this->getProjectPath($project_id, 'repository/archive.'.$format), $params); + return $this->get($this->getProjectPath($project_id, 'repository/archive.' . $format), $params); } /** - * @param int $project_id - * @param array $refs + * @param mixed[] $refs + * * @return mixed */ - public function mergeBase($project_id, $refs) + public function mergeBase(int $project_id, array $refs) { - return $this->get($this->getProjectPath($project_id, 'repository/merge_base'), array('refs' => $refs)); + return $this->get($this->getProjectPath($project_id, 'repository/merge_base'), ['refs' => $refs]); } - protected function createOptionsResolver() + protected function createOptionsResolver(): OptionsResolver { $allowedTypeValues = [ self::TYPE_BRANCH, - self::TYPE_TAG + self::TYPE_TAG, ]; $resolver = parent::createOptionsResolver(); diff --git a/lib/Gitlab/Api/RepositoryFiles.php b/src/Api/RepositoryFiles.php similarity index 74% rename from lib/Gitlab/Api/RepositoryFiles.php rename to src/Api/RepositoryFiles.php index bc20c00f0..3fbe16261 100644 --- a/lib/Gitlab/Api/RepositoryFiles.php +++ b/src/Api/RepositoryFiles.php @@ -1,40 +1,31 @@ get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($file_path)), array( - 'ref' => $ref - )); + return $this->get($this->getProjectPath($project_id, 'repository/files/' . $this->encodePath($file_path)), ['ref' => $ref]); } /** - * @param int $project_id - * @param string $file_path - * @param string $ref * @return mixed */ - public function getRawFile($project_id, $file_path, $ref) + public function getRawFile(int $project_id, string $file_path, string $ref) { - return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($file_path).'/raw'), array( - 'ref' => $ref, - )); + return $this->get($this->getProjectPath($project_id, 'repository/files/' . $this->encodePath($file_path) . '/raw'), ['ref' => $ref]); } /** - * @param int $project_id - * @param array $parameters ( + * @param mixed[] $parameters ( * * @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb. * @var string $branch Name of the branch. @@ -48,15 +39,14 @@ public function getRawFile($project_id, $file_path, $ref) * * @return mixed */ - public function createFile($project_id, array $parameters = []) + public function createFile(int $project_id, array $parameters = []) { $resolver = new OptionsResolver(); $resolver->setRequired('file_path'); $resolver->setRequired('branch'); $resolver->setDefined('start_branch'); $resolver->setDefined('encoding') - ->setAllowedValues('encoding', ['text', 'base64']) - ; + ->setAllowedValues('encoding', ['text', 'base64']); $resolver->setDefined('author_email'); $resolver->setDefined('author_name'); $resolver->setRequired('content'); @@ -64,12 +54,11 @@ public function createFile($project_id, array $parameters = []) $resolved = $resolver->resolve($parameters); - return $this->post($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved); + return $this->post($this->getProjectPath($project_id, 'repository/files/' . $this->encodePath($resolved['file_path'])), $resolved); } /** - * @param int $project_id - * @param array $parameters ( + * @param mixed[] $parameters ( * * @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb. * @var string $branch Name of the branch. @@ -84,15 +73,14 @@ public function createFile($project_id, array $parameters = []) * * @return mixed */ - public function updateFile($project_id, array $parameters = []) + public function updateFile(int $project_id, array $parameters = []) { $resolver = new OptionsResolver(); $resolver->setRequired('file_path'); $resolver->setRequired('branch'); $resolver->setDefined('start_branch'); $resolver->setDefined('encoding') - ->setAllowedValues('encoding', ['text', 'base64']) - ; + ->setAllowedValues('encoding', ['text', 'base64']); $resolver->setDefined('author_email'); $resolver->setDefined('author_name'); $resolver->setRequired('content'); @@ -101,12 +89,11 @@ public function updateFile($project_id, array $parameters = []) $resolved = $resolver->resolve($parameters); - return $this->put($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved); + return $this->put($this->getProjectPath($project_id, 'repository/files/' . $this->encodePath($resolved['file_path'])), $resolved); } /** - * @param int $project_id - * @param array $parameters ( + * @param mixed[] $parameters ( * * @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb. * @var string $branch Name of the branch. @@ -118,7 +105,7 @@ public function updateFile($project_id, array $parameters = []) * * @return mixed */ - public function deleteFile($project_id, array $parameters = []) + public function deleteFile(int $project_id, array $parameters = []) { $resolver = new OptionsResolver(); $resolver->setRequired('file_path'); @@ -130,6 +117,6 @@ public function deleteFile($project_id, array $parameters = []) $resolved = $resolver->resolve($parameters); - return $this->delete($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved); + return $this->delete($this->getProjectPath($project_id, 'repository/files/' . $this->encodePath($resolved['file_path'])), $resolved); } } diff --git a/src/Api/Schedules.php b/src/Api/Schedules.php new file mode 100644 index 000000000..17e2d8c76 --- /dev/null +++ b/src/Api/Schedules.php @@ -0,0 +1,52 @@ +post($this->getProjectPath($project_id, 'pipeline_schedules'), $params); + } + + /** + * @return mixed + */ + public function show(int $project_id, int $schedule_id) + { + return $this->get($this->getProjectPath($project_id, 'pipeline_schedules/' . $this->encodePath((string)$schedule_id))); + } + + /** + * @return mixed + */ + public function showAll(int $project_id) + { + return $this->get($this->getProjectPath($project_id, 'pipeline_schedules')); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function update(int $project_id, int $schedule_id, array $params) + { + return $this->put($this->getProjectPath($project_id, 'pipeline_schedules/' . $this->encodePath((string)$schedule_id)), $params); + } + + /** + * @return mixed + */ + public function remove(int $project_id, int $schedule_id) + { + return $this->delete($this->getProjectPath($project_id, 'pipeline_schedules/' . $this->encodePath((string)$schedule_id))); + } +} diff --git a/src/Api/Snippets.php b/src/Api/Snippets.php new file mode 100644 index 000000000..d408b4591 --- /dev/null +++ b/src/Api/Snippets.php @@ -0,0 +1,68 @@ +get($this->getProjectPath($project_id, 'snippets')); + } + + /** + * @return mixed + */ + public function show(int $project_id, int $snippet_id) + { + return $this->get($this->getProjectPath($project_id, 'snippets/' . $this->encodePath((string)$snippet_id))); + } + + /** + * @return mixed + */ + public function create(int $project_id, string $title, string $filename, string $code, string $visibility) + { + return $this->post($this->getProjectPath($project_id, 'snippets'), [ + 'title' => $title, + 'file_name' => $filename, + 'code' => $code, + 'visibility' => $visibility, + ]); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function update(int $project_id, int $snippet_id, array $params) + { + return $this->put($this->getProjectPath($project_id, 'snippets/' . $this->encodePath((string)$snippet_id)), $params); + } + + public function content(int $project_id, int $snippet_id): string + { + return $this->get($this->getProjectPath($project_id, 'snippets/' . $this->encodePath((string)$snippet_id) . '/raw')); + } + + /** + * @return mixed + */ + public function remove(int $project_id, int $snippet_id) + { + return $this->delete($this->getProjectPath($project_id, 'snippets/' . $this->encodePath((string)$snippet_id))); + } + + /** + * @return mixed + */ + public function awardEmoji(int $project_id, int $snippet_id) + { + return $this->get($this->getProjectPath($project_id, 'snippets/' . $this->encodePath((string)$snippet_id) . '/award_emoji')); + } +} diff --git a/src/Api/SystemHooks.php b/src/Api/SystemHooks.php new file mode 100644 index 000000000..2177aa33d --- /dev/null +++ b/src/Api/SystemHooks.php @@ -0,0 +1,40 @@ +get('hooks'); + } + + /** + * @return mixed + */ + public function create(string $url) + { + return $this->post('hooks', ['url' => $url]); + } + + /** + * @return mixed + */ + public function test(int $id) + { + return $this->get('hooks/' . $this->encodePath((string)$id)); + } + + /** + * @return mixed + */ + public function remove(int $id) + { + return $this->delete('hooks/' . $this->encodePath((string)$id)); + } +} diff --git a/src/Api/Tags.php b/src/Api/Tags.php new file mode 100644 index 000000000..363b593aa --- /dev/null +++ b/src/Api/Tags.php @@ -0,0 +1,62 @@ +get($this->getProjectPath($project_id, 'repository/tags')); + } + + /** + * @return mixed + */ + public function show(int $project_id, string $tag_name) + { + return $this->get($this->getProjectPath($project_id, 'repository/tags/' . $this->encodePath($tag_name))); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function create(int $project_id, array $params = []) + { + return $this->post($this->getProjectPath($project_id, 'repository/tags'), $params); + } + + /** + * @return mixed + */ + public function remove(int $project_id, string $tag_name) + { + return $this->delete($this->getProjectPath($project_id, 'repository/tags/' . $this->encodePath($tag_name))); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function createRelease(int $project_id, string $tag_name, array $params = []) + { + return $this->post($this->getProjectPath($project_id, 'repository/tags/' . $this->encodePath($tag_name) . '/release'), $params); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function updateRelease(int $project_id, string $tag_name, array $params = []) + { + return $this->put($this->getProjectPath($project_id, 'repository/tags/' . $this->encodePath($tag_name) . '/release'), $params); + } +} diff --git a/src/Api/Users.php b/src/Api/Users.php new file mode 100644 index 000000000..32ee1f7ef --- /dev/null +++ b/src/Api/Users.php @@ -0,0 +1,315 @@ +createOptionsResolver(); + $datetimeNormalizer = static function (Options $resolver, DateTimeInterface $value) { + return $value->format('c'); + }; + + $resolver->setDefined('search'); + $resolver->setDefined('username'); + $resolver->setDefined('external') + ->setAllowedTypes('external', 'bool'); + $resolver->setDefined('extern_uid'); + $resolver->setDefined('provider'); + $resolver->setDefined('created_before') + ->setAllowedTypes('created_before', DateTimeInterface::class) + ->setNormalizer('created_before', $datetimeNormalizer); + $resolver->setDefined('created_after') + ->setAllowedTypes('created_after', DateTimeInterface::class) + ->setNormalizer('created_after', $datetimeNormalizer); + $resolver->setDefined('active') + ->setAllowedTypes('active', 'bool') + ->setAllowedValues('active', true); + $resolver->setDefined('blocked') + ->setAllowedTypes('blocked', 'bool') + ->setAllowedValues('blocked', true); + + return $this->get('users', $resolver->resolve($parameters)); + } + + /** + * @return mixed + */ + public function show(int $id) + { + return $this->get('users/' . $this->encodePath((string)$id)); + } + + /** + * @return mixed + */ + public function usersProjects(int $id) + { + return $this->get('users/' . $this->encodePath((string)$id) . '/projects'); + } + + /** + * @return mixed + */ + public function user() + { + return $this->get('user'); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function create(string $email, string $password, array $params = []) + { + $params['email'] = $email; + $params['password'] = $password; + + return $this->post('users', $params); + } + + /** + * @param mixed[] $params + * @param mixed[] $files + * + * @return mixed + */ + public function update(int $id, array $params, array $files = []) + { + return $this->put('users/' . $this->encodePath((string)$id), $params, [], $files); + } + + /** + * @return mixed + */ + public function remove(int $id) + { + return $this->delete('users/' . $this->encodePath((string)$id)); + } + + /** + * @return mixed + */ + public function block(int $id) + { + return $this->post('users/' . $this->encodePath((string)$id) . '/block'); + } + + /** + * @return mixed + */ + public function unblock(int $id) + { + return $this->post('users/' . $this->encodePath((string)$id) . '/unblock'); + } + + /** + * @return mixed + */ + public function session(string $emailOrUsername, string $password) + { + return $this->post('session', [ + 'login' => $emailOrUsername, + 'email' => $emailOrUsername, + 'password' => $password, + ]); + } + + /** + * @return mixed + */ + public function login(string $email, string $password) + { + return $this->session($email, $password); + } + + /** + * @return mixed + */ + public function me() + { + return $this->get('user'); + } + + /** + * @return mixed + */ + public function keys() + { + return $this->get('user/keys'); + } + + /** + * @return mixed + */ + public function key(int $id) + { + return $this->get('user/keys/' . $this->encodePath((string)$id)); + } + + /** + * @return mixed + */ + public function createKey(string $title, string $key) + { + return $this->post('user/keys', [ + 'title' => $title, + 'key' => $key, + ]); + } + + /** + * @return mixed + */ + public function removeKey(int $id) + { + return $this->delete('user/keys/' . $this->encodePath((string)$id)); + } + + /** + * @return mixed + */ + public function userKeys(int $user_id) + { + return $this->get('users/' . $this->encodePath((string)$user_id) . '/keys'); + } + + /** + * @return mixed + */ + public function userKey(int $user_id, int $key_id) + { + return $this->get('users/' . $this->encodePath((string)$user_id) . '/keys/' . $this->encodePath((string)$key_id)); + } + + /** + * @return mixed + */ + public function createKeyForUser(int $user_id, string $title, string $key) + { + return $this->post('users/' . $this->encodePath((string)$user_id) . '/keys', [ + 'title' => $title, + 'key' => $key, + ]); + } + + /** + * @return mixed + */ + public function removeUserKey(int $user_id, int $key_id) + { + return $this->delete('users/' . $this->encodePath((string)$user_id) . '/keys/' . $this->encodePath((string)$key_id)); + } + + /** + * @return mixed + */ + public function emails() + { + return $this->get('user/emails'); + } + + /** + * @return mixed + */ + public function email(int $id) + { + return $this->get('user/emails/' . $this->encodePath((string)$id)); + } + + /** + * @return mixed + */ + public function userEmails(int $user_id) + { + return $this->get('users/' . $this->encodePath((string)$user_id) . '/emails'); + } + + /** + * @return mixed + */ + public function createEmailForUser(int $user_id, string $email, bool $skip_confirmation = false) + { + return $this->post('users/' . $this->encodePath((string)$user_id) . '/emails', [ + 'email' => $email, + 'skip_confirmation' => $skip_confirmation, + ]); + } + + /** + * @return mixed + */ + public function removeUserEmail(int $user_id, int $email_id) + { + return $this->delete('users/' . $this->encodePath((string)$user_id) . '/emails/' . $this->encodePath((string)$email_id)); + } + + /** + * @param mixed[] $params + * + * @return mixed + */ + public function userImpersonationTokens(int $user_id, array $params = []) + { + $resolver = $this->createOptionsResolver(); + + $resolver->setDefined('state') + ->setAllowedValues('state', ['all', 'active', 'inactive']); + + return $this->get('users/' . $this->encodePath((string)$user_id) . '/impersonation_tokens', $resolver->resolve($params)); + } + + /** + * @return mixed + */ + public function userImpersonationToken(int $user_id, int $impersonation_token_id) + { + return $this->get('users/' . $this->encodePath((string)$user_id) . '/impersonation_tokens/' . $this->encodePath((string)$impersonation_token_id)); + } + + /** + * @param mixed[] $scopes + * @param null $expires_at + * + * @return mixed + */ + public function createImpersonationToken(int $user_id, string $name, array $scopes, $expires_at = null) + { + return $this->post('users/' . $this->encodePath((string)$user_id) . '/impersonation_tokens', [ + 'name' => $name, + 'scopes' => $scopes, + 'expires_at' => $expires_at, + ]); + } + + /** + * @return mixed + */ + public function removeImpersonationToken(int $user_id, int $impersonation_token_id) + { + return $this->delete('users/' . $this->encodePath((string)$user_id) . '/impersonation_tokens/' . $this->encodePath((string)$impersonation_token_id)); + } +} diff --git a/src/Api/Version.php b/src/Api/Version.php new file mode 100644 index 000000000..bce29ae96 --- /dev/null +++ b/src/Api/Version.php @@ -0,0 +1,16 @@ +get('version'); + } +} diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 000000000..f588152b2 --- /dev/null +++ b/src/Client.php @@ -0,0 +1,355 @@ +responseHistory = new History(); + $this->httpClientBuilder = $httpClientBuilder ?: new Builder(); + + $this->httpClientBuilder->addPlugin(new GitlabExceptionThrower()); + $this->httpClientBuilder->addPlugin(new HistoryPlugin($this->responseHistory)); + $this->httpClientBuilder->addPlugin(new HeaderDefaultsPlugin(['User-Agent' => 'php-gitlab-api (http://github.com/jeroeny/gitlab-api)'])); + $this->httpClientBuilder->addPlugin(new RedirectPlugin()); + $this->httpClientBuilder->addPlugin(new ApiVersion()); + + $this->setUrl('https://gitlab.com'); + } + + /** + * Create a Gitlab\Client using an url. + * + * @return Client + */ + public static function create(string $url): self + { + $client = new self(); + $client->setUrl($url); + + return $client; + } + + /** + * Create a Gitlab\Api\Client using an HttpClient. + */ + public static function createWithHttpClient(HttpClient $httpClient): self + { + $builder = new Builder($httpClient); + + return new self($builder); + } + + public function deployKeys(): DeployKeys + { + return new DeployKeys($this); + } + + public function groups(): Groups + { + return new Groups($this); + } + + public function groupsMilestones(): GroupsMilestones + { + return new GroupsMilestones($this); + } + + public function issues(): Issues + { + return new Issues($this); + } + + public function issueBoards(): IssueBoards + { + return new IssueBoards($this); + } + + public function groupsBoards(): GroupsBoards + { + return new GroupsBoards($this); + } + + public function issueLinks(): IssueLinks + { + return new IssueLinks($this); + } + + public function jobs(): Jobs + { + return new Jobs($this); + } + + public function mergeRequests(): MergeRequests + { + return new MergeRequests($this); + } + + public function milestones(): Milestones + { + return new Milestones($this); + } + + public function namespaces(): ProjectNamespaces + { + return new ProjectNamespaces($this); + } + + public function projects(): Projects + { + return new Projects($this); + } + + public function repositories(): Repositories + { + return new Repositories($this); + } + + public function repositoryFiles(): RepositoryFiles + { + return new RepositoryFiles($this); + } + + public function snippets(): Snippets + { + return new Snippets($this); + } + + public function systemHooks(): SystemHooks + { + return new SystemHooks($this); + } + + public function users(): Users + { + return new Users($this); + } + + public function keys(): Keys + { + return new Keys($this); + } + + public function tags(): Tags + { + return new Tags($this); + } + + public function version(): Version + { + return new Version($this); + } + + public function deployments(): Deployments + { + return new Deployments($this); + } + + public function environments(): Environments + { + return new Environments($this); + } + + public function schedules(): Schedules + { + return new Schedules($this); + } + + public function issuesStatistics(): IssuesStatistics + { + return new IssuesStatistics($this); + } + + public function api(string $name): ApiBase + { + switch ($name) { + case 'deploy_keys': + return $this->deployKeys(); + case 'groups': + return $this->groups(); + case 'groupsMilestones': + return $this->groupsMilestones(); + case 'issues': + return $this->issues(); + case 'board': + case 'issue_boards': + return $this->issueBoards(); + case 'group_boards': + return $this->groupsBoards(); + case 'issue_links': + return $this->issueLinks(); + case 'jobs': + return $this->jobs(); + case 'mr': + case 'merge_requests': + return $this->mergeRequests(); + case 'milestones': + case 'ms': + return $this->milestones(); + case 'namespaces': + case 'ns': + return $this->namespaces(); + case 'projects': + return $this->projects(); + case 'repo': + case 'repositories': + return $this->repositories(); + case 'repositoryFiles': + return $this->repositoryFiles(); + case 'snippets': + return $this->snippets(); + case 'hooks': + case 'system_hooks': + return $this->systemHooks(); + case 'users': + return $this->users(); + case 'keys': + return $this->keys(); + case 'tags': + return $this->tags(); + case 'version': + return $this->version(); + case 'environments': + return $this->environments(); + case 'deployments': + return $this->deployments(); + case 'schedules': + return $this->schedules(); + case 'issues_statistics': + return $this->issuesStatistics(); + default: + throw new InvalidArgumentException('Invalid endpoint: "' . $name . '"'); + } + } + + /** + * Authenticate a user for all next requests + * + * @param string $token Gitlab private token + * @param string $authMethod One of the AUTH_* class constants + * + * @return $this + */ + public function authenticate(string $token, string $authMethod = self::AUTH_URL_TOKEN, ?string $sudo = null): self + { + $this->httpClientBuilder->removePlugin(Authentication::class); + $this->httpClientBuilder->addPlugin(new Authentication($authMethod, $token, $sudo)); + + return $this; + } + + public function setUrl(string $url): self + { + $this->httpClientBuilder->removePlugin(AddHostPlugin::class); + $this->httpClientBuilder->addPlugin(new AddHostPlugin(UriFactoryDiscovery::find()->createUri($url))); + + return $this; + } + + public function __get(string $api): ApiBase + { + return $this->api($api); + } + + public function getHttpClient(): HttpMethodsClient + { + return $this->httpClientBuilder->getHttpClient(); + } + + public function getResponseHistory(): History + { + return $this->responseHistory; + } +} diff --git a/src/Exception/ErrorException.php b/src/Exception/ErrorException.php new file mode 100644 index 000000000..08845d1c2 --- /dev/null +++ b/src/Exception/ErrorException.php @@ -0,0 +1,11 @@ + */ -class Builder +final class Builder { /** * The object that sends HTTP messages. @@ -36,16 +32,9 @@ class Builder */ private $pluginClient; - /** - * @var MessageFactory - */ + /** @var RequestFactory */ private $requestFactory; - /** - * @var StreamFactory - */ - private $streamFactory; - /** * True if we should create a new Plugin client at next request. * @@ -53,30 +42,18 @@ class Builder */ private $httpClientModified = true; - /** - * @var Plugin[] - */ + /** @var Plugin[] */ private $plugins = []; - /** - * @param HttpClient $httpClient - * @param RequestFactory $requestFactory - * @param StreamFactory $streamFactory - */ public function __construct( - HttpClient $httpClient = null, - RequestFactory $requestFactory = null, - StreamFactory $streamFactory = null + ?HttpClient $httpClient = null, + ?RequestFactory $requestFactory = null ) { - $this->httpClient = $httpClient ?: HttpClientDiscovery::find(); + $this->httpClient = $httpClient ?: HttpClientDiscovery::find(); $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); - $this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find(); } - /** - * @return HttpMethodsClient - */ - public function getHttpClient() + public function getHttpClient(): HttpMethodsClient { if ($this->httpClientModified) { $this->httpClientModified = false; @@ -92,27 +69,25 @@ public function getHttpClient() /** * Add a new plugin to the end of the plugin chain. - * - * @param Plugin $plugin */ - public function addPlugin(Plugin $plugin) + public function addPlugin(Plugin $plugin): void { - $this->plugins[] = $plugin; + $this->plugins[] = $plugin; $this->httpClientModified = true; } /** * Remove a plugin by its fully qualified class name (FQCN). - * - * @param string $fqcn */ - public function removePlugin($fqcn) + public function removePlugin(string $fqcn): void { foreach ($this->plugins as $idx => $plugin) { - if ($plugin instanceof $fqcn) { - unset($this->plugins[$idx]); - $this->httpClientModified = true; + if (! ($plugin instanceof $fqcn)) { + continue; } + + unset($this->plugins[$idx]); + $this->httpClientModified = true; } } } diff --git a/src/HttpClient/Message/QueryStringBuilder.php b/src/HttpClient/Message/QueryStringBuilder.php new file mode 100644 index 000000000..f429a0ec0 --- /dev/null +++ b/src/HttpClient/Message/QueryStringBuilder.php @@ -0,0 +1,83 @@ +getBody()->__toString(); if (strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0) { $content = json_decode($body, true); - if (JSON_ERROR_NONE === json_last_error()) { + if (json_last_error() === JSON_ERROR_NONE) { return $content; } } @@ -32,39 +42,33 @@ public static function getContent(ResponseInterface $response) /** * Extract pagination URIs from Link header. * - * @param ResponseInterface $response - * - * @return array|null + * @return mixed[]|null */ - public static function getPagination(ResponseInterface $response) + public static function getPagination(ResponseInterface $response): ?array { - if (!$response->hasHeader('Link')) { + if (! $response->hasHeader('Link')) { return null; } - $header = self::getHeader($response, 'Link'); - $pagination = array(); - foreach (explode(',', $header) as $link) { + $header = self::getHeader($response, 'Link'); + $pagination = []; + foreach (explode(',', $header ?? '') as $link) { preg_match('/<(.*)>; rel="(.*)"/i', trim($link, ','), $match); - if (3 === count($match)) { - $pagination[$match[2]] = $match[1]; + if (count($match) !== 3) { + continue; } + + $pagination[$match[2]] = $match[1]; } return $pagination; } - /** * Get the value for a single header. - * - * @param ResponseInterface $response - * @param string $name - * - * @return string|null */ - private static function getHeader(ResponseInterface $response, $name) + private static function getHeader(ResponseInterface $response, string $name): ?string { $headers = $response->getHeader($name); diff --git a/lib/Gitlab/HttpClient/Plugin/ApiVersion.php b/src/HttpClient/Plugin/ApiVersion.php similarity index 70% rename from lib/Gitlab/HttpClient/Plugin/ApiVersion.php rename to src/HttpClient/Plugin/ApiVersion.php index 61b475fdb..68c95a101 100644 --- a/lib/Gitlab/HttpClient/Plugin/ApiVersion.php +++ b/src/HttpClient/Plugin/ApiVersion.php @@ -1,30 +1,32 @@ */ -class ApiVersion implements Plugin +final class ApiVersion implements Plugin { + /** @var bool */ private $redirected = false; /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { $uri = $request->getUri(); - if (substr($uri->getPath(), 0, 8) !== '/api/v4/' && !$this->redirected) { - $request = $request->withUri($uri->withPath('/api/v4/'.$uri->getPath())); + if (substr($uri->getPath(), 0, 8) !== '/api/v4/' && ! $this->redirected) { + $request = $request->withUri($uri->withPath('/api/v4/' . $uri->getPath())); } return $next($request)->then(function (ResponseInterface $response) { diff --git a/lib/Gitlab/HttpClient/Plugin/Authentication.php b/src/HttpClient/Plugin/Authentication.php similarity index 59% rename from lib/Gitlab/HttpClient/Plugin/Authentication.php rename to src/HttpClient/Plugin/Authentication.php index aac0acc4a..5d7f10585 100644 --- a/lib/Gitlab/HttpClient/Plugin/Authentication.php +++ b/src/HttpClient/Plugin/Authentication.php @@ -1,81 +1,72 @@ - * @author Fabien Bourigault */ -class Authentication implements Plugin +final class Authentication implements Plugin { - /** - * @var string - */ + /** @var string */ private $method; - /** - * @var string - */ + /** @var string */ private $token; - /** - * @var string|null - */ + /** @var string|null */ private $sudo; - /** - * @param string $method - * @param string $token - * @param string|null $sudo - */ - public function __construct($method, $token, $sudo = null) + public function __construct(string $method, string $token, ?string $sudo = null) { - $this->method = $method; - $this->token = $token; - $this->sudo = $sudo; + $this->method = $method; + $this->token = $token; + $this->sudo = $sudo; } /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { switch ($this->method) { case Client::AUTH_HTTP_TOKEN: $request = $request->withHeader('PRIVATE-TOKEN', $this->token); - if (!is_null($this->sudo)) { + if ($this->sudo !== null) { $request = $request->withHeader('SUDO', $this->sudo); } break; case Client::AUTH_URL_TOKEN: - $uri = $request->getUri(); + $uri = $request->getUri(); $query = $uri->getQuery(); $parameters = [ 'private_token' => $this->token, ]; - if (!is_null($this->sudo)) { + if ($this->sudo !== null) { $parameters['sudo'] = $this->sudo; } $query .= empty($query) ? '' : '&'; - $query .= utf8_encode(http_build_query($parameters, '', '&')); + $query .= mb_convert_encoding(http_build_query($parameters, '', '&'), 'UTF-8'); - $uri = $uri->withQuery($query); + $uri = $uri->withQuery($query); $request = $request->withUri($uri); break; case Client::AUTH_OAUTH_TOKEN: - $request = $request->withHeader('Authorization', 'Bearer '.$this->token); - if (!is_null($this->sudo)) { + $request = $request->withHeader('Authorization', 'Bearer ' . $this->token); + if ($this->sudo !== null) { $request = $request->withHeader('SUDO', $this->sudo); } break; diff --git a/lib/Gitlab/HttpClient/Plugin/GitlabExceptionThrower.php b/src/HttpClient/Plugin/GitlabExceptionThrower.php similarity index 73% rename from lib/Gitlab/HttpClient/Plugin/GitlabExceptionThrower.php rename to src/HttpClient/Plugin/GitlabExceptionThrower.php index bb61e3121..9a46fad91 100644 --- a/lib/Gitlab/HttpClient/Plugin/GitlabExceptionThrower.php +++ b/src/HttpClient/Plugin/GitlabExceptionThrower.php @@ -1,44 +1,45 @@ - * @author Fabien Bourigault */ -class GitlabExceptionThrower implements Plugin +final class GitlabExceptionThrower implements Plugin { /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise { return $next($request)->then(function (ResponseInterface $response) { if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) { $content = ResponseMediator::getContent($response); if (is_array($content) && isset($content['message'])) { - if (400 == $response->getStatusCode()) { + if ($response->getStatusCode() === 400) { $message = $this->parseMessage($content['message']); - throw new ErrorException($message, 400); + throw new RuntimeException($message, 400); } } $errorMessage = null; if (isset($content['error'])) { - $errorMessage = $content['error']; - if (is_array($content['error'])) { - $errorMessage = implode("\n", $content['error']); - } + $errorMessage = is_array($content['error']) ? implode("\n", $content['error']) : $content['error']; } elseif (isset($content['message'])) { $errorMessage = $this->parseMessage($content['message']); } else { @@ -54,16 +55,14 @@ public function handleRequest(RequestInterface $request, callable $next, callabl /** * @param mixed $message - * - * @return string */ - private function parseMessage($message) + private function parseMessage($message): string { $string = $message; if (is_array($message)) { $format = '"%s" %s'; - $errors = array(); + $errors = []; foreach ($message as $field => $messages) { if (is_array($messages)) { @@ -71,7 +70,7 @@ private function parseMessage($message) foreach ($messages as $error) { $errors[] = sprintf($format, $field, $error); } - } elseif (is_integer($field)) { + } elseif (is_int($field)) { $errors[] = $messages; } else { $errors[] = sprintf($format, $field, $messages); diff --git a/lib/Gitlab/HttpClient/Plugin/History.php b/src/HttpClient/Plugin/History.php similarity index 58% rename from lib/Gitlab/HttpClient/Plugin/History.php rename to src/HttpClient/Plugin/History.php index 8412356fd..77c8b7809 100644 --- a/lib/Gitlab/HttpClient/Plugin/History.php +++ b/src/HttpClient/Plugin/History.php @@ -1,28 +1,23 @@ */ -class History implements Journal +final class History implements Journal { - /** - * @var ResponseInterface - */ + /** @var ResponseInterface */ private $lastResponse; - /** - * @return ResponseInterface|null - */ - public function getLastResponse() + public function getLastResponse(): ?ResponseInterface { return $this->lastResponse; } @@ -30,7 +25,7 @@ public function getLastResponse() /** * {@inheritdoc} */ - public function addSuccess(RequestInterface $request, ResponseInterface $response) + public function addSuccess(RequestInterface $request, ResponseInterface $response): void { $this->lastResponse = $response; } @@ -38,7 +33,7 @@ public function addSuccess(RequestInterface $request, ResponseInterface $respons /** * {@inheritdoc} */ - public function addFailure(RequestInterface $request, Exception $exception) + public function addFailure(RequestInterface $request, ClientExceptionInterface $exception): void { } } diff --git a/src/Model/Badge.php b/src/Model/Badge.php new file mode 100644 index 000000000..d9b952d97 --- /dev/null +++ b/src/Model/Badge.php @@ -0,0 +1,40 @@ +hydrate($data); + } + + public function __construct(Project $project, ?Client $client = null) + { + $this->setClient($client); + $this->setData('project', $project); + } +} diff --git a/lib/Gitlab/Model/Branch.php b/src/Model/Branch.php similarity index 56% rename from lib/Gitlab/Model/Branch.php rename to src/Model/Branch.php index 8c7ef36b4..651b3ccd2 100644 --- a/lib/Gitlab/Model/Branch.php +++ b/src/Model/Branch.php @@ -1,35 +1,32 @@ -hydrate($data); } - /** - * @param Project $project - * @param string $name - * @param Client $client - */ - public function __construct(Project $project, $name = null, Client $client = null) + public function __construct(Project $project, ?string $name = null, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); $this->setData('name', $name); } - /** - * @return Branch - */ - public function show() + public function show(): Branch { $data = $this->client->repositories()->branch($this->project->id, $this->name); return static::fromArray($this->getClient(), $this->project, $data); } - /** - * @param bool $devPush - * @param bool $devMerge - * @return Branch - */ - public function protect($devPush = false, $devMerge = false) + public function protect(bool $devPush = false, bool $devMerge = false): Branch { $data = $this->client->repositories()->protectBranch($this->project->id, $this->name, $devPush, $devMerge); return static::fromArray($this->getClient(), $this->project, $data); } - /** - * @return Branch - */ - public function unprotect() + public function unprotect(): Branch { $data = $this->client->repositories()->unprotectBranch($this->project->id, $this->name); return static::fromArray($this->getClient(), $this->project, $data); } - /** - * @return bool - */ - public function delete() + public function delete(): bool { $this->client->repositories()->deleteBranch($this->project->id, $this->name); @@ -95,49 +73,32 @@ public function delete() } /** - * @param array $parameters - * * @see Projects::commits for available parameters. * + * @param mixed[] $parameters + * * @return Commit[] */ - public function commits(array $parameters = []) + public function commits(array $parameters = []): array { return $this->project->commits($parameters); } - /** - * @param string $file_path - * @param string $content - * @param string $commit_message - * @return File - */ - public function createFile($file_path, $content, $commit_message) + public function createFile(string $file_path, string $content, string $commit_message): File { $data = $this->client->repositories()->createFile($this->project->id, $file_path, $content, $this->name, $commit_message); return File::fromArray($this->getClient(), $this->project, $data); } - /** - * @param string $file_path - * @param string $content - * @param string $commit_message - * @return File - */ - public function updateFile($file_path, $content, $commit_message) + public function updateFile(string $file_path, string $content, string $commit_message): File { $data = $this->client->repositories()->updateFile($this->project->id, $file_path, $content, $this->name, $commit_message); return File::fromArray($this->getClient(), $this->project, $data); } - /** - * @param string $file_path - * @param string $commit_message - * @return bool - */ - public function deleteFile($file_path, $commit_message) + public function deleteFile(string $file_path, string $commit_message): bool { $this->client->repositories()->deleteFile($this->project->id, $file_path, $this->name, $commit_message); diff --git a/lib/Gitlab/Model/Commit.php b/src/Model/Commit.php similarity index 72% rename from lib/Gitlab/Model/Commit.php rename to src/Model/Commit.php index 9b0073255..342d70e33 100644 --- a/lib/Gitlab/Model/Commit.php +++ b/src/Model/Commit.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Project $project - * @param int $id - * @param Client $client - */ - public function __construct(Project $project, $id = null, Client $client = null) + public function __construct(Project $project, ?int $id = null, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); diff --git a/lib/Gitlab/Model/CommitNote.php b/src/Model/CommitNote.php similarity index 55% rename from lib/Gitlab/Model/CommitNote.php rename to src/Model/CommitNote.php index 4b455c7f1..0572ba240 100644 --- a/lib/Gitlab/Model/CommitNote.php +++ b/src/Model/CommitNote.php @@ -1,35 +1,33 @@ -hydrate($data); } - /** - * @param Client $client - */ - public function __construct(Client $client = null) + public function __construct(?Client $client = null) { $this->setClient($client); } diff --git a/lib/Gitlab/Model/Comparison.php b/src/Model/Comparison.php similarity index 65% rename from lib/Gitlab/Model/Comparison.php rename to src/Model/Comparison.php index 27901829d..dd79221fc 100644 --- a/lib/Gitlab/Model/Comparison.php +++ b/src/Model/Comparison.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Project $project - * @param Client $client - */ - public function __construct(Project $project, Client $client = null) + public function __construct(Project $project, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); diff --git a/lib/Gitlab/Model/Contributor.php b/src/Model/Contributor.php similarity index 50% rename from lib/Gitlab/Model/Contributor.php rename to src/Model/Contributor.php index 658da48bc..34244dbe2 100644 --- a/lib/Gitlab/Model/Contributor.php +++ b/src/Model/Contributor.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Project $project - * @param Client $client - */ - public function __construct(Project $project, Client $client = null) + public function __construct(Project $project, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); diff --git a/lib/Gitlab/Model/Diff.php b/src/Model/Diff.php similarity index 55% rename from lib/Gitlab/Model/Diff.php rename to src/Model/Diff.php index 5fd95d5ac..bc0cb6d4d 100644 --- a/lib/Gitlab/Model/Diff.php +++ b/src/Model/Diff.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Project $project - * @param Client $client - */ - public function __construct(Project $project, Client $client = null) + public function __construct(Project $project, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return $this->diff; } diff --git a/lib/Gitlab/Model/Event.php b/src/Model/Event.php similarity index 64% rename from lib/Gitlab/Model/Event.php rename to src/Model/Event.php index 83565e174..62a04eb33 100644 --- a/lib/Gitlab/Model/Event.php +++ b/src/Model/Event.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Project $project - * @param Client $client - */ - public function __construct(Project $project, Client $client = null) + public function __construct(Project $project, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); diff --git a/src/Model/File.php b/src/Model/File.php new file mode 100644 index 000000000..a518af8b6 --- /dev/null +++ b/src/Model/File.php @@ -0,0 +1,39 @@ +hydrate($data); + } + + public function __construct(Project $project, ?string $file_path = null, ?Client $client = null) + { + $this->setClient($client); + $this->setData('project', $project); + $this->setData('file_path', $file_path); + } +} diff --git a/lib/Gitlab/Model/Group.php b/src/Model/Group.php similarity index 65% rename from lib/Gitlab/Model/Group.php rename to src/Model/Group.php index 97628f3d2..0d2445d5e 100644 --- a/lib/Gitlab/Model/Group.php +++ b/src/Model/Group.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Client $client - * @param string $name - * @param string $path - * @return Group - */ - public static function create(Client $client, $name, $path) + public static function create(Client $client, string $name, string $path): Group { $data = $client->groups()->create($name, $path); return static::fromArray($client, $data); } - /** - * @param int $id - * @param Client $client - */ - public function __construct($id, Client $client = null) + public function __construct(int $id, ?Client $client = null) { $this->setClient($client); $this->setData('id', $id); } - /** - * @return Group - */ - public function show() + public function show(): Group { $data = $this->client->groups()->show($this->id); - return Group::fromArray($this->getClient(), $data); + return self::fromArray($this->getClient(), $data); } - /** - * @param int $project_id - * @return Group - */ - public function transfer($project_id) + public function transfer(int $project_id): Group { $data = $this->client->groups()->transfer($this->id, $project_id); - return Group::fromArray($this->getClient(), $data); + return self::fromArray($this->getClient(), $data); } /** * @return User[] */ - public function members() + public function members(): array { $data = $this->client->groups()->members($this->id); - $members = array(); + $members = []; foreach ($data as $member) { $members[] = User::fromArray($this->getClient(), $member); } @@ -131,23 +112,14 @@ public function members() return $members; } - /** - * @param int $user_id - * @param int $access_level - * @return User - */ - public function addMember($user_id, $access_level) + public function addMember(int $user_id, int $access_level): User { $data = $this->client->groups()->addMember($this->id, $user_id, $access_level); return User::fromArray($this->getClient(), $data); } - /** - * @param int $user_id - * @return bool - */ - public function removeMember($user_id) + public function removeMember(int $user_id): bool { $this->client->groups()->removeMember($this->id, $user_id); @@ -157,11 +129,11 @@ public function removeMember($user_id) /** * @return Project[] */ - public function projects() + public function projects(): array { $data = $this->client->groups()->projects($this->id); - $projects = array(); + $projects = []; foreach ($data as $project) { $projects[] = Project::fromArray($this->getClient(), $project); } @@ -172,13 +144,13 @@ public function projects() /** * @return Group[] */ - public function subgroups() + public function subgroups(): array { $data = $this->client->groups()->subgroups($this->id); - $groups = array(); + $groups = []; foreach ($data as $group) { - $groups[] = Group::fromArray($this->getClient(), $group); + $groups[] = self::fromArray($this->getClient(), $group); } return $groups; diff --git a/lib/Gitlab/Model/GroupMilestone.php b/src/Model/GroupMilestone.php similarity index 61% rename from lib/Gitlab/Model/GroupMilestone.php rename to src/Model/GroupMilestone.php index 8814cc3de..d47027ea4 100644 --- a/lib/Gitlab/Model/GroupMilestone.php +++ b/src/Model/GroupMilestone.php @@ -1,12 +1,12 @@ setClient($client); $this->setData('id', $id); diff --git a/src/Model/Hook.php b/src/Model/Hook.php new file mode 100644 index 000000000..500105614 --- /dev/null +++ b/src/Model/Hook.php @@ -0,0 +1,59 @@ +hydrate($data); + } + + public static function create(Client $client, string $url): Hook + { + $data = $client->systemHooks()->create($url); + + return static::fromArray($client, $data); + } + + public function __construct(int $id, ?Client $client = null) + { + $this->setClient($client); + $this->setData('id', $id); + } + + public function test(): bool + { + $this->client->systemHooks()->test($this->id); + + return true; + } + + public function delete(): bool + { + $this->client->systemHooks()->remove($this->id); + + return true; + } +} diff --git a/lib/Gitlab/Model/Issue.php b/src/Model/Issue.php similarity index 64% rename from lib/Gitlab/Model/Issue.php rename to src/Model/Issue.php index 094a547cb..0bb29bd54 100644 --- a/lib/Gitlab/Model/Issue.php +++ b/src/Model/Issue.php @@ -1,10 +1,15 @@ -hydrate($data); } - /** - * @param Project $project - * @param int $iid - * @param Client $client - */ - public function __construct(Project $project, $iid = null, Client $client = null) + public function __construct(Project $project, ?int $iid = null, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); $this->setData('iid', $iid); } - /** - * @return Issue - */ - public function show() + public function show(): Issue { $data = $this->client->issues()->show($this->project->id, $this->iid); @@ -86,69 +78,44 @@ public function show() } /** - * @param array $params - * @return Issue + * @param mixed[] $params */ - public function update(array $params) + public function update(array $params): Issue { $data = $this->client->issues()->update($this->project->id, $this->iid, $params); return static::fromArray($this->getClient(), $this->project, $data); } - /** - * @param Project $toProject - * @return Issue - */ - public function move(Project $toProject) + public function move(Project $toProject): Issue { $data = $this->client->issues()->move($this->project->id, $this->iid, $toProject->id); return static::fromArray($this->getClient(), $toProject, $data); } - /** - * @param string $comment - * @return Issue - */ - public function close($comment = null) + public function close(?string $comment = null): Issue { if ($comment) { $this->addComment($comment); } - return $this->update(array( - 'state_event' => 'close' - )); + return $this->update(['state_event' => 'close']); } - /** - * @return Issue - */ - public function open() + public function open(): Issue { - return $this->update(array( - 'state_event' => 'reopen' - )); + return $this->update(['state_event' => 'reopen']); } - /** - * @return Issue - */ - public function reopen() + public function reopen(): Issue { return $this->open(); } - /** - * @param string $comment - * @return Note - */ - public function addComment($comment) + public function addComment(string $comment): Note { - $data = $this->client->issues()->addComment($this->project->id, $this->iid, array( - 'body' => $comment - )); + $data = $this->client->issues()->addComment($this->project->id, $this->iid, ['body' => $comment]); return Note::fromArray($this->getClient(), $this, $data); } @@ -156,10 +123,10 @@ public function addComment($comment) /** * @return Note[] */ - public function showComments() + public function showComments(): array { - $notes = array(); - $data = $this->client->issues()->showComments($this->project->id, $this->iid); + $notes = []; + $data = $this->client->issues()->showComments($this->project->id, $this->iid); foreach ($data as $note) { $notes[] = Note::fromArray($this->getClient(), $this, $note); @@ -168,31 +135,24 @@ public function showComments() return $notes; } - /** - * @return bool - */ - public function isClosed() + public function isClosed(): bool { return $this->state === 'closed'; } - /** - * @param string $label - * @return bool - */ - public function hasLabel($label) + public function hasLabel(string $label): bool { - return in_array($label, $this->labels); + return in_array($label, $this->labels, true); } /** * @return IssueLink[] */ - public function links() + public function links(): array { $data = $this->client->issueLinks()->all($this->project->id, $this->iid); - if (!is_array($data)) { - return array(); + if (! is_array($data)) { + return []; } $projects = $this->client->projects(); @@ -207,14 +167,13 @@ public function links() } /** - * @param Issue $target * @return Issue[] */ - public function addLink(Issue $target) + public function addLink(Issue $target): array { $data = $this->client->issueLinks()->create($this->project->id, $this->iid, $target->project->id, $target->iid); - if (!is_array($data)) { - return array(); + if (! is_array($data)) { + return []; } return [ @@ -224,15 +183,14 @@ public function addLink(Issue $target) } /** - * @param int $issue_link_id * @return Issue[] */ - public function removeLink($issue_link_id) + public function removeLink(int $issue_link_id): array { // The two related issues have the same link ID. $data = $this->client->issueLinks()->remove($this->project->id, $this->iid, $issue_link_id); - if (!is_array($data)) { - return array(); + if (! is_array($data)) { + return []; } $targetProject = Project::fromArray( diff --git a/src/Model/IssueLink.php b/src/Model/IssueLink.php new file mode 100644 index 000000000..a67cbf8e4 --- /dev/null +++ b/src/Model/IssueLink.php @@ -0,0 +1,38 @@ +hydrate($data); + } + + public function __construct(Issue $issue, ?int $issue_link_id = null, ?Client $client = null) + { + $this->setClient($client); + $this->setData('issue', $issue); + $this->setData('issue_link_id', $issue_link_id); + } +} diff --git a/lib/Gitlab/Model/Job.php b/src/Model/Job.php similarity index 72% rename from lib/Gitlab/Model/Job.php rename to src/Model/Job.php index 586e3a311..a7d085254 100644 --- a/lib/Gitlab/Model/Job.php +++ b/src/Model/Job.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Project $project - * @param int $id - * @param Client $client - */ - public function __construct(Project $project, $id = null, Client $client = null) + public function __construct(Project $project, ?int $id = null, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); diff --git a/src/Model/Key.php b/src/Model/Key.php new file mode 100644 index 000000000..968ca244d --- /dev/null +++ b/src/Model/Key.php @@ -0,0 +1,39 @@ +hydrate($data); + } + + public function __construct(?Client $client = null) + { + $this->setClient($client); + } +} diff --git a/src/Model/Label.php b/src/Model/Label.php new file mode 100644 index 000000000..0e4e33da3 --- /dev/null +++ b/src/Model/Label.php @@ -0,0 +1,38 @@ +hydrate($data); + } + + public function __construct(Project $project, ?Client $client = null) + { + $this->setClient($client); + $this->setData('project', $project); + } +} diff --git a/lib/Gitlab/Model/MergeRequest.php b/src/Model/MergeRequest.php similarity index 60% rename from lib/Gitlab/Model/MergeRequest.php rename to src/Model/MergeRequest.php index 03251f3b0..aee26af6a 100644 --- a/lib/Gitlab/Model/MergeRequest.php +++ b/src/Model/MergeRequest.php @@ -1,10 +1,13 @@ -hydrate($data); } - /** - * @param Project $project - * @param int $iid - * @param Client $client - */ - public function __construct(Project $project, $iid = null, Client $client = null) + public function __construct(Project $project, ?int $iid = null, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); $this->setData('iid', $iid); } - /** - * @return MergeRequest - */ - public function show() + public function show(): MergeRequest { $data = $this->client->mergeRequests()->show($this->project->id, $this->iid); @@ -111,77 +101,47 @@ public function show() } /** - * @param array $params - * @return MergeRequest + * @param mixed[] $params */ - public function update(array $params) + public function update(array $params): MergeRequest { $data = $this->client->mergeRequests()->update($this->project->id, $this->iid, $params); return static::fromArray($this->getClient(), $this->project, $data); } - /** - * @param string $comment - * @return MergeRequest - */ - public function close($comment = null) + public function close(?string $comment = null): MergeRequest { if ($comment) { $this->addComment($comment); } - return $this->update(array( - 'state_event' => 'close' - )); + return $this->update(['state_event' => 'close']); } - /** - * @return MergeRequest - */ - public function reopen() + public function reopen(): MergeRequest { - return $this->update(array( - 'state_event' => 'reopen' - )); + return $this->update(['state_event' => 'reopen']); } - /** - * @return MergeRequest - */ - public function open() + public function open(): MergeRequest { return $this->reopen(); } - /** - * @param string $message - * @return MergeRequest - */ - public function merge($message = null) + public function merge(?string $message = null): MergeRequest { - $data = $this->client->mergeRequests()->merge($this->project->id, $this->iid, array( - 'merge_commit_message' => $message - )); + $data = $this->client->mergeRequests()->merge($this->project->id, $this->iid, ['merge_commit_message' => $message]); return static::fromArray($this->getClient(), $this->project, $data); } - /** - * @return MergeRequest - */ - public function merged() + public function merged(): MergeRequest { - return $this->update(array( - 'state_event' => 'merge' - )); + return $this->update(['state_event' => 'merge']); } - /** - * @param string $comment - * @return Note - */ - public function addComment($comment) + public function addComment(string $comment): Note { $data = $this->client->mergeRequests()->addComment($this->project->id, $this->iid, $comment); @@ -191,10 +151,10 @@ public function addComment($comment) /** * @return Note[] */ - public function showComments() + public function showComments(): array { - $notes = array(); - $data = $this->client->mergeRequests()->showComments($this->project->id, $this->iid); + $notes = []; + $data = $this->client->mergeRequests()->showComments($this->project->id, $this->iid); foreach ($data as $note) { $notes[] = Note::fromArray($this->getClient(), $this, $note); @@ -203,22 +163,12 @@ public function showComments() return $notes; } - /** - * @return bool - */ - public function isClosed() + public function isClosed(): bool { - if (in_array($this->state, array('closed', 'merged'))) { - return true; - } - - return false; + return in_array($this->state, ['closed', 'merged'], true); } - /** - * @return MergeRequest - */ - public function changes() + public function changes(): MergeRequest { $data = $this->client->mergeRequests()->changes($this->project->id, $this->iid); diff --git a/lib/Gitlab/Model/Milestone.php b/src/Model/Milestone.php similarity index 60% rename from lib/Gitlab/Model/Milestone.php rename to src/Model/Milestone.php index 8b07d38af..4107ac7f8 100644 --- a/lib/Gitlab/Model/Milestone.php +++ b/src/Model/Milestone.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Project $project - * @param int $id - * @param Client $client - */ - public function __construct(Project $project, $id, Client $client = null) + public function __construct(Project $project, int $id, ?Client $client = null) { $this->setClient($client); $this->setData('id', $id); $this->setData('project', $project); } - /** - * @return Milestone - */ - public function show() + public function show(): Milestone { $data = $this->client->milestones()->show($this->project->id, $this->id); @@ -74,40 +63,33 @@ public function show() } /** - * @param array $params - * @return Milestone + * @param mixed[] $params */ - public function update(array $params) + public function update(array $params): Milestone { $data = $this->client->milestones()->update($this->project->id, $this->id, $params); return static::fromArray($this->getClient(), $this->project, $data); } - /** - * @return Milestone - */ - public function complete() + public function complete(): Milestone { - return $this->update(array('closed' => true)); + return $this->update(['closed' => true]); } - /** - * @return Milestone - */ - public function incomplete() + public function incomplete(): Milestone { - return $this->update(array('closed' => false)); + return $this->update(['closed' => false]); } /** * @return Issue[] */ - public function issues() + public function issues(): array { $data = $this->client->milestones()->issues($this->project->id, $this->id); - $issues = array(); + $issues = []; foreach ($data as $issue) { $issues[] = Issue::fromArray($this->getClient(), $this->project, $issue); } diff --git a/lib/Gitlab/Model/AbstractModel.php b/src/Model/Model.php similarity index 50% rename from lib/Gitlab/Model/AbstractModel.php rename to src/Model/Model.php index 7c11d4b21..7e4244b8d 100644 --- a/lib/Gitlab/Model/AbstractModel.php +++ b/src/Model/Model.php @@ -1,63 +1,54 @@ -client; } /** - * @param Client $client * @return $this */ - public function setClient(Client $client = null) + public function setClient(?Client $client = null): self { - if (null !== $client) { + if ($client !== null) { $this->client = $client; } return $this; } - /** - * @param string $api - * @return AbstractApi|mixed - */ - public function api($api) + public function api(string $api): ApiBase { return $this->getClient()->api($api); } /** - * @param array $data - * @return $this + * @param array $data */ - protected function hydrate(array $data = array()) + protected function hydrate(array $data = []): self { - if (!empty($data)) { + if ($data !== []) { foreach ($data as $field => $value) { $this->setData($field, $value); } @@ -67,13 +58,11 @@ protected function hydrate(array $data = array()) } /** - * @param string $field * @param mixed $value - * @return $this */ - protected function setData($field, $value) + protected function setData(string $field, $value): self { - if (in_array($field, static::$properties)) { + if (in_array($field, static::$properties, true)) { $this->data[$field] = $value; } @@ -81,33 +70,33 @@ protected function setData($field, $value) } /** - * @return array + * @return mixed[] */ - public function getData() + public function getData(): array { return $this->data; } /** - * @param string $property * @param mixed $value + * * @throws RuntimeException */ - public function __set($property, $value) + public function __set(string $property, $value): void { throw new RuntimeException('Model properties are immutable'); } /** - * @param string $property * @return mixed */ - public function __get($property) + public function __get(string $property) { - if (!in_array($property, static::$properties)) { + if (! in_array($property, static::$properties, true)) { throw new RuntimeException(sprintf( 'Property "%s" does not exist for %s object', - $property, get_called_class() + $property, + static::class )); } @@ -118,11 +107,7 @@ public function __get($property) return null; } - /** - * @param string $property - * @return bool - */ - public function __isset($property) + public function __isset(string $property): bool { return isset($this->data[$property]); } diff --git a/src/Model/Node.php b/src/Model/Node.php new file mode 100644 index 000000000..6ed4cdda1 --- /dev/null +++ b/src/Model/Node.php @@ -0,0 +1,44 @@ +hydrate($data); + } + + public function __construct(Project $project, ?int $id = null, ?Client $client = null) + { + $this->setClient($client); + $this->setData('project', $project); + $this->setData('id', $id); + } +} diff --git a/lib/Gitlab/Model/Note.php b/src/Model/Note.php similarity index 62% rename from lib/Gitlab/Model/Note.php rename to src/Model/Note.php index 3bfd1eec8..7bbdc7281 100644 --- a/lib/Gitlab/Model/Note.php +++ b/src/Model/Note.php @@ -1,11 +1,14 @@ -hydrate($data); } - /** - * @param Noteable $type - * @param Client $client - */ - public function __construct(Noteable $type, Client $client = null) + public function __construct(Noteable $type, ?Client $client = null) { $this->setClient($client); $this->setData('parent_type', get_class($type)); diff --git a/src/Model/Noteable.php b/src/Model/Noteable.php new file mode 100644 index 000000000..3df95fe35 --- /dev/null +++ b/src/Model/Noteable.php @@ -0,0 +1,32 @@ +hydrate($data); + } + + public function __construct(Project $project, ?int $id = null, ?Client $client = null) + { + $this->setClient($client); + $this->setData('project', $project); + $this->setData('id', $id); + } +} diff --git a/lib/Gitlab/Model/Project.php b/src/Model/Project.php similarity index 62% rename from lib/Gitlab/Model/Project.php rename to src/Model/Project.php index 012940251..af3cf6cbb 100644 --- a/lib/Gitlab/Model/Project.php +++ b/src/Model/Project.php @@ -1,12 +1,15 @@ -setClient($client); @@ -126,12 +125,9 @@ public static function fromArray(Client $client, array $data) } /** - * @param Client $client - * @param string $name - * @param array $params - * @return Project + * @param mixed[] $params */ - public static function create(Client $client, $name, array $params = array()) + public static function create(Client $client, string $name, array $params = []): Project { $data = $client->projects()->create($name, $params); @@ -139,33 +135,22 @@ public static function create(Client $client, $name, array $params = array()) } /** - * @param int $user_id - * @param Client $client - * @param string $name - * @param array $params - * @return Project + * @param mixed[] $params */ - public static function createForUser($user_id, Client $client, $name, array $params = array()) + public static function createForUser(int $user_id, Client $client, string $name, array $params = []): Project { $data = $client->projects()->createForUser($user_id, $name, $params); return static::fromArray($client, $data); } - /** - * @param int $id - * @param Client $client - */ - public function __construct($id = null, Client $client = null) + public function __construct(?int $id = null, ?Client $client = null) { $this->setClient($client); $this->setData('id', $id); } - /** - * @return Project - */ - public function show() + public function show(): Project { $data = $this->client->projects()->show($this->id); @@ -173,40 +158,30 @@ public function show() } /** - * @param array $params - * @return Project + * @param mixed[] $params */ - public function update(array $params) + public function update(array $params): Project { $data = $this->client->projects()->update($this->id, $params); return static::fromArray($this->getClient(), $data); } - /** - * @return Project - */ - public function archive() + public function archive(): Project { $data = $this->client->projects()->archive($this->id); return static::fromArray($this->getClient(), $data); } - /** - * @return Project - */ - public function unarchive() + public function unarchive(): Project { $data = $this->client->projects()->unarchive($this->id); return static::fromArray($this->getClient(), $data); } - /** - * @return bool - */ - public function remove() + public function remove(): bool { $this->client->projects()->remove($this->id); @@ -214,14 +189,13 @@ public function remove() } /** - * @param string $username_query * @return User[] */ - public function members($username_query = null) + public function members(?string $username_query = null): array { - $data = $this->client->projects()->members($this->id, $username_query); + $data = $this->client->projects()->members($this->id, ['query' => $username_query]); - $members = array(); + $members = []; foreach ($data as $member) { $members[] = User::fromArray($this->getClient(), $member); } @@ -229,46 +203,28 @@ public function members($username_query = null) return $members; } - /** - * @param int $user_id - * @return User - */ - public function member($user_id) + public function member(int $user_id): User { $data = $this->client->projects()->member($this->id, $user_id); return User::fromArray($this->getClient(), $data); } - /** - * @param int $user_id - * @param int $access_level - * @return User - */ - public function addMember($user_id, $access_level) + public function addMember(int $user_id, int $access_level): User { $data = $this->client->projects()->addMember($this->id, $user_id, $access_level); return User::fromArray($this->getClient(), $data); } - /** - * @param int $user_id - * @param int $access_level - * @return User - */ - public function saveMember($user_id, $access_level) + public function saveMember(int $user_id, int $access_level): User { $data = $this->client->projects()->saveMember($this->id, $user_id, $access_level); return User::fromArray($this->getClient(), $data); } - /** - * @param int $user_id - * @return bool - */ - public function removeMember($user_id) + public function removeMember(int $user_id): bool { $this->client->projects()->removeMember($this->id, $user_id); @@ -276,17 +232,17 @@ public function removeMember($user_id) } /** - * @param array $parameters - * - * @return ProjectHook[] * @see Projects::hooks() for available parameters. * + * @param mixed[] $parameters + * + * @return ProjectHook[] */ - public function hooks(array $parameters = []) + public function hooks(array $parameters = []): array { $data = $this->client->projects()->hooks($this->id, $parameters); - $hooks = array(); + $hooks = []; foreach ($data as $hook) { $hooks[] = ProjectHook::fromArray($this->getClient(), $this, $hook); } @@ -294,11 +250,7 @@ public function hooks(array $parameters = []) return $hooks; } - /** - * @param int $id - * @return ProjectHook - */ - public function hook($id) + public function hook(int $id): ProjectHook { $hook = new ProjectHook($this, $id, $this->getClient()); @@ -306,11 +258,9 @@ public function hook($id) } /** - * @param string $url - * @param array $events - * @return ProjectHook + * @param mixed[] $events */ - public function addHook($url, array $events = array()) + public function addHook(string $url, array $events = []): ProjectHook { $data = $this->client->projects()->addHook($this->id, $url, $events); @@ -318,22 +268,18 @@ public function addHook($url, array $events = array()) } /** - * @param int $hook_id - * @param array $params + * @param mixed[] $params + * * @return mixed */ - public function updateHook($hook_id, array $params) + public function updateHook(int $hook_id, array $params) { $hook = new ProjectHook($this, $hook_id, $this->getClient()); return $hook->update($params); } - /** - * @param int $hook_id - * @return bool - */ - public function removeHook($hook_id) + public function removeHook(int $hook_id): bool { $hook = new ProjectHook($this, $hook_id, $this->getClient()); @@ -343,11 +289,11 @@ public function removeHook($hook_id) /** * @return Key[] */ - public function deployKeys() + public function deployKeys(): array { $data = $this->client->projects()->deployKeys($this->id); - $keys = array(); + $keys = []; foreach ($data as $key) { $keys[] = Key::fromArray($this->getClient(), $key); } @@ -355,69 +301,42 @@ public function deployKeys() return $keys; } - /** - * @param int $key_id - * @return Key - */ - public function deployKey($key_id) + public function deployKey(int $key_id): Key { $data = $this->client->projects()->deployKey($this->id, $key_id); return Key::fromArray($this->getClient(), $data); } - /** - * @param string $title - * @param string $key - * @param bool $canPush - * @return Key - */ - public function addDeployKey($title, $key, $canPush = false) + public function addDeployKey(string $title, string $key, bool $canPush = false): Key { $data = $this->client->projects()->addDeployKey($this->id, $title, $key, $canPush); return Key::fromArray($this->getClient(), $data); } - /** - * @param string $key_id - * @return bool - */ - public function deleteDeployKey($key_id) + public function deleteDeployKey(int $key_id): bool { $this->client->projects()->deleteDeployKey($this->id, $key_id); return true; } - /** - * @param string $key_id - * @return bool - */ - public function enableDeployKey($key_id) + public function enableDeployKey(int $key_id): bool { $this->client->projects()->enableDeployKey($this->id, $key_id); return true; } - /** - * @param string $name - * @param string $ref - * @return Branch - */ - public function createBranch($name, $ref) + public function createBranch(string $name, string $ref): Branch { $data = $this->client->repositories()->createBranch($this->id, $name, $ref); return Branch::fromArray($this->getClient(), $this, $data); } - /** - * @param string $name - * @return bool - */ - public function deleteBranch($name) + public function deleteBranch(string $name): bool { $this->client->repositories()->deleteBranch($this->id, $name); @@ -427,11 +346,11 @@ public function deleteBranch($name) /** * @return Branch[] */ - public function branches() + public function branches(): array { $data = $this->client->repositories()->branches($this->id); - $branches = array(); + $branches = []; foreach ($data as $branch) { $branches[] = Branch::fromArray($this->getClient(), $this, $branch); } @@ -439,11 +358,7 @@ public function branches() return $branches; } - /** - * @param string $branch_name - * @return Branch - */ - public function branch($branch_name) + public function branch(string $branch_name): Branch { $branch = new Branch($this, $branch_name); $branch->setClient($this->getClient()); @@ -451,13 +366,7 @@ public function branch($branch_name) return $branch->show(); } - /** - * @param string $branch_name - * @param bool $devPush - * @param bool $devMerge - * @return Branch - */ - public function protectBranch($branch_name, $devPush = false, $devMerge = false) + public function protectBranch(string $branch_name, bool $devPush = false, bool $devMerge = false): Branch { $branch = new Branch($this, $branch_name); $branch->setClient($this->getClient()); @@ -465,11 +374,7 @@ public function protectBranch($branch_name, $devPush = false, $devMerge = false) return $branch->protect($devPush, $devMerge); } - /** - * @param string $branch_name - * @return Branch - */ - public function unprotectBranch($branch_name) + public function unprotectBranch(string $branch_name): Branch { $branch = new Branch($this, $branch_name); $branch->setClient($this->getClient()); @@ -480,11 +385,11 @@ public function unprotectBranch($branch_name) /** * @return Tag[] */ - public function tags() + public function tags(): array { $data = $this->client->repositories()->tags($this->id); - $tags = array(); + $tags = []; foreach ($data as $tag) { $tags[] = Tag::fromArray($this->getClient(), $this, $tag); } @@ -493,17 +398,17 @@ public function tags() } /** - * @param array $parameters - * - * @return Commit[] * @see Repositories::commits() for available parameters. * + * @param mixed[] $parameters + * + * @return Commit[] */ - public function commits(array $parameters = []) + public function commits(array $parameters = []): array { $data = $this->client->repositories()->commits($this->id, $parameters); - $commits = array(); + $commits = []; foreach ($data as $commit) { $commits[] = Commit::fromArray($this->getClient(), $this, $commit); } @@ -511,11 +416,7 @@ public function commits(array $parameters = []) return $commits; } - /** - * @param string $sha - * @return Commit - */ - public function commit($sha) + public function commit(string $sha): Commit { $data = $this->client->repositories()->commit($this->id, $sha); @@ -523,18 +424,17 @@ public function commit($sha) } /** - * @param string $ref - * @param array $parameters - * - * @return Commit[] * @see Repositories::commitComments() for available parameters. * + * @param mixed[] $parameters + * + * @return CommitNote[] */ - public function commitComments($ref, array $parameters = []) + public function commitComments(string $ref, array $parameters = []): array { $data = $this->client->repositories()->commitComments($this->id, $ref, $parameters); - $comments = array(); + $comments = []; foreach ($data as $comment) { $comments[] = CommitNote::fromArray($this->getClient(), $comment); } @@ -543,12 +443,9 @@ public function commitComments($ref, array $parameters = []) } /** - * @param string $ref - * @param string $note - * @param array $params - * @return CommitNote + * @param mixed[] $params */ - public function createCommitComment($ref, $note, array $params = array()) + public function createCommitComment(string $ref, string $note, array $params = []): CommitNote { $data = $this->client->repositories()->createCommitComment($this->id, $ref, $note, $params); @@ -556,20 +453,14 @@ public function createCommitComment($ref, $note, array $params = array()) } /** - * @param string $sha - * @return string + * @return string|string[] */ - public function diff($sha) + public function diff(string $sha) { return $this->client->repositories()->diff($this->id, $sha); } - /** - * @param string $from - * @param string $to - * @return Comparison - */ - public function compare($from, $to) + public function compare(string $from, string $to): Comparison { $data = $this->client->repositories()->compare($this->id, $from, $to); @@ -577,14 +468,15 @@ public function compare($from, $to) } /** - * @param array $params + * @param mixed[] $params + * * @return Node[] */ - public function tree(array $params = array()) + public function tree(array $params = []): array { $data = $this->client->repositories()->tree($this->id, $params); - $tree = array(); + $tree = []; foreach ($data as $node) { $tree[] = Node::fromArray($this->getClient(), $this, $node); } @@ -592,44 +484,27 @@ public function tree(array $params = array()) return $tree; } - /** - * @param string $sha - * @param string $filepath - * @return string - */ - public function blob($sha, $filepath) + public function blob(string $sha, string $filepath): string { return $this->client->repositories()->blob($this->id, $sha, $filepath); } /** - * @param $sha - * @param $filepath - * - * @return array + * @return mixed[] */ - public function getFile($sha, $filepath) + public function getFile(string $sha, string $filepath): array { return $this->client->repositories()->getFile($this->id, $filepath, $sha); } - /** - * @param string $file_path - * @param string $content - * @param string $branch_name - * @param string $commit_message - * @param string $author_email - * @param string $author_name - * @return File - */ public function createFile( - $file_path, - $content, - $branch_name, - $commit_message, - $author_email = null, - $author_name = null - ) { + string $file_path, + string $content, + string $branch_name, + string $commit_message, + ?string $author_email = null, + ?string $author_name = null + ): File { $parameters = [ 'file_path' => $file_path, 'branch' => $branch_name, @@ -650,23 +525,14 @@ public function createFile( return File::fromArray($this->getClient(), $this, $data); } - /** - * @param string $file_path - * @param string $content - * @param string $branch_name - * @param string $commit_message - * @param string $author_email - * @param string $author_name - * @return File - */ public function updateFile( - $file_path, - $content, - $branch_name, - $commit_message, - $author_email = null, - $author_name = null - ) { + string $file_path, + string $content, + string $branch_name, + string $commit_message, + ?string $author_email = null, + ?string $author_name = null + ): File { $parameters = [ 'file_path' => $file_path, 'branch' => $branch_name, @@ -687,15 +553,7 @@ public function updateFile( return File::fromArray($this->getClient(), $this, $data); } - /** - * @param string $file_path - * @param string $branch_name - * @param string $commit_message - * @param string $author_email - * @param string $author_name - * @return bool - */ - public function deleteFile($file_path, $branch_name, $commit_message, $author_email = null, $author_name = null) + public function deleteFile(string $file_path, string $branch_name, string $commit_message, ?string $author_email = null, ?string $author_name = null): bool { $parameters = [ 'file_path' => $file_path, @@ -717,17 +575,17 @@ public function deleteFile($file_path, $branch_name, $commit_message, $author_em } /** - * @param array $parameters - * - * @return Event[] * @see Projects::events() for available parameters. * + * @param mixed[] $parameters + * + * @return Event[] */ - public function events(array $parameters = []) + public function events(array $parameters = []): array { $data = $this->client->projects()->events($this->id, $parameters); - $events = array(); + $events = []; foreach ($data as $event) { $events[] = Event::fromArray($this->getClient(), $this, $event); } @@ -736,17 +594,17 @@ public function events(array $parameters = []) } /** - * @param array $parameters - * - * @return MergeRequest[] * @see MergeRequests::all() for available parameters. * + * @param mixed[] $parameters + * + * @return MergeRequest[] */ - public function mergeRequests(array $parameters = []) + public function mergeRequests(array $parameters = []): array { $data = $this->client->mergeRequests()->all($this->id, $parameters); - $mrs = array(); + $mrs = []; foreach ($data as $mr) { $mrs[] = MergeRequest::fromArray($this->getClient(), $this, $mr); } @@ -754,72 +612,53 @@ public function mergeRequests(array $parameters = []) return $mrs; } - /** - * @param int $id - * @return MergeRequest - */ - public function mergeRequest($id) + public function mergeRequest(int $id): MergeRequest { $mr = new MergeRequest($this, $id, $this->getClient()); return $mr->show(); } - /** - * @param string $source - * @param string $target - * @param string $title - * @param int $assignee - * @param string $description - * @return MergeRequest - */ - public function createMergeRequest($source, $target, $title, $assignee = null, $description = null) + public function createMergeRequest(string $source, string $target, string $title, ?int $assignee = null, ?string $description = null): MergeRequest { - $data = $this->client->mergeRequests()->create($this->id, $source, $target, $title, $assignee, $this->id, - $description); + $data = $this->client->mergeRequests()->create( + $this->id, + $source, + $target, + $title, + $assignee, + $this->id, + $description + ); return MergeRequest::fromArray($this->getClient(), $this, $data); } /** - * @param int $id - * @param array $params - * @return MergeRequest + * @param mixed[] $params */ - public function updateMergeRequest($id, array $params) + public function updateMergeRequest(int $id, array $params): MergeRequest { $mr = new MergeRequest($this, $id, $this->getClient()); return $mr->update($params); } - /** - * @param int $id - * @return MergeRequest - */ - public function closeMergeRequest($id) + public function closeMergeRequest(int $id): MergeRequest { $mr = new MergeRequest($this, $id, $this->getClient()); return $mr->close(); } - /** - * @param int $id - * @return MergeRequest - */ - public function openMergeRequest($id) + public function openMergeRequest(int $id): MergeRequest { $mr = new MergeRequest($this, $id, $this->getClient()); return $mr->reopen(); } - /** - * @param int $id - * @return MergeRequest - */ - public function mergeMergeRequest($id) + public function mergeMergeRequest(int $id): MergeRequest { $mr = new MergeRequest($this, $id, $this->getClient()); @@ -827,17 +666,17 @@ public function mergeMergeRequest($id) } /** - * @param array $parameters - * - * @return Issue[] * @see Issues::all() for available parameters. * + * @param mixed[] $parameters + * + * @return Issue[] */ - public function issues(array $parameters = []) + public function issues(array $parameters = []): array { $data = $this->client->issues()->all($this->id, $parameters); - $issues = array(); + $issues = []; foreach ($data as $issue) { $issues[] = Issue::fromArray($this->getClient(), $this, $issue); } @@ -846,23 +685,17 @@ public function issues(array $parameters = []) } /** - * @param string $title - * @param array $params - * @return Issue + * @param mixed[] $params */ - public function createIssue($title, array $params = array()) + public function createIssue(string $title, array $params = []): Issue { $params['title'] = $title; - $data = $this->client->issues()->create($this->id, $params); + $data = $this->client->issues()->create($this->id, $params); return Issue::fromArray($this->getClient(), $this, $data); } - /** - * @param int $iid - * @return Issue - */ - public function issue($iid) + public function issue(int $iid): Issue { $issue = new Issue($this, $iid, $this->getClient()); @@ -870,34 +703,23 @@ public function issue($iid) } /** - * @param int $iid - * @param array $params - * @return Issue + * @param mixed[] $params */ - public function updateIssue($iid, array $params) + public function updateIssue(int $iid, array $params): Issue { $issue = new Issue($this, $iid, $this->getClient()); return $issue->update($params); } - /** - * @param int $iid - * @param string $comment - * @return Issue - */ - public function closeIssue($iid, $comment = null) + public function closeIssue(int $iid, ?string $comment = null): Issue { $issue = new Issue($this, $iid, $this->getClient()); return $issue->close($comment); } - /** - * @param int $iid - * @return Issue - */ - public function openIssue($iid) + public function openIssue(int $iid): Issue { $issue = new Issue($this, $iid, $this->getClient()); @@ -905,17 +727,17 @@ public function openIssue($iid) } /** - * @param array $parameters - * - * @return Milestone[] * @see Milestones::all() for available parameters. * + * @param mixed[] $parameters + * + * @return Milestone[] */ - public function milestones(array $parameters = []) + public function milestones(array $parameters = []): array { $data = $this->client->milestones()->all($this->id, $parameters); - $milestones = array(); + $milestones = []; foreach ($data as $milestone) { $milestones[] = Milestone::fromArray($this->getClient(), $this, $milestone); } @@ -924,23 +746,17 @@ public function milestones(array $parameters = []) } /** - * @param string $title - * @param array $params - * @return Milestone + * @param mixed[] $params */ - public function createMilestone($title, array $params = array()) + public function createMilestone(string $title, array $params = []): Milestone { $params['title'] = $title; - $data = $this->client->milestones()->create($this->id, $params); + $data = $this->client->milestones()->create($this->id, $params); return Milestone::fromArray($this->getClient(), $this, $data); } - /** - * @param int $id - * @return Milestone - */ - public function milestone($id) + public function milestone(int $id): Milestone { $milestone = new Milestone($this, $id, $this->getClient()); @@ -948,11 +764,9 @@ public function milestone($id) } /** - * @param int $id - * @param array $params - * @return Milestone + * @param mixed[] $params */ - public function updateMilestone($id, array $params) + public function updateMilestone(int $id, array $params): Milestone { $milestone = new Milestone($this, $id, $this->getClient()); @@ -960,10 +774,9 @@ public function updateMilestone($id, array $params) } /** - * @param int $id * @return Issue[] */ - public function milestoneIssues($id) + public function milestoneIssues(int $id): array { $milestone = new Milestone($this, $id, $this->getClient()); @@ -973,11 +786,11 @@ public function milestoneIssues($id) /** * @return Snippet[] */ - public function snippets() + public function snippets(): array { $data = $this->client->snippets()->all($this->id); - $snippets = array(); + $snippets = []; foreach ($data as $snippet) { $snippets[] = Snippet::fromArray($this->getClient(), $this, $snippet); } @@ -985,36 +798,21 @@ public function snippets() return $snippets; } - /** - * @param string $title - * @param string $filename - * @param string $code - * @param string $visibility - * @return Snippet - */ - public function createSnippet($title, $filename, $code, $visibility) + public function createSnippet(string $title, string $filename, string $code, string $visibility): Snippet { $data = $this->client->snippets()->create($this->id, $title, $filename, $code, $visibility); return Snippet::fromArray($this->getClient(), $this, $data); } - /** - * @param int $id - * @return Snippet - */ - public function snippet($id) + public function snippet(int $id): Snippet { $snippet = new Snippet($this, $id, $this->getClient()); return $snippet->show(); } - /** - * @param int $id - * @return string - */ - public function snippetContent($id) + public function snippetContent(int $id): string { $snippet = new Snippet($this, $id, $this->getClient()); @@ -1022,74 +820,49 @@ public function snippetContent($id) } /** - * @param int $id - * @param array $params - * @return Snippet + * @param mixed[] $params */ - public function updateSnippet($id, array $params) + public function updateSnippet(int $id, array $params): Snippet { $snippet = new Snippet($this, $id, $this->getClient()); return $snippet->update($params); } - /** - * @param int $id - * @return bool - */ - public function removeSnippet($id) + public function removeSnippet(int $id): bool { $snippet = new Snippet($this, $id, $this->getClient()); return $snippet->remove(); } - /** - * @param int $group_id - * @return Group - */ - public function transfer($group_id) + public function transfer(int $group_id): Group { $group = new Group($group_id, $this->getClient()); return $group->transfer($this->id); } - /** - * @param int $id - * @return Project - */ - public function forkTo($id) + public function forkTo(int $id): Project { $data = $this->client->projects()->createForkRelation($id, $this->id); - return Project::fromArray($this->getClient(), $data); + return self::fromArray($this->getClient(), $data); } - /** - * @param int $id - * @return Project - */ - public function forkFrom($id) + public function forkFrom(int $id): Project { return $this->createForkRelation($id); } - /** - * @param int $id - * @return Project - */ - public function createForkRelation($id) + public function createForkRelation(int $id): Project { $data = $this->client->projects()->createForkRelation($this->id, $id); - return Project::fromArray($this->getClient(), $data); + return self::fromArray($this->getClient(), $data); } - /** - * @return bool - */ - public function removeForkRelation() + public function removeForkRelation(): bool { $this->client->projects()->removeForkRelation($this->id); @@ -1097,22 +870,16 @@ public function removeForkRelation() } /** - * @param string $service_name - * @param array $params - * @return bool + * @param mixed[] $params */ - public function setService($service_name, array $params = array()) + public function setService(string $service_name, array $params = []): bool { $this->client->projects()->setService($this->id, $service_name, $params); return true; } - /** - * @param string $service_name - * @return bool - */ - public function removeService($service_name) + public function removeService(string $service_name): bool { $this->client->projects()->removeService($this->id, $service_name); @@ -1122,11 +889,11 @@ public function removeService($service_name) /** * @return Label[] */ - public function labels() + public function labels(): array { $data = $this->client->projects()->labels($this->id); - $labels = array(); + $labels = []; foreach ($data as $label) { $labels[] = Label::fromArray($this->getClient(), $this, $label); } @@ -1134,27 +901,20 @@ public function labels() return $labels; } - /** - * @param string $name - * @param string $color - * @return Label - */ - public function addLabel($name, $color) + public function addLabel(string $name, string $color): Label { - $data = $this->client->projects()->addLabel($this->id, array( + $data = $this->client->projects()->addLabel($this->id, [ 'name' => $name, - 'color' => $color - )); + 'color' => $color, + ]); return Label::fromArray($this->getClient(), $this, $data); } /** - * @param string $name - * @param array $params - * @return Label + * @param mixed[] $params */ - public function updateLabel($name, array $params) + public function updateLabel(string $name, array $params): Label { if (isset($params['name'])) { $params['new_name'] = $params['name']; @@ -1167,11 +927,7 @@ public function updateLabel($name, array $params) return Label::fromArray($this->getClient(), $this, $data); } - /** - * @param string $name - * @return bool - */ - public function removeLabel($name) + public function removeLabel(string $name): bool { $this->client->projects()->removeLabel($this->id, $name); @@ -1179,13 +935,13 @@ public function removeLabel($name) } /** - * @return array + * @return mixed[] */ - public function contributors() + public function contributors(): array { $data = $this->client->repositories()->contributors($this->id); - $contributors = array(); + $contributors = []; foreach ($data as $contributor) { $contributors[] = Contributor::fromArray($this->getClient(), $this, $contributor); } @@ -1194,14 +950,15 @@ public function contributors() } /** - * @param array $scopes + * @param mixed[] $scopes + * * @return Job[] */ - public function jobs(array $scopes = []) + public function jobs(array $scopes = []): array { $data = $this->client->jobs()->all($this->id, $scopes); - $jobs = array(); + $jobs = []; foreach ($data as $job) { $jobs[] = Job::fromArray($this->getClient(), $this, $job); } @@ -1210,15 +967,15 @@ public function jobs(array $scopes = []) } /** - * @param int $pipeline_id - * @param array $scopes + * @param mixed[] $scopes + * * @return Job[] */ - public function pipelineJobs($pipeline_id, array $scopes = []) + public function pipelineJobs(int $pipeline_id, array $scopes = []): array { $data = $this->client->jobs()->pipelineJobs($this->id, $pipeline_id, $scopes); - $jobs = array(); + $jobs = []; foreach ($data as $job) { $jobs[] = Job::fromArray($this->getClient(), $this, $job); } @@ -1226,11 +983,7 @@ public function pipelineJobs($pipeline_id, array $scopes = []) return $jobs; } - /** - * @param int $job_id - * @return Job - */ - public function job($job_id) + public function job(int $job_id): Job { $data = $this->client->jobs()->show($this->id, $job_id); @@ -1240,11 +993,11 @@ public function job($job_id) /** * @return Badge[] */ - public function badges() + public function badges(): array { $data = $this->client->projects()->badges($this->id); - $badges = array(); + $badges = []; foreach ($data as $badge) { $badges[] = Badge::fromArray($this->getClient(), $this, $badge); } @@ -1253,11 +1006,9 @@ public function badges() } /** - * @param string $link_url - * @param string $color - * @return Badge + * @param mixed[] $params */ - public function addBadge(array $params) + public function addBadge(array $params): Badge { $data = $this->client->projects()->addBadge($this->id, $params); @@ -1265,11 +1016,9 @@ public function addBadge(array $params) } /** - * @param string $name - * @param array $params - * @return Badge + * @param mixed[] $params */ - public function updateBadge($badge_id, array $params) + public function updateBadge(int $badge_id, array $params): Badge { $params['badge_id'] = $badge_id; @@ -1278,11 +1027,7 @@ public function updateBadge($badge_id, array $params) return Badge::fromArray($this->getClient(), $this, $data); } - /** - * @param string $name - * @return bool - */ - public function removeBadge($badge_id) + public function removeBadge(int $badge_id): bool { $this->client->projects()->removeBadge($this->id, $badge_id); @@ -1290,12 +1035,12 @@ public function removeBadge($badge_id) } /** - * @param array $params - * @return Branch + * @param mixed[] $params */ - public function addProtectedBranch(array $params = []) + public function addProtectedBranch(array $params = []): Branch { $data = $this->client->projects()->addProtectedBranch($this->id, $params); + return Branch::fromArray($this->getClient(), $this, $data); } } diff --git a/lib/Gitlab/Model/ProjectHook.php b/src/Model/ProjectHook.php similarity index 62% rename from lib/Gitlab/Model/ProjectHook.php rename to src/Model/ProjectHook.php index 1789ca68b..638d266eb 100644 --- a/lib/Gitlab/Model/ProjectHook.php +++ b/src/Model/ProjectHook.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Project $project - * @param int $id - * @param Client $client - */ - public function __construct(Project $project, $id, Client $client = null) + public function __construct(Project $project, int $id, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); $this->setData('id', $id); } - /** - * @return ProjectHook - */ - public function show() + public function show(): ProjectHook { $data = $this->client->projects()->hook($this->project->id, $this->id); return static::fromArray($this->getClient(), $this->project, $data); } - /** - * @return bool - */ - public function delete() + public function delete(): bool { $this->client->projects()->removeHook($this->project->id, $this->id); return true; } - /** - * @return bool - */ - public function remove() + public function remove(): bool { return $this->delete(); } /** - * @param array $params - * @return ProjectHook + * @param mixed[] $params */ - public function update(array $params) + public function update(array $params): ProjectHook { $data = $this->client->projects()->updateHook($this->project->id, $this->id, $params); diff --git a/lib/Gitlab/Model/ProjectNamespace.php b/src/Model/ProjectNamespace.php similarity index 56% rename from lib/Gitlab/Model/ProjectNamespace.php rename to src/Model/ProjectNamespace.php index 9e3aee4ed..3988ad9c4 100644 --- a/lib/Gitlab/Model/ProjectNamespace.php +++ b/src/Model/ProjectNamespace.php @@ -1,10 +1,12 @@ -setClient($client); @@ -43,11 +41,7 @@ public static function fromArray(Client $client, array $data) return $project->hydrate($data); } - /** - * @param int $id - * @param Client $client - */ - public function __construct($id = null, Client $client = null) + public function __construct(?int $id = null, ?Client $client = null) { $this->setClient($client); $this->setData('id', $id); diff --git a/src/Model/Release.php b/src/Model/Release.php new file mode 100644 index 000000000..0d0437cd5 --- /dev/null +++ b/src/Model/Release.php @@ -0,0 +1,36 @@ +hydrate($data); + } + + public function __construct(?Client $client = null) + { + $this->setClient($client); + } +} diff --git a/lib/Gitlab/Model/Schedule.php b/src/Model/Schedule.php similarity index 54% rename from lib/Gitlab/Model/Schedule.php rename to src/Model/Schedule.php index ff7ba054a..a866e02a5 100644 --- a/lib/Gitlab/Model/Schedule.php +++ b/src/Model/Schedule.php @@ -1,12 +1,12 @@ hydrate($data); } - /** - * @param Project $project - * @param int $id - * @param Client $client - */ - public function __construct(Project $project, $id = null, Client $client = null) + public function __construct(Project $project, ?int $id = null, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); $this->setData('id', $id); } - /** - * @return Schedule - */ - public function show() + public function show(): Schedule { $data = $this->client->schedules()->show($this->project->id, $this->id); @@ -74,10 +61,9 @@ public function show() } /** - * @param array $params - * @return Schedule + * @param mixed[] $params */ - public function update(array $params) + public function update(array $params): Schedule { $data = $this->client->schedules()->update($this->project->id, $this->id, $params); diff --git a/lib/Gitlab/Model/Session.php b/src/Model/Session.php similarity index 53% rename from lib/Gitlab/Model/Session.php rename to src/Model/Session.php index 7cf4cfe79..e42eab085 100644 --- a/lib/Gitlab/Model/Session.php +++ b/src/Model/Session.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Client $client - */ - public function __construct(Client $client = null) + public function __construct(?Client $client = null) { $this->setClient($client); } - /** - * @return User - */ - public function me() + public function me(): User { $data = $this->client->users()->user(); return User::fromArray($this->getClient(), $data); } - /** - * @param string $email - * @param string $password - * @return Session - */ - public function login($email, $password) + public function login(string $email, string $password): Session { $data = $this->client->users()->session($email, $password); diff --git a/lib/Gitlab/Model/Snippet.php b/src/Model/Snippet.php similarity index 60% rename from lib/Gitlab/Model/Snippet.php rename to src/Model/Snippet.php index 1f021bcb4..7ba17d58b 100644 --- a/lib/Gitlab/Model/Snippet.php +++ b/src/Model/Snippet.php @@ -1,10 +1,12 @@ -hydrate($data); } - /** - * @param Project $project - * @param int $id - * @param Client $client - */ - public function __construct(Project $project, $id = null, Client $client = null) + public function __construct(Project $project, ?int $id = null, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); $this->setData('id', $id); } - /** - * @return Snippet - */ - public function show() + public function show(): Snippet { $data = $this->client->snippets()->show($this->project->id, $this->id); @@ -68,28 +57,21 @@ public function show() } /** - * @param array $params - * @return Snippet + * @param mixed[] $params */ - public function update(array $params) + public function update(array $params): Snippet { $data = $this->client->snippets()->update($this->project->id, $this->id, $params); return static::fromArray($this->getClient(), $this->project, $data); } - /** - * @return string - */ - public function content() + public function content(): string { return $this->client->snippets()->content($this->project->id, $this->id); } - /** - * @return bool - */ - public function remove() + public function remove(): bool { $this->client->snippets()->remove($this->project->id, $this->id); diff --git a/lib/Gitlab/Model/Tag.php b/src/Model/Tag.php similarity index 58% rename from lib/Gitlab/Model/Tag.php rename to src/Model/Tag.php index 643edd0f2..b02583560 100644 --- a/lib/Gitlab/Model/Tag.php +++ b/src/Model/Tag.php @@ -1,36 +1,33 @@ -hydrate($data); } - /** - * @param Project $project - * @param string $name - * @param Client $client - */ - public function __construct(Project $project, $name = null, Client $client = null) + public function __construct(Project $project, ?string $name = null, ?Client $client = null) { $this->setClient($client); $this->setData('project', $project); diff --git a/lib/Gitlab/Model/User.php b/src/Model/User.php similarity index 63% rename from lib/Gitlab/Model/User.php rename to src/Model/User.php index ef654756d..e919ff46d 100644 --- a/lib/Gitlab/Model/User.php +++ b/src/Model/User.php @@ -1,10 +1,12 @@ -users()->create($email, $password, $params); return static::fromArray($client, $data); } - /** - * @param int $id - * @param Client $client - */ - public function __construct($id = null, Client $client = null) + public function __construct(?int $id = null, ?Client $client = null) { $this->setClient($client); $this->setData('id', $id); } - /** - * @return User - */ - public function show() + public function show(): User { $data = $this->client->users()->show($this->id); @@ -112,40 +99,30 @@ public function show() } /** - * @param array $params - * @return User + * @param mixed[] $params */ - public function update(array $params) + public function update(array $params): User { $data = $this->client->users()->update($this->id, $params); return static::fromArray($this->getClient(), $data); } - /** - * @return bool - */ - public function remove() + public function remove(): bool { $this->client->users()->remove($this->id); return true; } - /** - * @return bool - */ - public function block() + public function block(): bool { $this->client->users()->block($this->id); return true; } - /** - * @return bool - */ - public function unblock() + public function unblock(): bool { $this->client->users()->unblock($this->id); @@ -155,11 +132,11 @@ public function unblock() /** * @return Key[] */ - public function keys() + public function keys(): array { $data = $this->client->users()->keys(); - $keys = array(); + $keys = []; foreach ($data as $key) { $keys[] = Key::fromArray($this->getClient(), $key); } @@ -167,58 +144,35 @@ public function keys() return $keys; } - /** - * @param string $title - * @param string $key - * @return Key - */ - public function createKey($title, $key) + public function createKey(string $title, string $key): Key { $data = $this->client->users()->createKey($title, $key); return Key::fromArray($this->getClient(), $data); } - /** - * @param string $title - * @param string $key - * @return Key - */ - public function createKeyForUser($user_id, $title, $key) + public function createKeyForUser(int $user_id, string $title, string $key): Key { $data = $this->client->users()->createKeyForUser($user_id, $title, $key); return Key::fromArray($this->getClient(), $data); } - /** - * @param int $id - * @return bool - */ - public function removeKey($id) + public function removeKey(int $id): bool { $this->client->users()->removeKey($id); return true; } - /** - * @param int $group_id - * @param int $access_level - * @return User - */ - public function addToGroup($group_id, $access_level) + public function addToGroup(int $group_id, int $access_level): User { $group = new Group($group_id, $this->getClient()); return $group->addMember($this->id, $access_level); } - /** - * @param int $group_id - * @return bool - */ - public function removeFromGroup($group_id) + public function removeFromGroup(int $group_id): bool { $group = new Group($group_id, $this->getClient()); diff --git a/src/ResultPager.php b/src/ResultPager.php new file mode 100644 index 000000000..e02cc75e5 --- /dev/null +++ b/src/ResultPager.php @@ -0,0 +1,71 @@ +api('someApi'); * $pager = new \Gitlab\ResultPager($client); - * - * @param \Gitlab\Client $client - * */ public function __construct(Client $client) { @@ -32,17 +33,17 @@ public function __construct(Client $client) /** * {@inheritdoc} */ - public function fetch(ApiInterface $api, $method, array $parameters = array()) + public function fetch(Api $api, $method, array $parameters = []): array { - return call_user_func_array(array($api, $method), $parameters); + return call_user_func_array([$api, $method], $parameters); } /** * {@inheritdoc} */ - public function fetchAll(ApiInterface $api, $method, array $parameters = array()) + public function fetchAll(Api $api, $method, array $parameters = []): array { - $result = call_user_func_array(array($api, $method), $parameters); + $result = call_user_func_array([$api, $method], $parameters); while ($this->hasNext()) { $result = array_merge($result, $this->fetchNext()); } @@ -53,7 +54,7 @@ public function fetchAll(ApiInterface $api, $method, array $parameters = array() /** * {@inheritdoc} */ - public function hasNext() + public function hasNext(): bool { return $this->has('next'); } @@ -61,7 +62,7 @@ public function hasNext() /** * {@inheritdoc} */ - public function fetchNext() + public function fetchNext(): array { return $this->get('next'); } @@ -69,7 +70,7 @@ public function fetchNext() /** * {@inheritdoc} */ - public function hasPrevious() + public function hasPrevious(): bool { return $this->has('prev'); } @@ -77,7 +78,7 @@ public function hasPrevious() /** * {@inheritdoc} */ - public function fetchPrevious() + public function fetchPrevious(): array { return $this->get('prev'); } @@ -85,7 +86,7 @@ public function fetchPrevious() /** * {@inheritdoc} */ - public function fetchFirst() + public function fetchFirst(): array { return $this->get('first'); } @@ -93,23 +94,23 @@ public function fetchFirst() /** * {@inheritdoc} */ - public function fetchLast() + public function fetchLast(): array { return $this->get('last'); } /** - * {@inheritdoc} + * @param string|int $key */ - protected function has($key) + protected function has($key): bool { $lastResponse = $this->client->getResponseHistory()->getLastResponse(); - if ($lastResponse == null) { + if ($lastResponse === null) { return false; } $pagination = ResponseMediator::getPagination($lastResponse); - if ($pagination == null) { + if ($pagination === null) { return false; } @@ -117,11 +118,13 @@ protected function has($key) } /** - * {@inheritdoc} + * @param string|int $key + * + * @return mixed */ protected function get($key) { - if (!$this->has($key)) { + if (! $this->has($key)) { return []; } diff --git a/test/Gitlab/Tests/Api/AbstractApiTest.php b/test/Gitlab/Tests/Api/AbstractApiTest.php deleted file mode 100644 index eaae170c4..000000000 --- a/test/Gitlab/Tests/Api/AbstractApiTest.php +++ /dev/null @@ -1,60 +0,0 @@ - [ - 'value1', - 'value2' - ] - ]; - $expectedBody = 'array_param[]=value1&array_param[]=value2'; - - $abstractApiMock = $this->getAbstractApiMock(); - $reflection = new ReflectionClass(get_class($abstractApiMock)); - $method = $reflection->getMethod('prepareBody'); - $method->setAccessible(true); - $stream = $method->invokeArgs( - $abstractApiMock, - [ - $parameters - ] - ); - - $this->assertEquals($expectedBody, urldecode((string)$stream)); - } - - protected function getAbstractApiMock(array $methods = []) - { - $httpClient = $this->getMockBuilder(HttpClient::class) - ->setMethods(array('sendRequest')) - ->getMock() - ; - $httpClient - ->expects($this->any()) - ->method('sendRequest') - ; - $client = Client::createWithHttpClient($httpClient); - - $abstractApiMock = $this->getMockBuilder('Gitlab\Api\AbstractApi') - ->setConstructorArgs([ - $client, - null - ]) - ->setMethods($methods) - ->getMockForAbstractClass() - ; - - return $abstractApiMock; - } -} diff --git a/test/Gitlab/Tests/Api/DeployKeysTest.php b/test/Gitlab/Tests/Api/DeployKeysTest.php deleted file mode 100644 index 25a796040..000000000 --- a/test/Gitlab/Tests/Api/DeployKeysTest.php +++ /dev/null @@ -1,44 +0,0 @@ -getMultipleDeployKeysData(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('deploy_keys', array('page' => 2, 'per_page' => 5)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(['page' => 2, 'per_page' => 5])); - } - - protected function getMultipleDeployKeysData() - { - return array( - array( - 'id' => 1, - 'title' => 'Public key', - 'key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=', - 'created_at' => '2013-10-02T10:12:29Z' - ), - array( - 'id' => 3, - 'title' => 'Another Public key', - 'key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=', - 'created_at' => '2013-10-02T11:12:29Z' - ) - ); - } - - protected function getApiClass() - { - return 'Gitlab\Api\DeployKeys'; - } -} diff --git a/test/Gitlab/Tests/Api/DeploymentsTest.php b/test/Gitlab/Tests/Api/DeploymentsTest.php deleted file mode 100644 index 3373ef4e1..000000000 --- a/test/Gitlab/Tests/Api/DeploymentsTest.php +++ /dev/null @@ -1,248 +0,0 @@ - '2016-08-11T07:36:40.222Z', - 'deployable' => - array( - 'commit' => - array( - 'author_email' => 'admin@example.com', - 'author_name' => 'Administrator', - 'created_at' => '2016-08-11T09:36:01.000+02:00', - 'id' => '99d03678b90d914dbb1b109132516d71a4a03ea8', - 'message' => 'Merge branch \'new-title\' into \'master\' - -Update README - - - -See merge request !1', - 'short_id' => '99d03678', - 'title' => 'Merge branch \'new-title\' into \'master\' -', - ), - 'coverage' => null, - 'created_at' => '2016-08-11T07:36:27.357Z', - 'finished_at' => '2016-08-11T07:36:39.851Z', - 'id' => 657, - 'name' => 'deploy', - 'ref' => 'master', - 'runner' => null, - 'stage' => 'deploy', - 'started_at' => null, - 'status' => 'success', - 'tag' => false, - 'user' => - array( - 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', - 'bio' => null, - 'created_at' => '2016-08-11T07:09:20.351Z', - 'id' => 1, - 'linkedin' => '', - 'location' => null, - 'name' => 'Administrator', - 'skype' => '', - 'state' => 'active', - 'twitter' => '', - 'username' => 'root', - 'web_url' => 'http://localhost:3000/root', - 'website_url' => '', - ), - ), - 'environment' => - array( - 'external_url' => 'https://about.gitlab.com', - 'id' => 9, - 'name' => 'production', - ), - 'id' => 41, - 'iid' => 1, - 'ref' => 'master', - 'sha' => '99d03678b90d914dbb1b109132516d71a4a03ea8', - 'user' => - array( - 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', - 'id' => 1, - 'name' => 'Administrator', - 'state' => 'active', - 'username' => 'root', - 'web_url' => 'http://localhost:3000/root', - ), - ), - array( - 'created_at' => '2016-08-11T11:32:35.444Z', - 'deployable' => - array( - 'commit' => - array( - 'author_email' => 'admin@example.com', - 'author_name' => 'Administrator', - 'created_at' => '2016-08-11T13:28:26.000+02:00', - 'id' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', - 'message' => 'Merge branch \'rename-readme\' into \'master\' - -Rename README - - - -See merge request !2', - 'short_id' => 'a91957a8', - 'title' => 'Merge branch \'rename-readme\' into \'master\' -', - ), - 'coverage' => null, - 'created_at' => '2016-08-11T11:32:24.456Z', - 'finished_at' => '2016-08-11T11:32:35.145Z', - 'id' => 664, - 'name' => 'deploy', - 'ref' => 'master', - 'runner' => null, - 'stage' => 'deploy', - 'started_at' => null, - 'status' => 'success', - 'tag' => false, - 'user' => - array( - 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', - 'bio' => null, - 'created_at' => '2016-08-11T07:09:20.351Z', - 'id' => 1, - 'linkedin' => '', - 'location' => null, - 'name' => 'Administrator', - 'skype' => '', - 'state' => 'active', - 'twitter' => '', - 'username' => 'root', - 'web_url' => 'http://localhost:3000/root', - 'website_url' => '', - ), - ), - 'environment' => - array( - 'external_url' => 'https://about.gitlab.com', - 'id' => 9, - 'name' => 'production', - ), - 'id' => 42, - 'iid' => 2, - 'ref' => 'master', - 'sha' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', - 'user' => - array( - 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', - 'id' => 1, - 'name' => 'Administrator', - 'state' => 'active', - 'username' => 'root', - 'web_url' => 'http://localhost:3000/root', - ), - ), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/deployments') - ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->all(1)); - } - - /** - * @test - */ - public function shouldShowDeployment() - { - $expectedArray = array( - array( - 'created_at' => '2016-08-11T11:32:35.444Z', - 'deployable' => - array( - 'commit' => - array( - 'author_email' => 'admin@example.com', - 'author_name' => 'Administrator', - 'created_at' => '2016-08-11T13:28:26.000+02:00', - 'id' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', - 'message' => 'Merge branch \'rename-readme\' into \'master\' - -Rename README - - - -See merge request !2', - 'short_id' => 'a91957a8', - 'title' => 'Merge branch \'rename-readme\' into \'master\' -', - ), - 'coverage' => null, - 'created_at' => '2016-08-11T11:32:24.456Z', - 'finished_at' => '2016-08-11T11:32:35.145Z', - 'id' => 664, - 'name' => 'deploy', - 'ref' => 'master', - 'runner' => null, - 'stage' => 'deploy', - 'started_at' => null, - 'status' => 'success', - 'tag' => false, - 'user' => - array( - 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', - 'bio' => null, - 'created_at' => '2016-08-11T07:09:20.351Z', - 'id' => 1, - 'linkedin' => '', - 'location' => null, - 'name' => 'Administrator', - 'skype' => '', - 'state' => 'active', - 'twitter' => '', - 'username' => 'root', - 'web_url' => 'http://localhost:3000/root', - 'website_url' => '', - ), - ), - 'environment' => - array( - 'external_url' => 'https://about.gitlab.com', - 'id' => 9, - 'name' => 'production', - ), - 'id' => 42, - 'iid' => 2, - 'ref' => 'master', - 'sha' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', - 'user' => - array( - 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', - 'id' => 1, - 'name' => 'Administrator', - 'state' => 'active', - 'username' => 'root', - 'web_url' => 'http://localhost:3000/root', - ), - ), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/deployments/42') - ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->show(1, 42)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Deployments'; - } -} diff --git a/test/Gitlab/Tests/Api/EnvironmentsTest.php b/test/Gitlab/Tests/Api/EnvironmentsTest.php deleted file mode 100644 index 935c0e67c..000000000 --- a/test/Gitlab/Tests/Api/EnvironmentsTest.php +++ /dev/null @@ -1,95 +0,0 @@ - 1, - 'name' => 'review/fix-foo', - 'slug' => 'review-fix-foo-dfjre3', - 'external_url' => 'https://review-fix-foo-dfjre3.example.gitlab.com' - ), - array( - 'id' => 2, - 'name' => 'review/fix-bar', - 'slug' => 'review-fix-bar-dfjre4', - 'external_url' => 'https://review-fix-bar-dfjre4.example.gitlab.com' - ), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/environments') - ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->all(1)); - } - - /** - * @test - */ - public function shouldCreateEnvironment() - { - $expectedArray = array( - array( - 'id' => 3, - 'name' => 'review/fix-baz', - 'slug' => 'review-fix-baz-dfjre5', - 'external_url' => 'https://review-fix-baz-dfjre5.example.gitlab.com' - ), - ); - - $params = array( - 'name' => 'review/fix-baz', - 'external_url' => 'https://review-fix-baz-dfjre5.example.gitlab.com' - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/environment', $params) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->create(1, $params)); - } - - /** - * @test - */ - public function shouldRemoveEnvironment() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/environments/3') - ->will($this->returnValue($expectedBool)); - $this->assertEquals($expectedBool, $api->remove(1, 3)); - } - - /** - * @test - */ - public function shouldStopEnvironment() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/environments/3/stop') - ->will($this->returnValue($expectedBool)); - $this->assertEquals($expectedBool, $api->stop(1, 3)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Environments'; - } -} diff --git a/test/Gitlab/Tests/Api/GroupBoardsTest.php b/test/Gitlab/Tests/Api/GroupBoardsTest.php deleted file mode 100644 index 8f9b9aa28..000000000 --- a/test/Gitlab/Tests/Api/GroupBoardsTest.php +++ /dev/null @@ -1,230 +0,0 @@ - 1, 'title' => 'A board'), - array('id' => 2, 'title' => 'Another board'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('boards', array()) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all()); - } - - /** - * @test - */ - public function shouldShowIssueBoard() - { - $expectedArray = array('id' => 2, 'name' => 'Another issue board'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/boards/2') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1, 2)); - } - - /** - * @test - */ - public function shouldCreateIssueBoard() - { - $expectedArray = array('id' => 3, 'name' => 'A new issue board'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups/1/boards', array('name' => 'A new issue board')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create(1, array('name' => 'A new issue board'))); - } - - /** - * @test - */ - public function shouldUpdateIssueBoard() - { - $expectedArray = array('id' => 2, 'name' => 'A renamed issue board'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('groups/1/boards/2', array('name' => 'A renamed issue board', 'labels' => 'foo')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(1, 2, array('name' => 'A renamed issue board', 'labels' => 'foo'))); - } - - /** - * @test - */ - public function shouldRemoveIssueBoard() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('groups/1/boards/2') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->remove(1, 2)); - } - - /** - * @test - */ - public function shouldGetAllLists() - { - $expectedArray = array( - array( - 'id' => 1, - 'label' => array( - 'name' => 'First label', - 'color' => '#F0AD4E', - 'description' => null - ), - 'position' => 1 - ), array( - 'id' => 2, - 'label' => array( - 'name' => 'Second label', - 'color' => '#F0AD4E', - 'description' => null - ), - 'position' => 2 - ) - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/boards/2/lists') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->allLists(1, 2)); - } - - /** - * @test - */ - public function shouldGetList() - { - $expectedArray = array( - array( - 'id' => 3, - 'label' => array( - 'name' => 'Some label', - 'color' => '#F0AD4E', - 'description' => null - ), - 'position' => 3 - ) - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/boards/2/lists/3') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->showList(1, 2, 3)); - } - - /** - * @test - */ - public function shouldCreateList() - { - $expectedArray = array( - array( - 'id' => 3, - 'label' => array( - 'name' => 'Some label', - 'color' => '#F0AD4E', - 'description' => null - ), - 'position' => 3 - ) - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups/1/boards/2/lists', array('label_id' => 4)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createList(1, 2, 4)); - } - - /** - * @test - */ - public function shouldUpdateList() - { - $expectedArray = array( - array( - 'id' => 3, - 'label' => array( - 'name' => 'Some label', - 'color' => '#F0AD4E', - 'description' => null - ), - 'position' => 1 - ) - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('groups/5/boards/2/lists/3', array('position' => 1)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateList(5, 2, 3, 1)); - } - - /** - * @test - */ - public function shouldDeleteList() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('groups/1/boards/2/lists/3') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->deleteList(1, 2, 3)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\GroupsBoards'; - } -} diff --git a/test/Gitlab/Tests/Api/GroupsMilestonesTest.php b/test/Gitlab/Tests/Api/GroupsMilestonesTest.php deleted file mode 100644 index 17eb96b24..000000000 --- a/test/Gitlab/Tests/Api/GroupsMilestonesTest.php +++ /dev/null @@ -1,137 +0,0 @@ - 1, 'title' => 'A milestone'), - array('id' => 2, 'title' => 'Another milestone'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/milestones') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1)); - } - - /** - * @test - */ - public function shouldShowMilestone() - { - $expectedArray = array('id' => 1, 'name' => 'A milestone'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/milestones/2') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1, 2)); - } - - /** - * @test - */ - public function shouldCreateMilestone() - { - $expectedArray = array('id' => 3, 'title' => 'A new milestone'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups/1/milestones', array('description' => 'Some text', 'title' => 'A new milestone')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create(1, array('description' => 'Some text', 'title' => 'A new milestone'))); - } - - /** - * @test - */ - public function shouldUpdateMilestone() - { - $expectedArray = array('id' => 3, 'title' => 'Updated milestone'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('groups/1/milestones/3', array('title' => 'Updated milestone', 'due_date' => '2015-04-01', 'state_event' => 'close')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(1, 3, array('title' => 'Updated milestone', 'due_date' => '2015-04-01', 'state_event' => 'close'))); - } - - /** - * @test - */ - public function shouldRemoveMilestone() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('groups/1/milestones/2') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->remove(1, 2)); - } - - /** - * @test - */ - public function shouldGetMilestonesIssues() - { - $expectedArray = array( - array('id' => 1, 'title' => 'An issue'), - array('id' => 2, 'title' => 'Another issue'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/milestones/3/issues') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->issues(1, 3)); - } - - /** - * @test - */ - public function shouldGetMilestonesMergeRequests() - { - $expectedArray = array( - array('id' => 1, 'title' => 'A merge request'), - array('id' => 2, 'title' => 'Another merge request'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/milestones/3/merge_requests') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->mergeRequests(1, 3)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\GroupsMilestones'; - } -} diff --git a/test/Gitlab/Tests/Api/GroupsTest.php b/test/Gitlab/Tests/Api/GroupsTest.php deleted file mode 100644 index b4da1de4d..000000000 --- a/test/Gitlab/Tests/Api/GroupsTest.php +++ /dev/null @@ -1,628 +0,0 @@ - 1, 'name' => 'A group'), - array('id' => 2, 'name' => 'Another group'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups', array('page' => 1, 'per_page' => 10)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(['page' => 1, 'per_page' => 10])); - } - - /** - * @test - */ - public function shouldGetAllGroupsWithBooleanParam() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A group'), - array('id' => 2, 'name' => 'Another group'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups', ['all_available' => 'false']) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(['all_available' => false])); - } - - /** - * @test - */ - public function shouldGetAllGroupProjectsWithBooleanParam() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A group'), - array('id' => 2, 'name' => 'Another group'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/projects', ['archived' => 'false']) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->projects(1, ['archived' => false])); - } - - /** - * @test - */ - public function shouldNotNeedPaginationWhenGettingGroups() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A group'), - array('id' => 2, 'name' => 'Another group'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups', array()) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all()); - } - - /** - * @test - */ - public function shouldShowGroup() - { - $expectedArray = array('id' => 1, 'name' => 'A group'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1)); - } - - /** - * @test - */ - public function shouldCreateGroup() - { - $expectedArray = array('id' => 1, 'name' => 'A new group'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups', array('name' => 'A new group', 'path' => 'a-new-group', 'visibility' => 'private')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create('A new group', 'a-new-group')); - } - - /** - * @test - */ - public function shouldCreateGroupWithDescriptionAndVisibility() - { - $expectedArray = array('id' => 1, 'name' => 'A new group', 'visibility_level' => 2); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups', array('name' => 'A new group', 'path' => 'a-new-group', 'description' => 'Description', 'visibility' => 'public')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create('A new group', 'a-new-group', 'Description', 'public')); - } - - /** - * @test - */ - public function shouldCreateGroupWithDescriptionVisibilityAndParentId() - { - $expectedArray = array('id' => 1, 'name' => 'A new group', 'visibility_level' => 2, 'parent_id' => 666); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups', array('name' => 'A new group', 'path' => 'a-new-group', 'description' => 'Description', 'visibility' => 'public', 'parent_id' => 666)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create('A new group', 'a-new-group', 'Description', 'public', null, null, 666)); - } - - /** - * @test - */ - public function shouldUpdateGroup() - { - $expectedArray = array('id' => 3, 'name' => 'Group name', 'path' => 'group-path'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('groups/3', array('name' => 'Group name', 'path' => 'group-path')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(3, array('name' => 'Group name', 'path' => 'group-path'))); - } - - /** - * @test - */ - public function shouldTransferProjectToGroup() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups/1/projects/2') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->transfer(1, 2)); - } - - /** - * @test - */ - public function shouldGetAllMembers() - { - $expectedArray = array( - array('id' => 1, 'name' => 'Matt'), - array('id' => 2, 'name' => 'Bob') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/members/all') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->allMembers(1)); - } - - /** - * @test - */ - public function shouldGetMembers() - { - $expectedArray = array( - array('id' => 1, 'name' => 'Matt'), - array('id' => 2, 'name' => 'Bob') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/members') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->members(1)); - } - - /** - * @test - */ - public function shouldAddMember() - { - $expectedArray = array('id' => 1, 'name' => 'Matt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups/1/members', array('user_id' => 2, 'access_level' => 3)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->addMember(1, 2, 3)); - } - - /** - * @test - */ - public function shouldSaveMember() - { - $expectedArray = array('id' => 1, 'name' => 'Matt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('groups/1/members/2', array('access_level' => 4)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->saveMember(1, 2, 4)); - } - - /** - * @test - */ - public function shouldRemoveMember() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('groups/1/members/2') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->removeMember(1, 2)); - } - - /** - * @test - */ - public function shouldRemoveGroup() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('groups/1') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->remove(1)); - } - - /** - * @test - */ - public function shouldGetAllSubgroups() - { - $expectedArray = array( - array('id' => 101, 'name' => 'A subgroup'), - array('id' => 1-2, 'name' => 'Another subggroup'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/subgroups', array('page' => 1, 'per_page' => 10)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->subgroups(1, ['page' => 1, 'per_page' => 10])); - } - - /** - * @test - */ - public function shouldGetLabels() - { - $expectedArray = array( - array('id' => 987, 'name' => 'bug', 'color' => '#000000'), - array('id' => 123, 'name' => 'feature', 'color' => '#ff0000') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/labels') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->labels(1)); - } - - /** - * @test - */ - public function shouldAddLabel() - { - $expectedArray = array('name' => 'bug', 'color' => '#000000'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups/1/labels', array('name' => 'wont-fix', 'color' => '#ffffff')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->addLabel(1, array('name' => 'wont-fix', 'color' => '#ffffff'))); - } - - /** - * @test - */ - public function shouldUpdateLabel() - { - $expectedArray = array('name' => 'bug', 'color' => '#00ffff'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('groups/1/labels', array('name' => 'bug', 'new_name' => 'big-bug', 'color' => '#00ffff')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateLabel(1, array('name' => 'bug', 'new_name' => 'big-bug', 'color' => '#00ffff'))); - } - - /** - * @test - */ - public function shouldRemoveLabel() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('groups/1/labels', array('name' => 'bug')) - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->removeLabel(1, 'bug')); - } - - public function shouldGetVariables() - { - $expectedArray = array( - array('key' => 'ftp_username', 'value' => 'ftp'), - array('key' => 'ftp_password', 'value' => 'somepassword') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/variables') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->variables(1)); - } - - /** - * @test - */ - public function shouldGetVariable() - { - $expectedArray = array('key' => 'ftp_username', 'value' => 'ftp'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/variables/ftp_username') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->variable(1, 'ftp_username')); - } - - public function shouldAddVariable() - { - $expectedKey = 'ftp_port'; - $expectedValue = '21'; - - $expectedArray = array( - 'key' => $expectedKey, - 'value' => $expectedValue, - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups/1/variables', $expectedArray) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue)); - } - - /** - * @test - */ - public function shouldAddVariableWithProtected() - { - $expectedArray = array( - 'key' => 'DEPLOY_SERVER', - 'value' => 'stage.example.com', - 'protected' => true, - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('groups/1/variables', $expectedArray) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true)); - } - - /** - * @test - */ - public function shouldUpdateVariable() - { - $expectedKey = 'ftp_port'; - $expectedValue = '22'; - - $expectedArray = array( - 'key' => 'ftp_port', - 'value' => '22', - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('groups/1/variables/'.$expectedKey, array('value' => $expectedValue)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateVariable(1, $expectedKey, $expectedValue)); - } - - /** - * @test - */ - public function shouldUpdateVariableWithProtected() - { - $expectedArray = array( - 'key' => 'DEPLOY_SERVER', - 'value' => 'stage.example.com', - 'protected' => true, - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('groups/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true)); - } - - /** - * @test - */ - public function shouldRemoveVariable() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('groups/1/variables/ftp_password') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->removeVariable(1, 'ftp_password')); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Groups'; - } - - /** - * @test - */ - public function shouldGetAllGroupProjectsWithIssuesEnabled() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A group', 'issues_enabled' => true), - array('id' => 2, 'name' => 'Another group', 'issues_enabled' => true), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/projects', ['with_issues_enabled' => 'true']) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->projects(1, ['with_issues_enabled' => true])); - } - - /** - * @test - */ - public function shouldGetAllGroupProjectsWithMergeRequestsEnabled() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A group', 'merge_requests_enabled' => true), - array('id' => 2, 'name' => 'Another group', 'merge_requests_enabled' => true), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/projects', ['with_merge_requests_enabled' => 'true']) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->projects(1, ['with_merge_requests_enabled' => true])); - } - - /** - * @test - */ - public function shouldGetAllGroupProjectsSharedToGroup() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A project', 'shared_with_groups' => [1]), - array('id' => 2, 'name' => 'Another project', 'shared_with_groups' => [1]), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/projects', ['with_shared' => 'true']) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->projects(1, ['with_shared' => true])); - } - - /** - * @test - */ - public function shouldGetAllGroupProjectsIncludingSubsgroups() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A project'), - array('id' => 2, 'name' => 'Another project', 'shared_with_groups' => [1]), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/projects', ['include_subgroups' => 'true']) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->projects(1, ['include_subgroups' => true])); - } - - /** - * @test - */ - public function shouldGetAllGroupProjectsIncludingCustomAttributes() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A project', 'custom_Attr' => true), - array('id' => 2, 'name' => 'Another project', 'custom_Attr' => true), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/projects', ['with_custom_attributes' => 'true']) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->projects(1, ['with_custom_attributes' => true])); - } -} diff --git a/test/Gitlab/Tests/Api/IssueBoardsTest.php b/test/Gitlab/Tests/Api/IssueBoardsTest.php deleted file mode 100644 index 2b90db426..000000000 --- a/test/Gitlab/Tests/Api/IssueBoardsTest.php +++ /dev/null @@ -1,230 +0,0 @@ - 1, 'title' => 'A board'), - array('id' => 2, 'title' => 'Another board'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('boards', array()) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all()); - } - - /** - * @test - */ - public function shouldShowIssueBoard() - { - $expectedArray = array('id' => 2, 'name' => 'Another issue board'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/boards/2') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1, 2)); - } - - /** - * @test - */ - public function shouldCreateIssueBoard() - { - $expectedArray = array('id' => 3, 'name' => 'A new issue board'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/boards', array('name' => 'A new issue board')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create(1, array('name' => 'A new issue board'))); - } - - /** - * @test - */ - public function shouldUpdateIssueBoard() - { - $expectedArray = array('id' => 2, 'name' => 'A renamed issue board'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/boards/2', array('name' => 'A renamed issue board', 'labels' => 'foo')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(1, 2, array('name' => 'A renamed issue board', 'labels' => 'foo'))); - } - - /** - * @test - */ - public function shouldRemoveIssueBoard() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/boards/2') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->remove(1, 2)); - } - - /** - * @test - */ - public function shouldGetAllLists() - { - $expectedArray = array( - array( - 'id' => 1, - 'label' => array( - 'name' => 'First label', - 'color' => '#F0AD4E', - 'description' => null - ), - 'position' => 1 - ), array( - 'id' => 2, - 'label' => array( - 'name' => 'Second label', - 'color' => '#F0AD4E', - 'description' => null - ), - 'position' => 2 - ) - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/boards/2/lists') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->allLists(1, 2)); - } - - /** - * @test - */ - public function shouldGetList() - { - $expectedArray = array( - array( - 'id' => 3, - 'label' => array( - 'name' => 'Some label', - 'color' => '#F0AD4E', - 'description' => null - ), - 'position' => 3 - ) - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/boards/2/lists/3') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->showList(1, 2, 3)); - } - - /** - * @test - */ - public function shouldCreateList() - { - $expectedArray = array( - array( - 'id' => 3, - 'label' => array( - 'name' => 'Some label', - 'color' => '#F0AD4E', - 'description' => null - ), - 'position' => 3 - ) - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/boards/2/lists', array('label_id' => 4)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createList(1, 2, 4)); - } - - /** - * @test - */ - public function shouldUpdateList() - { - $expectedArray = array( - array( - 'id' => 3, - 'label' => array( - 'name' => 'Some label', - 'color' => '#F0AD4E', - 'description' => null - ), - 'position' => 1 - ) - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/5/boards/2/lists/3', array('position' => 1)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateList(5, 2, 3, 1)); - } - - /** - * @test - */ - public function shouldDeleteList() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/boards/2/lists/3') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->deleteList(1, 2, 3)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\IssueBoards'; - } -} diff --git a/test/Gitlab/Tests/Api/IssueLinksTest.php b/test/Gitlab/Tests/Api/IssueLinksTest.php deleted file mode 100644 index 6080b82df..000000000 --- a/test/Gitlab/Tests/Api/IssueLinksTest.php +++ /dev/null @@ -1,74 +0,0 @@ - 100), - array('issue_link_id' => 101), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues/10/links') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1, 10)); - } - - /** - * @test - */ - public function shouldCreateIssueLink() - { - $expectedArray = array( - 'source_issue' => array('iid' => 10, 'project_id' => 1), - 'target_issue' => array('iid' => 20, 'project_id' => 2), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/issues/10/links', array('target_project_id' => 2, 'target_issue_iid' => 20)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create(1, 10, 2, 20)); - } - - /** - * @test - */ - public function shouldRemoveIssueLink() - { - $expectedArray = array( - 'source_issue' => array('iid' => 10, 'project_id' => 1), - 'target_issue' => array('iid' => 20, 'project_id' => 2), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/issues/10/links/100') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->remove(1, 10, 100)); - } -} diff --git a/test/Gitlab/Tests/Api/IssuesStatisticsTest.php b/test/Gitlab/Tests/Api/IssuesStatisticsTest.php deleted file mode 100644 index 5a798793b..000000000 --- a/test/Gitlab/Tests/Api/IssuesStatisticsTest.php +++ /dev/null @@ -1,92 +0,0 @@ -getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('issues_statistics', [ - 'milestone' => '', - 'labels' => '', - 'scope' => 'created-by-me', - 'author_id' => 1, - 'author_username' => '', - 'assignee_id' => 1, - 'assignee_username' => '', - 'my_reaction_emoji' => '', - 'search' => '', - 'created_after' => $now->format('c'), - 'created_before' => $now->format('c'), - 'updated_after' => $now->format('c'), - 'updated_before' => $now->format('c'), - 'confidential' => 'false' - ]) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->all([ - 'milestone' => '', - 'labels' => '', - 'scope' => 'created-by-me', - 'author_id' => 1, - 'author_username' => '', - 'assignee_id' => 1, - 'assignee_username' => '', - 'my_reaction_emoji' => '', - 'search' => '', - 'created_after' => $now, - 'created_before' => $now, - 'updated_after' => $now, - 'updated_before' => $now, - 'confidential' => false - ])); - } - - /** - * @test - */ - public function shouldGetProject() - { - $expectedArray = array(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues_statistics', []) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->project(1, [])); - } - - /** - * @test - */ - public function shouldGetGroup() - { - $expectedArray = array(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('groups/1/issues_statistics', []) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->group(1, [])); - } - - - protected function getApiClass() - { - return IssuesStatistics::class; - } -} diff --git a/test/Gitlab/Tests/Api/IssuesTest.php b/test/Gitlab/Tests/Api/IssuesTest.php deleted file mode 100644 index 60f37a3cd..000000000 --- a/test/Gitlab/Tests/Api/IssuesTest.php +++ /dev/null @@ -1,461 +0,0 @@ - 1, 'title' => 'An issue'), - array('id' => 2, 'title' => 'Another issue'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('issues', array()) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all()); - } - - /** - * @test - */ - public function shouldGetProjectIssuesWithPagination() - { - $expectedArray = array( - array('id' => 1, 'title' => 'An issue'), - array('id' => 2, 'title' => 'Another issue'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues', array('page' => 2, 'per_page' => 5)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1, ['page' => 2, 'per_page' => 5])); - } - - /** - * @test - */ - public function shouldGetProjectIssuesWithParams() - { - $expectedArray = array( - array('id' => 1, 'title' => 'An issue'), - array('id' => 2, 'title' => 'Another issue'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues', array('order_by' => 'created_at', 'sort' => 'desc', 'labels' => 'foo,bar', 'state' => 'opened')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1, array('order_by' => 'created_at', 'sort' => 'desc', 'labels' => 'foo,bar', 'state' => 'opened'))); - } - - /** - * @test - */ - public function shouldShowIssue() - { - $expectedArray = array('id' => 2, 'title' => 'Another issue'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues/2') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1, 2)); - } - - /** - * @test - */ - public function shouldCreateIssue() - { - $expectedArray = array('id' => 3, 'title' => 'A new issue'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/issues', array('title' => 'A new issue', 'labels' => 'foo,bar')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create(1, array('title' => 'A new issue', 'labels' => 'foo,bar'))); - } - - /** - * @test - */ - public function shouldUpdateIssue() - { - $expectedArray = array('id' => 2, 'title' => 'A renamed issue'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/issues/2', array('title' => 'A renamed issue', 'labels' => 'foo')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(1, 2, array('title' => 'A renamed issue', 'labels' => 'foo'))); - } - - /** - * @test - */ - public function shouldMoveIssue() - { - $expectedArray = array('id' => 2, 'title' => 'A moved issue'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/issues/2/move', array('to_project_id' => 3)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->move(1, 2, 3)); - } - - /** - * @test - */ - public function shouldGetIssueComments() - { - $expectedArray = array( - array('id' => 1, 'body' => 'A comment'), - array('id' => 2, 'body' => 'Another comment') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues/2/notes') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->showComments(1, 2)); - } - - /** - * @test - */ - public function shouldGetIssueComment() - { - $expectedArray = array('id' => 3, 'body' => 'A new comment'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues/2/notes/3') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->showComment(1, 2, 3)); - } - - /** - * @test - */ - public function shouldCreateComment() - { - $expectedArray = array('id' => 3, 'body' => 'A new comment'); - - $api = $this->getApiMock(); - $api->expects($this->exactly(2)) - ->method('post') - ->with('projects/1/issues/2/notes', array('body' => 'A new comment')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->addComment(1, 2, array('body' => 'A new comment'))); - $this->assertEquals($expectedArray, $api->addComment(1, 2, 'A new comment')); - } - - /** - * @test - */ - public function shouldUpdateComment() - { - $expectedArray = array('id' => 3, 'body' => 'An edited comment'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/issues/2/notes/3', array('body' => 'An edited comment')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateComment(1, 2, 3, 'An edited comment')); - } - - /** - * @test - */ - public function shouldGetIssueDiscussions() - { - $expectedArray = array( - array('id' => 'abc', 'body' => 'A discussion'), - array('id' => 'def', 'body' => 'Another discussion') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues/2/discussions') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->showDiscussions(1, 2)); - } - - /** - * @test - */ - public function shouldGetIssueDiscussion() - { - $expectedArray = array('id' => 'abc', 'body' => 'A discussion'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues/2/discussions/abc') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->showDiscussion(1, 2, 'abc')); - } - - /** - * @test - */ - public function shouldCreateDiscussion() - { - $expectedArray = array('id' => 'abc', 'body' => 'A new discussion'); - - $api = $this->getApiMock(); - $api->expects($this->exactly(2)) - ->method('post') - ->with('projects/1/issues/2/discussions', array('body' => 'A new discussion')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->addDiscussion(1, 2, array('body' => 'A new discussion'))); - $this->assertEquals($expectedArray, $api->addDiscussion(1, 2, 'A new discussion')); - } - - /** - * @test - */ - public function shouldCreateDiscussionNote() - { - $expectedArray = array('id' => 3, 'body' => 'A new discussion note'); - - $api = $this->getApiMock(); - $api->expects($this->exactly(2)) - ->method('post') - ->with('projects/1/issues/2/discussions/abc/notes', array('body' => 'A new discussion note')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->addDiscussionNote(1, 2, 'abc', array('body' => 'A new discussion note'))); - $this->assertEquals($expectedArray, $api->addDiscussionNote(1, 2, 'abc', 'A new discussion note')); - } - - /** - * @test - */ - public function shouldUpdateDiscussionNote() - { - $expectedArray = array('id' => 3, 'body' => 'An edited discussion note'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/issues/2/discussions/abc/notes/3', array('body' => 'An edited discussion note')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateDiscussionNote(1, 2, 'abc', 3, 'An edited discussion note')); - } - - /** - * @test - */ - public function shouldRemoveDiscussionNote() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/issues/2/discussions/abc/notes/3') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->removeDiscussionNote(1, 2, 'abc', 3)); - } - - /** - * @test - */ - public function shouldSetTimeEstimate() - { - $expectedArray = array('time_estimate' => 14400, 'total_time_spent' => 0, 'human_time_estimate' => '4h', 'human_total_time_spent' => null); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/issues/2/time_estimate', array('duration' => '4h')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->setTimeEstimate(1, 2, '4h')); - } - - /** - * @test - */ - public function shouldResetTimeEstimate() - { - $expectedArray = array('time_estimate' => 0, 'total_time_spent' => 0, 'human_time_estimate' => null, 'human_total_time_spent' => null); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/issues/2/reset_time_estimate') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->resetTimeEstimate(1, 2)); - } - - /** - * @test - */ - public function shouldAddSpentTime() - { - $expectedArray = array('time_estimate' => 0, 'total_time_spent' => 14400, 'human_time_estimate' => null, 'human_total_time_spent' => '4h'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/issues/2/add_spent_time', array('duration' => '4h')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->addSpentTime(1, 2, '4h')); - } - - /** - * @test - */ - public function shouldResetSpentTime() - { - $expectedArray = array('time_estimate' => 0, 'total_time_spent' => 0, 'human_time_estimate' => null, 'human_total_time_spent' => null); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/issues/2/reset_spent_time') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->resetSpentTime(1, 2)); - } - - /** - * @test - */ - public function shouldGetIssueTimeStats() - { - $expectedArray = array('time_estimate' => 14400, 'total_time_spent' => 5400, 'human_time_estimate' => '4h', 'human_total_time_spent' => '1h 30m'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues/2/time_stats') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->getTimeStats(1, 2)); - } - - /** - * @test - */ - public function shouldGetIssueAwardEmoji() - { - $expectedArray = array( - array('id' => 1, 'name' => 'sparkles'), - array('id' => 2, 'name' => 'heart_eyes'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues/2/award_emoji') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->awardEmoji(1, 2)); - } - - /** - * @test - */ - public function shouldGetIssueClosedByMergeRequests() - { - $expectedArray = array( - array('id' => 1, 'iid' => '1111', 'title' => 'Just saving the world'), - array('id' => 2, 'iid' => '1112', 'title' => 'Adding new feature to get merge requests that close an issue'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues/2/closed_by') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->closedByMergeRequests(1, 2)); - } - - /** - * @test - */ - public function shouldGetProjectIssuesByAssignee() - { - $expectedArray = array( - array('id' => 1, 'title' => 'An issue'), - array('id' => 2, 'title' => 'Another issue'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues', array('assignee_id' => 1)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1, array('assignee_id' => 1))); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Issues'; - } -} diff --git a/test/Gitlab/Tests/Api/JobsTest.php b/test/Gitlab/Tests/Api/JobsTest.php deleted file mode 100644 index 63f9e2f9a..000000000 --- a/test/Gitlab/Tests/Api/JobsTest.php +++ /dev/null @@ -1,228 +0,0 @@ - 1, 'name' => 'A job'), - array('id' => 2, 'name' => 'Another job'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/jobs', array( - 'scope' => ['pending'] - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1, ['scope' => Jobs::SCOPE_PENDING])); - } - - /** - * @test - */ - public function shouldGetPipelineJobs() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A job'), - array('id' => 2, 'name' => 'Another job'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/pipelines/2/jobs', array( - 'scope' => ['pending', 'running'] - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->pipelineJobs(1, 2, ['scope' => [Jobs::SCOPE_PENDING, Jobs::SCOPE_RUNNING]])); - } - - /** - * @test - */ - public function shouldGetJob() - { - $expectedArray = array('id' => 3, 'name' => 'A job'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/jobs/3') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1, 3)); - } - - /** - * @test - */ - public function shouldGetArtifacts() - { - $returnedStream = new Response(200, [], 'foobar'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('getAsResponse') - ->with('projects/1/jobs/3/artifacts') - ->will($this->returnValue($returnedStream)) - ; - - $this->assertEquals('foobar', $api->artifacts(1, 3)->getContents()); - } - - /** - * @test - */ - public function shouldGetArtifactsByRefName() - { - $returnedStream = new Response(200, [], 'foobar'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('getAsResponse') - ->with('projects/1/jobs/artifacts/master/download', array( - 'job' => 'job_name' - )) - ->will($this->returnValue($returnedStream)) - ; - - $this->assertEquals('foobar', $api->artifactsByRefName(1, 'master', 'job_name')->getContents()); - } - - /** - * @test - */ - public function shouldGetArtifactByRefName() - { - $returnedStream = new Response(200, [], 'foobar'); - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('getAsResponse') - ->with('projects/1/jobs/artifacts/master/raw/artifact_path', array( - 'job' => 'job_name' - )) - ->will($this->returnValue($returnedStream)) - ; - $this->assertEquals('foobar', $api->artifactByRefName(1, 'master', 'job_name', 'artifact_path')->getContents()); - } - - /** - * @test - */ - public function shouldGetTrace() - { - $expectedString = "some trace"; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/jobs/3/trace') - ->will($this->returnValue($expectedString)) - ; - - $this->assertEquals($expectedString, $api->trace(1, 3)); - } - - /** - * @test - */ - public function shouldCancel() - { - $expectedArray = array('id' => 3, 'name' => 'A job'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/jobs/3/cancel') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->cancel(1, 3)); - } - - /** - * @test - */ - public function shouldRetry() - { - $expectedArray = array('id' => 3, 'name' => 'A job'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/jobs/3/retry') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->retry(1, 3)); - } - - /** - * @test - */ - public function shouldErase() - { - $expectedArray = array('id' => 3, 'name' => 'A job'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/jobs/3/erase') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->erase(1, 3)); - } - - /** - * @test - */ - public function shouldKeepArtifacts() - { - $expectedArray = array('id' => 3, 'name' => 'A job'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/jobs/3/artifacts/keep') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->keepArtifacts(1, 3)); - } - - /** - * @test - */ - public function shouldPlay() - { - $expectedArray = array('id' => 3, 'name' => 'A job'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/jobs/3/play') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->play(1, 3)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Jobs'; - } -} diff --git a/test/Gitlab/Tests/Api/KeysTest.php b/test/Gitlab/Tests/Api/KeysTest.php deleted file mode 100644 index 355e77bdf..000000000 --- a/test/Gitlab/Tests/Api/KeysTest.php +++ /dev/null @@ -1,24 +0,0 @@ - 1, 'title' => 'A key', 'key' => 'ssh-rsa key', 'created_at' => '2016-01-01T01:00:00.000Z'); - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('keys/1') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->show(1)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Keys'; - } -} diff --git a/test/Gitlab/Tests/Api/MergeRequestsTest.php b/test/Gitlab/Tests/Api/MergeRequestsTest.php deleted file mode 100644 index b9bf8cbc3..000000000 --- a/test/Gitlab/Tests/Api/MergeRequestsTest.php +++ /dev/null @@ -1,508 +0,0 @@ -getMultipleMergeRequestsData(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests', array()) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1)); - } - - /** - * @test - */ - public function shouldGetAllWithParams() - { - $expectedArray = $this->getMultipleMergeRequestsData(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests', [ - 'page' => 2, - 'per_page' => 5, - 'labels' => 'label1,label2,label3', - 'milestone' => 'milestone1', - 'order_by' => 'updated_at', - 'state' => 'all', - 'sort' => 'desc', - 'scope' => 'all', - 'author_id' => 1, - 'assignee_id' => 1, - 'source_branch' => 'develop', - 'target_branch' => 'master', - ]) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1, [ - 'page' => 2, - 'per_page' => 5, - 'labels' => 'label1,label2,label3', - 'milestone' => 'milestone1', - 'order_by' => 'updated_at', - 'state' => 'all', - 'sort' => 'desc', - 'scope' => 'all', - 'author_id' => 1, - 'assignee_id' => 1, - 'source_branch' => 'develop', - 'target_branch' => 'master', - ])); - } - - /** - * @test - */ - public function shouldGetAllWithDateTimeParams() - { - $expectedArray = $this->getMultipleMergeRequestsData(); - - $createdAfter = new \DateTime('2018-01-01 00:00:00'); - $createdBefore = new \DateTime('2018-01-31 00:00:00'); - - $expectedWithArray = [ - 'created_after' => $createdAfter->format(DATE_ATOM), - 'created_before' => $createdBefore->format(DATE_ATOM), - ]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests', $expectedWithArray) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals( - $expectedArray, - $api->all(1, ['created_after' => $createdAfter, 'created_before' => $createdBefore]) - ); - } - - /** - * @test - */ - public function shouldShowMergeRequest() - { - $expectedArray = array('id' => 2, 'name' => 'A merge request'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests/2') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1, 2)); - } - - /** - * @test - */ - public function shouldCreateMergeRequestWithoutOptionalParams() - { - $expectedArray = array('id' => 3, 'title' => 'Merge Request'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/merge_requests', array( - 'title' => 'Merge Request', - 'target_branch' => 'master', - 'source_branch' => 'develop', - 'description' => null, - 'assignee_id' => null, - 'target_project_id' => null - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create(1, 'develop', 'master', 'Merge Request')); - } - - /** - * @test - */ - public function shouldCreateMergeRequestWithOptionalParams() - { - $expectedArray = array('id' => 3, 'title' => 'Merge Request'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/merge_requests', array( - 'title' => 'Merge Request', - 'target_branch' => 'master', - 'source_branch' => 'develop', - 'description' => 'Some changes', - 'assignee_id' => 6, - 'target_project_id' => 20 - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create(1, 'develop', 'master', 'Merge Request', 6, 20, 'Some changes')); - } - - /** - * @test - */ - public function shouldUpdateMergeRequest() - { - $expectedArray = array('id' => 2, 'title' => 'Updated title'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/merge_requests/2', array('title' => 'Updated title', 'description' => 'No so many changes now', 'state_event' => 'close')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(1, 2, array( - 'title' => 'Updated title', - 'description' => 'No so many changes now', - 'state_event' => 'close' - ))); - } - - /** - * @test - */ - public function shouldMergeMergeRequest() - { - $expectedArray = array('id' => 2, 'title' => 'Updated title'); - - $api = $this->getApiMock(); - $api->expects($this->exactly(2)) - ->method('put') - ->with('projects/1/merge_requests/2/merge', array('merge_commit_message' => 'Accepted')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->merge(1, 2, 'Accepted')); - $this->assertEquals($expectedArray, $api->merge(1, 2, array('merge_commit_message' => 'Accepted'))); - } - - /** - * @test - */ - public function shouldGetMergeRequestNotes() - { - $expectedArray = array( - array('id' => 1, 'body' => 'A comment'), - array('id' => 2, 'body' => 'Another comment') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests/2/notes') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->showNotes(1, 2)); - } - - /** - * @test - */ - public function shouldRemoveMergeRequestNote() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/merge_requests/2/notes/1') - ->will($this->returnValue($expectedBool)); - $this->assertEquals($expectedBool, $api->removeNote(1, 2, 1)); - } - - /** - * @test - */ - public function shouldGetMergeRequestChanges() - { - $expectedArray = array('id' => 1, 'title' => 'A merge request'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests/2/changes') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->changes(1, 2)); - } - - /** - * @test - */ - public function shouldGetMergeRequestDiscussions() - { - $expectedArray = array( - array('id' => 'abc', 'body' => 'A discussion'), - array('id' => 'def', 'body' => 'Another discussion') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests/2/discussions') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->showDiscussions(1, 2)); - } - - /** - * @test - */ - public function shouldGetMergeRequestDiscussion() - { - $expectedArray = array('id' => 'abc', 'body' => 'A discussion'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests/2/discussions/abc') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->showDiscussion(1, 2, 'abc')); - } - - /** - * @test - */ - public function shouldCreateDiscussion() - { - $expectedArray = array('id' => 'abc', 'body' => 'A new discussion'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/merge_requests/2/discussions', array('body' => 'A new discussion')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->addDiscussion(1, 2, array('body' => 'A new discussion'))); - } - - /** - * @test - */ - public function shouldResolveDiscussion() - { - $expectedArray = array('id' => 'abc', 'resolved' => true); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/merge_requests/2/discussions/abc', array('resolved' => true)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->resolveDiscussion(1, 2, 'abc', true)); - } - - /** - * @test - */ - public function shouldUnresolveDiscussion() - { - $expectedArray = array('id' => 'abc', 'resolved' => false); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/merge_requests/2/discussions/abc', array('resolved' => false)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->resolveDiscussion(1, 2, 'abc', false)); - } - - /** - * @test - */ - public function shouldCreateDiscussionNote() - { - $expectedArray = array('id' => 3, 'body' => 'A new discussion note'); - - $api = $this->getApiMock(); - $api->expects($this->exactly(2)) - ->method('post') - ->with('projects/1/merge_requests/2/discussions/abc/notes', array('body' => 'A new discussion note')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->addDiscussionNote(1, 2, 'abc', array('body' => 'A new discussion note'))); - $this->assertEquals($expectedArray, $api->addDiscussionNote(1, 2, 'abc', 'A new discussion note')); - } - - /** - * @test - */ - public function shouldUpdateDiscussionNote() - { - $expectedArray = array('id' => 3, 'body' => 'An edited discussion note'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/merge_requests/2/discussions/abc/notes/3', array('body' => 'An edited discussion note')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateDiscussionNote(1, 2, 'abc', 3, array('body' => 'An edited discussion note'))); - } - - /** - * @test - */ - public function shouldRemoveDiscussionNote() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/merge_requests/2/discussions/abc/notes/3') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->removeDiscussionNote(1, 2, 'abc', 3)); - } - - /** - * @test - */ - public function shouldGetIssuesClosedByMergeRequest() - { - $expectedArray = array('id' => 1, 'title' => 'A merge request'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests/2/closes_issues') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->closesIssues(1, 2)); - } - - /** - * @test - */ - public function shouldGetMergeRequestByIid() - { - $expectedArray = array('id' => 1, 'title' => 'A merge request'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests', array('iids' => [2])) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1, ['iids' => [2]])); - } - - /** - * @test - */ - public function shouldApproveMergeRequest() - { - $expectedArray = array('id' => 1, 'title' => 'Approvals API'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/merge_requests/2/approve') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->approve(1, 2)); - } - - /** - * @test - */ - public function shouldUnApproveMergeRequest() - { - $expectedArray = array('id' => 1, 'title' => 'Approvals API'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/merge_requests/2/unapprove') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->unapprove(1, 2)); - } - - /** - * @test - */ - public function shouldGetMergeRequestApprovals() - { - $expectedArray = array('id' => 1, 'title' => 'Approvals API'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests', array('iids' => [2])) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1, ['iids' => [2]])); - } - - /** - * @test - */ - public function shouldGetMergeRequestAwardEmoji() - { - $expectedArray = array( - array('id' => 1, 'name' => 'sparkles'), - array('id' => 2, 'name' => 'heart_eyes'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/merge_requests/2/award_emoji') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->awardEmoji(1, 2)); - } - - protected function getMultipleMergeRequestsData() - { - return array( - array('id' => 1, 'title' => 'A merge request'), - array('id' => 2, 'title' => 'Another merge request') - ); - } - - protected function getApiClass() - { - return 'Gitlab\Api\MergeRequests'; - } -} diff --git a/test/Gitlab/Tests/Api/MilestonesTest.php b/test/Gitlab/Tests/Api/MilestonesTest.php deleted file mode 100644 index e45be8ea0..000000000 --- a/test/Gitlab/Tests/Api/MilestonesTest.php +++ /dev/null @@ -1,117 +0,0 @@ - 1, 'title' => 'A milestone'), - array('id' => 2, 'title' => 'Another milestone'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/milestones') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1)); - } - - /** - * @test - */ - public function shouldShowMilestone() - { - $expectedArray = array('id' => 1, 'name' => 'A milestone'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/milestones/2') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1, 2)); - } - - /** - * @test - */ - public function shouldCreateMilestone() - { - $expectedArray = array('id' => 3, 'title' => 'A new milestone'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/milestones', array('description' => 'Some text', 'title' => 'A new milestone')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create(1, array('description' => 'Some text', 'title' => 'A new milestone'))); - } - - /** - * @test - */ - public function shouldUpdateMilestone() - { - $expectedArray = array('id' => 3, 'title' => 'Updated milestone'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/milestones/3', array('title' => 'Updated milestone', 'due_date' => '2015-04-01', 'state_event' => 'close')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(1, 3, array('title' => 'Updated milestone', 'due_date' => '2015-04-01', 'state_event' => 'close'))); - } - - /** - * @test - */ - public function shouldRemoveMilestone() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/milestones/2') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->remove(1, 2)); - } - - /** - * @test - */ - public function shouldGetMilestonesIssues() - { - $expectedArray = array( - array('id' => 1, 'title' => 'An issue'), - array('id' => 2, 'title' => 'Another issue'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/milestones/3/issues') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->issues(1, 3)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Milestones'; - } -} diff --git a/test/Gitlab/Tests/Api/ProjectNamespacesTest.php b/test/Gitlab/Tests/Api/ProjectNamespacesTest.php deleted file mode 100644 index 3279e412a..000000000 --- a/test/Gitlab/Tests/Api/ProjectNamespacesTest.php +++ /dev/null @@ -1,48 +0,0 @@ - 1, 'name' => 'bespokes'), - array('id' => 2, 'name' => 'internal') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('namespaces', array()) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all()); - } - - /** - * @test - */ - public function shouldShowNamespace() - { - $expectedArray = array('id' => 1, 'name' => 'internal'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('namespaces/1') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\ProjectNamespaces'; - } -} diff --git a/test/Gitlab/Tests/Api/ProjectsTest.php b/test/Gitlab/Tests/Api/ProjectsTest.php deleted file mode 100644 index 55079343e..000000000 --- a/test/Gitlab/Tests/Api/ProjectsTest.php +++ /dev/null @@ -1,1704 +0,0 @@ -getMultipleProjectsData(); - - $api = $this->getMultipleProjectsRequestMock('projects', $expectedArray); - - $this->assertEquals($expectedArray, $api->all()); - } - - /** - * @test - */ - public function shouldGetAllProjectsSortedByName() - { - $expectedArray = $this->getMultipleProjectsData(); - - $api = $this->getMultipleProjectsRequestMock('projects', $expectedArray, - ['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc']); - - $this->assertEquals($expectedArray, - $api->all(['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc'])); - } - - /** - * @test - */ - public function shouldNotNeedPaginationWhenGettingProjects() - { - $expectedArray = $this->getMultipleProjectsData(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->all()); - } - - /** - * @test - */ - public function shouldGetAccessibleProjects() - { - $expectedArray = $this->getMultipleProjectsData(); - - $api = $this->getMultipleProjectsRequestMock('projects', $expectedArray); - - $this->assertEquals($expectedArray, $api->all()); - } - - /** - * @test - */ - public function shouldGetOwnedProjects() - { - $expectedArray = $this->getMultipleProjectsData(); - - $api = $this->getMultipleProjectsRequestMock('projects', $expectedArray, ['owned' => 'true']); - - $this->assertEquals($expectedArray, $api->all(['owned' => true])); - } - - /** - * @test - */ - public function shouldGetNotArchivedProjects() - { - $expectedArray = $this->getMultipleProjectsData(); - - $api = $this->getMultipleProjectsRequestMock('projects', $expectedArray, ['archived' => 'false']); - - $this->assertEquals($expectedArray, $api->all(['archived' => false])); - } - - /** - * @test - * @dataProvider possibleAccessLevels - */ - public function shouldGetProjectsWithMinimumAccessLevel($level) - { - $expectedArray = $this->getMultipleProjectsData(); - - $api = $this->getMultipleProjectsRequestMock('projects', $expectedArray, ['min_access_level' => $level]); - - $this->assertEquals($expectedArray, $api->all(['min_access_level' => $level])); - } - - /** - * @test - */ - public function shouldSearchProjects() - { - $expectedArray = $this->getMultipleProjectsData(); - - $api = $this->getMultipleProjectsRequestMock('projects', $expectedArray, ['search' => 'a project']); - $this->assertEquals($expectedArray, $api->all(['search' => 'a project'])); - } - - /** - * @test - */ - public function shouldShowProject() - { - $expectedArray = array('id' => 1, 'name' => 'Project Name'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->show(1)); - } - - /** - * @test - */ - public function shouldShowProjectWithStatistics() - { - $expectedArray = array( - 'id' => 1, - 'name' => 'Project Name', - 'statistics' => array( - 'commit_count' => 37, - 'storage_size' => 1038090, - 'repository_size' => 1038090, - 'lfs_objects_size' => 0, - 'job_artifacts_size' => 0 - ) - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1', ['statistics' => true]) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->show(1, ['statistics' => true])); - } - - /** - * @test - */ - public function shouldCreateProject() - { - $expectedArray = array('id' => 1, 'name' => 'Project Name'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects', array('name' => 'Project Name', 'issues_enabled' => true)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->create('Project Name', array( - 'issues_enabled' => true - ))); - } - - /** - * @test - */ - public function shouldUpdateProject() - { - $expectedArray = array('id' => 1, 'name' => 'Updated Name'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1', array('name' => 'Updated Name', 'issues_enabled' => true)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->update(1, array( - 'name' => 'Updated Name', - 'issues_enabled' => true - ))); - } - - /** - * @test - */ - public function shouldArchiveProject() - { - $expectedArray = array('id' => 1, 'archived' => true); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/archive') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->archive(1)); - } - - /** - * @test - */ - public function shouldUnarchiveProject() - { - $expectedArray = array('id' => 1, 'archived' => false); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/unarchive') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->unarchive(1)); - } - - /** - * @test - */ - public function shouldCreateProjectForUser() - { - $expectedArray = array('id' => 1, 'name' => 'Project Name'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/user/1', array('name' => 'Project Name', 'issues_enabled' => true)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->createForUser(1, 'Project Name', array( - 'issues_enabled' => true - ))); - } - - /** - * @test - */ - public function shouldRemoveProject() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1') - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->remove(1)); - } - - /** - * @test - */ - public function shouldGetPipelines() - { - $expectedArray = array( - array('id' => 1, 'status' => 'success', 'ref' => 'new-pipeline'), - array('id' => 2, 'status' => 'failed', 'ref' => 'new-pipeline'), - array('id' => 3, 'status' => 'pending', 'ref' => 'test-pipeline') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/pipelines') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->pipelines(1)); - } - - /** - * Check we can request project issues. - * - * @test - */ - public function shouldGetProjectIssues() - { - $expectedArray = $this->getProjectIssuesExpectedArray(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->issues(1)); - } - - /** - * Check we can request project issues with query parameters. - * - * @test - */ - public function shouldGetProjectIssuesParameters() - { - $expectedArray = $this->getProjectIssuesExpectedArray(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/issues') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->issues(1, array('state' => 'opened'))); - } - - /** - * Get expected array for tests which check project issues method. - * - * @return array - * Project issues list. - */ - public function getProjectIssuesExpectedArray() - { - return [ - [ - 'state' => 'opened', - 'description' => 'Ratione dolores corrupti mollitia soluta quia.', - 'author' => [ - 'state' => 'active', - 'id' => 18, - 'web_url' => 'https => //gitlab.example.com/eileen.lowe', - 'name' => 'Alexandra Bashirian', - 'avatar_url' => null, - 'username' => 'eileen.lowe' - ], - 'milestone' => [ - 'project_id' => 1, - 'description' => 'Ducimus nam enim ex consequatur cumque ratione.', - 'state' => 'closed', - 'due_date' => null, - 'iid' => 2, - 'created_at' => '2016-01-04T15 => 31 => 39.996Z', - 'title' => 'v4.0', - 'id' => 17, - 'updated_at' => '2016-01-04T15 => 31 => 39.996Z' - ], - 'project_id' => 1, - 'assignees' => [ - [ - 'state' => 'active', - 'id' => 1, - 'name' => 'Administrator', - 'web_url' => 'https => //gitlab.example.com/root', - 'avatar_url' => null, - 'username' => 'root' - ] - ], - 'assignee' => [ - 'state' => 'active', - 'id' => 1, - 'name' => 'Administrator', - 'web_url' => 'https => //gitlab.example.com/root', - 'avatar_url' => null, - 'username' => 'root' - ], - 'updated_at' => '2016-01-04T15 => 31 => 51.081Z', - 'closed_at' => null, - 'closed_by' => null, - 'id' => 76, - 'title' => 'Consequatur vero maxime deserunt laboriosam est voluptas dolorem.', - 'created_at' => '2016-01-04T15 => 31 => 51.081Z', - 'iid' => 6, - 'labels' => [], - 'user_notes_count' => 1, - 'due_date' => '2016-07-22', - 'web_url' => 'http => //example.com/example/example/issues/6', - 'confidential' => false, - 'weight' => null, - 'discussion_locked' => false, - 'time_stats' => [ - 'time_estimate' => 0, - 'total_time_spent' => 0, - 'human_time_estimate' => null, - 'human_total_time_spent' => null - ], - ] - ]; - } - - /** - * @test - */ - public function shouldGetBoards() - { - $expectedArray = $this->getProjectIssuesExpectedArray(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/boards') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->boards(1)); - } - - /** - * Get expected array for tests which check project boards - * - * @return array - * Project issues list. - */ - public function getProjectBoardsExpectedArray() - { - return [ - [ - 'id' => 1, - 'project' => [ - 'id' => 5, - 'name' => 'Diaspora Project Site', - 'name_with_namespace' => 'Diaspora / Diaspora Project Site', - 'path' => 'diaspora-project-site', - 'path_with_namespace' => 'diaspora/diaspora-project-site', - 'http_url_to_repo' => 'http => //example.com/diaspora/diaspora-project-site.git', - 'web_url' => 'http => //example.com/diaspora/diaspora-project-site' - ], - 'milestone' => [ - 'id' => 12, - 'title' => '10.0', - ], - 'lists' => [ - [ - 'id' => 1, - 'label' => [ - 'name' => 'Testing', - 'color' => '#F0AD4E', - 'description' => null - ], - 'position' => 1 - ], - [ - 'id' => 2, - 'label' => [ - 'name' => 'Ready', - 'color' => '#FF0000', - 'description' => null - ], - 'position' => 2 - ], - [ - 'id' => 3, - 'label' => [ - 'name' => 'Production', - 'color' => '#FF5F00', - 'description' => null - ], - 'position' => 3 - ] - ] - ] - ]; - } - - /** - * @test - */ - public function shouldGetPipelinesWithBooleanParam() - { - $expectedArray = array( - array('id' => 1, 'status' => 'success', 'ref' => 'new-pipeline'), - array('id' => 2, 'status' => 'failed', 'ref' => 'new-pipeline'), - array('id' => 3, 'status' => 'pending', 'ref' => 'test-pipeline') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/pipelines', ['yaml_errors' => 'false']) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->pipelines(1, ['yaml_errors' => false])); - } - - /** - * @test - */ - public function shouldGetPipelinesWithSHA() - { - $expectedArray = array( - array('id' => 1, 'status' => 'success', 'ref' => 'new-pipeline'), - array('id' => 2, 'status' => 'failed', 'ref' => 'new-pipeline'), - array('id' => 3, 'status' => 'pending', 'ref' => 'test-pipeline') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/pipelines', ['sha' => '123']) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->pipelines(1, ['sha' => '123'])); - } - - /** - * @test - */ - public function shouldGetPipeline() - { - $expectedArray = array( - array('id' => 1, 'status' => 'success', 'ref' => 'new-pipeline'), - array('id' => 2, 'status' => 'failed', 'ref' => 'new-pipeline'), - array('id' => 3, 'status' => 'pending', 'ref' => 'test-pipeline') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/pipelines/3') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->pipeline(1, 3)); - } - - /** - * @test - */ - public function shouldCreatePipeline() - { - $expectedArray = array( - array('id' => 4, 'status' => 'created', 'ref' => 'test-pipeline') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/pipeline', array('ref' => 'test-pipeline')) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->createPipeline(1, 'test-pipeline')); - } - - /** - * @test - */ - public function shouldRetryPipeline() - { - $expectedArray = array( - array('id' => 5, 'status' => 'pending', 'ref' => 'test-pipeline') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/pipelines/4/retry') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->retryPipeline(1, 4)); - } - - /** - * @test - */ - public function shouldCancelPipeline() - { - $expectedArray = array( - array('id' => 6, 'status' => 'cancelled', 'ref' => 'test-pipeline') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/pipelines/6/cancel') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->cancelPipeline(1, 6)); - } - - /** - * @test - */ - public function shouldDeletePipeline() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/pipelines/3') - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->deletePipeline(1, 3)); - } - - /** - * @test - */ - public function shouldGetAllMembers() - { - $expectedArray = array( - array('id' => 1, 'name' => 'Matt'), - array('id' => 2, 'name' => 'Bob') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/members/all') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->allMembers(1)); - } - - /** - * @test - */ - public function shouldGetMembers() - { - $expectedArray = array( - array('id' => 1, 'name' => 'Matt'), - array('id' => 2, 'name' => 'Bob') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/members') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->members(1)); - } - - /** - * @test - */ - public function shouldGetMembersWithQuery() - { - $expectedArray = array( - array('id' => 1, 'name' => 'Matt') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/members', array('query' => 'at')) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->members(1, 'at')); - } - - /** - * @test - */ - public function shouldGetMembersWithNullQuery() - { - $expectedArray = array( - array('id' => 1, 'name' => 'Matt'), - array('id' => 2, 'name' => 'Bob') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/members') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->members(1, null)); - } - - /** - * @test - */ - public function shouldGetMembersWithPagination() - { - $expectedArray = array( - array('id' => 1, 'name' => 'Matt'), - array('id' => 2, 'name' => 'Bob') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/members', array( - 'page' => 2, - 'per_page' => 15 - )) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->members(1, array('page' => 2, 'per_page' => 15))); - } - - /** - * @test - */ - public function shouldGetMember() - { - $expectedArray = array('id' => 2, 'name' => 'Matt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/members/2') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->member(1, 2)); - } - - /** - * @test - */ - public function shouldAddMember() - { - $expectedArray = array('id' => 1, 'name' => 'Matt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/members', array('user_id' => 2, 'access_level' => 3)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->addMember(1, 2, 3)); - } - - /** - * @test - */ - public function shouldSaveMember() - { - $expectedArray = array('id' => 1, 'name' => 'Matt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/members/2', array('access_level' => 4)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->saveMember(1, 2, 4)); - } - - /** - * @test - */ - public function shouldRemoveMember() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/members/2') - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->removeMember(1, 2)); - } - - /** - * @test - */ - public function shouldGetHooks() - { - $expectedArray = array( - array('id' => 1, 'name' => 'Test hook'), - array('id' => 2, 'name' => 'Another hook'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/hooks') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->hooks(1)); - } - - /** - * @test - */ - public function shouldGetHook() - { - $expectedArray = array('id' => 2, 'name' => 'Another hook'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/hooks/2') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->hook(1, 2)); - } - - /** - * @test - */ - public function shouldAddHook() - { - $expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/hooks', array( - 'url' => 'http://www.example.com', - 'push_events' => true, - 'issues_events' => true, - 'merge_requests_events' => true - )) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com', - array('push_events' => true, 'issues_events' => true, 'merge_requests_events' => true))); - } - - /** - * @test - */ - public function shouldAddHookWithOnlyUrl() - { - $expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/hooks', array('url' => 'http://www.example.com', 'push_events' => true)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com')); - } - - /** - * @test - */ - public function shouldAddHookWithoutPushEvents() - { - $expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/hooks', array('url' => 'http://www.example.com', 'push_events' => false)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->addHook(1, 'http://www.example.com', array('push_events' => false))); - } - - /** - * @test - */ - public function shouldUpdateHook() - { - $expectedArray = array('id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/hooks/3', array('url' => 'http://www.example-test.com', 'push_events' => false)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, - $api->updateHook(1, 3, array('url' => 'http://www.example-test.com', 'push_events' => false))); - } - - /** - * @test - */ - public function shouldRemoveHook() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/hooks/2') - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->removeHook(1, 2)); - } - - /** - * @test - */ - public function shouldTransfer() - { - $expectedArray = array( - 'id' => 1, - 'name' => 'Project Name', - 'namespace' => array('name' => 'a_namespace'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/transfer', ['namespace' => 'a_namespace']) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->transfer(1, 'a_namespace')); - } - - /** - * @test - */ - public function shouldGetDeployKeys() - { - $expectedArray = array( - array('id' => 1, 'title' => 'test-key'), - array('id' => 2, 'title' => 'another-key') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/deploy_keys') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->deployKeys(1)); - } - - /** - * @test - */ - public function shouldGetDeployKey() - { - $expectedArray = array('id' => 2, 'title' => 'another-key'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/deploy_keys/2') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->deployKey(1, 2)); - } - - /** - * @test - */ - public function shouldAddKey() - { - $expectedArray = array('id' => 3, 'title' => 'new-key', 'can_push' => false); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/deploy_keys', array('title' => 'new-key', 'key' => '...', 'can_push' => false)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->addDeployKey(1, 'new-key', '...')); - } - - /** - * @test - */ - public function shouldAddKeyWithPushOption() - { - $expectedArray = array('id' => 3, 'title' => 'new-key', 'can_push' => true); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/deploy_keys', array('title' => 'new-key', 'key' => '...', 'can_push' => true)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->addDeployKey(1, 'new-key', '...', true)); - } - - /** - * @test - */ - public function shouldDeleteDeployKey() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/deploy_keys/3') - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->deleteDeployKey(1, 3)); - } - - /** - * @test - */ - public function shoudEnableDeployKey() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/deploy_keys/3/enable') - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->enableDeployKey(1, 3)); - } - - /** - * @test - */ - public function shouldGetEvents() - { - $expectedArray = array( - array('id' => 1, 'title' => 'An event'), - array('id' => 2, 'title' => 'Another event') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/events', array()) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->events(1)); - } - - /** - * @test - */ - public function shouldGetEventsWithDateTimeParams() - { - $expectedArray = [ - ['id' => 1, 'title' => 'An event'], - ['id' => 2, 'title' => 'Another event'] - ]; - - $after = new \DateTime('2018-01-01 00:00:00'); - $before = new \DateTime('2018-01-31 00:00:00'); - - $expectedWithArray = [ - 'after' => $after->format('Y-m-d'), - 'before' => $before->format('Y-m-d'), - ]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/events', $expectedWithArray) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->events(1, ['after' => $after, 'before' => $before])); - } - - /** - * @test - */ - public function shouldGetEventsWithPagination() - { - $expectedArray = array( - array('id' => 1, 'title' => 'An event'), - array('id' => 2, 'title' => 'Another event') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/events', array( - 'page' => 2, - 'per_page' => 15 - )) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->events(1, ['page' => 2, 'per_page' => 15])); - } - - /** - * @test - */ - public function shouldGetLabels() - { - $expectedArray = array( - array('id' => 987, 'name' => 'bug', 'color' => '#000000'), - array('id' => 123, 'name' => 'feature', 'color' => '#ff0000') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/labels') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->labels(1)); - } - - /** - * @test - */ - public function shouldAddLabel() - { - $expectedArray = array('name' => 'bug', 'color' => '#000000'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/labels', array('name' => 'wont-fix', 'color' => '#ffffff')) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->addLabel(1, array('name' => 'wont-fix', 'color' => '#ffffff'))); - } - - /** - * @test - */ - public function shouldUpdateLabel() - { - $expectedArray = array('name' => 'bug', 'color' => '#00ffff'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/labels', array('name' => 'bug', 'new_name' => 'big-bug', 'color' => '#00ffff')) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, - $api->updateLabel(1, array('name' => 'bug', 'new_name' => 'big-bug', 'color' => '#00ffff'))); - } - - /** - * @test - */ - public function shouldRemoveLabel() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/labels', array('name' => 'bug')) - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->removeLabel(1, 'bug')); - } - - /** - * @test - */ - public function shouldGetLanguages() - { - $expectedArray = ['php' => 100]; - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->languages(1)); - } - - /** - * @test - */ - public function shouldForkWithNamespace() - { - $expectedArray = [ - 'namespace' => 'new_namespace', - ]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/fork', $expectedArray) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->fork(1, [ - 'namespace' => 'new_namespace', - ])); - } - - /** - * @test - */ - public function shouldForkWithNamespaceAndPath() - { - $expectedArray = [ - 'namespace' => 'new_namespace', - 'path' => 'new_path', - ]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/fork', $expectedArray) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->fork(1, [ - 'namespace' => 'new_namespace', - 'path' => 'new_path', - ])); - } - - /** - * @test - */ - public function shouldForkWithNamespaceAndPathAndName() - { - $expectedArray = [ - 'namespace' => 'new_namespace', - 'path' => 'new_path', - 'name' => 'new_name' - ]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/fork', $expectedArray) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->fork(1, [ - 'namespace' => 'new_namespace', - 'path' => 'new_path', - 'name' => 'new_name' - ])); - } - - /** - * @test - */ - public function shouldCreateForkRelation() - { - $expectedArray = array('project_id' => 1, 'forked_id' => 2); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/fork/2') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->createForkRelation(1, 2)); - } - - /** - * @test - */ - public function shouldRemoveForkRelation() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/2/fork') - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->removeForkRelation(2)); - } - - /** - * @test - */ - public function shouldSetService() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/services/hipchat', array('param' => 'value')) - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->setService(1, 'hipchat', array('param' => 'value'))); - } - - /** - * @test - */ - public function shouldRemoveService() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/services/hipchat') - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->removeService(1, 'hipchat')); - } - - /** - * @test - */ - public function shouldGetVariables() - { - $expectedArray = array( - array('key' => 'ftp_username', 'value' => 'ftp'), - array('key' => 'ftp_password', 'value' => 'somepassword') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/variables') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->variables(1)); - } - - /** - * @test - */ - public function shouldGetVariable() - { - $expectedArray = array('key' => 'ftp_username', 'value' => 'ftp'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/variables/ftp_username') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->variable(1, 'ftp_username')); - } - - /** - * @test - */ - public function shouldAddVariable() - { - $expectedKey = 'ftp_port'; - $expectedValue = '21'; - - $expectedArray = array( - 'key' => $expectedKey, - 'value' => $expectedValue, - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/variables', $expectedArray) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue)); - } - - /** - * @test - */ - public function shouldAddVariableWithProtected() - { - $expectedArray = array( - 'key' => 'DEPLOY_SERVER', - 'value' => 'stage.example.com', - 'protected' => true, - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/variables', $expectedArray) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true)); - } - - /** - * @test - */ - public function shouldAddVariableWithEnvironment() - { - $expectedArray = array( - 'key' => 'DEPLOY_SERVER', - 'value' => 'stage.example.com', - 'environment_scope' => 'staging', - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/variables', $expectedArray) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, - $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging')); - } - - /** - * @test - */ - public function shouldAddVariableWithProtectionAndEnvironment() - { - $expectedArray = array( - 'key' => 'DEPLOY_SERVER', - 'value' => 'stage.example.com', - 'protected' => true, - 'environment_scope' => 'staging', - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/variables', $expectedArray) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, - $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging')); - } - - /** - * @test - */ - public function shouldUpdateVariable() - { - $expectedKey = 'ftp_port'; - $expectedValue = '22'; - - $expectedArray = array( - 'key' => 'ftp_port', - 'value' => '22', - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/variables/' . $expectedKey, array('value' => $expectedValue)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->updateVariable(1, $expectedKey, $expectedValue)); - } - - /** - * @test - */ - public function shouldUpdateVariableWithProtected() - { - $expectedArray = array( - 'key' => 'DEPLOY_SERVER', - 'value' => 'stage.example.com', - 'protected' => true, - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true)); - } - - /** - * @test - */ - public function shouldUpdateVariableWithEnvironment() - { - $expectedArray = array( - 'key' => 'DEPLOY_SERVER', - 'value' => 'stage.example.com', - 'environment_scope' => 'staging', - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/variables/DEPLOY_SERVER', - array('value' => 'stage.example.com', 'environment_scope' => 'staging')) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, - $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging')); - } - - /** - * @test - */ - public function shouldUpdateVariableWithProtectedAndEnvironment() - { - $expectedArray = array( - 'key' => 'DEPLOY_SERVER', - 'value' => 'stage.example.com', - 'protected' => true, - 'environment_scope' => 'staging', - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/variables/DEPLOY_SERVER', - array('value' => 'stage.example.com', 'protected' => true, 'environment_scope' => 'staging')) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, - $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging')); - } - - /** - * @test - */ - public function shouldRemoveVariable() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/variables/ftp_password') - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->removeVariable(1, 'ftp_password')); - } - - protected function getMultipleProjectsRequestMock($path, $expectedArray = array(), $expectedParameters = array()) - { - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with($path, $expectedParameters) - ->will($this->returnValue($expectedArray)); - - return $api; - } - - /** - * @test - */ - public function shouldGetDeployments() - { - $expectedArray = array( - array('id' => 1, 'sha' => '0000001'), - array('id' => 2, 'sha' => '0000002'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/deployments', array()) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->deployments(1)); - } - - /** - * @test - */ - public function shouldGetDeploymentsWithPagination() - { - $expectedArray = array( - array('id' => 1, 'sha' => '0000001'), - array('id' => 2, 'sha' => '0000002'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/deployments', array( - 'page' => 2, - 'per_page' => 15 - )) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->deployments(1, ['page' => 2, 'per_page' => 15])); - } - - protected function getMultipleProjectsData() - { - return array( - array('id' => 1, 'name' => 'A project'), - array('id' => 2, 'name' => 'Another project') - ); - } - - public function possibleAccessLevels() - { - return [ - [10], - [20], - [30], - [40], - [50], - ]; - } - - public function getBadgeExpectedArray() - { - return [ - [ - "id" => 1, - "link_url" => "http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}", - "image_url" => "https://shields.io/my/badge", - "rendered_link_url" => "http://example.com/ci_status.svg?project=example-org/example-project&ref=master", - "rendered_image_url" => "https://shields.io/my/badge", - "kind" => "project" - ], - [ - "id" => 2, - "link_url" => "http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}", - "image_url" => "https://shields.io/my/badge", - "rendered_link_url" => "http://example.com/ci_status.svg?project=example-org/example-project&ref=master", - "rendered_image_url" => "https://shields.io/my/badge", - "kind" => "group" - ], - ]; - } - - /** - * @test - */ - public function shouldGetBadges() - { - $expectedArray = $this->getBadgeExpectedArray(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/badges') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->badges(1)); - } - - /** - * @test - */ - public function shouldGetBadge() - { - $expectedBadgesArray = $this->getBadgeExpectedArray(); - $expectedArray = array( - $expectedBadgesArray[0] - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/badges/1') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->badge(1, 1)); - } - - /** - * @test - */ - public function shouldAddBadge() - { - $link_url = 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}'; - $image_url = 'https://shields.io/my/badge'; - $expectedArray = array( - 'id' => 3, - 'link_url' => $link_url, - 'image_url' => $image_url, - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/badges', array('link_url' => $link_url, 'image_url' => $image_url)) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, - $api->addBadge(1, array('link_url' => $link_url, 'image_url' => $image_url))); - } - - /** - * @test - */ - public function shouldUpdateBadge() - { - $image_url = 'https://shields.io/my/new/badge'; - $expectedArray = array( - 'id' => 2, - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/badges/2') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->updateBadge(1, 2, array('image_url' => $image_url))); - } - - /** - * @test - */ - public function shouldRemoveBadge() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/badges/1') - ->will($this->returnValue($expectedBool)); - - $this->assertEquals($expectedBool, $api->removeBadge(1, 1)); - } - /** - * @test - */ - public function shouldAddProtectedBranch() - { - $expectedArray = [ - 'name' => 'master', - 'push_access_level' => [ - 'access_level' => 0, - 'access_level_description' => 'No one' - ], - 'merge_access_levels' => [ - 'access_level' => 0, - 'access_level_description' => 'Developers + Maintainers' - ] - ]; - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/protected_branches', - array('name' => 'master', 'push_access_level' => 0, 'merge_access_level' => 30)) - ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->addProtectedBranch(1, array('name' => 'master', 'push_access_level' => 0, 'merge_access_level' => 30))); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Projects'; - } -} diff --git a/test/Gitlab/Tests/Api/RepositoriesTest.php b/test/Gitlab/Tests/Api/RepositoriesTest.php deleted file mode 100644 index 078ad8b1f..000000000 --- a/test/Gitlab/Tests/Api/RepositoriesTest.php +++ /dev/null @@ -1,630 +0,0 @@ - 'master'), - array('name' => 'develop') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/branches', ['search' => '^term']) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->branches(1, ['search' => '^term'])); - } - - /** - * @test - */ - public function shouldGetBranch() - { - $expectedArray = array('name' => 'master'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/branches/master') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->branch(1, 'master')); - } - - /** - * @test - */ - public function shouldCreateBranch() - { - $expectedArray = array('name' => 'feature'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/repository/branches', array('branch' => 'feature', 'ref' => 'master')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createBranch(1, 'feature', 'master')); - } - - /** - * @test - */ - public function shouldDeleteBranch() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/repository/branches/feature%2FTEST-15') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->deleteBranch(1, 'feature/TEST-15')); - } - - /** - * @test - */ - public function shouldProtectBranch() - { - $expectedArray = array('name' => 'master'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/repository/branches/master/protect', array('developers_can_push' => false, 'developers_can_merge' => false)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->protectBranch(1, 'master')); - } - - /** - * @test - */ - public function shouldProtectBranchWithPermissions() - { - $expectedArray = array('name' => 'master'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/repository/branches/master/protect', array('developers_can_push' => true, 'developers_can_merge' => true)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->protectBranch(1, 'master', true, true)); - } - - /** - * @test - */ - public function shouldUnprotectBranch() - { - $expectedArray = array('name' => 'master'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/repository/branches/master/unprotect') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->unprotectBranch(1, 'master')); - } - - /** - * @test - */ - public function shouldGetTags() - { - $expectedArray = array( - array('name' => '1.0'), - array('name' => '1.1') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/tags') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->tags(1)); - } - - /** - * @test - */ - public function shouldCreateTag() - { - $expectedArray = array('name' => '1.0'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/repository/tags', array( - 'tag_name' => '1.0', - 'ref' => 'abcd1234', - 'message' => '1.0 release' - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createTag(1, '1.0', 'abcd1234', '1.0 release')); - } - - /** - * @test - */ - public function shouldCreateRelease() - { - $project_id = 1; - $tagName = 'sometag'; - $description = '1.0 release'; - - $expectedArray = array( 'name' => $tagName ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/' . $project_id . '/repository/tags/' . $tagName . '/release', array( - 'id' => $project_id, - 'tag_name' => $tagName, - 'description' => $description - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createRelease($project_id, $tagName, $description)); - } - - /** - * @test - */ - public function shouldUpdateRelease() - { - $project_id = 1; - $tagName = 'sometag'; - $description = '1.0 release'; - - $expectedArray = array( 'description' => $tagName ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/' . $project_id . '/repository/tags/' . $tagName . '/release', array( - 'id' => $project_id, - 'tag_name' => $tagName, - 'description' => $description - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateRelease($project_id, $tagName, $description)); - } - - /** - * @test - */ - public function shouldGetReleases() - { - $project_id = 1; - - $expectedArray = array( - array( - 'tag_name' => 'v0.2', - 'description' => '## CHANGELOG\r\n\r\n- Escape label and milestone titles to prevent XSS in GFM autocomplete. !2740\r\n- Prevent private snippets from being embeddable.\r\n- Add subresources removal to member destroy service.', - 'name' => 'Awesome app v0.2 beta' - ) - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/releases') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->releases($project_id)); - } - - /** - * @test - */ - public function shouldGetCommits() - { - $expectedArray = array( - array('id' => 'abcd1234', 'title' => 'A commit'), - array('id' => 'efgh5678', 'title' => 'Another commit') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/commits', array()) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->commits(1)); - } - - /** - * @test - */ - public function shouldGetCommitsWithParams() - { - $expectedArray = array( - array('id' => 'abcd1234', 'title' => 'A commit'), - array('id' => 'efgh5678', 'title' => 'Another commit') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/commits', array('page' => 2, 'per_page' => 25, 'ref_name' => 'master', 'all' => true, 'with_stats' => true, 'path' => 'file_path/file_name')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->commits(1, ['page' => 2, 'per_page' => 25, 'ref_name' => 'master', 'all' => true, 'with_stats' => true, 'path' => 'file_path/file_name'])); - } - - /** - * @test - */ - public function shouldGetCommitsWithTimeParams() - { - $expectedArray = [ - ['id' => 'abcd1234', 'title' => 'A commit'], - ['id' => 'efgh5678', 'title' => 'Another commit'] - ]; - - $since = new \DateTime('2018-01-01 00:00:00'); - $until = new \DateTime('2018-01-31 00:00:00'); - - $expectedWithArray = [ - 'since' => $since->format(DATE_ATOM), - 'until' => $until->format(DATE_ATOM), - ]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/commits', $expectedWithArray) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->commits(1, ['since' => $since, 'until' => $until])); - } - - /** - * @test - */ - public function shouldGetCommit() - { - $expectedArray = array('id' => 'abcd1234', 'title' => 'A commit'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/commits/abcd1234') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->commit(1, 'abcd1234')); - } - - /** - * @test - */ - public function shouldGetCommitRefs() - { - $expectedArray = [ - ['type' => 'branch', 'name' => 'master'], - ['type' => 'tag', 'name' => 'v1.1.0'], - ]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/commits/abcd1234/refs') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->commitRefs(1, 'abcd1234')); - } - - /** - * @dataProvider dataGetCommitRefsWithParams - * @test - * - * @param string $type - * @param array $expectedArray - */ - public function shouldGetCommitRefsWithParams($type, array $expectedArray) - { - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/commits/abcd1234/refs', ['type' => $type]) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->commitRefs(1, 'abcd1234', ['type' => $type])); - } - - public function dataGetCommitRefsWithParams() - { - return [ - 'type_tag' => [ - 'type' => Repositories::TYPE_TAG, - 'expectedArray' => [['type' => 'tag', 'name' => 'v1.1.0']] - ], - 'type_branch' => [ - 'type' => Repositories::TYPE_BRANCH, - 'expectedArray' => [['type' => 'branch', 'name' => 'master']] - ], - ]; - } - - /** - * @test - */ - public function shouldCreateCommit() - { - $expectedArray = array('title' => 'Initial commit.', 'author_name' => 'John Doe', 'author_email' => 'john@example.com'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/repository/commits') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createCommit(1, [ - 'branch' => 'master', - 'commit_message' => 'Initial commit.', - 'actions' => [ - [ - 'action' => 'create', - 'file_path' => 'README.md', - 'content' => '# My new project', - ], - [ - 'action' => 'create', - 'file_path' => 'LICENSE', - 'content' => 'MIT License...', - ], - ], - 'author_name' => 'John Doe', - 'author_email' => 'john@example.com', - ])); - } - - /** - * @test - */ - public function shouldGetCommitComments() - { - $expectedArray = array( - array('note' => 'A commit message'), - array('note' => 'Another commit message') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/commits/abcd1234/comments') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->commitComments(1, 'abcd1234')); - } - - /** - * @test - */ - public function shouldCreateCommitComment() - { - $expectedArray = array('id' => 2, 'title' => 'A new comment'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/repository/commits/abcd1234/comments', array('note' => 'A new comment')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createCommitComment(1, 'abcd1234', 'A new comment')); - } - - /** - * @test - */ - public function shouldCreateCommitCommentWithParams() - { - $expectedArray = array('id' => 2, 'title' => 'A new comment'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/repository/commits/abcd1234/comments', array( - 'note' => 'A new comment', - 'path' => '/some/file.txt', - 'line' => 123, 'line_type' => 'old' - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createCommitComment(1, 'abcd1234', 'A new comment', array( - 'path' => '/some/file.txt', - 'line' => 123, - 'line_type' => 'old' - ))); - } - - /** - * @test - */ - public function shouldCompareStraight() - { - $expectedArray = array('commit' => 'object'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/compare?from=master&to=feature&straight=true') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->compare(1, 'master', 'feature', true)); - } - - /** - * @test - */ - public function shouldNotCompareStraight() - { - $expectedArray = array('commit' => 'object'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/compare?from=master&to=feature&straight=false') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->compare(1, 'master', 'feature')); - } - - /** - * @test - */ - public function shouldGetDiff() - { - $expectedArray = array( - array('diff' => '--- ...'), - array('diff' => '+++ ...') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/commits/abcd1234/diff') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->diff(1, 'abcd1234')); - } - - /** - * @test - */ - public function shouldGetTree() - { - $expectedArray = array( - array('name' => 'file1.txt'), - array('name' => 'file2.csv') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/tree') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->tree(1)); - } - - /** - * @test - */ - public function shouldGetTreeWithParams() - { - $expectedArray = array( - array('name' => 'dir/file1.txt'), - array('name' => 'dir/file2.csv') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/tree', array('path' => 'dir/', 'ref_name' => 'master')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->tree(1, array('path' => 'dir/', 'ref_name' => 'master'))); - } - - /** - * @test - */ - public function shouldGetContributors() - { - $expectedArray = array( - array('name' => 'Matt'), - array('name' => 'Bob') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/contributors') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->contributors(1)); - } - - /** - * @test - */ - public function shouldGetMergeBase() - { - $expectedArray = array( - 'id' => 'abcd1234abcd1234abcd1234abcd1234abcd1234', - 'short_id' => 'abcd1234', - 'title' => 'A commit', - 'created_at' => '2018-01-01T00:00:00.000Z', - 'parent_ids' => array( - 'efgh5678efgh5678efgh5678efgh5678efgh5678', - ), - 'message' => 'A commit', - 'author_name' => 'Jane Doe', - 'author_email' => 'jane@example.org', - 'authored_date' => '2018-01-01T00:00:00.000Z', - 'committer_name' => 'Jane Doe', - 'committer_email' => 'jane@example.org', - 'committed_date' => '2018-01-01T00:00:00.000Z', - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/merge_base', array('refs' => array('efgh5678efgh5678efgh5678efgh5678efgh5678', '1234567812345678123456781234567812345678'))) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->mergeBase(1, array('efgh5678efgh5678efgh5678efgh5678efgh5678', '1234567812345678123456781234567812345678'))); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Repositories'; - } -} diff --git a/test/Gitlab/Tests/Api/RepositoryFilesTest.php b/test/Gitlab/Tests/Api/RepositoryFilesTest.php deleted file mode 100644 index d37b60f47..000000000 --- a/test/Gitlab/Tests/Api/RepositoryFilesTest.php +++ /dev/null @@ -1,279 +0,0 @@ -getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/files/dir%2Ffile1%2Etxt/raw', array('ref' => 'abcd1234')) - ->will($this->returnValue($expectedString)) - ; - - $this->assertEquals($expectedString, $api->getRawFile(1, 'dir/file1.txt', 'abcd1234')); - } - - /** - * @test - */ - public function shouldGetFile() - { - $expectedArray = array('file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/files/dir%2Ffile1%2Etxt', array('ref' => 'abcd1234')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->getFile(1, 'dir/file1.txt', 'abcd1234')); - } - - /** - * @test - */ - public function shouldCreateFile() - { - $expectedArray = array('file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/repository/files/dir%2Ffile1%2Etxt', array( - 'file_path' => 'dir/file1.txt', - 'branch' => 'master', - 'content' => 'some contents', - 'commit_message' => 'Added new file', - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createFile(1, [ - 'file_path' => 'dir/file1.txt', - 'content' => 'some contents', - 'branch' => 'master', - 'commit_message' => 'Added new file', - ])); - } - - /** - * @test - */ - public function shouldCreateFileWithEncoding() - { - $expectedArray = array('file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/repository/files/dir%2Ffile1%2Etxt', array( - 'file_path' => 'dir/file1.txt', - 'branch' => 'master', - 'encoding' => 'text', - 'content' => 'some contents', - 'commit_message' => 'Added new file', - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createFile(1, [ - 'file_path' => 'dir/file1.txt', - 'content' => 'some contents', - 'branch' => 'master', - 'commit_message' => 'Added new file', - 'encoding' => 'text', - ])); - } - - /** - * @test - */ - public function shouldCreateFileWithAuthor() - { - $expectedArray = array('file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/repository/files/dir%2Ffile1%2Etxt', array( - 'file_path' => 'dir/file1.txt', - 'branch' => 'master', - 'content' => 'some contents', - 'commit_message' => 'Added new file', - 'author_email' => 'gitlab@example.com', - 'author_name' => 'GitLab User', - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createFile(1, [ - 'file_path' => 'dir/file1.txt', - 'content' => 'some contents', - 'branch' => 'master', - 'commit_message' => 'Added new file', - 'author_email' => 'gitlab@example.com', - 'author_name' => 'GitLab User', - ])); - } - - /** - * @test - */ - public function shouldUpdateFile() - { - $expectedArray = array('file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/repository/files/dir%2Ffile1%2Etxt', array( - 'file_path' => 'dir/file1.txt', - 'branch' => 'master', - 'content' => 'some new contents', - 'commit_message' => 'Updated new file', - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateFile(1, [ - 'file_path' => 'dir/file1.txt', - 'content' => 'some new contents', - 'branch' => 'master', - 'commit_message' => 'Updated new file', - ])); - } - - /** - * @test - */ - public function shouldUpdateFileWithEncoding() - { - $expectedArray = array('file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/repository/files/dir%2Ffile1%2Etxt', array( - 'file_path' => 'dir/file1.txt', - 'branch' => 'master', - 'encoding' => 'base64', - 'content' => 'c29tZSBuZXcgY29udGVudHM=', - 'commit_message' => 'Updated file', - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateFile(1, [ - 'file_path' => 'dir/file1.txt', - 'content' => 'c29tZSBuZXcgY29udGVudHM=', - 'branch' => 'master', - 'commit_message' => 'Updated file', - 'encoding' => 'base64', - ])); - } - - /** - * @test - */ - public function shouldUpdateFileWithAuthor() - { - $expectedArray = array('file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/repository/files/dir%2Ffile1%2Etxt', array( - 'file_path' => 'dir/file1.txt', - 'branch' => 'master', - 'content' => 'some new contents', - 'commit_message' => 'Updated file', - 'author_email' => 'gitlab@example.com', - 'author_name' => 'GitLab User', - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->updateFile(1, [ - 'file_path' => 'dir/file1.txt', - 'content' => 'some new contents', - 'branch' => 'master', - 'commit_message' => 'Updated file', - 'author_email' => 'gitlab@example.com', - 'author_name' => 'GitLab User', - ])); - } - - /** - * @test - */ - public function shouldDeleteFile() - { - $expectedArray = ["file_name" => "app/project.rb", "branch" => "master"]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/repository/files/dir%2Ffile1%2Etxt', array( - 'file_path' => 'dir/file1.txt', - 'branch' => 'master', - 'commit_message' => 'Deleted file', - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->deleteFile(1, [ - 'file_path' => 'dir/file1.txt', - 'branch' => 'master', - 'commit_message' => 'Deleted file' - ])); - } - - /** - * @test - */ - public function shouldDeleteFileWithAuthor() - { - $expectedArray = ["file_name" => "app/project.rb", "branch" => "master"]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/repository/files/dir%2Ffile1%2Etxt', array( - 'file_path' => 'dir/file1.txt', - 'branch' => 'master', - 'commit_message' => 'Deleted file', - 'author_email' => 'gitlab@example.com', - 'author_name' => 'GitLab User', - )) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->deleteFile(1, [ - 'file_path' => 'dir/file1.txt', - 'branch' => 'master', - 'commit_message' => 'Deleted file', - 'author_email' => 'gitlab@example.com', - 'author_name' => 'GitLab User', - ])); - } - - /** - * @return string - */ - protected function getApiClass() - { - return RepositoryFiles::class; - } -} diff --git a/test/Gitlab/Tests/Api/ScheduleTest.php b/test/Gitlab/Tests/Api/ScheduleTest.php deleted file mode 100644 index 9472329df..000000000 --- a/test/Gitlab/Tests/Api/ScheduleTest.php +++ /dev/null @@ -1,129 +0,0 @@ - 13, - "description" => "Test schedule pipeline", - "ref" => "master", - "cron" => "* * * * *", - "cron_timezone" => "Asia/Tokyo", - "next_run_at" => "2017-05-19T13:41:00.000Z", - "active" => true, - "created_at" => "2017-05-19T13:31:08.849Z", - "updated_at" => "2017-05-19T13:40:17.727Z" - ]; - - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/pipeline_schedules', [ - "id" => 13, - "description" => "Test schedule pipeline", - "ref" => "master", - "cron" => "* * * * *", - "cron_timezone" => "Asia/Tokyo", - "next_run_at" => "2017-05-19T13:41:00.000Z", - "active" => true, - "created_at" => "2017-05-19T13:31:08.849Z", - "updated_at" => "2017-05-19T13:40:17.727Z" - ]) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->create(1, - [ - "id" => 13, - "description" => "Test schedule pipeline", - "ref" => "master", - "cron" => "* * * * *", - "cron_timezone" => "Asia/Tokyo", - "next_run_at" => "2017-05-19T13:41:00.000Z", - "active" => true, - "created_at" => "2017-05-19T13:31:08.849Z", - "updated_at" => "2017-05-19T13:40:17.727Z" - ] - )); - } - - /** - * @test - */ - public function shouldShowSchedule() - { - $expectedArray = array('id' => 1, 'name' => 'A schedule'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/pipeline_schedules/2') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1, 2)); - } - - /** - * @test - */ - public function shouldShowAllSchedule() - { - $expectedArray = array('id' => 1, 'name' => 'A schedule'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/pipeline_schedules') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->showAll(1)); - } - - /** - * @test - */ - public function shouldUpdateSchedule() - { - $expectedArray = array('id' => 3, 'title' => 'Updated schedule'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/pipeline_schedules/3', array('title' => 'Updated schedule', 'due_date' => '2015-04-01', 'state_event' => 'close')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(1, 3, array('title' => 'Updated schedule', 'due_date' => '2015-04-01', 'state_event' => 'close'))); - } - - /** - * @test - */ - public function shouldRemoveSchedule() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/pipeline_schedules/2') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->remove(1, 2)); - } - - - protected function getApiClass() - { - return 'Gitlab\Api\Schedules'; - } -} diff --git a/test/Gitlab/Tests/Api/SnippetsTest.php b/test/Gitlab/Tests/Api/SnippetsTest.php deleted file mode 100644 index af9dd48cf..000000000 --- a/test/Gitlab/Tests/Api/SnippetsTest.php +++ /dev/null @@ -1,134 +0,0 @@ - 1, 'title' => 'A snippet'), - array('id' => 2, 'title' => 'Another snippet'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/snippets') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(1)); - } - - /** - * @test - */ - public function shouldShowSnippet() - { - $expectedArray = array('id' => 2, 'title' => 'Another snippet'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/snippets/2') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1, 2)); - } - - /** - * @test - */ - public function shouldCreateSnippet() - { - $expectedArray = array('id' => 3, 'title' => 'A new snippet'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/snippets', array('title' => 'A new snippet', 'code' => 'A file', 'file_name' => 'file.txt', 'visibility' => 'public')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create(1, 'A new snippet', 'file.txt', 'A file', 'public')); - } - - /** - * @test - */ - public function shouldUpdateSnippet() - { - $expectedArray = array('id' => 3, 'title' => 'Updated snippet'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/snippets/3', array('title' => 'Updated snippet', 'code' => 'New content', 'file_name' => 'new_file.txt')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(1, 3, array('file_name' => 'new_file.txt', 'code' => 'New content', 'title' => 'Updated snippet'))); - } - - /** - * @test - */ - public function shouldShowContent() - { - $expectedString = 'New content'; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/snippets/3/raw') - ->will($this->returnValue($expectedString)) - ; - - $this->assertEquals($expectedString, $api->content(1, 3)); - } - - /** - * @test - */ - public function shouldRemoveSnippet() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/snippets/3') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->remove(1, 3)); - } - - /** - * @test - */ - public function shouldGetSnippetAwardEmoji() - { - $expectedArray = array( - array('id' => 1, 'name' => 'sparkles'), - array('id' => 2, 'name' => 'heart_eyes'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/snippets/2/award_emoji') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->awardEmoji(1, 2)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Snippets'; - } -} diff --git a/test/Gitlab/Tests/Api/SystemHooksTest.php b/test/Gitlab/Tests/Api/SystemHooksTest.php deleted file mode 100644 index b65eb442d..000000000 --- a/test/Gitlab/Tests/Api/SystemHooksTest.php +++ /dev/null @@ -1,80 +0,0 @@ - 1, 'url' => 'http://www.example.com'), - array('id' => 2, 'url' => 'http://www.example.org'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('hooks') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all()); - } - - /** - * @test - */ - public function shouldCreateHook() - { - $expectedArray = array('id' => 3, 'url' => 'http://www.example.net'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('hooks', array('url' => 'http://www.example.net')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create('http://www.example.net')); - } - - /** - * @test - */ - public function shouldTestHook() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('hooks/3') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->test(3)); - } - - /** - * @test - */ - public function shouldRemoveHook() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('hooks/3') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->remove(3)); - } - - protected function getApiClass() - { - return 'Gitlab\Api\SystemHooks'; - } -} diff --git a/test/Gitlab/Tests/Api/TagsTest.php b/test/Gitlab/Tests/Api/TagsTest.php deleted file mode 100644 index f1ed9a616..000000000 --- a/test/Gitlab/Tests/Api/TagsTest.php +++ /dev/null @@ -1,151 +0,0 @@ - 'v1.0.0'), - array('name' => 'v1.1.0'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/tags') - ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->all(1)); - } - - /** - * @test - */ - public function shouldShowTag() - { - $expectedArray = array( - array('name' => 'v1.0.0'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('projects/1/repository/tags/v1%2E0%2E0') - ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->show(1, 'v1.0.0')); - } - - /** - * @test - */ - public function shouldCreateTag() - { - $expectedArray = array( - array('name' => 'v1.1.0'), - ); - - $params = array( - 'id' => 1, - 'tag_name' => 'v1.1.0', - 'ref' => 'ref/heads/master' - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/repository/tags', $params) - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->create(1, $params)); - } - - /** - * @test - */ - public function shouldRemoveTag() - { - $expectedArray = array( - array('name' => 'v1.1.0'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('projects/1/repository/tags/v1%2E1%2E0') - ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->remove(1, 'v1.1.0')); - } - - /** - * @test - * @dataProvider releaseDataProvider - * @param string $releaseName - * @param string $description - * @param array $expectedResult - */ - public function shouldCreateRelease($releaseName, $description, $expectedResult) - { - $params = array( - 'description' => $description, - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('projects/1/repository/tags/' . str_replace(['/', '.'], ['%2F', '%2E'], $releaseName) . '/release', $params) - ->will($this->returnValue($expectedResult)); - - $this->assertEquals($expectedResult, $api->createRelease(1, $releaseName, $params)); - } - - /** - * @test - * @dataProvider releaseDataProvider - * @param string $releaseName - * @param string $description - * @param array $expectedResult - */ - public function shouldUpdateRelease($releaseName, $description, $expectedResult) - { - $params = array( - 'description' => $description, - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('projects/1/repository/tags/' . str_replace(['/', '.'], ['%2F', '%2E'], $releaseName) . '/release', $params) - ->will($this->returnValue($expectedResult)); - - $this->assertEquals($expectedResult, $api->updateRelease(1, $releaseName, $params)); - } - - public function releaseDataProvider() - { - return array( - array( - 'tagName' => 'v1.1.0', - 'description' => 'Amazing release. Wow', - 'expectedResult' => array( - 'tag_name' => '1.0.0', - 'description' => 'Amazing release. Wow', - ), - ), - array( - 'tagName' => 'version/1.1.0', - 'description' => 'Amazing release. Wow', - 'expectedResult' => array( - 'tag_name' => 'version/1.1.0', - 'description' => 'Amazing release. Wow', - ), - ), - ); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Tags'; - } -} diff --git a/test/Gitlab/Tests/Api/TestCase.php b/test/Gitlab/Tests/Api/TestCase.php deleted file mode 100644 index 32dc404e3..000000000 --- a/test/Gitlab/Tests/Api/TestCase.php +++ /dev/null @@ -1,36 +0,0 @@ -getMockBuilder(HttpClient::class) - ->setMethods(array('sendRequest')) - ->getMock(); - $httpClient - ->expects($this->any()) - ->method('sendRequest'); - - $client = Client::createWithHttpClient($httpClient); - - return $this->getMockBuilder($this->getApiClass()) - ->setMethods(array_merge(array('getAsResponse', 'get', 'post', 'postRaw', 'patch', 'delete', 'put', 'head'), $methods)) - ->setConstructorArgs(array($client)) - ->getMock(); - } -} diff --git a/test/Gitlab/Tests/Api/UsersTest.php b/test/Gitlab/Tests/Api/UsersTest.php deleted file mode 100644 index 0724e4c3e..000000000 --- a/test/Gitlab/Tests/Api/UsersTest.php +++ /dev/null @@ -1,624 +0,0 @@ - 1, 'name' => 'Matt'), - array('id' => 2, 'name' => 'John'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users', array()) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all()); - } - - /** - * @test - */ - public function shouldGetActiveUsers() - { - $expectedArray = array( - array('id' => 1, 'name' => 'Matt'), - array('id' => 2, 'name' => 'John'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users', array('active' => true)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->all(['active' => true])); - } - - /** - * @test - */ - public function shouldGetUsersWithDateTimeParams() - { - $expectedArray = [ - ['id' => 1, 'name' => 'Matt'], - ['id' => 2, 'name' => 'John'], - ]; - - $createdAfter = new \DateTime('2018-01-01 00:00:00'); - $createdBefore = new \DateTime('2018-01-31 00:00:00'); - - $expectedWithArray = [ - 'created_after' => $createdAfter->format(DATE_ATOM), - 'created_before' => $createdBefore->format(DATE_ATOM), - ]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users', $expectedWithArray) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals( - $expectedArray, - $api->all(['created_after' => $createdAfter, 'created_before' => $createdBefore]) - ); - } - - /** - * @test - */ - public function shouldShowUser() - { - $expectedArray = array('id' => 1, 'name' => 'Matt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users/1') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->show(1)); - } - - /** - * @test - */ - public function shouldShowUsersProjects() - { - $expectedArray = array( - array('id' => 1, 'name' => 'matt-project-1'), - array('id' => 2, 'name' => 'matt-project-2') - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users/1/projects') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->usersProjects(1)); - } - - /** - * @test - */ - public function shouldCreateUser() - { - $expectedArray = array('id' => 3, 'name' => 'Billy'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('users', array('email' => 'billy@example.com', 'password' => 'password')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create('billy@example.com', 'password')); - } - - /** - * @test - */ - public function shouldCreateUserWithAdditionalInfo() - { - $expectedArray = array('id' => 3, 'name' => 'Billy'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('users', array('email' => 'billy@example.com', 'password' => 'password', 'name' => 'Billy', 'bio' => 'A person')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->create('billy@example.com', 'password', array('name' => 'Billy', 'bio' => 'A person'))); - } - - /** - * @test - */ - public function shouldUpdateUser() - { - $expectedArray = array('id' => 3, 'name' => 'Billy Bob'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('users/3', array('name' => 'Billy Bob')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(3, array('name' => 'Billy Bob'))); - - $expectedArray = array('id' => 4, 'avatar_url' => 'http://localhost:3000/uploads/user/avatar/4/image.jpg'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('users/4', array(), array(), array('avatar' => '/some/image.jpg')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->update(4, array(), array('avatar' => '/some/image.jpg'))); - } - - /** - * @test - */ - public function shouldRemoveUser() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('users/1') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->remove(1)); - } - - /** - * @test - */ - public function shouldBlockUser() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('users/1/block') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->block(1)); - } - - /** - * @test - */ - public function shouldUnblockUser() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('users/1/unblock') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->unblock(1)); - } - - /** - * @test - */ - public function shouldShowCurrentUser() - { - $expectedArray = array('id' => 1, 'name' => 'Matt'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('user') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->me()); - } - - /** - * @test - */ - public function shouldGetCurrentUserKeys() - { - $expectedArray = array( - array('id' => 1, 'title' => 'A key'), - array('id' => 2, 'name' => 'Another key'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('user/keys') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->keys(1)); - } - - /** - * @test - */ - public function shouldGetCurrentUserKey() - { - $expectedArray = array('id' => 1, 'title' => 'A key'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('user/keys/1') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->key(1)); - } - - /** - * @test - */ - public function shouldCreateKeyForCurrentUser() - { - $expectedArray = array('id' => 3, 'title' => 'A new key'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('user/keys', array('title' => 'A new key', 'key' => '...')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createKey('A new key', '...')); - } - - /** - * @test - */ - public function shouldDeleteKeyForCurrentUser() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('user/keys/3') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->removeKey(3)); - } - - /** - * @test - */ - public function shouldGetUserKeys() - { - $expectedArray = array( - array('id' => 1, 'title' => 'A key'), - array('id' => 2, 'name' => 'Another key'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users/1/keys') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->userKeys(1)); - } - - /** - * @test - */ - public function shouldGetUserKey() - { - $expectedArray = array('id' => 2, 'title' => 'Another key'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users/1/keys/2') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->userKey(1, 2)); - } - - /** - * @test - */ - public function shouldCreateKeyForUser() - { - $expectedArray = array('id' => 3, 'title' => 'A new key'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('users/1/keys', array('title' => 'A new key', 'key' => '...')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createKeyForUser(1, 'A new key', '...')); - } - - /** - * @test - */ - public function shouldDeleteKeyForUser() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('users/1/keys/3') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->removeUserKey(1, 3)); - } - - /** - * @test - */ - public function shouldAttemptLogin() - { - $expectedArray = array('id' => 1, 'name' => 'Matt'); - - $api = $this->getApiMock(); - $api->expects($this->exactly(2)) - ->method('post') - ->with('session', array('login' => 'matt', 'password' => 'password', 'email' => 'matt')) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->session('matt', 'password')); - $this->assertEquals($expectedArray, $api->login('matt', 'password')); - } - - /** - * @test - */ - public function shouldGetUserEmails() - { - $expectedArray = array( - array('id' => 1, 'email' => 'foo@bar.baz'), - array('id' => 2, 'email' => 'foo@bar.qux'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('user/emails') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->emails()); - } - - /** - * @test - */ - public function shouldGetSpecificUserEmail() - { - $expectedArray = array('id' => 1, 'email' => 'foo@bar.baz'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('user/emails/1') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->email(1)); - } - - /** - * @test - */ - public function shouldGetEmailsForUser() - { - $expectedArray = array( - array('id' => 1, 'email' => 'foo@bar.baz'), - array('id' => 2, 'email' => 'foo@bar.qux'), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users/1/emails') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->userEmails(1)); - } - - /** - * @test - */ - public function shouldCreateEmailForUser() - { - $expectedArray = array('id' => 3, 'email' => 'foo@bar.example'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('users/1/emails', array('email' => 'foo@bar.example', 'skip_confirmation' => false)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createEmailForUser(1, 'foo@bar.example')); - } - - /** - * @test - */ - public function shouldCreateConfirmedEmailForUser() - { - $expectedArray = array('id' => 4, 'email' => 'foo@baz.example'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('users/1/emails', array('email' => 'foo@baz.example', 'skip_confirmation' => true)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createEmailForUser(1, 'foo@baz.example', true)); - } - - /** - * @test - */ - public function shouldDeleteEmailForUser() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('users/1/emails/3') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->removeUserEmail(1, 3)); - } - - /** - * @test - */ - public function shouldGetCurrentUserImpersonationTokens() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A Name', 'revoked' => false), - array('id' => 2, 'name' => 'A Name', 'revoked' => false), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users/1/impersonation_tokens') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->userImpersonationTokens(1)); - } - - /** - * @test - */ - public function shouldGetUserImpersonationToken() - { - $expectedArray = array('id' => 2, 'name' => 'name'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users/1/impersonation_tokens/1') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->userImpersonationToken(1, 1)); - } - - /** - * @test - */ - public function shouldCreateImpersonationTokenForUser() - { - $expectedArray = array('id' => 1, 'name' => 'name'); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('users/1/impersonation_tokens', array('name' => 'name', 'scopes' => ['api'] ,'expires_at' => null)) - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->createImpersonationToken(1, 'name', ['api'])); - } - - /** - * @test - */ - public function shouldDeleteImpersonationTokenForUser() - { - $expectedBool = true; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('users/1/impersonation_tokens/1') - ->will($this->returnValue($expectedBool)) - ; - - $this->assertEquals($expectedBool, $api->removeImpersonationToken(1, 1)); - } - - /** - * @test - */ - public function shouldGetCurrentUserActiveImpersonationTokens() - { - $expectedArray = array( - array('id' => 1, 'name' => 'A Name', 'revoked' => true), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users/1/impersonation_tokens') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->userImpersonationTokens(1, ['state' => 'active'])); - } - - /** - * @test - */ - public function shouldGetCurrentUserInactiveImpersonationTokens() - { - $expectedArray = array( - array('id' => 2, 'name' => 'A Name', 'revoked' => false), - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('users/1/impersonation_tokens') - ->will($this->returnValue($expectedArray)) - ; - - $this->assertEquals($expectedArray, $api->userImpersonationTokens(1, ['state' => 'inactive'])); - } - - protected function getApiClass() - { - return 'Gitlab\Api\Users'; - } -} diff --git a/test/Gitlab/Tests/Api/VersionTest.php b/test/Gitlab/Tests/Api/VersionTest.php deleted file mode 100644 index c85a2414e..000000000 --- a/test/Gitlab/Tests/Api/VersionTest.php +++ /dev/null @@ -1,26 +0,0 @@ - "8.13.0-pre", - "revision" => "4e963fe", - ); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('version') - ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->show()); - } - protected function getApiClass() - { - return 'Gitlab\Api\Version'; - } -} diff --git a/test/Gitlab/Tests/Model/IssueLinkTest.php b/test/Gitlab/Tests/Model/IssueLinkTest.php deleted file mode 100644 index d029e477a..000000000 --- a/test/Gitlab/Tests/Model/IssueLinkTest.php +++ /dev/null @@ -1,53 +0,0 @@ -getMockBuilder(Issue::class) - ->disableOriginalConstructor() - ->getMock() - ; - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $issueLink = new IssueLink($issue, 1, $client); - - $this->assertSame(1, $issueLink->issue_link_id); - $this->assertSame($issue, $issueLink->issue); - $this->assertSame($client, $issueLink->getClient()); - } - - /** - * @test - */ - public function testFromArray() - { - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - $project = $this->getMockBuilder(Project::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $issueLink = IssueLink::fromArray($client, $project, ['issue_link_id' => 1, 'iid' => 10]); - - $this->assertSame(1, $issueLink->issue_link_id); - $this->assertInstanceOf(Issue::class, $issueLink->issue); - $this->assertSame(10, $issueLink->issue->iid); - $this->assertSame($client, $issueLink->getClient()); - } -} diff --git a/test/Gitlab/Tests/Model/IssueTest.php b/test/Gitlab/Tests/Model/IssueTest.php deleted file mode 100644 index b7d86b371..000000000 --- a/test/Gitlab/Tests/Model/IssueTest.php +++ /dev/null @@ -1,290 +0,0 @@ -assertSame($project, $sUT->project); - $this->assertSame(null, $sUT->iid); - $this->assertSame(null, $sUT->getClient()); - } - - public function testCorrectConstructWithoutClient() - { - $project = new Project(); - - $sUT = new Issue($project, 10); - - $this->assertSame($project, $sUT->project); - $this->assertSame(10, $sUT->iid); - $this->assertSame(null, $sUT->getClient()); - } - - public function testCorrectConstruct() - { - $project = new Project(); - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $sUT = new Issue($project, 10, $client); - - $this->assertSame($project, $sUT->project); - $this->assertSame(10, $sUT->iid); - $this->assertSame($client, $sUT->getClient()); - } - - public function testFromArray() - { - $project = new Project(); - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $sUT = Issue::fromArray($client, $project, ['iid' => 10]); - - $this->assertSame($project, $sUT->project); - $this->assertSame(10, $sUT->iid); - $this->assertSame($client, $sUT->getClient()); - } - - private function getIssueMock(array $data = []) - { - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock(); - - $project = new Project(1, $client); - - return Issue::fromArray($client, $project, $data); - } - - public function testIsClosed() - { - $opened_data = [ - 'iid' => 1, - 'state' => 'opened', - ]; - $opened_issue = $this->getIssueMock($opened_data); - - $this->assertFalse($opened_issue->isClosed()); - - $closed_data = [ - 'iid' => 1, - 'state' => 'closed', - ]; - $closed_issue = $this->getIssueMock($closed_data); - - $this->assertTrue($closed_issue->isClosed()); - } - - public function testHasLabel() - { - $data = [ - 'iid' => 1, - 'labels' => ['foo', 'bar'], - ]; - $issue = $this->getIssueMock($data); - - $this->assertTrue($issue->hasLabel('foo')); - $this->assertTrue($issue->hasLabel('bar')); - $this->assertFalse($issue->hasLabel('')); - } - - public function testMove() - { - $project = new Project(1); - $toProject = new Project(2); - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock(); - $issues = $this->getMockBuilder(Issues::class) - ->disableOriginalConstructor() - ->getMock(); - $client->expects($this->once()) - ->method('issues') - ->willReturn($issues); - $issues->expects($this->once()) - ->method('move') - ->willReturn(['iid' => 11]); - - $issue = Issue::fromArray($client, $project, ['iid' => 10])->move($toProject); - - $this->assertInstanceOf(Issue::class, $issue); - $this->assertSame($client, $issue->getClient()); - $this->assertSame($toProject, $issue->project); - $this->assertSame(11, $issue->iid); - } - - /** - * @test - */ - public function testLinks() - { - $issueLinks = $this->getMockBuilder(IssueLinks::class) - ->disableOriginalConstructor() - ->getMock() - ; - $projects = $this->getMockBuilder(Projects::class) - ->disableOriginalConstructor() - ->getMock() - ; - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $client->method('issueLinks')->willReturn($issueLinks); - $client->method('projects')->willReturn($projects); - - $issueLinks->expects($this->once()) - ->method('all') - ->with(1, 10) - ->willReturn([ - ['issue_link_id' => 100, 'iid' => 10, 'project_id' => 1], - ['issue_link_id' => 200, 'iid' => 20, 'project_id' => 2] - ]) - ; - $projects->expects($this->exactly(2)) - ->method('show') - ->withConsecutive([1], [2]) - ->will($this->onConsecutiveCalls(['id' => 1], ['id' => 2])) - ; - - - $issue = new Issue(new Project(1, $client), 10, $client); - $issueLinks = $issue->links(); - - - $this->assertInternalType('array', $issueLinks); - $this->assertCount(2, $issueLinks); - - $this->assertInstanceOf(IssueLink::class, $issueLinks[0]); - $this->assertSame(100, $issueLinks[0]->issue_link_id); - $this->assertInstanceOf(Issue::class, $issueLinks[0]->issue); - $this->assertSame(10, $issueLinks[0]->issue->iid); - $this->assertInstanceOf(Project::class, $issueLinks[0]->issue->project); - $this->assertSame(1, $issueLinks[0]->issue->project->id); - - $this->assertInstanceOf(IssueLink::class, $issueLinks[1]); - $this->assertSame(200, $issueLinks[1]->issue_link_id); - $this->assertInstanceOf(Issue::class, $issueLinks[1]->issue); - $this->assertSame(20, $issueLinks[1]->issue->iid); - $this->assertInstanceOf(Project::class, $issueLinks[1]->issue->project); - $this->assertSame(2, $issueLinks[1]->issue->project->id); - } - - /** - * @test - */ - public function testAddLink() - { - $issueLinks = $this->getMockBuilder(IssueLinks::class) - ->disableOriginalConstructor() - ->getMock() - ; - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $client->method('issueLinks')->willReturn($issueLinks); - - $issueLinks->expects($this->once()) - ->method('create') - ->with(1, 10, 2, 20) - ->willReturn([ - 'source_issue' => ['iid' => 10, 'project_id' => 1], - 'target_issue' => ['iid' => 20, 'project_id' => 2] - ]) - ; - - $issue = new Issue(new Project(1, $client), 10, $client); - $issueLinks = $issue->addLink(new Issue(new Project(2, $client), 20, $client)); - - - $this->assertInternalType('array', $issueLinks); - $this->assertCount(2, $issueLinks); - - $this->assertInstanceOf(Issue::class, $issueLinks['source_issue']); - $this->assertSame(10, $issueLinks['source_issue']->iid); - $this->assertInstanceOf(Project::class, $issueLinks['source_issue']->project); - $this->assertSame(1, $issueLinks['source_issue']->project->id); - - $this->assertInstanceOf(Issue::class, $issueLinks['target_issue']); - $this->assertSame(20, $issueLinks['target_issue']->iid); - $this->assertInstanceOf(Project::class, $issueLinks['target_issue']->project); - $this->assertSame(2, $issueLinks['target_issue']->project->id); - } - - /** - * @test - */ - public function testRemoveLink() - { - $issueLinks = $this->getMockBuilder(IssueLinks::class) - ->disableOriginalConstructor() - ->getMock() - ; - $projects = $this->getMockBuilder(Projects::class) - ->disableOriginalConstructor() - ->getMock() - ; - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $client->method('issueLinks')->willReturn($issueLinks); - $client->method('projects')->willReturn($projects); - - $issueLinks->expects($this->once()) - ->method('remove') - ->with(1, 10, 100) - ->willReturn([ - 'source_issue' => ['iid' => 10, 'project_id' => 1], - 'target_issue' => ['iid' => 20, 'project_id' => 2] - ]) - ; - $projects->expects($this->once()) - ->method('show') - ->with(2) - ->willReturn(['id' => 2]) - ; - - - $issue = new Issue(new Project(1, $client), 10, $client); - $issueLinks = $issue->removeLink(100); - - - $this->assertInternalType('array', $issueLinks); - $this->assertCount(2, $issueLinks); - - $this->assertInstanceOf(Issue::class, $issueLinks['source_issue']); - $this->assertSame(10, $issueLinks['source_issue']->iid); - $this->assertInstanceOf(Project::class, $issueLinks['source_issue']->project); - $this->assertSame(1, $issueLinks['source_issue']->project->id); - - $this->assertInstanceOf(Issue::class, $issueLinks['target_issue']); - $this->assertSame(20, $issueLinks['target_issue']->iid); - $this->assertInstanceOf(Project::class, $issueLinks['target_issue']->project); - $this->assertSame(2, $issueLinks['target_issue']->project->id); - } -} diff --git a/test/Gitlab/Tests/Model/LabelTest.php b/test/Gitlab/Tests/Model/LabelTest.php deleted file mode 100644 index 4b260db44..000000000 --- a/test/Gitlab/Tests/Model/LabelTest.php +++ /dev/null @@ -1,49 +0,0 @@ -assertSame(null, $sUT->getClient()); - } - - public function testCorrectConstruct() - { - $project = new Project(); - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $sUT = new Label($project, $client); - - $this->assertSame($client, $sUT->getClient()); - } - - public function testFromArray() - { - $project = new Project(); - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $sUT = Label::fromArray($client, $project, ['color' => '#FF0000', 'name' => 'Testing', 'id' => 123]); - - $this->assertSame('#FF0000', $sUT->color); - $this->assertSame('Testing', $sUT->name); - $this->assertSame(123, $sUT->id); - $this->assertSame($client, $sUT->getClient()); - } -} diff --git a/test/Gitlab/Tests/Model/ReleaseTest.php b/test/Gitlab/Tests/Model/ReleaseTest.php deleted file mode 100644 index 58df38f0b..000000000 --- a/test/Gitlab/Tests/Model/ReleaseTest.php +++ /dev/null @@ -1,32 +0,0 @@ - 'v1.0.0', - 'description' => 'Amazing release. Wow', - ); - - $project = new Project(); - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $release = Release::fromArray($client, $params); - - $this->assertSame($params['tag_name'], $release->tag_name); - $this->assertSame($params['description'], $release->description); - } -} diff --git a/test/Gitlab/Tests/ResultPagerTest.php b/test/Gitlab/Tests/ResultPagerTest.php deleted file mode 100644 index e795d26d0..000000000 --- a/test/Gitlab/Tests/ResultPagerTest.php +++ /dev/null @@ -1,113 +0,0 @@ -getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $api = $this->getMockBuilder(ApiInterface::class) - ->setMethods(['__construct', 'all']) - ->getMock() - ; - $api->expects($this->once()) - ->method('all') - ->willReturn(['project1', 'project2']) - ; - - $pager = new ResultPager($client); - - $result = $pager->fetch($api, 'all'); - - $this->assertEquals(['project1', 'project2'], $result); - } - - public function testFetchAll() - { - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $history = $this->getMockBuilder(History::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $response1 = (new Response)->withHeader('Link', '; rel="next",'); - $response2 = (new Response)->withHeader('Link', '; rel="next",') - ->withHeader('Content-Type', 'application/json') - ->withBody(stream_for('["project3", "project4"]')) - ; - $response3 = (new Response)->withHeader('Content-Type', 'application/json') - ->withBody(stream_for('["project5", "project6"]')) - ; - - $history - ->method('getLastResponse') - ->will($this->onConsecutiveCalls( - $response1, - $response1, - $response1, - $response2, - $response2, - $response2, - $response3 - )) - ; - - $httpClient = $this->getMockBuilder(HttpMethodsClient::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $httpClient->expects($this->exactly(2)) - ->method('get') - ->withConsecutive( - ['https://example.gitlab.com/projects?page=2'], - ['https://example.gitlab.com/projects?page=3'] - ) - ->will($this->onConsecutiveCalls( - $response2, - $response3 - )) - ; - - $client - ->method('getResponseHistory') - ->willReturn($history) - ; - $client - ->method('getHttpClient') - ->willReturn($httpClient) - ; - - $api = $this->getMockBuilder(ApiInterface::class) - ->setMethods(['__construct', 'all']) - ->getMock(); - $api->expects($this->exactly(1)) - ->method('all') - ->willReturn(['project1', 'project2']) - ; - - $pager = new ResultPager($client); - - $result = $pager->fetchAll($api, 'all'); - - $this->assertEquals(['project1', 'project2', 'project3', 'project4', 'project5', 'project6'], $result); - } -} diff --git a/tests/unit/Api/DeployKeysTest.php b/tests/unit/Api/DeployKeysTest.php new file mode 100644 index 000000000..964287710 --- /dev/null +++ b/tests/unit/Api/DeployKeysTest.php @@ -0,0 +1,51 @@ + $expected + * + * @dataProvider getMultipleDeployKeysData + */ + public function testShouldGetAllDeployKeys(array $expected): void + { + $this->setResponseBody($expected); + $this->assertEquals($expected, $this->api->all(['page' => 2, 'per_page' => 5])); + } + + public function getMultipleDeployKeysData(): array + { + return [ + [ + [ + 'id' => 1, + 'title' => 'Public key', + 'key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=', + 'created_at' => '2013-10-02T10:12:29Z', + ], + ], + [ + [ + 'id' => 3, + 'title' => 'Another Public key', + 'key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=', + 'created_at' => '2013-10-02T11:12:29Z', + ], + ], + ]; + } + + protected function getApiClass(): string + { + return DeployKeys::class; + } +} diff --git a/tests/unit/Api/DeploymentsTest.php b/tests/unit/Api/DeploymentsTest.php new file mode 100644 index 000000000..7c8b3601a --- /dev/null +++ b/tests/unit/Api/DeploymentsTest.php @@ -0,0 +1,234 @@ + '2016-08-11T07:36:40.222Z', + 'deployable' => [ + 'commit' => [ + 'author_email' => 'admin@example.com', + 'author_name' => 'Administrator', + 'created_at' => '2016-08-11T09:36:01.000+02:00', + 'id' => '99d03678b90d914dbb1b109132516d71a4a03ea8', + 'message' => 'Merge branch \'new-title\' into \'master\' + +Update README + + + +See merge request !1', + 'short_id' => '99d03678', + 'title' => 'Merge branch \'new-title\' into \'master\' +', + ], + 'coverage' => null, + 'created_at' => '2016-08-11T07:36:27.357Z', + 'finished_at' => '2016-08-11T07:36:39.851Z', + 'id' => 657, + 'name' => 'deploy', + 'ref' => 'master', + 'runner' => null, + 'stage' => 'deploy', + 'started_at' => null, + 'status' => 'success', + 'tag' => false, + 'user' => [ + 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', + 'bio' => null, + 'created_at' => '2016-08-11T07:09:20.351Z', + 'id' => 1, + 'linkedin' => '', + 'location' => null, + 'name' => 'Administrator', + 'skype' => '', + 'state' => 'active', + 'twitter' => '', + 'username' => 'root', + 'web_url' => 'http://localhost:3000/root', + 'website_url' => '', + ], + ], + 'environment' => [ + 'external_url' => 'https://about.gitlab.com', + 'id' => 9, + 'name' => 'production', + ], + 'id' => 41, + 'iid' => 1, + 'ref' => 'master', + 'sha' => '99d03678b90d914dbb1b109132516d71a4a03ea8', + 'user' => [ + 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', + 'id' => 1, + 'name' => 'Administrator', + 'state' => 'active', + 'username' => 'root', + 'web_url' => 'http://localhost:3000/root', + ], + ], + [ + 'created_at' => '2016-08-11T11:32:35.444Z', + 'deployable' => [ + 'commit' => [ + 'author_email' => 'admin@example.com', + 'author_name' => 'Administrator', + 'created_at' => '2016-08-11T13:28:26.000+02:00', + 'id' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', + 'message' => 'Merge branch \'rename-readme\' into \'master\' + +Rename README + + + +See merge request !2', + 'short_id' => 'a91957a8', + 'title' => 'Merge branch \'rename-readme\' into \'master\' +', + ], + 'coverage' => null, + 'created_at' => '2016-08-11T11:32:24.456Z', + 'finished_at' => '2016-08-11T11:32:35.145Z', + 'id' => 664, + 'name' => 'deploy', + 'ref' => 'master', + 'runner' => null, + 'stage' => 'deploy', + 'started_at' => null, + 'status' => 'success', + 'tag' => false, + 'user' => [ + 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', + 'bio' => null, + 'created_at' => '2016-08-11T07:09:20.351Z', + 'id' => 1, + 'linkedin' => '', + 'location' => null, + 'name' => 'Administrator', + 'skype' => '', + 'state' => 'active', + 'twitter' => '', + 'username' => 'root', + 'web_url' => 'http://localhost:3000/root', + 'website_url' => '', + ], + ], + 'environment' => [ + 'external_url' => 'https://about.gitlab.com', + 'id' => 9, + 'name' => 'production', + ], + 'id' => 42, + 'iid' => 2, + 'ref' => 'master', + 'sha' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', + 'user' => [ + 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', + 'id' => 1, + 'name' => 'Administrator', + 'state' => 'active', + 'username' => 'root', + 'web_url' => 'http://localhost:3000/root', + ], + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1)); + } + + /** + * @test + */ + public function shouldShowDeployment(): void + { + $expectedArray = [ + [ + 'created_at' => '2016-08-11T11:32:35.444Z', + 'deployable' => [ + 'commit' => [ + 'author_email' => 'admin@example.com', + 'author_name' => 'Administrator', + 'created_at' => '2016-08-11T13:28:26.000+02:00', + 'id' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', + 'message' => 'Merge branch \'rename-readme\' into \'master\' + +Rename README + + + +See merge request !2', + 'short_id' => 'a91957a8', + 'title' => 'Merge branch \'rename-readme\' into \'master\' +', + ], + 'coverage' => null, + 'created_at' => '2016-08-11T11:32:24.456Z', + 'finished_at' => '2016-08-11T11:32:35.145Z', + 'id' => 664, + 'name' => 'deploy', + 'ref' => 'master', + 'runner' => null, + 'stage' => 'deploy', + 'started_at' => null, + 'status' => 'success', + 'tag' => false, + 'user' => [ + 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', + 'bio' => null, + 'created_at' => '2016-08-11T07:09:20.351Z', + 'id' => 1, + 'linkedin' => '', + 'location' => null, + 'name' => 'Administrator', + 'skype' => '', + 'state' => 'active', + 'twitter' => '', + 'username' => 'root', + 'web_url' => 'http://localhost:3000/root', + 'website_url' => '', + ], + ], + 'environment' => [ + 'external_url' => 'https://about.gitlab.com', + 'id' => 9, + 'name' => 'production', + ], + 'id' => 42, + 'iid' => 2, + 'ref' => 'master', + 'sha' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a', + 'user' => [ + 'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', + 'id' => 1, + 'name' => 'Administrator', + 'state' => 'active', + 'username' => 'root', + 'web_url' => 'http://localhost:3000/root', + ], + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 42)); + } + + protected function getApiClass(): string + { + return Deployments::class; + } +} diff --git a/tests/unit/Api/EnvironmentsTest.php b/tests/unit/Api/EnvironmentsTest.php new file mode 100644 index 000000000..ab09f18ba --- /dev/null +++ b/tests/unit/Api/EnvironmentsTest.php @@ -0,0 +1,80 @@ + 1, + 'name' => 'review/fix-foo', + 'slug' => 'review-fix-foo-dfjre3', + 'external_url' => 'https://review-fix-foo-dfjre3.example.gitlab.com', + ], + [ + 'id' => 2, + 'name' => 'review/fix-bar', + 'slug' => 'review-fix-bar-dfjre4', + 'external_url' => 'https://review-fix-bar-dfjre4.example.gitlab.com', + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1)); + } + + /** + * @test + */ + public function shouldCreateEnvironment(): void + { + $expectedArray = [ + [ + 'id' => 3, + 'name' => 'review/fix-baz', + 'slug' => 'review-fix-baz-dfjre5', + 'external_url' => 'https://review-fix-baz-dfjre5.example.gitlab.com', + ], + ]; + + $params = [ + 'name' => 'review/fix-baz', + 'external_url' => 'https://review-fix-baz-dfjre5.example.gitlab.com', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, $params)); + } + + /** + * @test + */ + public function shouldRemoveEnvironment(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(1, 3)); + } + + public function testShouldStopEnvironment(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->stop(1, 3)); + } + + protected function getApiClass(): string + { + return Environments::class; + } +} diff --git a/tests/unit/Api/GroupBoardsTest.php b/tests/unit/Api/GroupBoardsTest.php new file mode 100644 index 000000000..09b8a0e0f --- /dev/null +++ b/tests/unit/Api/GroupBoardsTest.php @@ -0,0 +1,175 @@ + 1, 'title' => 'A board'], + ['id' => 2, 'title' => 'Another board'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all()); + } + + /** + * @test + */ + public function shouldShowIssueBoard(): void + { + $expectedArray = ['id' => 2, 'name' => 'Another issue board']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 2)); + } + + /** + * @test + */ + public function shouldCreateIssueBoard(): void + { + $expectedArray = ['id' => 3, 'name' => 'A new issue board']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, ['name' => 'A new issue board'])); + } + + /** + * @test + */ + public function shouldUpdateIssueBoard(): void + { + $expectedArray = ['id' => 2, 'name' => 'A renamed issue board']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(1, 2, ['name' => 'A renamed issue board', 'labels' => 'foo'])); + } + + /** + * @test + */ + public function shouldRemoveIssueBoard(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(1, 2)); + } + + /** + * @test + */ + public function shouldGetAllLists(): void + { + $expectedArray = [ + [ + 'id' => 1, + 'label' => [ + 'name' => 'First label', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 1, + ], [ + 'id' => 2, + 'label' => [ + 'name' => 'Second label', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 2, + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->allLists(1, 2)); + } + + /** + * @test + */ + public function shouldGetList(): void + { + $expectedArray = [ + [ + 'id' => 3, + 'label' => [ + 'name' => 'Some label', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 3, + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->showList(1, 2, 3)); + } + + /** + * @test + */ + public function shouldCreateList(): void + { + $expectedArray = [ + [ + 'id' => 3, + 'label' => [ + 'name' => 'Some label', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 3, + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createList(1, 2, 4)); + } + + /** + * @test + */ + public function shouldUpdateList(): void + { + $expectedArray = [ + [ + 'id' => 3, + 'label' => [ + 'name' => 'Some label', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 1, + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateList(5, 2, 3, 1)); + } + + /** + * @test + */ + public function shouldDeleteList(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->deleteList(1, 2, 3)); + } + + protected function getApiClass(): string + { + return GroupsBoards::class; + } +} diff --git a/tests/unit/Api/GroupsMilestonesTest.php b/tests/unit/Api/GroupsMilestonesTest.php new file mode 100644 index 000000000..b51bb3aac --- /dev/null +++ b/tests/unit/Api/GroupsMilestonesTest.php @@ -0,0 +1,102 @@ + 1, 'title' => 'A milestone'], + ['id' => 2, 'title' => 'Another milestone'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1)); + } + + /** + * @test + */ + public function shouldShowMilestone(): void + { + $expectedArray = ['id' => 1, 'name' => 'A milestone']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 2)); + } + + /** + * @test + */ + public function shouldCreateMilestone(): void + { + $expectedArray = ['id' => 3, 'title' => 'A new milestone']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, ['description' => 'Some text', 'title' => 'A new milestone'])); + } + + /** + * @test + */ + public function shouldUpdateMilestone(): void + { + $expectedArray = ['id' => 3, 'title' => 'Updated milestone']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(1, 3, ['title' => 'Updated milestone', 'due_date' => '2015-04-01', 'state_event' => 'close'])); + } + + /** + * @test + */ + public function shouldRemoveMilestone(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(1, 2)); + } + + /** + * @test + */ + public function shouldGetMilestonesIssues(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'An issue'], + ['id' => 2, 'title' => 'Another issue'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->issues(1, 3)); + } + + /** + * @test + */ + public function shouldGetMilestonesMergeRequests(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'A merge request'], + ['id' => 2, 'title' => 'Another merge request'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->mergeRequests(1, 3)); + } + + protected function getApiClass(): string + { + return GroupsMilestones::class; + } +} diff --git a/tests/unit/Api/GroupsTest.php b/tests/unit/Api/GroupsTest.php new file mode 100644 index 000000000..7d3f8becd --- /dev/null +++ b/tests/unit/Api/GroupsTest.php @@ -0,0 +1,427 @@ + 1, 'name' => 'A group'], + ['id' => 2, 'name' => 'Another group'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(['page' => 1, 'per_page' => 10])); + } + + /** + * @test + */ + public function shouldGetAllGroupsWithBooleanParam(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A group'], + ['id' => 2, 'name' => 'Another group'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(['all_available' => false])); + } + + /** + * @test + */ + public function shouldGetAllGroupProjectsWithBooleanParam(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A group'], + ['id' => 2, 'name' => 'Another group'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->projects(1, ['archived' => false])); + } + + /** + * @test + */ + public function shouldNotNeedPaginationWhenGettingGroups(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A group'], + ['id' => 2, 'name' => 'Another group'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all()); + } + + /** + * @test + */ + public function shouldShowGroup(): void + { + $expectedArray = ['id' => 1, 'name' => 'A group']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1)); + } + + /** + * @test + */ + public function shouldCreateGroup(): void + { + $expectedArray = ['id' => 1, 'name' => 'A new group']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create('A new group', 'a-new-group')); + } + + /** + * @test + */ + public function shouldCreateGroupWithDescriptionAndVisibility(): void + { + $expectedArray = ['id' => 1, 'name' => 'A new group', 'visibility_level' => 2]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create('A new group', 'a-new-group', 'Description', 'public')); + } + + /** + * @test + */ + public function shouldCreateGroupWithDescriptionVisibilityAndParentId(): void + { + $expectedArray = ['id' => 1, 'name' => 'A new group', 'visibility_level' => 2, 'parent_id' => 666]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create('A new group', 'a-new-group', 'Description', 'public', null, null, 666)); + } + + /** + * @test + */ + public function shouldUpdateGroup(): void + { + $expectedArray = ['id' => 3, 'name' => 'Group name', 'path' => 'group-path']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(3, ['name' => 'Group name', 'path' => 'group-path'])); + } + + /** + * @test + */ + public function shouldTransferProjectToGroup(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->transfer(1, 2)); + } + + /** + * @test + */ + public function shouldGetAllMembers(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Matt'], + ['id' => 2, 'name' => 'Bob'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->allMembers(1)); + } + + /** + * @test + */ + public function shouldGetMembers(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Matt'], + ['id' => 2, 'name' => 'Bob'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->members(1)); + } + + /** + * @test + */ + public function shouldAddMember(): void + { + $expectedArray = ['id' => 1, 'name' => 'Matt']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addMember(1, 2, 3)); + } + + /** + * @test + */ + public function shouldSaveMember(): void + { + $expectedArray = ['id' => 1, 'name' => 'Matt']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->saveMember(1, 2, 4)); + } + + /** + * @test + */ + public function shouldRemoveMember(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeMember(1, 2)); + } + + /** + * @test + */ + public function shouldRemoveGroup(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(1)); + } + + /** + * @test + */ + public function shouldGetAllSubgroups(): void + { + $expectedArray = [ + ['id' => 101, 'name' => 'A subgroup'], + ['id' => 1 - 2, 'name' => 'Another subggroup'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->subgroups(1, ['page' => 1, 'per_page' => 10])); + } + + /** + * @test + */ + public function shouldGetLabels(): void + { + $expectedArray = [ + ['id' => 987, 'name' => 'bug', 'color' => '#000000'], + ['id' => 123, 'name' => 'feature', 'color' => '#ff0000'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->labels(1)); + } + + /** + * @test + */ + public function shouldAddLabel(): void + { + $expectedArray = ['name' => 'bug', 'color' => '#000000']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addLabel(1, ['name' => 'wont-fix', 'color' => '#ffffff'])); + } + + /** + * @test + */ + public function shouldUpdateLabel(): void + { + $expectedArray = ['name' => 'bug', 'color' => '#00ffff']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateLabel(1, ['name' => 'bug', 'new_name' => 'big-bug', 'color' => '#00ffff'])); + } + + /** + * @test + */ + public function shouldRemoveLabel(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeLabel(1, 'bug')); + } + + public function shouldGetVariables(): void + { + $expectedArray = [ + ['key' => 'ftp_username', 'value' => 'ftp'], + ['key' => 'ftp_password', 'value' => 'somepassword'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->variables(1)); + } + + /** + * @test + */ + public function shouldGetVariable(): void + { + $expectedArray = ['key' => 'ftp_username', 'value' => 'ftp']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->variable(1, 'ftp_username')); + } + + public function shouldAddVariable(): void + { + $expectedKey = 'ftp_port'; + $expectedValue = '21'; + + $expectedArray = [ + 'key' => $expectedKey, + 'value' => $expectedValue, + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addVariable(1, $expectedKey, $expectedValue)); + } + + /** + * @test + */ + public function shouldAddVariableWithProtected(): void + { + $expectedArray = [ + 'key' => 'DEPLOY_SERVER', + 'value' => 'stage.example.com', + 'protected' => true, + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true)); + } + + /** + * @test + */ + public function shouldUpdateVariable(): void + { + $expectedKey = 'ftp_port'; + $expectedValue = '22'; + + $expectedArray = [ + 'key' => 'ftp_port', + 'value' => '22', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateVariable(1, $expectedKey, $expectedValue)); + } + + /** + * @test + */ + public function shouldUpdateVariableWithProtected(): void + { + $expectedArray = [ + 'key' => 'DEPLOY_SERVER', + 'value' => 'stage.example.com', + 'protected' => true, + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true)); + } + + /** + * @test + */ + public function shouldRemoveVariable(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeVariable(1, 'ftp_password')); + } + + /** + * @test + */ + public function shouldGetAllGroupProjectsWithIssuesEnabled(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A group', 'issues_enabled' => true], + ['id' => 2, 'name' => 'Another group', 'issues_enabled' => true], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->projects(1, ['with_issues_enabled' => true])); + } + + /** + * @test + */ + public function shouldGetAllGroupProjectsWithMergeRequestsEnabled(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A group', 'merge_requests_enabled' => true], + ['id' => 2, 'name' => 'Another group', 'merge_requests_enabled' => true], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->projects(1, ['with_merge_requests_enabled' => true])); + } + + /** + * @test + */ + public function shouldGetAllGroupProjectsSharedToGroup(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A project', 'shared_with_groups' => [1]], + ['id' => 2, 'name' => 'Another project', 'shared_with_groups' => [1]], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->projects(1, ['with_shared' => true])); + } + + /** + * @test + */ + public function shouldGetAllGroupProjectsIncludingSubsgroups(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A project'], + ['id' => 2, 'name' => 'Another project', 'shared_with_groups' => [1]], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->projects(1, ['include_subgroups' => true])); + } + + /** + * @test + */ + public function shouldGetAllGroupProjectsIncludingCustomAttributes(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A project', 'custom_Attr' => true], + ['id' => 2, 'name' => 'Another project', 'custom_Attr' => true], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->projects(1, ['with_custom_attributes' => true])); + } +} diff --git a/tests/unit/Api/IssueBoardsTest.php b/tests/unit/Api/IssueBoardsTest.php new file mode 100644 index 000000000..b69294994 --- /dev/null +++ b/tests/unit/Api/IssueBoardsTest.php @@ -0,0 +1,175 @@ + 1, 'title' => 'A board'], + ['id' => 2, 'title' => 'Another board'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all()); + } + + /** + * @test + */ + public function shouldShowIssueBoard(): void + { + $expectedArray = ['id' => 2, 'name' => 'Another issue board']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 2)); + } + + /** + * @test + */ + public function shouldCreateIssueBoard(): void + { + $expectedArray = ['id' => 3, 'name' => 'A new issue board']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, ['name' => 'A new issue board'])); + } + + /** + * @test + */ + public function shouldUpdateIssueBoard(): void + { + $expectedArray = ['id' => 2, 'name' => 'A renamed issue board']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(1, 2, ['name' => 'A renamed issue board', 'labels' => 'foo'])); + } + + /** + * @test + */ + public function shouldRemoveIssueBoard(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(1, 2)); + } + + /** + * @test + */ + public function shouldGetAllLists(): void + { + $expectedArray = [ + [ + 'id' => 1, + 'label' => [ + 'name' => 'First label', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 1, + ], [ + 'id' => 2, + 'label' => [ + 'name' => 'Second label', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 2, + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->allLists(1, 2)); + } + + /** + * @test + */ + public function shouldGetList(): void + { + $expectedArray = [ + [ + 'id' => 3, + 'label' => [ + 'name' => 'Some label', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 3, + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->showList(1, 2, 3)); + } + + /** + * @test + */ + public function shouldCreateList(): void + { + $expectedArray = [ + [ + 'id' => 3, + 'label' => [ + 'name' => 'Some label', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 3, + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createList(1, 2, 4)); + } + + /** + * @test + */ + public function shouldUpdateList(): void + { + $expectedArray = [ + [ + 'id' => 3, + 'label' => [ + 'name' => 'Some label', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 1, + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateList(5, 2, 3, 1)); + } + + /** + * @test + */ + public function shouldDeleteList(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->deleteList(1, 2, 3)); + } + + protected function getApiClass(): string + { + return IssueBoards::class; + } +} diff --git a/tests/unit/Api/IssueLinksTest.php b/tests/unit/Api/IssueLinksTest.php new file mode 100644 index 000000000..adfd695a8 --- /dev/null +++ b/tests/unit/Api/IssueLinksTest.php @@ -0,0 +1,63 @@ + 100], + ['issue_link_id' => 101], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1, 10)); + } + + /** + * @test + */ + public function shouldCreateIssueLink(): void + { + $expectedArray = [ + 'source_issue' => ['iid' => 10, 'project_id' => 1], + 'target_issue' => ['iid' => 20, 'project_id' => 2], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, 10, 2, 20)); + } + + /** + * @test + */ + public function shouldRemoveIssueLink(): void + { + $expectedArray = [ + 'source_issue' => ['iid' => 10, 'project_id' => 1], + 'target_issue' => ['iid' => 20, 'project_id' => 2], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->remove(1, 10, 100)); + } +} diff --git a/tests/unit/Api/IssuesStatisticsTest.php b/tests/unit/Api/IssuesStatisticsTest.php new file mode 100644 index 000000000..1e70b94b1 --- /dev/null +++ b/tests/unit/Api/IssuesStatisticsTest.php @@ -0,0 +1,67 @@ +setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all([ + 'milestone' => '', + 'labels' => '', + 'scope' => 'created-by-me', + 'author_id' => 1, + 'author_username' => '', + 'assignee_id' => 1, + 'assignee_username' => '', + 'my_reaction_emoji' => '', + 'search' => '', + 'created_after' => $now, + 'created_before' => $now, + 'updated_after' => $now, + 'updated_before' => $now, + 'confidential' => false, + ])); + } + + /** + * @test + */ + public function shouldGetProject(): void + { + $expectedArray = []; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->project(1, [])); + } + + /** + * @test + */ + public function shouldGetGroup(): void + { + $expectedArray = []; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->group(1, [])); + } + + protected function getApiClass(): string + { + return IssuesStatistics::class; + } +} diff --git a/tests/unit/Api/IssuesTest.php b/tests/unit/Api/IssuesTest.php new file mode 100644 index 000000000..dc221db35 --- /dev/null +++ b/tests/unit/Api/IssuesTest.php @@ -0,0 +1,318 @@ + 1, 'title' => 'An issue'], + ['id' => 2, 'title' => 'Another issue'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all()); + } + + /** + * @test + */ + public function shouldGetProjectIssuesWithPagination(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'An issue'], + ['id' => 2, 'title' => 'Another issue'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1, ['page' => 2, 'per_page' => 5])); + } + + /** + * @test + */ + public function shouldGetProjectIssuesWithParams(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'An issue'], + ['id' => 2, 'title' => 'Another issue'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1, ['order_by' => 'created_at', 'sort' => 'desc', 'labels' => 'foo,bar', 'state' => 'opened'])); + } + + /** + * @test + */ + public function shouldShowIssue(): void + { + $expectedArray = ['id' => 2, 'title' => 'Another issue']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 2)); + } + + /** + * @test + */ + public function shouldCreateIssue(): void + { + $expectedArray = ['id' => 3, 'title' => 'A new issue']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, ['title' => 'A new issue', 'labels' => 'foo,bar'])); + } + + /** + * @test + */ + public function shouldUpdateIssue(): void + { + $expectedArray = ['id' => 2, 'title' => 'A renamed issue']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(1, 2, ['title' => 'A renamed issue', 'labels' => 'foo'])); + } + + /** + * @test + */ + public function shouldMoveIssue(): void + { + $expectedArray = ['id' => 2, 'title' => 'A moved issue']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->move(1, 2, 3)); + } + + /** + * @test + */ + public function shouldGetIssueComments(): void + { + $expectedArray = [ + ['id' => 1, 'body' => 'A comment'], + ['id' => 2, 'body' => 'Another comment'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->showComments(1, 2)); + } + + /** + * @test + */ + public function shouldGetIssueComment(): void + { + $expectedArray = ['id' => 3, 'body' => 'A new comment']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->showComment(1, 2, 3)); + } + + /** + * @test + */ + public function shouldCreateComment(): void + { + $expectedArray = ['id' => 3, 'body' => 'A new comment']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addComment(1, 2, ['body' => 'A new comment'])); + $this->assertEquals($expectedArray, $this->api->addComment(1, 2, 'A new comment')); + } + + /** + * @test + */ + public function shouldUpdateComment(): void + { + $expectedArray = ['id' => 3, 'body' => 'An edited comment']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateComment(1, 2, 3, 'An edited comment')); + } + + /** + * @test + */ + public function shouldGetIssueDiscussions(): void + { + $expectedArray = [ + ['id' => 'abc', 'body' => 'A discussion'], + ['id' => 'def', 'body' => 'Another discussion'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->showDiscussions(1, 2)); + } + + /** + * @test + */ + public function shouldGetIssueDiscussion(): void + { + $expectedArray = ['id' => 'abc', 'body' => 'A discussion']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->showDiscussion(1, 2, 'abc')); + } + + /** + * @test + */ + public function shouldCreateDiscussion(): void + { + $expectedArray = ['id' => 'abc', 'body' => 'A new discussion']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addDiscussion(1, 2, ['body' => 'A new discussion'])); + $this->assertEquals($expectedArray, $this->api->addDiscussion(1, 2, 'A new discussion')); + } + + /** + * @test + */ + public function shouldCreateDiscussionNote(): void + { + $expectedArray = ['id' => 3, 'body' => 'A new discussion note']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addDiscussionNote(1, 2, 'abc', ['body' => 'A new discussion note'])); + $this->assertEquals($expectedArray, $this->api->addDiscussionNote(1, 2, 'abc', 'A new discussion note')); + } + + /** + * @test + */ + public function shouldUpdateDiscussionNote(): void + { + $expectedArray = ['id' => 3, 'body' => 'An edited discussion note']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateDiscussionNote(1, 2, 'abc', 3, 'An edited discussion note')); + } + + /** + * @test + */ + public function shouldRemoveDiscussionNote(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeDiscussionNote(1, 2, 'abc', 3)); + } + + /** + * @test + */ + public function shouldSetTimeEstimate(): void + { + $expectedArray = ['time_estimate' => 14400, 'total_time_spent' => 0, 'human_time_estimate' => '4h', 'human_total_time_spent' => null]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->setTimeEstimate(1, 2, '4h')); + } + + /** + * @test + */ + public function shouldResetTimeEstimate(): void + { + $expectedArray = ['time_estimate' => 0, 'total_time_spent' => 0, 'human_time_estimate' => null, 'human_total_time_spent' => null]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->resetTimeEstimate(1, 2)); + } + + /** + * @test + */ + public function shouldAddSpentTime(): void + { + $expectedArray = ['time_estimate' => 0, 'total_time_spent' => 14400, 'human_time_estimate' => null, 'human_total_time_spent' => '4h']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addSpentTime(1, 2, '4h')); + } + + /** + * @test + */ + public function shouldResetSpentTime(): void + { + $expectedArray = ['time_estimate' => 0, 'total_time_spent' => 0, 'human_time_estimate' => null, 'human_total_time_spent' => null]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->resetSpentTime(1, 2)); + } + + /** + * @test + */ + public function shouldGetIssueTimeStats(): void + { + $expectedArray = ['time_estimate' => 14400, 'total_time_spent' => 5400, 'human_time_estimate' => '4h', 'human_total_time_spent' => '1h 30m']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->getTimeStats(1, 2)); + } + + /** + * @test + */ + public function shouldGetIssueAwardEmoji(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'sparkles'], + ['id' => 2, 'name' => 'heart_eyes'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->awardEmoji(1, 2)); + } + + /** + * @test + */ + public function shouldGetIssueClosedByMergeRequests(): void + { + $expectedArray = [ + ['id' => 1, 'iid' => '1111', 'title' => 'Just saving the world'], + ['id' => 2, 'iid' => '1112', 'title' => 'Adding new feature to get merge requests that close an issue'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->closedByMergeRequests(1, 2)); + } + + /** + * @test + */ + public function shouldGetProjectIssuesByAssignee(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'An issue'], + ['id' => 2, 'title' => 'Another issue'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1, ['assignee_id' => 1])); + } + + protected function getApiClass(): string + { + return Issues::class; + } +} diff --git a/tests/unit/Api/JobsTest.php b/tests/unit/Api/JobsTest.php new file mode 100644 index 000000000..08bfb8d63 --- /dev/null +++ b/tests/unit/Api/JobsTest.php @@ -0,0 +1,150 @@ + 1, 'name' => 'A job'], + ['id' => 2, 'name' => 'Another job'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1, ['scope' => Jobs::SCOPE_PENDING])); + } + + /** + * @test + */ + public function shouldGetPipelineJobs(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A job'], + ['id' => 2, 'name' => 'Another job'], + ]; + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->pipelineJobs(1, 2, ['scope' => [Jobs::SCOPE_PENDING, Jobs::SCOPE_RUNNING]])); + } + + /** + * @test + */ + public function shouldGetJob(): void + { + $expectedArray = ['id' => 3, 'name' => 'A job']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 3)); + } + + /** + * @test + */ + public function shouldGetArtifacts(): void + { + $this->setResponse(new Response(200, [], 'foobar')); + $this->assertEquals('foobar', $this->api->artifacts(1, 3)->getContents()); + } + + /** + * @test + */ + public function shouldGetArtifactsByRefName(): void + { + $this->setResponse(new Response(200, [], 'foobar')); + $this->assertEquals('foobar', $this->api->artifactsByRefName(1, 'master', 'job_name')->getContents()); + } + + /** + * @test + */ + public function shouldGetArtifactByRefName(): void + { + $this->setResponse(new Response(200, [], 'foobar')); + $this->assertEquals('foobar', $this->api->artifactByRefName(1, 'master', 'job_name', 'artifact_path')->getContents()); + } + + /** + * @test + */ + public function shouldGetTrace(): void + { + $expectedString = 'some trace'; + + $this->setResponseBody($expectedString); + $this->assertEquals($expectedString, $this->api->trace(1, 3)); + } + + /** + * @test + */ + public function shouldCancel(): void + { + $expectedArray = ['id' => 3, 'name' => 'A job']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->cancel(1, 3)); + } + + /** + * @test + */ + public function shouldRetry(): void + { + $expectedArray = ['id' => 3, 'name' => 'A job']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->retry(1, 3)); + } + + /** + * @test + */ + public function shouldErase(): void + { + $expectedArray = ['id' => 3, 'name' => 'A job']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->erase(1, 3)); + } + + /** + * @test + */ + public function shouldKeepArtifacts(): void + { + $expectedArray = ['id' => 3, 'name' => 'A job']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->keepArtifacts(1, 3)); + } + + /** + * @test + */ + public function shouldPlay(): void + { + $expectedArray = ['id' => 3, 'name' => 'A job']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->play(1, 3)); + } + + protected function getApiClass(): string + { + return Jobs::class; + } +} diff --git a/tests/unit/Api/KeysTest.php b/tests/unit/Api/KeysTest.php new file mode 100644 index 000000000..fa12e693d --- /dev/null +++ b/tests/unit/Api/KeysTest.php @@ -0,0 +1,28 @@ + 1, 'title' => 'A key', 'key' => 'ssh-rsa key', 'created_at' => '2016-01-01T01:00:00.000Z']; + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1)); + } + + protected function getApiClass(): string + { + return Keys::class; + } +} diff --git a/tests/unit/Api/MergeRequestsTest.php b/tests/unit/Api/MergeRequestsTest.php new file mode 100644 index 000000000..a64e01a70 --- /dev/null +++ b/tests/unit/Api/MergeRequestsTest.php @@ -0,0 +1,331 @@ +getMultipleMergeRequestsData(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1)); + } + + /** + * @test + */ + public function shouldGetAllWithParams(): void + { + $expectedArray = $this->getMultipleMergeRequestsData(); + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1, [ + 'page' => 2, + 'per_page' => 5, + 'labels' => 'label1,label2,label3', + 'milestone' => 'milestone1', + 'order_by' => 'updated_at', + 'state' => 'all', + 'sort' => 'desc', + 'scope' => 'all', + 'author_id' => 1, + 'assignee_id' => 1, + 'source_branch' => 'develop', + 'target_branch' => 'master', + ])); + } + + /** + * @test + */ + public function shouldGetAllWithDateTimeParams(): void + { + $expectedArray = $this->getMultipleMergeRequestsData(); + + $createdAfter = new DateTimeImmutable('2018-01-01 00:00:00'); + $createdBefore = new DateTimeImmutable('2018-01-31 00:00:00'); + + $this->setResponseBody($expectedArray); + $this->assertEquals( + $expectedArray, + $this->api->all(1, ['created_after' => $createdAfter, 'created_before' => $createdBefore]) + ); + } + + /** + * @test + */ + public function shouldShowMergeRequest(): void + { + $expectedArray = ['id' => 2, 'name' => 'A merge request']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 2)); + } + + /** + * @test + */ + public function shouldCreateMergeRequestWithoutOptionalParams(): void + { + $expectedArray = ['id' => 3, 'title' => 'Merge Request']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, 'develop', 'master', 'Merge Request')); + } + + /** + * @test + */ + public function shouldCreateMergeRequestWithOptionalParams(): void + { + $expectedArray = ['id' => 3, 'title' => 'Merge Request']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, 'develop', 'master', 'Merge Request', 6, 20, 'Some changes')); + } + + /** + * @test + */ + public function shouldUpdateMergeRequest(): void + { + $expectedArray = ['id' => 2, 'title' => 'Updated title']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(1, 2, [ + 'title' => 'Updated title', + 'description' => 'No so many changes now', + 'state_event' => 'close', + ])); + } + + /** + * @test + */ + public function shouldMergeMergeRequest(): void + { + $expectedArray = ['id' => 2, 'title' => 'Updated title']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->merge(1, 2, 'Accepted')); + $this->assertEquals($expectedArray, $this->api->merge(1, 2, ['merge_commit_message' => 'Accepted'])); + } + + /** + * @test + */ + public function shouldGetMergeRequestNotes(): void + { + $expectedArray = [ + ['id' => 1, 'body' => 'A comment'], + ['id' => 2, 'body' => 'Another comment'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->showNotes(1, 2)); + } + + /** + * @test + */ + public function shouldRemoveMergeRequestNote(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeNote(1, 2, 1)); + } + + /** + * @test + */ + public function shouldGetMergeRequestChanges(): void + { + $expectedArray = ['id' => 1, 'title' => 'A merge request']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->changes(1, 2)); + } + + /** + * @test + */ + public function shouldGetMergeRequestDiscussions(): void + { + $expectedArray = [ + ['id' => 'abc', 'body' => 'A discussion'], + ['id' => 'def', 'body' => 'Another discussion'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->showDiscussions(1, 2)); + } + + /** + * @test + */ + public function shouldGetMergeRequestDiscussion(): void + { + $expectedArray = ['id' => 'abc', 'body' => 'A discussion']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->showDiscussion(1, 2, 'abc')); + } + + /** + * @test + */ + public function shouldCreateDiscussion(): void + { + $expectedArray = ['id' => 'abc', 'body' => 'A new discussion']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addDiscussion(1, 2, ['body' => 'A new discussion'])); + } + + /** + * @test + */ + public function shouldResolveDiscussion(): void + { + $expectedArray = ['id' => 'abc', 'resolved' => true]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->resolveDiscussion(1, 2, 'abc', true)); + } + + /** + * @test + */ + public function shouldUnresolveDiscussion(): void + { + $expectedArray = ['id' => 'abc', 'resolved' => false]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->resolveDiscussion(1, 2, 'abc', false)); + } + + /** + * @test + */ + public function shouldCreateDiscussionNote(): void + { + $expectedArray = ['id' => 3, 'body' => 'A new discussion note']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addDiscussionNote(1, 2, 'abc', ['body' => 'A new discussion note'])); + $this->assertEquals($expectedArray, $this->api->addDiscussionNote(1, 2, 'abc', 'A new discussion note')); + } + + /** + * @test + */ + public function shouldUpdateDiscussionNote(): void + { + $expectedArray = ['id' => 3, 'body' => 'An edited discussion note']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateDiscussionNote(1, 2, 'abc', 3, ['body' => 'An edited discussion note'])); + } + + /** + * @test + */ + public function shouldRemoveDiscussionNote(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeDiscussionNote(1, 2, 'abc', 3)); + } + + /** + * @test + */ + public function shouldGetIssuesClosedByMergeRequest(): void + { + $expectedArray = ['id' => 1, 'title' => 'A merge request']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->closesIssues(1, 2)); + } + + /** + * @test + */ + public function shouldGetMergeRequestByIid(): void + { + $expectedArray = ['id' => 1, 'title' => 'A merge request']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1, ['iids' => [2]])); + } + + /** + * @test + */ + public function shouldApproveMergeRequest(): void + { + $expectedArray = ['id' => 1, 'title' => 'Approvals API']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->approve(1, 2)); + } + + /** + * @test + */ + public function shouldUnApproveMergeRequest(): void + { + $expectedArray = ['id' => 1, 'title' => 'Approvals API']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->unapprove(1, 2)); + } + + /** + * @test + */ + public function shouldGetMergeRequestApprovals(): void + { + $expectedArray = ['id' => 1, 'title' => 'Approvals API']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1, ['iids' => [2]])); + } + + /** + * @test + */ + public function shouldGetMergeRequestAwardEmoji(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'sparkles'], + ['id' => 2, 'name' => 'heart_eyes'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->awardEmoji(1, 2)); + } + + protected function getMultipleMergeRequestsData(): array + { + return [ + ['id' => 1, 'title' => 'A merge request'], + ['id' => 2, 'title' => 'Another merge request'], + ]; + } + + protected function getApiClass(): string + { + return MergeRequests::class; + } +} diff --git a/tests/unit/Api/MilestonesTest.php b/tests/unit/Api/MilestonesTest.php new file mode 100644 index 000000000..15759e895 --- /dev/null +++ b/tests/unit/Api/MilestonesTest.php @@ -0,0 +1,88 @@ + 1, 'title' => 'A milestone'], + ['id' => 2, 'title' => 'Another milestone'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1)); + } + + /** + * @test + */ + public function shouldShowMilestone(): void + { + $expectedArray = ['id' => 1, 'name' => 'A milestone']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 2)); + } + + /** + * @test + */ + public function shouldCreateMilestone(): void + { + $expectedArray = ['id' => 3, 'title' => 'A new milestone']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, ['description' => 'Some text', 'title' => 'A new milestone'])); + } + + /** + * @test + */ + public function shouldUpdateMilestone(): void + { + $expectedArray = ['id' => 3, 'title' => 'Updated milestone']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(1, 3, ['title' => 'Updated milestone', 'due_date' => '2015-04-01', 'state_event' => 'close'])); + } + + /** + * @test + */ + public function shouldRemoveMilestone(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(1, 2)); + } + + /** + * @test + */ + public function shouldGetMilestonesIssues(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'An issue'], + ['id' => 2, 'title' => 'Another issue'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->issues(1, 3)); + } + + protected function getApiClass(): string + { + return Milestones::class; + } +} diff --git a/tests/unit/Api/ProjectNamespacesTest.php b/tests/unit/Api/ProjectNamespacesTest.php new file mode 100644 index 000000000..2e2f5dc92 --- /dev/null +++ b/tests/unit/Api/ProjectNamespacesTest.php @@ -0,0 +1,43 @@ + 1, 'name' => 'bespokes'], + ['id' => 2, 'name' => 'internal'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all()); + } + + /** + * @test + */ + public function shouldShowNamespace(): void + { + $expectedArray = ['id' => 1, 'name' => 'internal']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1)); + } + + protected function getApiClass(): string + { + return ProjectNamespaces::class; + } +} diff --git a/tests/unit/Api/ProjectsTest.php b/tests/unit/Api/ProjectsTest.php new file mode 100644 index 000000000..15bc07557 --- /dev/null +++ b/tests/unit/Api/ProjectsTest.php @@ -0,0 +1,1275 @@ +getMultipleProjectsData(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all()); + } + + /** + * @test + */ + public function shouldGetAllProjectsSortedByName(): void + { + $expectedArray = $this->getMultipleProjectsData(); + + $this->setResponseBody($expectedArray); + $this->assertEquals( + $expectedArray, + $this->api->all(['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc']) + ); + } + + /** + * @test + */ + public function shouldNotNeedPaginationWhenGettingProjects(): void + { + $expectedArray = $this->getMultipleProjectsData(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all()); + } + + /** + * @test + */ + public function shouldGetAccessibleProjects(): void + { + $expectedArray = $this->getMultipleProjectsData(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all()); + } + + /** + * @test + */ + public function shouldGetOwnedProjects(): void + { + $expectedArray = $this->getMultipleProjectsData(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(['owned' => true])); + } + + /** + * @test + */ + public function shouldGetNotArchivedProjects(): void + { + $expectedArray = $this->getMultipleProjectsData(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(['archived' => false])); + } + + /** + * @test + * @dataProvider possibleAccessLevels + */ + public function shouldGetProjectsWithMinimumAccessLevel(int $level): void + { + $expectedArray = $this->getMultipleProjectsData(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(['min_access_level' => $level])); + } + + /** + * @test + */ + public function shouldSearchProjects(): void + { + $expectedArray = $this->getMultipleProjectsData(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(['search' => 'a project'])); + } + + /** + * @test + */ + public function shouldShowProject(): void + { + $expectedArray = ['id' => 1, 'name' => 'Project Name']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1)); + } + + /** + * @test + */ + public function shouldShowProjectWithStatistics(): void + { + $expectedArray = [ + 'id' => 1, + 'name' => 'Project Name', + 'statistics' => [ + 'commit_count' => 37, + 'storage_size' => 1038090, + 'repository_size' => 1038090, + 'lfs_objects_size' => 0, + 'job_artifacts_size' => 0, + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, ['statistics' => true])); + } + + /** + * @test + */ + public function shouldCreateProject(): void + { + $expectedArray = ['id' => 1, 'name' => 'Project Name']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create('Project Name', ['issues_enabled' => true])); + } + + /** + * @test + */ + public function shouldUpdateProject(): void + { + $expectedArray = ['id' => 1, 'name' => 'Updated Name']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(1, [ + 'name' => 'Updated Name', + 'issues_enabled' => true, + ])); + } + + /** + * @test + */ + public function shouldArchiveProject(): void + { + $expectedArray = ['id' => 1, 'archived' => true]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->archive(1)); + } + + /** + * @test + */ + public function shouldUnarchiveProject(): void + { + $expectedArray = ['id' => 1, 'archived' => false]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->unarchive(1)); + } + + /** + * @test + */ + public function shouldCreateProjectForUser(): void + { + $expectedArray = ['id' => 1, 'name' => 'Project Name']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createForUser(1, 'Project Name', ['issues_enabled' => true])); + } + + /** + * @test + */ + public function shouldRemoveProject(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(1)); + } + + /** + * @test + */ + public function shouldGetPipelines(): void + { + $expectedArray = [ + ['id' => 1, 'status' => 'success', 'ref' => 'new-pipeline'], + ['id' => 2, 'status' => 'failed', 'ref' => 'new-pipeline'], + ['id' => 3, 'status' => 'pending', 'ref' => 'test-pipeline'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->pipelines(1)); + } + + /** + * Check we can request project issues. + * + * @test + */ + public function shouldGetProjectIssues(): void + { + $expectedArray = $this->getProjectIssuesExpectedArray(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->issues(1)); + } + + /** + * Check we can request project issues with query parameters. + * + * @test + */ + public function shouldGetProjectIssuesParameters(): void + { + $expectedArray = $this->getProjectIssuesExpectedArray(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->issues(1, ['state' => 'opened'])); + } + + /** + * Get expected array for tests which check project issues method. + * + * @return mixed[] + * Project issues list. + */ + public function getProjectIssuesExpectedArray(): array + { + return [ + [ + 'state' => 'opened', + 'description' => 'Ratione dolores corrupti mollitia soluta quia.', + 'author' => [ + 'state' => 'active', + 'id' => 18, + 'web_url' => 'https => //gitlab.example.com/eileen.lowe', + 'name' => 'Alexandra Bashirian', + 'avatar_url' => null, + 'username' => 'eileen.lowe', + ], + 'milestone' => [ + 'project_id' => 1, + 'description' => 'Ducimus nam enim ex consequatur cumque ratione.', + 'state' => 'closed', + 'due_date' => null, + 'iid' => 2, + 'created_at' => '2016-01-04T15 => 31 => 39.996Z', + 'title' => 'v4.0', + 'id' => 17, + 'updated_at' => '2016-01-04T15 => 31 => 39.996Z', + ], + 'project_id' => 1, + 'assignees' => [ + [ + 'state' => 'active', + 'id' => 1, + 'name' => 'Administrator', + 'web_url' => 'https => //gitlab.example.com/root', + 'avatar_url' => null, + 'username' => 'root', + ], + ], + 'assignee' => [ + 'state' => 'active', + 'id' => 1, + 'name' => 'Administrator', + 'web_url' => 'https => //gitlab.example.com/root', + 'avatar_url' => null, + 'username' => 'root', + ], + 'updated_at' => '2016-01-04T15 => 31 => 51.081Z', + 'closed_at' => null, + 'closed_by' => null, + 'id' => 76, + 'title' => 'Consequatur vero maxime deserunt laboriosam est voluptas dolorem.', + 'created_at' => '2016-01-04T15 => 31 => 51.081Z', + 'iid' => 6, + 'labels' => [], + 'user_notes_count' => 1, + 'due_date' => '2016-07-22', + 'web_url' => 'http => //example.com/example/example/issues/6', + 'confidential' => false, + 'weight' => null, + 'discussion_locked' => false, + 'time_stats' => [ + 'time_estimate' => 0, + 'total_time_spent' => 0, + 'human_time_estimate' => null, + 'human_total_time_spent' => null, + ], + ], + ]; + } + + /** + * @test + */ + public function shouldGetBoards(): void + { + $expectedArray = $this->getProjectIssuesExpectedArray(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->boards(1)); + } + + /** + * Get expected array for tests which check project boards + * + * @return mixed[] + * Project issues list. + */ + public function getProjectBoardsExpectedArray(): array + { + return [ + [ + 'id' => 1, + 'project' => [ + 'id' => 5, + 'name' => 'Diaspora Project Site', + 'name_with_namespace' => 'Diaspora / Diaspora Project Site', + 'path' => 'diaspora-project-site', + 'path_with_namespace' => 'diaspora/diaspora-project-site', + 'http_url_to_repo' => 'http => //example.com/diaspora/diaspora-project-site.git', + 'web_url' => 'http => //example.com/diaspora/diaspora-project-site', + ], + 'milestone' => [ + 'id' => 12, + 'title' => '10.0', + ], + 'lists' => [ + [ + 'id' => 1, + 'label' => [ + 'name' => 'Testing', + 'color' => '#F0AD4E', + 'description' => null, + ], + 'position' => 1, + ], + [ + 'id' => 2, + 'label' => [ + 'name' => 'Ready', + 'color' => '#FF0000', + 'description' => null, + ], + 'position' => 2, + ], + [ + 'id' => 3, + 'label' => [ + 'name' => 'Production', + 'color' => '#FF5F00', + 'description' => null, + ], + 'position' => 3, + ], + ], + ], + ]; + } + + /** + * @test + */ + public function shouldGetPipelinesWithBooleanParam(): void + { + $expectedArray = [ + ['id' => 1, 'status' => 'success', 'ref' => 'new-pipeline'], + ['id' => 2, 'status' => 'failed', 'ref' => 'new-pipeline'], + ['id' => 3, 'status' => 'pending', 'ref' => 'test-pipeline'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->pipelines(1, ['yaml_errors' => false])); + } + + /** + * @test + */ + public function shouldGetPipelinesWithSHA(): void + { + $expectedArray = [ + ['id' => 1, 'status' => 'success', 'ref' => 'new-pipeline'], + ['id' => 2, 'status' => 'failed', 'ref' => 'new-pipeline'], + ['id' => 3, 'status' => 'pending', 'ref' => 'test-pipeline'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->pipelines(1, ['sha' => '123'])); + } + + /** + * @test + */ + public function shouldGetPipeline(): void + { + $expectedArray = [ + ['id' => 1, 'status' => 'success', 'ref' => 'new-pipeline'], + ['id' => 2, 'status' => 'failed', 'ref' => 'new-pipeline'], + ['id' => 3, 'status' => 'pending', 'ref' => 'test-pipeline'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->pipeline(1, 3)); + } + + /** + * @test + */ + public function shouldCreatePipeline(): void + { + $expectedArray = [ + ['id' => 4, 'status' => 'created', 'ref' => 'test-pipeline'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createPipeline(1, 'test-pipeline')); + } + + /** + * @test + */ + public function shouldRetryPipeline(): void + { + $expectedArray = [ + ['id' => 5, 'status' => 'pending', 'ref' => 'test-pipeline'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->retryPipeline(1, 4)); + } + + /** + * @test + */ + public function shouldCancelPipeline(): void + { + $expectedArray = [ + ['id' => 6, 'status' => 'cancelled', 'ref' => 'test-pipeline'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->cancelPipeline(1, 6)); + } + + /** + * @test + */ + public function shouldDeletePipeline(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->deletePipeline(1, 3)); + } + + /** + * @test + */ + public function shouldGetAllMembers(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Matt'], + ['id' => 2, 'name' => 'Bob'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->allMembers(1)); + } + + /** + * @test + */ + public function shouldGetMembers(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Matt'], + ['id' => 2, 'name' => 'Bob'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->members(1)); + } + + /** + * @test + */ + public function shouldGetMembersWithQuery(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Matt'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->members(1, ['query' => 'at'])); + } + + /** + * @test + */ + public function shouldGetMembersWithNullQuery(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Matt'], + ['id' => 2, 'name' => 'Bob'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->members(1, [])); + } + + /** + * @test + */ + public function shouldGetMembersWithPagination(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Matt'], + ['id' => 2, 'name' => 'Bob'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->members(1, ['page' => 2, 'per_page' => 15])); + } + + /** + * @test + */ + public function shouldGetMember(): void + { + $expectedArray = ['id' => 2, 'name' => 'Matt']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->member(1, 2)); + } + + /** + * @test + */ + public function shouldAddMember(): void + { + $expectedArray = ['id' => 1, 'name' => 'Matt']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addMember(1, 2, 3)); + } + + /** + * @test + */ + public function shouldSaveMember(): void + { + $expectedArray = ['id' => 1, 'name' => 'Matt']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->saveMember(1, 2, 4)); + } + + /** + * @test + */ + public function shouldRemoveMember(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeMember(1, 2)); + } + + /** + * @test + */ + public function shouldGetHooks(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Test hook'], + ['id' => 2, 'name' => 'Another hook'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->hooks(1)); + } + + /** + * @test + */ + public function shouldGetHook(): void + { + $expectedArray = ['id' => 2, 'name' => 'Another hook']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->hook(1, 2)); + } + + /** + * @test + */ + public function shouldAddHook(): void + { + $expectedArray = ['id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addHook( + 1, + 'http://www.example.com', + ['push_events' => true, 'issues_events' => true, 'merge_requests_events' => true] + )); + } + + /** + * @test + */ + public function shouldAddHookWithOnlyUrl(): void + { + $expectedArray = ['id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addHook(1, 'http://www.example.com')); + } + + /** + * @test + */ + public function shouldAddHookWithoutPushEvents(): void + { + $expectedArray = ['id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addHook(1, 'http://www.example.com', ['push_events' => false])); + } + + /** + * @test + */ + public function shouldUpdateHook(): void + { + $expectedArray = ['id' => 3, 'name' => 'A new hook', 'url' => 'http://www.example.com']; + + $this->setResponseBody($expectedArray); + $this->assertEquals( + $expectedArray, + $this->api->updateHook(1, 3, ['url' => 'http://www.example-test.com', 'push_events' => false]) + ); + } + + /** + * @test + */ + public function shouldRemoveHook(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeHook(1, 2)); + } + + /** + * @test + */ + public function shouldTransfer(): void + { + $expectedArray = [ + 'id' => 1, + 'name' => 'Project Name', + 'namespace' => ['name' => 'a_namespace'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->transfer(1, 'a_namespace')); + } + + /** + * @test + */ + public function shouldGetDeployKeys(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'test-key'], + ['id' => 2, 'title' => 'another-key'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->deployKeys(1)); + } + + /** + * @test + */ + public function shouldGetDeployKey(): void + { + $expectedArray = ['id' => 2, 'title' => 'another-key']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->deployKey(1, 2)); + } + + /** + * @test + */ + public function shouldAddKey(): void + { + $expectedArray = ['id' => 3, 'title' => 'new-key', 'can_push' => false]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addDeployKey(1, 'new-key', '...')); + } + + /** + * @test + */ + public function shouldAddKeyWithPushOption(): void + { + $expectedArray = ['id' => 3, 'title' => 'new-key', 'can_push' => true]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addDeployKey(1, 'new-key', '...', true)); + } + + /** + * @test + */ + public function shouldDeleteDeployKey(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->deleteDeployKey(1, 3)); + } + + /** + * @test + */ + public function shoudEnableDeployKey(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->enableDeployKey(1, 3)); + } + + /** + * @test + */ + public function shouldGetEvents(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'An event'], + ['id' => 2, 'title' => 'Another event'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->events(1)); + } + + /** + * @test + */ + public function shouldGetEventsWithDateTimeParams(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'An event'], + ['id' => 2, 'title' => 'Another event'], + ]; + + $after = new DateTimeImmutable('2018-01-01 00:00:00'); + $before = new DateTimeImmutable('2018-01-31 00:00:00'); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->events(1, ['after' => $after, 'before' => $before])); + } + + /** + * @test + */ + public function shouldGetEventsWithPagination(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'An event'], + ['id' => 2, 'title' => 'Another event'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->events(1, ['page' => 2, 'per_page' => 15])); + } + + /** + * @test + */ + public function shouldGetLabels(): void + { + $expectedArray = [ + ['id' => 987, 'name' => 'bug', 'color' => '#000000'], + ['id' => 123, 'name' => 'feature', 'color' => '#ff0000'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->labels(1)); + } + + /** + * @test + */ + public function shouldAddLabel(): void + { + $expectedArray = ['name' => 'bug', 'color' => '#000000']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addLabel(1, ['name' => 'wont-fix', 'color' => '#ffffff'])); + } + + /** + * @test + */ + public function shouldUpdateLabel(): void + { + $expectedArray = ['name' => 'bug', 'color' => '#00ffff']; + + $this->setResponseBody($expectedArray); + $this->assertEquals( + $expectedArray, + $this->api->updateLabel(1, ['name' => 'bug', 'new_name' => 'big-bug', 'color' => '#00ffff']) + ); + } + + /** + * @test + */ + public function shouldRemoveLabel(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeLabel(1, 'bug')); + } + + /** + * @test + */ + public function shouldGetLanguages(): void + { + $expectedArray = ['php' => 100]; + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->languages(1)); + } + + /** + * @test + */ + public function shouldForkWithNamespace(): void + { + $expectedArray = ['namespace' => 'new_namespace']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->fork(1, ['namespace' => 'new_namespace'])); + } + + /** + * @test + */ + public function shouldForkWithNamespaceAndPath(): void + { + $expectedArray = [ + 'namespace' => 'new_namespace', + 'path' => 'new_path', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->fork(1, [ + 'namespace' => 'new_namespace', + 'path' => 'new_path', + ])); + } + + /** + * @test + */ + public function shouldForkWithNamespaceAndPathAndName(): void + { + $expectedArray = [ + 'namespace' => 'new_namespace', + 'path' => 'new_path', + 'name' => 'new_name', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->fork(1, [ + 'namespace' => 'new_namespace', + 'path' => 'new_path', + 'name' => 'new_name', + ])); + } + + /** + * @test + */ + public function shouldCreateForkRelation(): void + { + $expectedArray = ['project_id' => 1, 'forked_id' => 2]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createForkRelation(1, 2)); + } + + /** + * @test + */ + public function shouldRemoveForkRelation(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeForkRelation(2)); + } + + /** + * @test + */ + public function shouldSetService(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->setService(1, 'hipchat', ['param' => 'value'])); + } + + /** + * @test + */ + public function shouldRemoveService(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeService(1, 'hipchat')); + } + + /** + * @test + */ + public function shouldGetVariables(): void + { + $expectedArray = [ + ['key' => 'ftp_username', 'value' => 'ftp'], + ['key' => 'ftp_password', 'value' => 'somepassword'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->variables(1)); + } + + /** + * @test + */ + public function shouldGetVariable(): void + { + $expectedArray = ['key' => 'ftp_username', 'value' => 'ftp']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->variable(1, 'ftp_username')); + } + + /** + * @test + */ + public function shouldAddVariable(): void + { + $expectedKey = 'ftp_port'; + $expectedValue = '21'; + + $expectedArray = [ + 'key' => $expectedKey, + 'value' => $expectedValue, + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addVariable(1, $expectedKey, $expectedValue)); + } + + /** + * @test + */ + public function shouldAddVariableWithProtected(): void + { + $expectedArray = [ + 'key' => 'DEPLOY_SERVER', + 'value' => 'stage.example.com', + 'protected' => true, + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true)); + } + + /** + * @test + */ + public function shouldAddVariableWithEnvironment(): void + { + $expectedArray = [ + 'key' => 'DEPLOY_SERVER', + 'value' => 'stage.example.com', + 'environment_scope' => 'staging', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals( + $expectedArray, + $this->api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging') + ); + } + + /** + * @test + */ + public function shouldAddVariableWithProtectionAndEnvironment(): void + { + $expectedArray = [ + 'key' => 'DEPLOY_SERVER', + 'value' => 'stage.example.com', + 'protected' => true, + 'environment_scope' => 'staging', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals( + $expectedArray, + $this->api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging') + ); + } + + /** + * @test + */ + public function shouldUpdateVariable(): void + { + $expectedKey = 'ftp_port'; + $expectedValue = '22'; + + $expectedArray = [ + 'key' => 'ftp_port', + 'value' => '22', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateVariable(1, $expectedKey, $expectedValue)); + } + + /** + * @test + */ + public function shouldUpdateVariableWithProtected(): void + { + $expectedArray = [ + 'key' => 'DEPLOY_SERVER', + 'value' => 'stage.example.com', + 'protected' => true, + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true)); + } + + /** + * @test + */ + public function shouldUpdateVariableWithEnvironment(): void + { + $expectedArray = [ + 'key' => 'DEPLOY_SERVER', + 'value' => 'stage.example.com', + 'environment_scope' => 'staging', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals( + $expectedArray, + $this->api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging') + ); + } + + /** + * @test + */ + public function shouldUpdateVariableWithProtectedAndEnvironment(): void + { + $expectedArray = [ + 'key' => 'DEPLOY_SERVER', + 'value' => 'stage.example.com', + 'protected' => true, + 'environment_scope' => 'staging', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals( + $expectedArray, + $this->api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging') + ); + } + + /** + * @test + */ + public function shouldRemoveVariable(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeVariable(1, 'ftp_password')); + } + + /** + * @test + */ + public function shouldGetDeployments(): void + { + $expectedArray = [ + ['id' => 1, 'sha' => '0000001'], + ['id' => 2, 'sha' => '0000002'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->deployments(1)); + } + + /** + * @test + */ + public function shouldGetDeploymentsWithPagination(): void + { + $expectedArray = [ + ['id' => 1, 'sha' => '0000001'], + ['id' => 2, 'sha' => '0000002'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->deployments(1, ['page' => 2, 'per_page' => 15])); + } + + protected function getMultipleProjectsData(): array + { + return [ + ['id' => 1, 'name' => 'A project'], + ['id' => 2, 'name' => 'Another project'], + ]; + } + + public function possibleAccessLevels(): array + { + return [ + [10], + [20], + [30], + [40], + [50], + ]; + } + + public function getBadgeExpectedArray(): array + { + return [ + [ + 'id' => 1, + 'link_url' => 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}', + 'image_url' => 'https://shields.io/my/badge', + 'rendered_link_url' => 'http://example.com/ci_status.svg?project=example-org/example-project&ref=master', + 'rendered_image_url' => 'https://shields.io/my/badge', + 'kind' => 'project', + ], + [ + 'id' => 2, + 'link_url' => 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}', + 'image_url' => 'https://shields.io/my/badge', + 'rendered_link_url' => 'http://example.com/ci_status.svg?project=example-org/example-project&ref=master', + 'rendered_image_url' => 'https://shields.io/my/badge', + 'kind' => 'group', + ], + ]; + } + + /** + * @test + */ + public function shouldGetBadges(): void + { + $expectedArray = $this->getBadgeExpectedArray(); + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->badges(1)); + } + + /** + * @test + */ + public function shouldGetBadge(): void + { + $expectedBadgesArray = $this->getBadgeExpectedArray(); + $expectedArray = [$expectedBadgesArray[0]]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->badge(1, 1)); + } + + /** + * @test + */ + public function shouldAddBadge(): void + { + $link_url = 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}'; + $image_url = 'https://shields.io/my/badge'; + $expectedArray = [ + 'id' => 3, + 'link_url' => $link_url, + 'image_url' => $image_url, + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals( + $expectedArray, + $this->api->addBadge(1, ['link_url' => $link_url, 'image_url' => $image_url]) + ); + } + + /** + * @test + */ + public function shouldUpdateBadge(): void + { + $image_url = 'https://shields.io/my/new/badge'; + $expectedArray = ['id' => 2]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateBadge(1, 2, ['image_url' => $image_url])); + } + + /** + * @test + */ + public function shouldRemoveBadge(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeBadge(1, 1)); + } + + /** + * @test + */ + public function shouldAddProtectedBranch(): void + { + $expectedArray = [ + 'name' => 'master', + 'push_access_level' => [ + 'access_level' => 0, + 'access_level_description' => 'No one', + ], + 'merge_access_levels' => [ + 'access_level' => 0, + 'access_level_description' => 'Developers + Maintainers', + ], + ]; + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->addProtectedBranch(1, ['name' => 'master', 'push_access_level' => 0, 'merge_access_level' => 30])); + } + + protected function getApiClass(): string + { + return Projects::class; + } +} diff --git a/tests/unit/Api/RepositoriesTest.php b/tests/unit/Api/RepositoriesTest.php new file mode 100644 index 000000000..b4e4f4d6d --- /dev/null +++ b/tests/unit/Api/RepositoriesTest.php @@ -0,0 +1,441 @@ + 'master'], + ['name' => 'develop'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->branches(1, ['search' => '^term'])); + } + + /** + * @test + */ + public function shouldGetBranch(): void + { + $expectedArray = ['name' => 'master']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->branch(1, 'master')); + } + + /** + * @test + */ + public function shouldCreateBranch(): void + { + $expectedArray = ['name' => 'feature']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createBranch(1, 'feature', 'master')); + } + + /** + * @test + */ + public function shouldDeleteBranch(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->deleteBranch(1, 'feature/TEST-15')); + } + + /** + * @test + */ + public function shouldProtectBranch(): void + { + $expectedArray = ['name' => 'master']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->protectBranch(1, 'master')); + } + + /** + * @test + */ + public function shouldProtectBranchWithPermissions(): void + { + $expectedArray = ['name' => 'master']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->protectBranch(1, 'master', true, true)); + } + + /** + * @test + */ + public function shouldUnprotectBranch(): void + { + $expectedArray = ['name' => 'master']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->unprotectBranch(1, 'master')); + } + + /** + * @test + */ + public function shouldGetTags(): void + { + $expectedArray = [ + ['name' => '1.0'], + ['name' => '1.1'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->tags(1)); + } + + /** + * @test + */ + public function shouldCreateTag(): void + { + $expectedArray = ['name' => '1.0']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createTag(1, '1.0', 'abcd1234', '1.0 release')); + } + + /** + * @test + */ + public function shouldCreateRelease(): void + { + $project_id = 1; + $tagName = 'sometag'; + $description = '1.0 release'; + + $expectedArray = ['name' => $tagName]; + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createRelease($project_id, $tagName, $description)); + } + + /** + * @test + */ + public function shouldUpdateRelease(): void + { + $project_id = 1; + $tagName = 'sometag'; + $description = '1.0 release'; + + $expectedArray = ['description' => $tagName]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->updateRelease($project_id, $tagName, $description)); + } + + /** + * @test + */ + public function shouldGetReleases(): void + { + $project_id = 1; + + $expectedArray = [ + [ + 'tag_name' => 'v0.2', + 'description' => '## CHANGELOG\r\n\r\n- Escape label and milestone titles to prevent XSS in GFM autocomplete. !2740\r\n- Prevent private snippets from being embeddable.\r\n- Add subresources removal to member destroy service.', + 'name' => 'Awesome app v0.2 beta', + ], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->releases($project_id)); + } + + /** + * @test + */ + public function shouldGetCommits(): void + { + $expectedArray = [ + ['id' => 'abcd1234', 'title' => 'A commit'], + ['id' => 'efgh5678', 'title' => 'Another commit'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->commits(1)); + } + + /** + * @test + */ + public function shouldGetCommitsWithParams(): void + { + $expectedArray = [ + ['id' => 'abcd1234', 'title' => 'A commit'], + ['id' => 'efgh5678', 'title' => 'Another commit'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->commits(1, ['page' => 2, 'per_page' => 25, 'ref_name' => 'master', 'all' => true, 'with_stats' => true, 'path' => 'file_path/file_name'])); + } + + /** + * @test + */ + public function shouldGetCommitsWithTimeParams(): void + { + $expectedArray = [ + ['id' => 'abcd1234', 'title' => 'A commit'], + ['id' => 'efgh5678', 'title' => 'Another commit'], + ]; + + $since = new DateTimeImmutable('2018-01-01 00:00:00'); + $until = new DateTimeImmutable('2018-01-31 00:00:00'); + + $expectedWithArray = [ + 'since' => $since->format(DATE_ATOM), + 'until' => $until->format(DATE_ATOM), + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->commits(1, ['since' => $since, 'until' => $until])); + } + + /** + * @test + */ + public function shouldGetCommit(): void + { + $expectedArray = ['id' => 'abcd1234', 'title' => 'A commit']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->commit(1, 'abcd1234')); + } + + /** + * @test + */ + public function shouldGetCommitRefs(): void + { + $expectedArray = [ + ['type' => 'branch', 'name' => 'master'], + ['type' => 'tag', 'name' => 'v1.1.0'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->commitRefs(1, 'abcd1234')); + } + + /** + * @param mixed[] $expectedArray + * + * @dataProvider dataGetCommitRefsWithParams + */ + public function testShouldGetCommitRefsWithParams(string $type, array $expectedArray): void + { + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->commitRefs(1, 'abcd1234', ['type' => $type])); + } + + public function dataGetCommitRefsWithParams(): array + { + return [ + 'type_tag' => [ + 'type' => Repositories::TYPE_TAG, + 'expectedArray' => [['type' => 'tag', 'name' => 'v1.1.0']], + ], + 'type_branch' => [ + 'type' => Repositories::TYPE_BRANCH, + 'expectedArray' => [['type' => 'branch', 'name' => 'master']], + ], + ]; + } + + /** + * @test + */ + public function shouldCreateCommit(): void + { + $expectedArray = ['title' => 'Initial commit.', 'author_name' => 'John Doe', 'author_email' => 'john@example.com']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createCommit(1, [ + 'branch' => 'master', + 'commit_message' => 'Initial commit.', + 'actions' => [ + [ + 'action' => 'create', + 'file_path' => 'README.md', + 'content' => '# My new project', + ], + [ + 'action' => 'create', + 'file_path' => 'LICENSE', + 'content' => 'MIT License...', + ], + ], + 'author_name' => 'John Doe', + 'author_email' => 'john@example.com', + ])); + } + + /** + * @test + */ + public function shouldGetCommitComments(): void + { + $expectedArray = [ + ['note' => 'A commit message'], + ['note' => 'Another commit message'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->commitComments(1, 'abcd1234')); + } + + /** + * @test + */ + public function shouldCreateCommitComment(): void + { + $expectedArray = ['id' => 2, 'title' => 'A new comment']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createCommitComment(1, 'abcd1234', 'A new comment')); + } + + /** + * @test + */ + public function shouldCreateCommitCommentWithParams(): void + { + $expectedArray = ['id' => 2, 'title' => 'A new comment']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createCommitComment(1, 'abcd1234', 'A new comment', [ + 'path' => '/some/file.txt', + 'line' => 123, + 'line_type' => 'old', + ])); + } + + /** + * @test + */ + public function shouldCompareStraight(): void + { + $expectedArray = ['commit' => 'object']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->compare(1, 'master', 'feature', true)); + } + + /** + * @test + */ + public function shouldNotCompareStraight(): void + { + $expectedArray = ['commit' => 'object']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->compare(1, 'master', 'feature')); + } + + /** + * @test + */ + public function shouldGetDiff(): void + { + $expectedArray = [ + ['diff' => '--- ...'], + ['diff' => '+++ ...'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->diff(1, 'abcd1234')); + } + + /** + * @test + */ + public function shouldGetTree(): void + { + $expectedArray = [ + ['name' => 'file1.txt'], + ['name' => 'file2.csv'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->tree(1)); + } + + /** + * @test + */ + public function shouldGetTreeWithParams(): void + { + $expectedArray = [ + ['name' => 'dir/file1.txt'], + ['name' => 'dir/file2.csv'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->tree(1, ['path' => 'dir/', 'ref_name' => 'master'])); + } + + /** + * @test + */ + public function shouldGetContributors(): void + { + $expectedArray = [ + ['name' => 'Matt'], + ['name' => 'Bob'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->contributors(1)); + } + + /** + * @test + */ + public function shouldGetMergeBase(): void + { + $expectedArray = [ + 'id' => 'abcd1234abcd1234abcd1234abcd1234abcd1234', + 'short_id' => 'abcd1234', + 'title' => 'A commit', + 'created_at' => '2018-01-01T00:00:00.000Z', + 'parent_ids' => ['efgh5678efgh5678efgh5678efgh5678efgh5678'], + 'message' => 'A commit', + 'author_name' => 'Jane Doe', + 'author_email' => 'jane@example.org', + 'authored_date' => '2018-01-01T00:00:00.000Z', + 'committer_name' => 'Jane Doe', + 'committer_email' => 'jane@example.org', + 'committed_date' => '2018-01-01T00:00:00.000Z', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->mergeBase(1, ['efgh5678efgh5678efgh5678efgh5678efgh5678', '1234567812345678123456781234567812345678'])); + } + + protected function getApiClass(): string + { + return Repositories::class; + } +} diff --git a/tests/unit/Api/RepositoryFilesTest.php b/tests/unit/Api/RepositoryFilesTest.php new file mode 100644 index 000000000..6b3eaaa22 --- /dev/null +++ b/tests/unit/Api/RepositoryFilesTest.php @@ -0,0 +1,173 @@ +setResponseBody($expectedString); + $this->assertEquals($expectedString, $this->api->getRawFile(1, 'dir/file1.txt', 'abcd1234')); + } + + /** + * @test + */ + public function shouldGetFile(): void + { + $expectedArray = ['file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->getFile(1, 'dir/file1.txt', 'abcd1234')); + } + + /** + * @test + */ + public function shouldCreateFile(): void + { + $expectedArray = ['file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt']; + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createFile(1, [ + 'file_path' => 'dir/file1.txt', + 'content' => 'some contents', + 'branch' => 'master', + 'commit_message' => 'Added new file', + ])); + } + + /** + * @test + */ + public function shouldCreateFileWithEncoding(): void + { + $expectedArray = ['file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt']; + $this->setResponseBody($expectedArray); + + $this->assertEquals($expectedArray, $this->api->createFile(1, [ + 'file_path' => 'dir/file1.txt', + 'content' => 'some contents', + 'branch' => 'master', + 'commit_message' => 'Added new file', + 'encoding' => 'text', + ])); + } + + /** + * @test + */ + public function shouldCreateFileWithAuthor(): void + { + $expectedArray = ['file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt']; + $this->setResponseBody($expectedArray); + + $this->assertEquals($expectedArray, $this->api->createFile(1, [ + 'file_path' => 'dir/file1.txt', + 'content' => 'some contents', + 'branch' => 'master', + 'commit_message' => 'Added new file', + 'author_email' => 'gitlab@example.com', + 'author_name' => 'GitLab User', + ])); + } + + /** + * @test + */ + public function shouldUpdateFile(): void + { + $expectedArray = ['file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt']; + $this->setResponseBody($expectedArray); + + $this->assertEquals($expectedArray, $this->api->updateFile(1, [ + 'file_path' => 'dir/file1.txt', + 'content' => 'some new contents', + 'branch' => 'master', + 'commit_message' => 'Updated new file', + ])); + } + + /** + * @test + */ + public function shouldUpdateFileWithEncoding(): void + { + $expectedArray = ['file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt']; + $this->setResponseBody($expectedArray); + + $this->assertEquals($expectedArray, $this->api->updateFile(1, [ + 'file_path' => 'dir/file1.txt', + 'content' => 'c29tZSBuZXcgY29udGVudHM=', + 'branch' => 'master', + 'commit_message' => 'Updated file', + 'encoding' => 'base64', + ])); + } + + /** + * @test + */ + public function shouldUpdateFileWithAuthor(): void + { + $expectedArray = ['file_name' => 'file1.txt', 'file_path' => 'dir/file1.txt']; + $this->setResponseBody($expectedArray); + + $this->assertEquals($expectedArray, $this->api->updateFile(1, [ + 'file_path' => 'dir/file1.txt', + 'content' => 'some new contents', + 'branch' => 'master', + 'commit_message' => 'Updated file', + 'author_email' => 'gitlab@example.com', + 'author_name' => 'GitLab User', + ])); + } + + /** + * @test + */ + public function shouldDeleteFile(): void + { + $expectedArray = ['file_name' => 'app/project.rb', 'branch' => 'master']; + $this->setResponseBody($expectedArray); + + $this->assertEquals($expectedArray, $this->api->deleteFile(1, [ + 'file_path' => 'dir/file1.txt', + 'branch' => 'master', + 'commit_message' => 'Deleted file', + ])); + } + + /** + * @test + */ + public function shouldDeleteFileWithAuthor(): void + { + $expectedArray = ['file_name' => 'app/project.rb', 'branch' => 'master']; + $this->setResponseBody($expectedArray); + + $this->assertEquals($expectedArray, $this->api->deleteFile(1, [ + 'file_path' => 'dir/file1.txt', + 'branch' => 'master', + 'commit_message' => 'Deleted file', + 'author_email' => 'gitlab@example.com', + 'author_name' => 'GitLab User', + ])); + } + + protected function getApiClass(): string + { + return RepositoryFiles::class; + } +} diff --git a/tests/unit/Api/SchedulesTest.php b/tests/unit/Api/SchedulesTest.php new file mode 100644 index 000000000..68b96b4e3 --- /dev/null +++ b/tests/unit/Api/SchedulesTest.php @@ -0,0 +1,93 @@ + 13, + 'description' => 'Test schedule pipeline', + 'ref' => 'master', + 'cron' => '* * * * *', + 'cron_timezone' => 'Asia/Tokyo', + 'next_run_at' => '2017-05-19T13:41:00.000Z', + 'active' => true, + 'created_at' => '2017-05-19T13:31:08.849Z', + 'updated_at' => '2017-05-19T13:40:17.727Z', + ]; + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create( + 1, + [ + 'id' => 13, + 'description' => 'Test schedule pipeline', + 'ref' => 'master', + 'cron' => '* * * * *', + 'cron_timezone' => 'Asia/Tokyo', + 'next_run_at' => '2017-05-19T13:41:00.000Z', + 'active' => true, + 'created_at' => '2017-05-19T13:31:08.849Z', + 'updated_at' => '2017-05-19T13:40:17.727Z', + ] + )); + } + + /** + * @test + */ + public function shouldShowSchedule(): void + { + $expectedArray = ['id' => 1, 'name' => 'A schedule']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 2)); + } + + /** + * @test + */ + public function shouldShowAllSchedule(): void + { + $expectedArray = ['id' => 1, 'name' => 'A schedule']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->showAll(1)); + } + + /** + * @test + */ + public function shouldUpdateSchedule(): void + { + $expectedArray = ['id' => 3, 'title' => 'Updated schedule']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(1, 3, ['title' => 'Updated schedule', 'due_date' => '2015-04-01', 'state_event' => 'close'])); + } + + /** + * @test + */ + public function shouldRemoveSchedule(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(1, 2)); + } + + protected function getApiClass(): string + { + return Schedules::class; + } +} diff --git a/tests/unit/Api/SnippetsTest.php b/tests/unit/Api/SnippetsTest.php new file mode 100644 index 000000000..89015c8b6 --- /dev/null +++ b/tests/unit/Api/SnippetsTest.php @@ -0,0 +1,99 @@ + 1, 'title' => 'A snippet'], + ['id' => 2, 'title' => 'Another snippet'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1)); + } + + /** + * @test + */ + public function shouldShowSnippet(): void + { + $expectedArray = ['id' => 2, 'title' => 'Another snippet']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 2)); + } + + /** + * @test + */ + public function shouldCreateSnippet(): void + { + $expectedArray = ['id' => 3, 'title' => 'A new snippet']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, 'A new snippet', 'file.txt', 'A file', 'public')); + } + + /** + * @test + */ + public function shouldUpdateSnippet(): void + { + $expectedArray = ['id' => 3, 'title' => 'Updated snippet']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(1, 3, ['file_name' => 'new_file.txt', 'code' => 'New content', 'title' => 'Updated snippet'])); + } + + /** + * @test + */ + public function shouldShowContent(): void + { + $expectedString = 'New content'; + + $this->setResponseBody($expectedString); + $this->assertEquals($expectedString, $this->api->content(1, 3)); + } + + /** + * @test + */ + public function shouldRemoveSnippet(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(1, 3)); + } + + /** + * @test + */ + public function shouldGetSnippetAwardEmoji(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'sparkles'], + ['id' => 2, 'name' => 'heart_eyes'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->awardEmoji(1, 2)); + } + + protected function getApiClass(): string + { + return Snippets::class; + } +} diff --git a/tests/unit/Api/SystemHooksTest.php b/tests/unit/Api/SystemHooksTest.php new file mode 100644 index 000000000..f7b6611e2 --- /dev/null +++ b/tests/unit/Api/SystemHooksTest.php @@ -0,0 +1,61 @@ + 1, 'url' => 'http://www.example.com'], + ['id' => 2, 'url' => 'http://www.example.org'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all()); + } + + /** + * @test + */ + public function shouldCreateHook(): void + { + $expectedArray = ['id' => 3, 'url' => 'http://www.example.net']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create('http://www.example.net')); + } + + /** + * @test + */ + public function shouldTestHook(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->test(3)); + } + + /** + * @test + */ + public function shouldRemoveHook(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(3)); + } + + protected function getApiClass(): string + { + return SystemHooks::class; + } +} diff --git a/tests/unit/Api/TagsTest.php b/tests/unit/Api/TagsTest.php new file mode 100644 index 000000000..400df1cd4 --- /dev/null +++ b/tests/unit/Api/TagsTest.php @@ -0,0 +1,126 @@ + 'v1.0.0'], + ['name' => 'v1.1.0'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(1)); + } + + /** + * @test + */ + public function shouldShowTag(): void + { + $expectedArray = [ + ['name' => 'v1.0.0'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1, 'v1.0.0')); + } + + /** + * @test + */ + public function shouldCreateTag(): void + { + $expectedArray = [ + ['name' => 'v1.1.0'], + ]; + + $params = [ + 'id' => 1, + 'tag_name' => 'v1.1.0', + 'ref' => 'ref/heads/master', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create(1, $params)); + } + + /** + * @test + */ + public function shouldRemoveTag(): void + { + $expectedArray = [ + ['name' => 'v1.1.0'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->remove(1, 'v1.1.0')); + } + + /** + * @param mixed[] $expectedResult + * + * @test + * @dataProvider releaseDataProvider + */ + public function shouldCreateRelease(string $releaseName, string $description, array $expectedResult): void + { + $params = ['description' => $description]; + + $this->setResponseBody($expectedResult); + $this->assertEquals($expectedResult, $this->api->createRelease(1, $releaseName, $params)); + } + + /** + * @param mixed[] $expectedResult + * + * @dataProvider releaseDataProvider + */ + public function testShouldUpdateRelease(string $releaseName, string $description, array $expectedResult): void + { + $params = ['description' => $description]; + + $this->setResponseBody($expectedResult); + $this->assertEquals($expectedResult, $this->api->updateRelease(1, $releaseName, $params)); + } + + public function releaseDataProvider(): array + { + return [ + [ + 'tagName' => 'v1.1.0', + 'description' => 'Amazing release. Wow', + 'expectedResult' => [ + 'tag_name' => '1.0.0', + 'description' => 'Amazing release. Wow', + ], + ], + [ + 'tagName' => 'version/1.1.0', + 'description' => 'Amazing release. Wow', + 'expectedResult' => [ + 'tag_name' => 'version/1.1.0', + 'description' => 'Amazing release. Wow', + ], + ], + ]; + } + + protected function getApiClass(): string + { + return Tags::class; + } +} diff --git a/tests/unit/Api/TestCase.php b/tests/unit/Api/TestCase.php new file mode 100644 index 000000000..2afb419a0 --- /dev/null +++ b/tests/unit/Api/TestCase.php @@ -0,0 +1,29 @@ +api = $this->getApiInstance(); + } + + protected function getApiInstance(): Api + { + $apiClass = $this->getApiClass(); + + return new $apiClass($this->client); + } +} diff --git a/tests/unit/Api/UsersTest.php b/tests/unit/Api/UsersTest.php new file mode 100644 index 000000000..94f0b46c8 --- /dev/null +++ b/tests/unit/Api/UsersTest.php @@ -0,0 +1,406 @@ + 1, 'name' => 'Matt'], + ['id' => 2, 'name' => 'John'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all()); + } + + /** + * @test + */ + public function shouldGetActiveUsers(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Matt'], + ['id' => 2, 'name' => 'John'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->all(['active' => true])); + } + + /** + * @test + */ + public function shouldGetUsersWithDateTimeParams(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'Matt'], + ['id' => 2, 'name' => 'John'], + ]; + + $createdAfter = new DateTimeImmutable('2018-01-01 00:00:00'); + $createdBefore = new DateTimeImmutable('2018-01-31 00:00:00'); + + $this->setResponseBody($expectedArray); + $this->assertEquals( + $expectedArray, + $this->api->all(['created_after' => $createdAfter, 'created_before' => $createdBefore]) + ); + } + + /** + * @test + */ + public function shouldShowUser(): void + { + $expectedArray = ['id' => 1, 'name' => 'Matt']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show(1)); + } + + /** + * @test + */ + public function shouldShowUsersProjects(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'matt-project-1'], + ['id' => 2, 'name' => 'matt-project-2'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->usersProjects(1)); + } + + /** + * @test + */ + public function shouldCreateUser(): void + { + $expectedArray = ['id' => 3, 'name' => 'Billy']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create('billy@example.com', 'password')); + } + + /** + * @test + */ + public function shouldCreateUserWithAdditionalInfo(): void + { + $expectedArray = ['id' => 3, 'name' => 'Billy']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->create('billy@example.com', 'password', ['name' => 'Billy', 'bio' => 'A person'])); + } + + /** + * @test + */ + public function shouldUpdateUser(): void + { + $expectedArray = ['id' => 3, 'name' => 'Billy Bob']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->update(3, ['name' => 'Billy Bob'])); + } + + /** + * @test + */ + public function shouldRemoveUser(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->remove(1)); + } + + /** + * @test + */ + public function shouldBlockUser(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->block(1)); + } + + /** + * @test + */ + public function shouldUnblockUser(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->unblock(1)); + } + + /** + * @test + */ + public function shouldShowCurrentUser(): void + { + $expectedArray = ['id' => 1, 'name' => 'Matt']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->me()); + } + + /** + * @test + */ + public function shouldGetCurrentUserKeys(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'A key'], + ['id' => 2, 'name' => 'Another key'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->keys()); + } + + /** + * @test + */ + public function shouldGetCurrentUserKey(): void + { + $expectedArray = ['id' => 1, 'title' => 'A key']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->key(1)); + } + + /** + * @test + */ + public function shouldCreateKeyForCurrentUser(): void + { + $expectedArray = ['id' => 3, 'title' => 'A new key']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createKey('A new key', '...')); + } + + /** + * @test + */ + public function shouldDeleteKeyForCurrentUser(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeKey(3)); + } + + /** + * @test + */ + public function shouldGetUserKeys(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'A key'], + ['id' => 2, 'name' => 'Another key'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->userKeys(1)); + } + + /** + * @test + */ + public function shouldGetUserKey(): void + { + $expectedArray = ['id' => 2, 'title' => 'Another key']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->userKey(1, 2)); + } + + /** + * @test + */ + public function shouldCreateKeyForUser(): void + { + $expectedArray = ['id' => 3, 'title' => 'A new key']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createKeyForUser(1, 'A new key', '...')); + } + + /** + * @test + */ + public function shouldDeleteKeyForUser(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeUserKey(1, 3)); + } + + /** + * @test + */ + public function shouldAttemptLogin(): void + { + $expectedArray = ['id' => 1, 'name' => 'Matt']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->session('matt', 'password')); + $this->assertEquals($expectedArray, $this->api->login('matt', 'password')); + } + + /** + * @test + */ + public function shouldGetUserEmails(): void + { + $expectedArray = [ + ['id' => 1, 'email' => 'foo@bar.baz'], + ['id' => 2, 'email' => 'foo@bar.qux'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->emails()); + } + + /** + * @test + */ + public function shouldGetSpecificUserEmail(): void + { + $expectedArray = ['id' => 1, 'email' => 'foo@bar.baz']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->email(1)); + } + + /** + * @test + */ + public function shouldGetEmailsForUser(): void + { + $expectedArray = [ + ['id' => 1, 'email' => 'foo@bar.baz'], + ['id' => 2, 'email' => 'foo@bar.qux'], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->userEmails(1)); + } + + /** + * @test + */ + public function shouldCreateEmailForUser(): void + { + $expectedArray = ['id' => 3, 'email' => 'foo@bar.example']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createEmailForUser(1, 'foo@bar.example')); + } + + /** + * @test + */ + public function shouldCreateConfirmedEmailForUser(): void + { + $expectedArray = ['id' => 4, 'email' => 'foo@baz.example']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createEmailForUser(1, 'foo@baz.example', true)); + } + + /** + * @test + */ + public function shouldDeleteEmailForUser(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeUserEmail(1, 3)); + } + + /** + * @test + */ + public function shouldGetCurrentUserImpersonationTokens(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A Name', 'revoked' => false], + ['id' => 2, 'name' => 'A Name', 'revoked' => false], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->userImpersonationTokens(1)); + } + + /** + * @test + */ + public function shouldGetUserImpersonationToken(): void + { + $expectedArray = ['id' => 2, 'name' => 'name']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->userImpersonationToken(1, 1)); + } + + /** + * @test + */ + public function shouldCreateImpersonationTokenForUser(): void + { + $expectedArray = ['id' => 1, 'name' => 'name']; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->createImpersonationToken(1, 'name', ['api'])); + } + + /** + * @test + */ + public function shouldDeleteImpersonationTokenForUser(): void + { + $this->setResponseBody(true); + $this->assertTrue($this->api->removeImpersonationToken(1, 1)); + } + + /** + * @test + */ + public function shouldGetCurrentUserActiveImpersonationTokens(): void + { + $expectedArray = [ + ['id' => 1, 'name' => 'A Name', 'revoked' => true], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->userImpersonationTokens(1, ['state' => 'active'])); + } + + /** + * @test + */ + public function shouldGetCurrentUserInactiveImpersonationTokens(): void + { + $expectedArray = [ + ['id' => 2, 'name' => 'A Name', 'revoked' => false], + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->userImpersonationTokens(1, ['state' => 'inactive'])); + } + + protected function getApiClass(): string + { + return Users::class; + } +} diff --git a/tests/unit/Api/VersionTest.php b/tests/unit/Api/VersionTest.php new file mode 100644 index 000000000..c5fc80724 --- /dev/null +++ b/tests/unit/Api/VersionTest.php @@ -0,0 +1,32 @@ + '8.13.0-pre', + 'revision' => '4e963fe', + ]; + + $this->setResponseBody($expectedArray); + $this->assertEquals($expectedArray, $this->api->show()); + } + + protected function getApiClass(): string + { + return Version::class; + } +} diff --git a/test/Gitlab/Tests/HttpClient/BuilderTest.php b/tests/unit/HttpClient/BuilderTest.php similarity index 80% rename from test/Gitlab/Tests/HttpClient/BuilderTest.php rename to tests/unit/HttpClient/BuilderTest.php index 7fdeea5f1..7e74a4507 100644 --- a/test/Gitlab/Tests/HttpClient/BuilderTest.php +++ b/tests/unit/HttpClient/BuilderTest.php @@ -1,5 +1,7 @@ - */ -class BuilderTest extends TestCase +final class BuilderTest extends TestCase { - /** - * @var Builder - */ + /** @var Builder */ private $subject; - public function setUp() + public function setUp(): void { $this->subject = new Builder( $this->getMockBuilder(HttpClient::class)->getMock(), @@ -29,7 +26,7 @@ public function setUp() ); } - public function testAddPluginShouldInvalidateHttpClient() + public function testAddPluginShouldInvalidateHttpClient(): void { $client = $this->subject->getHttpClient(); @@ -38,7 +35,7 @@ public function testAddPluginShouldInvalidateHttpClient() $this->assertNotSame($client, $this->subject->getHttpClient()); } - public function testRemovePluginShouldInvalidateHttpClient() + public function testRemovePluginShouldInvalidateHttpClient(): void { $this->subject->addPlugin($this->getMockBuilder(Plugin::class)->getMock()); @@ -49,7 +46,7 @@ public function testRemovePluginShouldInvalidateHttpClient() $this->assertNotSame($client, $this->subject->getHttpClient()); } - public function testHttpClientShouldBeAnHttpMethodsClient() + public function testHttpClientShouldBeAnHttpMethodsClient(): void { $this->assertInstanceOf(HttpMethodsClient::class, $this->subject->getHttpClient()); } diff --git a/test/Gitlab/Tests/HttpClient/Message/QueryStringBuilderTest.php b/tests/unit/HttpClient/Message/QueryStringBuilderTest.php similarity index 78% rename from test/Gitlab/Tests/HttpClient/Message/QueryStringBuilderTest.php rename to tests/unit/HttpClient/Message/QueryStringBuilderTest.php index b052147dc..5f96f1335 100644 --- a/test/Gitlab/Tests/HttpClient/Message/QueryStringBuilderTest.php +++ b/tests/unit/HttpClient/Message/QueryStringBuilderTest.php @@ -1,25 +1,28 @@ assertEquals($expected, QueryStringBuilder::build($query)); } - public function queryStringProvider() + /** + * @return mixed[] + */ + public function queryStringProvider(): iterable { //Scalar value. yield [ @@ -38,7 +41,7 @@ public function queryStringProvider() yield [ ['iids' => [0 => 88, 2 => 86]], //iids[0]=88&iids[2]=86 - 'iids%5B0%5D=88&iids%5B2%5D=86' + 'iids%5B0%5D=88&iids%5B2%5D=86', ]; yield [ @@ -48,15 +51,15 @@ public function queryStringProvider() 'title' => 'test', 'assignee_id' => null, 'target_project_id' => null, - 'description' => null + 'description' => null, ], - 'source_branch=test_source&target_branch=test_master&title=test' + 'source_branch=test_source&target_branch=test_master&title=test', ]; //Boolean encoding yield [ - ['push_events' => false, 'merge_requests_events' => 1], - 'push_events=0&merge_requests_events=1' + ['push_events' => 0, 'merge_requests_events' => 1], + 'push_events=0&merge_requests_events=1', ]; //A deeply nested array. @@ -74,9 +77,7 @@ public function queryStringProvider() ], 'nested' => [ 'a' => [ - [ - 'b' => 'c', - ], + ['b' => 'c'], [ 'd' => 'e', 'f' => [ @@ -95,14 +96,13 @@ public function queryStringProvider() //&nested[a][][b]=c&nested[a][][d]=e //&nested[a][][f][g]=h&nested[a][][f][i]=j //&nested[a][][f][k][]=87&nested[a][][f][k][]=89 - 'search=a%20project'. - '&owned=true'. - '&iids%5B%5D=88&iids%5B%5D=86'. - '&assoc%5Ba%5D=b&assoc%5Bc%5D%5Bd%5D=e&assoc%5Bc%5D%5Bf%5D=g'. - '&nested%5Ba%5D%5B%5D%5Bb%5D=c&nested%5Ba%5D%5B%5D%5Bd%5D=e'. - '&nested%5Ba%5D%5B%5D%5Bf%5D%5Bg%5D=h&nested%5Ba%5D%5B%5D%5Bf%5D%5Bi%5D=j'. - '&nested%5Ba%5D%5B%5D%5Bf%5D%5Bk%5D%5B%5D=87&nested%5Ba%5D%5B%5D%5Bf%5D%5Bk%5D%5B%5D=89' - , + 'search=a%20project' . + '&owned=true' . + '&iids%5B%5D=88&iids%5B%5D=86' . + '&assoc%5Ba%5D=b&assoc%5Bc%5D%5Bd%5D=e&assoc%5Bc%5D%5Bf%5D=g' . + '&nested%5Ba%5D%5B%5D%5Bb%5D=c&nested%5Ba%5D%5B%5D%5Bd%5D=e' . + '&nested%5Ba%5D%5B%5D%5Bf%5D%5Bg%5D=h&nested%5Ba%5D%5B%5D%5Bf%5D%5Bi%5D=j' . + '&nested%5Ba%5D%5B%5D%5Bf%5D%5Bk%5D%5B%5D=87&nested%5Ba%5D%5B%5D%5Bf%5D%5Bk%5D%5B%5D=89', ]; } } diff --git a/test/Gitlab/Tests/HttpClient/Message/ResponseMediatorTest.php b/tests/unit/HttpClient/Message/ResponseMediatorTest.php similarity index 58% rename from test/Gitlab/Tests/HttpClient/Message/ResponseMediatorTest.php rename to tests/unit/HttpClient/Message/ResponseMediatorTest.php index 92b7cc1f7..37420cebf 100644 --- a/test/Gitlab/Tests/HttpClient/Message/ResponseMediatorTest.php +++ b/tests/unit/HttpClient/Message/ResponseMediatorTest.php @@ -1,23 +1,24 @@ - */ -class ResponseMediatorTest extends TestCase +final class ResponseMediatorTest extends TestCase { - public function testGetContent() + public function testGetContent(): void { - $body = array('foo' => 'bar'); + $body = ['foo' => 'bar']; $response = new Response( 200, - array('Content-Type'=>'application/json'), - \GuzzleHttp\Psr7\stream_for(json_encode($body)) + ['Content-Type' => 'application/json'], + stream_for(json_encode($body)) ); $this->assertEquals($body, ResponseMediator::getContent($response)); @@ -26,13 +27,13 @@ public function testGetContent() /** * If content-type is not json we should get the raw body. */ - public function testGetContentNotJson() + public function testGetContentNotJson(): void { - $body = 'foobar'; + $body = 'foobar'; $response = new Response( 200, - array(), - \GuzzleHttp\Psr7\stream_for($body) + [], + stream_for($body) ); $this->assertEquals($body, ResponseMediator::getContent($response)); @@ -41,19 +42,19 @@ public function testGetContentNotJson() /** * Make sure we return the body if we have invalid json */ - public function testGetContentInvalidJson() + public function testGetContentInvalidJson(): void { - $body = 'foobar'; + $body = 'foobar'; $response = new Response( 200, - array('Content-Type'=>'application/json'), - \GuzzleHttp\Psr7\stream_for($body) + ['Content-Type' => 'application/json'], + stream_for($body) ); $this->assertEquals($body, ResponseMediator::getContent($response)); } - public function testGetPagination() + public function testGetPagination(): void { $header = <<; rel="first", @@ -62,16 +63,16 @@ public function testGetPagination() ; rel="last", TEXT; - $pagination = array( + $pagination = [ 'first' => 'https://example.gitlab.com', 'next' => 'https://example.gitlab.com', 'prev' => 'https://example.gitlab.com', - 'last' => 'https://example.gitlab.com' - ); + 'last' => 'https://example.gitlab.com', + ]; // response mock - $response = new Response(200, array('link'=>$header)); - $result = ResponseMediator::getPagination($response); + $response = new Response(200, ['link' => $header]); + $result = ResponseMediator::getPagination($response); $this->assertEquals($pagination, $result); } diff --git a/test/Gitlab/Tests/HttpClient/Plugin/ApiVersionTest.php b/tests/unit/HttpClient/Plugin/ApiVersionTest.php similarity index 52% rename from test/Gitlab/Tests/HttpClient/Plugin/ApiVersionTest.php rename to tests/unit/HttpClient/Plugin/ApiVersionTest.php index d692d5d82..de840e13a 100644 --- a/test/Gitlab/Tests/HttpClient/Plugin/ApiVersionTest.php +++ b/tests/unit/HttpClient/Plugin/ApiVersionTest.php @@ -1,74 +1,71 @@ getMockBuilder(\stdClass::class) + $callback = $this->getMockBuilder(stdClass::class) ->setMethods(['next']) - ->getMock() - ; + ->getMock(); $callback->expects($this->once()) ->method('next') ->with($this->isInstanceOf(RequestInterface::class)) - ->willReturn($promise) - ; + ->willReturn($promise); - $this->assertEquals($promise, $plugin->handleRequest($request, [$callback, 'next'], function () { + $this->assertEquals($promise, $plugin->handleRequest($request, [$callback, 'next'], static function (): void { })); } - public function testPrefixRequestPath() + public function testPrefixRequestPath(): void { - $request = new Request('GET', 'projects'); + $request = new Request('GET', 'projects'); $expected = new Request('GET', '/api/v4/projects'); - $plugin = new ApiVersion(); - $promise = new HttpFulfilledPromise(new Response()); + $plugin = new ApiVersion(); + $promise = new HttpFulfilledPromise(new Response()); - $callback = $this->getMockBuilder(\stdClass::class) + $callback = $this->getMockBuilder(stdClass::class) ->setMethods(['next']) - ->getMock() - ; + ->getMock(); $callback->expects($this->once()) ->method('next') ->with($expected) - ->willReturn($promise) - ; + ->willReturn($promise); - $plugin->handleRequest($request, [$callback, 'next'], function () { + $plugin->handleRequest($request, [$callback, 'next'], static function (): void { }); } - public function testNoPrefixingRequired() + public function testNoPrefixingRequired(): void { $request = new Request('GET', '/api/v4/projects'); - $plugin = new ApiVersion(); + $plugin = new ApiVersion(); $promise = new HttpFulfilledPromise(new Response()); - $callback = $this->getMockBuilder(\stdClass::class) + $callback = $this->getMockBuilder(stdClass::class) ->setMethods(['next']) - ->getMock() - ; + ->getMock(); $callback->expects($this->once()) ->method('next') ->with($request) - ->willReturn($promise) - ; + ->willReturn($promise); - $plugin->handleRequest($request, [$callback, 'next'], function () { + $plugin->handleRequest($request, [$callback, 'next'], static function (): void { }); } } diff --git a/tests/unit/MockApi.php b/tests/unit/MockApi.php new file mode 100644 index 000000000..509af6644 --- /dev/null +++ b/tests/unit/MockApi.php @@ -0,0 +1,26 @@ +all; + } +} diff --git a/test/Gitlab/Tests/Model/GroupMilestoneTest.php b/tests/unit/Model/GroupMilestoneTest.php similarity index 60% rename from test/Gitlab/Tests/Model/GroupMilestoneTest.php rename to tests/unit/Model/GroupMilestoneTest.php index 1919b66ae..564d0660a 100644 --- a/test/Gitlab/Tests/Model/GroupMilestoneTest.php +++ b/tests/unit/Model/GroupMilestoneTest.php @@ -1,40 +1,29 @@ getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock(); - - $group = $this->getMockBuilder(Group::class) - ->disableOriginalConstructor() - ->getMock(); + $group = new Group(1); - $groupMilestone = new GroupMilestone($group, 1, $client); + $groupMilestone = new GroupMilestone($group, 1, $this->client); $this->assertSame(1, $groupMilestone->id); $this->assertSame($group, $groupMilestone->group); - $this->assertSame($client, $groupMilestone->getClient()); + $this->assertSame($this->client, $groupMilestone->getClient()); } - public function testFromArray() + public function testFromArray(): void { - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock(); - - $group = $this->getMockBuilder(Group::class) - ->disableOriginalConstructor() - ->getMock(); + $group = new Group(1); $data = [ 'id' => 1, @@ -46,10 +35,10 @@ public function testFromArray() 'created_at' => '2019-04-30T23:59:59.000Z', 'updated_at' => '2019-04-30T23:59:59.000Z', 'due_date' => '2019-05-10', - 'start_date' => '2019-05-03' + 'start_date' => '2019-05-03', ]; - $groupMilestone = GroupMilestone::fromArray($client, $group, $data); + $groupMilestone = GroupMilestone::fromArray($this->client, $group, $data); $this->assertInstanceOf(GroupMilestone::class, $groupMilestone); $this->assertSame($data['id'], $groupMilestone->id); diff --git a/test/Gitlab/Tests/Model/GroupTest.php b/tests/unit/Model/GroupTest.php similarity index 72% rename from test/Gitlab/Tests/Model/GroupTest.php rename to tests/unit/Model/GroupTest.php index e38a639a2..b756f8a32 100644 --- a/test/Gitlab/Tests/Model/GroupTest.php +++ b/tests/unit/Model/GroupTest.php @@ -1,21 +1,18 @@ getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock(); - $data = [ 'id' => 1, 'name' => 'Foobar Group', @@ -38,7 +35,7 @@ public function testFromArray() ], ]; - $group = Group::fromArray($client, $data); + $group = Group::fromArray($this->client, $data); $this->assertInstanceOf(Group::class, $group); $this->assertSame($data['id'], $group->id); @@ -62,33 +59,22 @@ public function testFromArray() $this->assertInstanceOf(Project::class, $group->shared_projects[0]); } - public function testProjects() + public function testProjects(): void { - $group_data = [ + $group_data = [ 'id' => 1, 'name' => 'Grouped', 'path' => '', - 'description' => 'Amazing group. Wow' + 'description' => 'Amazing group. Wow', ]; $project_data = [ 'id' => 1, - 'name' => 'A Project' + 'name' => 'A Project', ]; - //Mock API methods - $client = $this->getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock() - ; - $groups = $this->getMockBuilder(Groups::class) - ->disableOriginalConstructor() - ->getMock() - ; - $client->method('groups')->willReturn($groups); - $groups->method('projects')->willReturn([$project_data]); + $this->setResponseBody([$project_data]); - //Create model objects - $group = Group::fromArray($client, $group_data); + $group = Group::fromArray($this->client, $group_data); $projects = $group->projects(); $this->assertSame(1, count($projects)); diff --git a/tests/unit/Model/IssueLinkTest.php b/tests/unit/Model/IssueLinkTest.php new file mode 100644 index 000000000..0bb02993c --- /dev/null +++ b/tests/unit/Model/IssueLinkTest.php @@ -0,0 +1,37 @@ +client); + + $this->assertSame(1, $issueLink->issue_link_id); + $this->assertSame($issue, $issueLink->issue); + $this->assertSame($this->client, $issueLink->getClient()); + } + + public function testFromArray(): void + { + $issueLink = IssueLink::fromArray($this->client, new Project(), ['issue_link_id' => 1, 'iid' => 10]); + + $this->assertSame(1, $issueLink->issue_link_id); + $this->assertInstanceOf(Issue::class, $issueLink->issue); + $this->assertSame(10, $issueLink->issue->iid); + $this->assertSame($this->client, $issueLink->getClient()); + } +} diff --git a/tests/unit/Model/IssueTest.php b/tests/unit/Model/IssueTest.php new file mode 100644 index 000000000..de232af27 --- /dev/null +++ b/tests/unit/Model/IssueTest.php @@ -0,0 +1,200 @@ +assertSame($project, $sUT->project); + $this->assertNull($sUT->iid); + $this->assertNull($sUT->getClient()); + } + + public function testCorrectConstructWithoutClient(): void + { + $project = new Project(); + + $sUT = new Issue($project, 10); + + $this->assertSame($project, $sUT->project); + $this->assertSame(10, $sUT->iid); + $this->assertNull($sUT->getClient()); + } + + public function testCorrectConstruct(): void + { + $project = new Project(); + + $sUT = new Issue($project, 10, $this->client); + + $this->assertSame($project, $sUT->project); + $this->assertSame(10, $sUT->iid); + $this->assertSame($this->client, $sUT->getClient()); + } + + public function testFromArray(): void + { + $project = new Project(); + + $sUT = Issue::fromArray($this->client, $project, ['iid' => 10]); + + $this->assertSame($project, $sUT->project); + $this->assertSame(10, $sUT->iid); + $this->assertSame($this->client, $sUT->getClient()); + } + + /** + * @param mixed[] $data + */ + public function getIssueMock(array $data = []): Issue + { + $client = new Client(); + $project = new Project(1, $client); + + return Issue::fromArray($client, $project, $data); + } + + public function testIsClosed(): void + { + $opened_data = [ + 'iid' => 1, + 'state' => 'opened', + ]; + $opened_issue = $this->getIssueMock($opened_data); + + $this->assertFalse($opened_issue->isClosed()); + + $closed_data = [ + 'iid' => 1, + 'state' => 'closed', + ]; + $closed_issue = $this->getIssueMock($closed_data); + + $this->assertTrue($closed_issue->isClosed()); + } + + public function testHasLabel(): void + { + $data = [ + 'iid' => 1, + 'labels' => ['foo', 'bar'], + ]; + $issue = $this->getIssueMock($data); + + $this->assertTrue($issue->hasLabel('foo')); + $this->assertTrue($issue->hasLabel('bar')); + $this->assertFalse($issue->hasLabel('')); + } + + public function testMove(): void + { + $project = new Project(1); + $toProject = new Project(2); + + $this->setResponseBody(['iid' => 11]); + + $issue = Issue::fromArray($this->client, $project, ['iid' => 10])->move($toProject); + + $this->assertInstanceOf(Issue::class, $issue); + $this->assertSame($this->client, $issue->getClient()); + $this->assertSame($toProject, $issue->project); + $this->assertSame(11, $issue->iid); + } + + public function testLinks(): void + { + $this->httpClient->on( + new RequestMatcher('issues/'), + $this->createResponse([ + ['issue_link_id' => 100, 'iid' => 10, 'project_id' => 1], + ['issue_link_id' => 200, 'iid' => 20, 'project_id' => 2], + ]) + ); + + $this->httpClient->on( + new RequestMatcher('projects/'), + $this->createResponse(['id' => 1]) + ); + + $issue = new Issue(new Project(1, $this->client), 10, $this->client); + $issueLinks = $issue->links(); + + $this->assertIsArray($issueLinks); + $this->assertCount(2, $issueLinks); + + $this->assertInstanceOf(IssueLink::class, $issueLinks[0]); + $this->assertSame(100, $issueLinks[0]->issue_link_id); + $this->assertInstanceOf(Issue::class, $issueLinks[0]->issue); + $this->assertSame(10, $issueLinks[0]->issue->iid); + $this->assertInstanceOf(Project::class, $issueLinks[0]->issue->project); + $this->assertSame(1, $issueLinks[0]->issue->project->id); + + $this->assertInstanceOf(IssueLink::class, $issueLinks[1]); + $this->assertSame(200, $issueLinks[1]->issue_link_id); + $this->assertInstanceOf(Issue::class, $issueLinks[1]->issue); + $this->assertSame(20, $issueLinks[1]->issue->iid); + $this->assertInstanceOf(Project::class, $issueLinks[1]->issue->project); + $this->assertSame(1, $issueLinks[1]->issue->project->id); + } + + public function testAddLink(): void + { + $this->setResponseBody([ + 'source_issue' => ['iid' => 10, 'project_id' => 1], + 'target_issue' => ['iid' => 20, 'project_id' => 2], + ]); + $issue = new Issue(new Project(1, $this->client), 10, $this->client); + $issueLinks = $issue->addLink(new Issue(new Project(2, $this->client), 20, $this->client)); + + $this->assertIsArray($issueLinks); + $this->assertCount(2, $issueLinks); + + $this->assertInstanceOf(Issue::class, $issueLinks['source_issue']); + $this->assertSame(10, $issueLinks['source_issue']->iid); + $this->assertInstanceOf(Project::class, $issueLinks['source_issue']->project); + $this->assertSame(1, $issueLinks['source_issue']->project->id); + + $this->assertInstanceOf(Issue::class, $issueLinks['target_issue']); + $this->assertSame(20, $issueLinks['target_issue']->iid); + $this->assertInstanceOf(Project::class, $issueLinks['target_issue']->project); + $this->assertSame(2, $issueLinks['target_issue']->project->id); + } + + public function testRemoveLink(): void + { + $this->setResponseBody([ + 'id' => 2, + 'source_issue' => ['iid' => 10, 'project_id' => 1], + 'target_issue' => ['iid' => 20, 'project_id' => 2], + ]); + + $issue = new Issue(new Project(1, $this->client), 10, $this->client); + $issueLinks = $issue->removeLink(100); + + $this->assertIsArray($issueLinks); + $this->assertCount(2, $issueLinks); + + $this->assertInstanceOf(Issue::class, $issueLinks['source_issue']); + $this->assertSame(10, $issueLinks['source_issue']->iid); + $this->assertInstanceOf(Project::class, $issueLinks['source_issue']->project); + $this->assertSame(1, $issueLinks['source_issue']->project->id); + + $this->assertInstanceOf(Issue::class, $issueLinks['target_issue']); + $this->assertSame(20, $issueLinks['target_issue']->iid); + $this->assertInstanceOf(Project::class, $issueLinks['target_issue']->project); + $this->assertSame(2, $issueLinks['target_issue']->project->id); + } +} diff --git a/tests/unit/Model/LabelTest.php b/tests/unit/Model/LabelTest.php new file mode 100644 index 000000000..7c9c589ed --- /dev/null +++ b/tests/unit/Model/LabelTest.php @@ -0,0 +1,40 @@ +assertNull($sUT->getClient()); + } + + public function testCorrectConstruct(): void + { + $project = new Project(); + $sUT = new Label($project, $this->client); + + $this->assertSame($this->client, $sUT->getClient()); + } + + public function testFromArray(): void + { + $project = new Project(); + + $sUT = Label::fromArray($this->client, $project, ['color' => '#FF0000', 'name' => 'Testing', 'id' => 123]); + + $this->assertSame('#FF0000', $sUT->color); + $this->assertSame('Testing', $sUT->name); + $this->assertSame(123, $sUT->id); + $this->assertSame($this->client, $sUT->getClient()); + } +} diff --git a/test/Gitlab/Tests/Model/ProjectTest.php b/tests/unit/Model/ProjectTest.php similarity index 91% rename from test/Gitlab/Tests/Model/ProjectTest.php rename to tests/unit/Model/ProjectTest.php index 53b3b9785..cc6602367 100644 --- a/test/Gitlab/Tests/Model/ProjectTest.php +++ b/tests/unit/Model/ProjectTest.php @@ -1,22 +1,19 @@ getMockBuilder(Client::class) - ->disableOriginalConstructor() - ->getMock(); - $data = [ 'id' => 4, 'description' => null, @@ -28,11 +25,9 @@ public function testFromArray() 'readme_url' => 'http://example.com/diaspora/diaspora-client/blob/master/README.md', 'tag_list' => [ 'example', - 'disapora client' - ], - 'owner' => [ - 'id' => 3, + 'disapora client', ], + 'owner' => ['id' => 3], 'name' => 'Diaspora Client', 'name_with_namespace' => 'Diaspora / Diaspora Client', 'path' => 'diaspora-client', @@ -48,9 +43,7 @@ public function testFromArray() 'created_at' => '2013-09-30T13:46:02Z', 'last_activity_at' => '2013-09-30T13:46:02Z', 'creator_id' => 3, - 'namespace' => [ - 'id' => 3, - ], + 'namespace' => ['id' => 3], 'import_status' => 'none', 'archived' => false, 'avatar_url' => 'http://example.com/uploads/project/avatar/4/uploads/avatar.png', @@ -60,7 +53,7 @@ public function testFromArray() 'runners_token' => 'b8547b1dc37721d05889db52fa2f02', 'public_jobs' => true, 'shared_with_groups' => [ - ['id' => 12] + ['id' => 12], ], 'only_allow_merge_if_pipeline_succeeds' => false, 'only_allow_merge_if_all_discussions_are_resolved' => false, @@ -69,7 +62,7 @@ public function testFromArray() 'approvals_before_merge' => 0, ]; - $project = Project::fromArray($client, $data); + $project = Project::fromArray($this->client, $data); $this->assertInstanceOf(Project::class, $project); $this->assertSame($data['id'], $project->id); diff --git a/tests/unit/Model/ReleaseTest.php b/tests/unit/Model/ReleaseTest.php new file mode 100644 index 000000000..bd8650d1d --- /dev/null +++ b/tests/unit/Model/ReleaseTest.php @@ -0,0 +1,24 @@ + 'v1.0.0', + 'description' => 'Amazing release. Wow', + ]; + + $release = Release::fromArray($this->client, $params); + + $this->assertSame($params['tag_name'], $release->tag_name); + $this->assertSame($params['description'], $release->description); + } +} diff --git a/tests/unit/ResultPagerTest.php b/tests/unit/ResultPagerTest.php new file mode 100644 index 000000000..b2b84252a --- /dev/null +++ b/tests/unit/ResultPagerTest.php @@ -0,0 +1,23 @@ +client); + $api->all = $expected; + $pager = new SimpleResultPager($this->client); + + $result = $pager->fetch($api, 'all', [1]); + + $this->assertEquals($expected, $result); + } +} diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php new file mode 100644 index 000000000..e1fc694a1 --- /dev/null +++ b/tests/unit/TestCase.php @@ -0,0 +1,71 @@ +httpClient = new MockClient(); + $this->builder = new Builder($this->httpClient); + $this->client = new Client($this->builder); + } + + protected function setResponse(ResponseInterface $response): void + { + $this->httpClient->setDefaultResponse($response); + } + + /** + * @param mixed[]|string|int|bool|float $responseBodyContents + */ + protected function setResponseBody($responseBodyContents): void + { + $this->httpClient->setDefaultResponse($this->createResponse($responseBodyContents)); + } + + /** + * @param mixed[]|string|int|bool|float $responseBodyContents + */ + protected function createResponse($responseBodyContents): ResponseInterface + { + $responseBody = $this->createMock(StreamInterface::class); + $responseBody + ->expects($this->any()) + ->method('__toString') + ->willReturn(is_array($responseBodyContents) ? json_encode($responseBodyContents) : $responseBodyContents); + + $response = $this->createMock(ResponseInterface::class); + $response + ->expects($this->any()) + ->method('getBody') + ->willReturn($responseBody); + + $response + ->expects($this->any()) + ->method('getHeaderLine') + ->willReturn(is_array($responseBodyContents) ? 'application/json' : 'txt/txt'); + + return $response; + } +}