diff --git a/.drone.jsonnet b/.drone.jsonnet index 0bedfefe..e13974c7 100644 --- a/.drone.jsonnet +++ b/.drone.jsonnet @@ -25,7 +25,7 @@ local composer(phpversion, params) = { local phpunit(phpversion) = { name: "PHPUnit", image: "joomlaprojects/docker-images:php" + phpversion, - [if phpversion == "8.2" then "failure"]: "ignore", + [if phpversion == "8.3" then "failure"]: "ignore", commands: ["vendor/bin/phpunit"] }; @@ -47,39 +47,33 @@ local pipeline(name, phpversion, params) = { steps: [ { name: "composer", - image: "joomlaprojects/docker-images:php7.4", + image: "joomlaprojects/docker-images:php8.1", volumes: volumes, commands: [ "php -v", - "composer update", - "composer require phpmd/phpmd phpstan/phpstan" + "composer update" ] }, { name: "phpcs", - image: "joomlaprojects/docker-images:php7.4", + image: "joomlaprojects/docker-images:php8.1", depends: [ "composer" ], commands: [ - "vendor/bin/phpcs --config-set installed_paths vendor/joomla/coding-standards", "vendor/bin/phpcs --standard=ruleset.xml src/" ] }, { - name: "phpmd", - image: "joomlaprojects/docker-images:php7.4", + name: "phan", + image: "joomlaprojects/docker-images:php8.1-ast", depends: [ "composer" ], failure: "ignore", commands: [ - "vendor/bin/phpmd src text cleancode", - "vendor/bin/phpmd src text codesize", - "vendor/bin/phpmd src text controversial", - "vendor/bin/phpmd src text design", - "vendor/bin/phpmd src text unusedcode", + "vendor/bin/phan" ] }, { name: "phpstan", - image: "joomlaprojects/docker-images:php7.4", + image: "joomlaprojects/docker-images:php8.1", depends: [ "composer" ], failure: "ignore", commands: [ @@ -88,7 +82,7 @@ local pipeline(name, phpversion, params) = { }, { name: "phploc", - image: "joomlaprojects/docker-images:php7.4", + image: "joomlaprojects/docker-images:php8.1", depends: [ "composer" ], failure: "ignore", commands: [ @@ -97,7 +91,7 @@ local pipeline(name, phpversion, params) = { }, { name: "phpcpd", - image: "joomlaprojects/docker-images:php7.4", + image: "joomlaprojects/docker-images:php8.1", depends: [ "composer" ], failure: "ignore", commands: [ @@ -106,11 +100,7 @@ local pipeline(name, phpversion, params) = { } ] }, - pipeline("7.2 lowest", "7.2", "--prefer-stable --prefer-lowest"), - pipeline("7.2", "7.2", "--prefer-stable"), - pipeline("7.3", "7.3", "--prefer-stable"), - pipeline("7.4", "7.4", "--prefer-stable"), - pipeline("8.0", "8.0", "--prefer-stable"), + pipeline("8.1 lowest", "8.1", "--prefer-stable --prefer-lowest"), pipeline("8.1", "8.1", "--prefer-stable"), - pipeline("8.2", "8.2", "--prefer-stable --ignore-platform-reqs"), + pipeline("8.2", "8.2", "--prefer-stable"), ] diff --git a/.drone.yml b/.drone.yml index 6a9a6920..0b8d4d90 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,347 +1,115 @@ --- -{ - "kind": "pipeline", - "name": "Codequality", - "steps": [ - { - "commands": [ - "php -v", - "composer update", - "composer require phpmd/phpmd phpstan/phpstan" - ], - "image": "joomlaprojects/docker-images:php7.4", - "name": "composer", - "volumes": [ - { - "name": "composer-cache", - "path": "/tmp/composer-cache" - } - ] - }, - { - "commands": [ - "vendor/bin/phpcs --config-set installed_paths vendor/joomla/coding-standards", - "vendor/bin/phpcs --standard=ruleset.xml src/" - ], - "depends": [ - "composer" - ], - "image": "joomlaprojects/docker-images:php7.4", - "name": "phpcs" - }, - { - "commands": [ - "vendor/bin/phpmd src text cleancode", - "vendor/bin/phpmd src text codesize", - "vendor/bin/phpmd src text controversial", - "vendor/bin/phpmd src text design", - "vendor/bin/phpmd src text unusedcode" - ], - "depends": [ - "composer" - ], - "failure": "ignore", - "image": "joomlaprojects/docker-images:php7.4", - "name": "phpmd" - }, - { - "commands": [ - "vendor/bin/phpstan analyse src" - ], - "depends": [ - "composer" - ], - "failure": "ignore", - "image": "joomlaprojects/docker-images:php7.4", - "name": "phpstan" - }, - { - "commands": [ - "phploc src" - ], - "depends": [ - "composer" - ], - "failure": "ignore", - "image": "joomlaprojects/docker-images:php7.4", - "name": "phploc" - }, - { - "commands": [ - "phpcpd src" - ], - "depends": [ - "composer" - ], - "failure": "ignore", - "image": "joomlaprojects/docker-images:php7.4", - "name": "phpcpd" - } - ], - "volumes": [ - { - "host": { - "path": "/tmp/composer-cache" - }, - "name": "composer-cache" - } - ] -} +kind: pipeline +name: Codequality +steps: +- commands: + - php -v + - composer update + image: joomlaprojects/docker-images:php8.1 + name: composer + volumes: + - name: composer-cache + path: /tmp/composer-cache +- commands: + - vendor/bin/phpcs --standard=ruleset.xml src/ + depends: + - composer + image: joomlaprojects/docker-images:php8.1 + name: phpcs +- commands: + - vendor/bin/phan + depends: + - composer + failure: ignore + image: joomlaprojects/docker-images:php8.1-ast + name: phan +- commands: + - vendor/bin/phpstan analyse src + depends: + - composer + failure: ignore + image: joomlaprojects/docker-images:php8.1 + name: phpstan +- commands: + - phploc src + depends: + - composer + failure: ignore + image: joomlaprojects/docker-images:php8.1 + name: phploc +- commands: + - phpcpd src + depends: + - composer + failure: ignore + image: joomlaprojects/docker-images:php8.1 + name: phpcpd +volumes: +- host: + path: /tmp/composer-cache + name: composer-cache --- -{ - "kind": "pipeline", - "name": "PHP 7.2 lowest", - "steps": [ - { - "commands": [ - "php -v", - "composer update --prefer-stable --prefer-lowest" - ], - "image": "joomlaprojects/docker-images:php7.2", - "name": "composer", - "volumes": [ - { - "name": "composer-cache", - "path": "/tmp/composer-cache" - } - ] - }, - { - "commands": [ - "vendor/bin/phpunit" - ], - "image": "joomlaprojects/docker-images:php7.2", - "name": "PHPUnit" - } - ], - "volumes": [ - { - "host": { - "path": "/tmp/composer-cache" - }, - "name": "composer-cache" - } - ] -} +kind: pipeline +name: PHP 8.1 lowest +steps: +- commands: + - php -v + - composer update --prefer-stable --prefer-lowest + image: joomlaprojects/docker-images:php8.1 + name: composer + volumes: + - name: composer-cache + path: /tmp/composer-cache +- commands: + - vendor/bin/phpunit + image: joomlaprojects/docker-images:php8.1 + name: PHPUnit +volumes: +- host: + path: /tmp/composer-cache + name: composer-cache --- -{ - "kind": "pipeline", - "name": "PHP 7.2", - "steps": [ - { - "commands": [ - "php -v", - "composer update --prefer-stable" - ], - "image": "joomlaprojects/docker-images:php7.2", - "name": "composer", - "volumes": [ - { - "name": "composer-cache", - "path": "/tmp/composer-cache" - } - ] - }, - { - "commands": [ - "vendor/bin/phpunit" - ], - "image": "joomlaprojects/docker-images:php7.2", - "name": "PHPUnit" - } - ], - "volumes": [ - { - "host": { - "path": "/tmp/composer-cache" - }, - "name": "composer-cache" - } - ] -} +kind: pipeline +name: PHP 8.1 +steps: +- commands: + - php -v + - composer update --prefer-stable + image: joomlaprojects/docker-images:php8.1 + name: composer + volumes: + - name: composer-cache + path: /tmp/composer-cache +- commands: + - vendor/bin/phpunit + image: joomlaprojects/docker-images:php8.1 + name: PHPUnit +volumes: +- host: + path: /tmp/composer-cache + name: composer-cache --- -{ - "kind": "pipeline", - "name": "PHP 7.3", - "steps": [ - { - "commands": [ - "php -v", - "composer update --prefer-stable" - ], - "image": "joomlaprojects/docker-images:php7.3", - "name": "composer", - "volumes": [ - { - "name": "composer-cache", - "path": "/tmp/composer-cache" - } - ] - }, - { - "commands": [ - "vendor/bin/phpunit" - ], - "image": "joomlaprojects/docker-images:php7.3", - "name": "PHPUnit" - } - ], - "volumes": [ - { - "host": { - "path": "/tmp/composer-cache" - }, - "name": "composer-cache" - } - ] -} ---- -{ - "kind": "pipeline", - "name": "PHP 7.4", - "steps": [ - { - "commands": [ - "php -v", - "composer update --prefer-stable" - ], - "image": "joomlaprojects/docker-images:php7.4", - "name": "composer", - "volumes": [ - { - "name": "composer-cache", - "path": "/tmp/composer-cache" - } - ] - }, - { - "commands": [ - "vendor/bin/phpunit" - ], - "image": "joomlaprojects/docker-images:php7.4", - "name": "PHPUnit" - } - ], - "volumes": [ - { - "host": { - "path": "/tmp/composer-cache" - }, - "name": "composer-cache" - } - ] -} ---- -{ - "kind": "pipeline", - "name": "PHP 8.0", - "steps": [ - { - "commands": [ - "php -v", - "composer update --prefer-stable" - ], - "image": "joomlaprojects/docker-images:php8.0", - "name": "composer", - "volumes": [ - { - "name": "composer-cache", - "path": "/tmp/composer-cache" - } - ] - }, - { - "commands": [ - "vendor/bin/phpunit" - ], - "image": "joomlaprojects/docker-images:php8.0", - "name": "PHPUnit" - } - ], - "volumes": [ - { - "host": { - "path": "/tmp/composer-cache" - }, - "name": "composer-cache" - } - ] -} ---- -{ - "kind": "pipeline", - "name": "PHP 8.1", - "steps": [ - { - "commands": [ - "php -v", - "composer update --prefer-stable" - ], - "image": "joomlaprojects/docker-images:php8.1", - "name": "composer", - "volumes": [ - { - "name": "composer-cache", - "path": "/tmp/composer-cache" - } - ] - }, - { - "commands": [ - "vendor/bin/phpunit" - ], - "image": "joomlaprojects/docker-images:php8.1", - "name": "PHPUnit" - } - ], - "volumes": [ - { - "host": { - "path": "/tmp/composer-cache" - }, - "name": "composer-cache" - } - ] -} ---- -{ - "kind": "pipeline", - "name": "PHP 8.2", - "steps": [ - { - "commands": [ - "php -v", - "composer update --prefer-stable --ignore-platform-reqs" - ], - "image": "joomlaprojects/docker-images:php8.2", - "name": "composer", - "volumes": [ - { - "name": "composer-cache", - "path": "/tmp/composer-cache" - } - ] - }, - { - "commands": [ - "vendor/bin/phpunit" - ], - "failure": "ignore", - "image": "joomlaprojects/docker-images:php8.2", - "name": "PHPUnit" - } - ], - "volumes": [ - { - "host": { - "path": "/tmp/composer-cache" - }, - "name": "composer-cache" - } - ] -} +kind: pipeline +name: PHP 8.2 +steps: +- commands: + - php -v + - composer update --prefer-stable + image: joomlaprojects/docker-images:php8.2 + name: composer + volumes: + - name: composer-cache + path: /tmp/composer-cache +- commands: + - vendor/bin/phpunit + image: joomlaprojects/docker-images:php8.2 + name: PHPUnit +volumes: +- host: + path: /tmp/composer-cache + name: composer-cache --- kind: signature -hmac: 0f630909a1c433137ead18e482d0367110ab613381af2840e3cde18d54fd9ac9 +hmac: 5e5d25d380c8471aa6a55d802339463ba763c8b2337c8810984e93a0c2f6b6e4 ... diff --git a/.editorconfig b/.editorconfig index c8bb3e7b..78a64203 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,7 +2,8 @@ # Unix-style newlines with a newline ending every file [*] -indent_style = tab +indent_style = space +indent_size = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true diff --git a/.phan/config.php b/.phan/config.php new file mode 100644 index 00000000..7face806 --- /dev/null +++ b/.phan/config.php @@ -0,0 +1,349 @@ + '8.1', + + // If enabled, missing properties will be created when + // they are first seen. If false, we'll report an + // error message if there is an attempt to write + // to a class property that wasn't explicitly + // defined. + 'allow_missing_properties' => true, + + // If enabled, null can be cast to any type and any + // type can be cast to null. Setting this to true + // will cut down on false positives. + 'null_casts_as_any_type' => true, + + // If enabled, allow null to be cast as any array-like type. + // + // This is an incremental step in migrating away from `null_casts_as_any_type`. + // If `null_casts_as_any_type` is true, this has no effect. + 'null_casts_as_array' => true, + + // If enabled, allow any array-like type to be cast to null. + // This is an incremental step in migrating away from `null_casts_as_any_type`. + // If `null_casts_as_any_type` is true, this has no effect. + 'array_casts_as_null' => true, + + // If enabled, scalars (int, float, bool, string, null) + // are treated as if they can cast to each other. + // This does not affect checks of array keys. See `scalar_array_key_cast`. + 'scalar_implicit_cast' => true, + + // If enabled, any scalar array keys (int, string) + // are treated as if they can cast to each other. + // E.g. `array` can cast to `array` and vice versa. + // Normally, a scalar type such as int could only cast to/from int and mixed. + 'scalar_array_key_cast' => true, + + // If this has entries, scalars (int, float, bool, string, null) + // are allowed to perform the casts listed. + // + // E.g. `['int' => ['float', 'string'], 'float' => ['int'], 'string' => ['int'], 'null' => ['string']]` + // allows casting null to a string, but not vice versa. + // (subset of `scalar_implicit_cast`) + 'scalar_implicit_partial' => [], + + // If enabled, Phan will warn if **any** type in a method invocation's object + // is definitely not an object, + // or if **any** type in an invoked expression is not a callable. + // Setting this to true will introduce numerous false positives + // (and reveal some bugs). + 'strict_method_checking' => false, + + // If enabled, Phan will warn if **any** type of the object expression for a property access + // does not contain that property. + 'strict_object_checking' => false, + + // If enabled, Phan will warn if **any** type in the argument's union type + // cannot be cast to a type in the parameter's expected union type. + // Setting this to true will introduce numerous false positives + // (and reveal some bugs). + 'strict_param_checking' => false, + + // If enabled, Phan will warn if **any** type in a property assignment's union type + // cannot be cast to a type in the property's declared union type. + // Setting this to true will introduce numerous false positives + // (and reveal some bugs). + 'strict_property_checking' => false, + + // If enabled, Phan will warn if **any** type in a returned value's union type + // cannot be cast to the declared return type. + // Setting this to true will introduce numerous false positives + // (and reveal some bugs). + 'strict_return_checking' => false, + + // If true, seemingly undeclared variables in the global + // scope will be ignored. + // + // This is useful for projects with complicated cross-file + // globals that you have no hope of fixing. + 'ignore_undeclared_variables_in_global_scope' => true, + + // Set this to false to emit `PhanUndeclaredFunction` issues for internal functions that Phan has signatures for, + // but aren't available in the codebase, or from Reflection. + // (may lead to false positives if an extension isn't loaded) + // + // If this is true(default), then Phan will not warn. + // + // Even when this is false, Phan will still infer return values and check parameters of internal functions + // if Phan has the signatures. + 'ignore_undeclared_functions_with_known_signatures' => true, + + // Backwards Compatibility Checking. This is slow + // and expensive, but you should consider running + // it before upgrading your version of PHP to a + // new version that has backward compatibility + // breaks. + // + // If you are migrating from PHP 5 to PHP 7, + // you should also look into using + // [php7cc (no longer maintained)](https://github.com/sstalle/php7cc) + // and [php7mar](https://github.com/Alexia/php7mar), + // which have different backwards compatibility checks. + // + // If you are still using versions of php older than 5.6, + // `PHP53CompatibilityPlugin` may be worth looking into if you are not running + // syntax checks for php 5.3 through another method such as + // `InvokePHPNativeSyntaxCheckPlugin` (see .phan/plugins/README.md). + 'backward_compatibility_checks' => false, + + // If true, check to make sure the return type declared + // in the doc-block (if any) matches the return type + // declared in the method signature. + 'check_docblock_signature_return_type_match' => true, + + // This setting maps case-insensitive strings to union types. + // + // This is useful if a project uses phpdoc that differs from the phpdoc2 standard. + // + // If the corresponding value is the empty string, + // then Phan will ignore that union type (E.g. can ignore 'the' in `@return the value`) + // + // If the corresponding value is not empty, + // then Phan will act as though it saw the corresponding UnionTypes(s) + // when the keys show up in a UnionType of `@param`, `@return`, `@var`, `@property`, etc. + // + // This matches the **entire string**, not parts of the string. + // (E.g. `@return the|null` will still look for a class with the name `the`, but `@return the` will be ignored with the below setting) + // + // (These are not aliases, this setting is ignored outside of doc comments). + // (Phan does not check if classes with these names exist) + // + // Example setting: `['unknown' => '', 'number' => 'int|float', 'char' => 'string', 'long' => 'int', 'the' => '']` + 'phpdoc_type_mapping' => [], + + // Set to true in order to attempt to detect dead + // (unreferenced) code. Keep in mind that the + // results will only be a guess given that classes, + // properties, constants and methods can be referenced + // as variables (like `$class->$property` or + // `$class->$method()`) in ways that we're unable + // to make sense of. + // + // To more aggressively detect dead code, + // you may want to set `dead_code_detection_prefer_false_negative` to `false`. + 'dead_code_detection' => false, + + // Set to true in order to attempt to detect unused variables. + // `dead_code_detection` will also enable unused variable detection. + // + // This has a few known false positives, e.g. for loops or branches. + 'unused_variable_detection' => false, + + // Set to true in order to attempt to detect redundant and impossible conditions. + // + // This has some false positives involving loops, + // variables set in branches of loops, and global variables. + 'redundant_condition_detection' => false, + + // If enabled, Phan will act as though it's certain of real return types of a subset of internal functions, + // even if those return types aren't available in reflection (real types were taken from php 7.3 or 8.0-dev, depending on target_php_version). + // + // Note that with php 7 and earlier, php would return null or false for many internal functions if the argument types or counts were incorrect. + // As a result, enabling this setting with target_php_version 8.0 may result in false positives for `--redundant-condition-detection` when codebases also support php 7.x. + 'assume_real_types_for_internal_functions' => false, + + // If true, this runs a quick version of checks that takes less + // time at the cost of not running as thorough + // of an analysis. You should consider setting this + // to true only when you wish you had more **undiagnosed** issues + // to fix in your code base. + // + // In quick-mode the scanner doesn't rescan a function + // or a method's code block every time a call is seen. + // This means that the problem here won't be detected: + // + // ```php + // false, + + // Override to hardcode existence and types of (non-builtin) globals in the global scope. + // Class names should be prefixed with `\`. + // + // (E.g. `['_FOO' => '\FooClass', 'page' => '\PageClass', 'userId' => 'int']`) + 'globals_type_map' => [], + + // The minimum severity level to report on. This can be + // set to `Issue::SEVERITY_LOW`, `Issue::SEVERITY_NORMAL` or + // `Issue::SEVERITY_CRITICAL`. Setting it to only + // critical issues is a good place to start on a big + // sloppy mature code base. + 'minimum_severity' => Issue::SEVERITY_NORMAL, + + // Add any issue types (such as `'PhanUndeclaredMethod'`) + // to this list to inhibit them from being reported. + // 'PhanDeprecatedClass', 'PhanUndeclaredConstant','PhanDeprecatedFunction' + 'suppress_issue_types' => [], + + // A regular expression to match files to be excluded + // from parsing and analysis and will not be read at all. + // + // This is useful for excluding groups of test or example + // directories/files, unanalyzable files, or files that + // can't be removed for whatever reason. + // (e.g. `'@Test\.php$@'`, or `'@vendor/.*/(tests|Tests)/@'`) + 'exclude_file_regex' => '@^vendor/.*/(tests?|Tests?)/@', + + // A list of files that will be excluded from parsing and analysis + // and will not be read at all. + // + // This is useful for excluding hopelessly unanalyzable + // files that can't be removed for whatever reason. + 'exclude_file_list' => [ + ], + + // A directory list that defines files that will be excluded + // from static analysis, but whose class and method + // information should be included. + // + // Generally, you'll want to include the directories for + // third-party code (such as "vendor/") in this list. + // + // n.b.: If you'd like to parse but not analyze 3rd + // party code, directories containing that code + // should be added to the `directory_list` as well as + // to `exclude_analysis_directory_list`. + 'exclude_analysis_directory_list' => [ + 'vendor/' + ], + + // Enable this to enable checks of require/include statements referring to valid paths. + // The settings `include_paths` and `warn_about_relative_include_statement` affect the checks. + 'enable_include_path_checks' => false, + + // The number of processes to fork off during the analysis + // phase. + 'processes' => 1, + + // List of case-insensitive file extensions supported by Phan. + // (e.g. `['php', 'html', 'htm']`) + 'analyzed_file_extensions' => [ + 'php', + ], + + // You can put paths to stubs of internal extensions in this config option. + // If the corresponding extension is **not** loaded, then Phan will use the stubs instead. + // Phan will continue using its detailed type annotations, + // but load the constants, classes, functions, and classes (and their Reflection types) + // from these stub files (doubling as valid php files). + // Use a different extension from php to avoid accidentally loading these. + // The `tools/make_stubs` script can be used to generate your own stubs (compatible with php 7.0+ right now) + // + // (e.g. `['xdebug' => '.phan/internal_stubs/xdebug.phan_php']`) + 'autoload_internal_extension_signatures' => [ + ], + + // A list of plugin files to execute. + // + // Plugins which are bundled with Phan can be added here by providing their name (e.g. `'AlwaysReturnPlugin'`) + // + // Documentation about available bundled plugins can be found [here](https://github.com/phan/phan/tree/v4/.phan/plugins). + // + // Alternately, you can pass in the full path to a PHP file with the plugin's implementation (e.g. `'vendor/phan/phan/.phan/plugins/AlwaysReturnPlugin.php'`) + 'plugins' => [], + + // A list of directories that should be parsed for class and + // method information. After excluding the directories + // defined in `exclude_analysis_directory_list`, the remaining + // files will be statically analyzed for errors. + // + // Thus, both first-party and third-party code being used by + // your application should be included in this list. + 'directory_list' => [ + 'src', + 'vendor' + ], + + // A list of individual files to include in analysis + // with a path relative to the root directory of the + // project. + 'file_list' => [], +]; diff --git a/README.md b/README.md index 56187dc1..8296f2b2 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,22 @@ -# The Github Package [![Build Status](https://ci.joomla.org/api/badges/joomla-framework/github-api/status.svg?ref=refs/heads/2.0-dev)](https://ci.joomla.org/joomla-framework/github-api) +# The GitHub Package [![Build Status](https://ci.joomla.org/api/badges/joomla-framework/github-api/status.svg?ref=refs/heads/2.0-dev)](https://ci.joomla.org/joomla-framework/github-api) -## Using the Github Package +[![Latest Stable Version](https://poser.pugx.org/joomla/github/v/stable)](https://packagist.org/packages/joomla/github) +[![Total Downloads](https://poser.pugx.org/joomla/github/downloads)](https://packagist.org/packages/joomla/github) +[![Latest Unstable Version](https://poser.pugx.org/joomla/github/v/unstable)](https://packagist.org/packages/joomla/github) +[![License](https://poser.pugx.org/joomla/github/license)](https://packagist.org/packages/joomla/github) -The Github package is designed to be a straightforward interface for working with Github. It is based on version 3 of -the Github API. You can find documentation on the API at [http://developer.github.com/v3/](http://developer.github.com/v3/). +## Using the GitHub Package -Github is built upon the Http package which provides an easy way to consume URLs and web services in a transport +The GitHub package is designed to be a straightforward interface for working with GitHub. It is based on version 3 of +the GitHub API. You can find documentation on the API at [http://developer.github.com/v3/](http://developer.github.com/v3/). + +GitHub is built upon the Http package which provides an easy way to consume URLs and web services in a transport independent way. `Joomla\Http` currently supports streams, sockets and cURL. It is possible to create a custom -context and inject it into the Github class if one so desires. +context and inject it into the GitHub class if one so desires. -### Instantiating Github +### Instantiating GitHub -Instantiating Github is easy: +Instantiating GitHub is easy: ```php use Joomla\Github\Github; @@ -19,11 +24,11 @@ use Joomla\Github\Github; $github = new Github; ``` -This creates a basic Github object that can be used to access publicly available resources on [github.com](https://github.com). +This creates a basic GitHub object that can be used to access publicly available resources on [github.com](https://github.com). Sometimes it is necessary to specify additional options. This can be done by injecting in a Registry object with your -preferred options. Support is available for optionally providing a custom Github account username and password, -as well as a custom URL for the Github server (as would be the case for using a local instance of [Github Enterprise](https://enterprise.github.com)). +preferred options. Support is available for optionally providing a custom GitHub account username and password, +as well as a custom URL for the GitHub server (as would be the case for using a local instance of [GitHub Enterprise](https://enterprise.github.com)). ```php use Joomla\Github\Github; @@ -39,7 +44,7 @@ $github = new Github($options); A `gh.token` option is also available. -Here is an example demonstrating more of the Github package: +Here is an example demonstrating more of the GitHub package: ```php use Joomla\Github\Github; @@ -67,13 +72,13 @@ $summary = implode("\n", $issueSummary); $github->gists->create(array('issue_summary.txt' => $summary)); ``` -## Accessing the Github APIs +## Accessing the GitHub APIs -The Github object using magic methods to access sub-packages of the Github server's API that can be accessed using +The GitHub object using magic methods to access sub-packages of the GitHub server's API that can be accessed using the `->` object operator. Where a result is returned by a PHP method, the result is the PHP equivalent of the JSON response that can be found in -the Github API documentation. +the GitHub API documentation. ### Activity @@ -743,7 +748,7 @@ $github->pulls->comments->delete(':owner', ':repo', ':commentId'); ## See Also The following resources contain more information: [Joomla! API Reference](http://api.joomla.org), -[Github API Reference](http://developer.github.com). +[GitHub API Reference](http://developer.github.com). ## Installation via Composer diff --git a/Tests/GithubObjectTest.php b/Tests/GithubObjectTest.php index b85cb8eb..43bf0795 100755 --- a/Tests/GithubObjectTest.php +++ b/Tests/GithubObjectTest.php @@ -1,4 +1,5 @@ object = new ObjectMock($this->options, $this->client); - } - - /** - * Data provider method for the fetchUrl method tests. - * - * @return array - * - * @since 1.0 - */ - public function fetchUrlData() - { - return array( - 'Standard github - no pagination data' => array( - 'https://api.github.com', - '/gists', - 0, - 0, - 'https://api.github.com/gists' - ), - 'Enterprise github - no pagination data' => array( - 'https://mygithub.com', - '/gists', - 0, - 0, - 'https://mygithub.com/gists' - ), - 'Standard github - page 3' => array( - 'https://api.github.com', - '/gists', - 3, - 0, - 'https://api.github.com/gists?page=3' - ), - 'Enterprise github - page 3, 50 per page' => array( - 'https://mygithub.com', - '/gists', - 3, - 50, - 'https://mygithub.com/gists?page=3&per_page=50' - ), - ); - } - - /** - * Tests the fetchUrl method - * - * @param string $apiUrl @todo - * @param string $path @todo - * @param integer $page @todo - * @param integer $limit @todo - * @param string $expected @todo - * - * @return void - * - * @since 1.0 - * @dataProvider fetchUrlData - */ - public function testFetchUrl($apiUrl, $path, $page, $limit, $expected) - { - $this->options->set('api.url', $apiUrl); - - $this->assertThat( - $this->object->fetchUrl($path, $page, $limit), - $this->equalTo($expected) - ); - } - - /** - * Tests the fetchUrl method with basic authentication data - * - * @return void - * - * @since 1.0 - */ - public function testFetchUrlBasicAuth() - { - $this->options->set('api.url', 'https://api.github.com'); - - $this->options->set('api.username', 'MyTestUser'); - $this->options->set('api.password', 'MyTestPass'); - - $this->assertThat( - $this->object->fetchUrl('/gists', 0, 0), - $this->equalTo('https://MyTestUser:MyTestPass@api.github.com/gists'), - 'URL is not as expected.' - ); - } - - /** - * Tests the fetchUrl method using an oAuth token. - * - * @return void - */ - public function testFetchUrlToken() - { - $this->options->set('api.url', 'https://api.github.com'); - - $this->options->set('gh.token', 'MyTestToken'); - - $this->assertThat( - (string) $this->object->fetchUrl('/gists', 0, 0), - $this->equalTo('https://api.github.com/gists'), - 'URL is not as expected.' - ); - - $this->assertThat( - $this->client->getOption('headers'), - $this->equalTo(['Authorization' => 'token MyTestToken']), - 'Token should be propagated as a header.' - ); - } + /** + * @var ObjectMock Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @return void + * + * @since 1.0 + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new ObjectMock($this->options, $this->client); + } + + /** + * Data provider method for the fetchUrl method tests. + * + * @return array + * + * @since 1.0 + */ + public function fetchUrlData() + { + return [ + 'Standard github - no pagination data' => [ + 'https://api.github.com', + '/gists', + 0, + 0, + 'https://api.github.com/gists', + ], + 'Enterprise github - no pagination data' => [ + 'https://mygithub.com', + '/gists', + 0, + 0, + 'https://mygithub.com/gists', + ], + 'Standard github - page 3' => [ + 'https://api.github.com', + '/gists', + 3, + 0, + 'https://api.github.com/gists?page=3', + ], + 'Enterprise github - page 3, 50 per page' => [ + 'https://mygithub.com', + '/gists', + 3, + 50, + 'https://mygithub.com/gists?page=3&per_page=50', + ], + ]; + } + + /** + * Tests the fetchUrl method + * + * @param string $apiUrl @todo + * @param string $path @todo + * @param integer $page @todo + * @param integer $limit @todo + * @param string $expected @todo + * + * @return void + * + * @since 1.0 + * @dataProvider fetchUrlData + */ + public function testFetchUrl($apiUrl, $path, $page, $limit, $expected) + { + $this->options->set('api.url', $apiUrl); + + $this->assertThat( + $this->object->fetchUrl($path, $page, $limit), + $this->equalTo($expected) + ); + } + + /** + * Tests the fetchUrl method with basic authentication data + * + * @return void + * + * @since 1.0 + */ + public function testFetchUrlBasicAuth() + { + $this->options->set('api.url', 'https://api.github.com'); + + $this->options->set('api.username', 'MyTestUser'); + $this->options->set('api.password', 'MyTestPass'); + + $this->assertThat( + $this->object->fetchUrl('/gists', 0, 0), + $this->equalTo('https://MyTestUser:MyTestPass@api.github.com/gists'), + 'URL is not as expected.' + ); + } + + /** + * Tests the fetchUrl method using an oAuth token. + * + * @return void + */ + public function testFetchUrlToken() + { + $this->options->set('api.url', 'https://api.github.com'); + + $this->options->set('gh.token', 'MyTestToken'); + + $this->assertThat( + (string) $this->object->fetchUrl('/gists', 0, 0), + $this->equalTo('https://api.github.com/gists'), + 'URL is not as expected.' + ); + + $this->assertThat( + $this->client->getOption('headers'), + $this->equalTo(['Authorization' => 'token MyTestToken']), + 'Token should be propagated as a header.' + ); + } } diff --git a/Tests/GithubTest.php b/Tests/GithubTest.php index 68960064..f372a0cb 100755 --- a/Tests/GithubTest.php +++ b/Tests/GithubTest.php @@ -1,4 +1,5 @@ object = new Github($this->options, $this->client); - } - - /** - * Tests the magic __get method - forks - * - * @return void - * - * @since 1.0 - */ - public function testGetForks() - { - $this->assertThat( - $this->object->repositories->forks, - $this->isInstanceOf('Joomla\Github\Package\Repositories\Forks') - ); - } - - /** - * Tests the magic __get method - commits - * - * @return void - * - * @since 1.0 - */ - public function testGetCommits() - { - $this->assertThat( - $this->object->repositories->commits, - $this->isInstanceOf('Joomla\Github\Package\Repositories\Commits') - ); - } - - /** - * Tests the magic __get method - statuses - * - * @return void - * - * @since 1.0 - */ - public function testGetStatuses() - { - $this->assertThat( - $this->object->repositories->statuses, - $this->isInstanceOf('Joomla\Github\Package\Repositories\Statuses') - ); - } - - /** - * Tests the magic __get method - hooks - * - * @return void - * - * @since 1.0 - */ - public function testGetHooks() - { - $this->assertThat( - $this->object->repositories->hooks, - $this->isInstanceOf('Joomla\Github\Package\Repositories\Hooks') - ); - } - - /** - * Tests the magic __get method - failure - * - * @return void - * - * @since 1.0 - */ - public function testGetFailure() - { - $this->expectException(\InvalidArgumentException::class); - - $this->object->other; - } - - /** - * Tests the setOption method - * - * @return void - * - * @since 1.0 - */ - public function testSetOption() - { - $this->object->setOption('api.url', 'https://example.com/settest'); - - $this->assertThat( - $this->options->get('api.url'), - $this->equalTo('https://example.com/settest') - ); - } - - /** - * Tests the getOption method - * - * @return void - * - * @since 1.0 - */ - public function testGetOption() - { - $this->options->set('api.url', 'https://example.com/gettest'); - - $this->assertThat( - $this->object->getOption('api.url'), - $this->equalTo('https://example.com/gettest') - ); - } + /** + * @var Github Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @return void + * + * @since 1.0 + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Github($this->options, $this->client); + } + + /** + * Tests the magic __get method - forks + * + * @return void + * + * @since 1.0 + */ + public function testGetForks() + { + $this->assertThat( + $this->object->repositories->forks, + $this->isInstanceOf('Joomla\Github\Package\Repositories\Forks') + ); + } + + /** + * Tests the magic __get method - commits + * + * @return void + * + * @since 1.0 + */ + public function testGetCommits() + { + $this->assertThat( + $this->object->repositories->commits, + $this->isInstanceOf('Joomla\Github\Package\Repositories\Commits') + ); + } + + /** + * Tests the magic __get method - statuses + * + * @return void + * + * @since 1.0 + */ + public function testGetStatuses() + { + $this->assertThat( + $this->object->repositories->statuses, + $this->isInstanceOf('Joomla\Github\Package\Repositories\Statuses') + ); + } + + /** + * Tests the magic __get method - hooks + * + * @return void + * + * @since 1.0 + */ + public function testGetHooks() + { + $this->assertThat( + $this->object->repositories->hooks, + $this->isInstanceOf('Joomla\Github\Package\Repositories\Hooks') + ); + } + + /** + * Tests the magic __get method - failure + * + * @return void + * + * @since 1.0 + */ + public function testGetFailure() + { + $this->expectException(\InvalidArgumentException::class); + + $this->object->other; + } + + /** + * Tests the setOption method + * + * @return void + * + * @since 1.0 + */ + public function testSetOption() + { + $this->object->setOption('api.url', 'https://example.com/settest'); + + $this->assertThat( + $this->options->get('api.url'), + $this->equalTo('https://example.com/settest') + ); + } + + /** + * Tests the getOption method + * + * @return void + * + * @since 1.0 + */ + public function testGetOption() + { + $this->options->set('api.url', 'https://example.com/gettest'); + + $this->assertThat( + $this->object->getOption('api.url'), + $this->equalTo('https://example.com/gettest') + ); + } } diff --git a/Tests/Package/Activity/EventsTest.php b/Tests/Package/Activity/EventsTest.php index c214f922..1e39801c 100644 --- a/Tests/Package/Activity/EventsTest.php +++ b/Tests/Package/Activity/EventsTest.php @@ -1,4 +1,5 @@ object = new Events($this->options, $this->client); - } - - /** - * Tests the getPublic method - * - * @return void - */ - public function testGetPublic() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/events') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getPublic(), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getRepository method - * - * @return void - */ - public function testGetRepository() - { - $path = '/repos/' . $this->owner . '/' . $this->repo . '/events'; - - $this->client->expects($this->once()) - ->method('get') - ->with($path) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getRepository($this->owner, $this->repo), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getIssue method - * - * @return void - */ - public function testGetIssue() - { - $path = '/repos/' . $this->owner . '/' . $this->repo . '/issues/events'; - - $this->client->expects($this->once()) - ->method('get') - ->with($path) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getIssue($this->owner, $this->repo), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getNetwork method - * - * @return void - */ - public function testGetNetwork() - { - $path = '/networks/' . $this->owner . '/' . $this->repo . '/events'; - - $this->client->expects($this->once()) - ->method('get') - ->with($path) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getNetwork($this->owner, $this->repo), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getOrg method - * - * @return void - */ - public function testGetOrg() - { - $path = '/orgs/' . $this->owner . '/events'; - - $this->client->expects($this->once()) - ->method('get') - ->with($path) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getOrg($this->owner), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getUser method - * - * @return void - */ - public function testGetUser() - { - $path = '/users/' . $this->owner . '/received_events'; - - $this->client->expects($this->once()) - ->method('get') - ->with($path) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getUser($this->owner), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getUserPublic method - * - * @return void - */ - public function testGetUserPublic() - { - $path = '/users/' . $this->owner . '/received_events/public'; - - $this->client->expects($this->once()) - ->method('get') - ->with($path) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getUserPublic($this->owner), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getByUser method - * - * @return void - */ - public function testGetByUser() - { - $path = '/users/' . $this->owner . '/events'; - - $this->client->expects($this->once()) - ->method('get') - ->with($path) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getByUser($this->owner), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getByUserPublic method - * - * @return void - */ - public function testGetByUserPublic() - { - $path = '/users/' . $this->owner . '/events/public'; - - $this->client->expects($this->once()) - ->method('get') - ->with($path) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getByUserPublic($this->owner), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getUserOrg method - * - * @return void - */ - public function testGetUserOrg() - { - $path = '/users/' . $this->owner . '/events/orgs/' . $this->repo; - - $this->client->expects($this->once()) - ->method('get') - ->with($path) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getUserOrg($this->owner, $this->repo), - $this->equalTo(json_decode($this->response->body)) - ); - } + /** + * @var Events Object under test. + * @since 1.0 + */ + protected $object; + + /** + * @var string + * @since 1.0 + */ + protected $owner = 'joomla'; + + /** + * @var string + * @since 1.0 + */ + protected $repo = 'joomla-framework'; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Events($this->options, $this->client); + } + + /** + * Tests the getPublic method + * + * @return void + */ + public function testGetPublic() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/events') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getPublic(), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getRepository method + * + * @return void + */ + public function testGetRepository() + { + $path = '/repos/' . $this->owner . '/' . $this->repo . '/events'; + + $this->client->expects($this->once()) + ->method('get') + ->with($path) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getRepository($this->owner, $this->repo), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getIssue method + * + * @return void + */ + public function testGetIssue() + { + $path = '/repos/' . $this->owner . '/' . $this->repo . '/issues/events'; + + $this->client->expects($this->once()) + ->method('get') + ->with($path) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getIssue($this->owner, $this->repo), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getNetwork method + * + * @return void + */ + public function testGetNetwork() + { + $path = '/networks/' . $this->owner . '/' . $this->repo . '/events'; + + $this->client->expects($this->once()) + ->method('get') + ->with($path) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getNetwork($this->owner, $this->repo), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getOrg method + * + * @return void + */ + public function testGetOrg() + { + $path = '/orgs/' . $this->owner . '/events'; + + $this->client->expects($this->once()) + ->method('get') + ->with($path) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getOrg($this->owner), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getUser method + * + * @return void + */ + public function testGetUser() + { + $path = '/users/' . $this->owner . '/received_events'; + + $this->client->expects($this->once()) + ->method('get') + ->with($path) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getUser($this->owner), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getUserPublic method + * + * @return void + */ + public function testGetUserPublic() + { + $path = '/users/' . $this->owner . '/received_events/public'; + + $this->client->expects($this->once()) + ->method('get') + ->with($path) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getUserPublic($this->owner), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getByUser method + * + * @return void + */ + public function testGetByUser() + { + $path = '/users/' . $this->owner . '/events'; + + $this->client->expects($this->once()) + ->method('get') + ->with($path) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getByUser($this->owner), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getByUserPublic method + * + * @return void + */ + public function testGetByUserPublic() + { + $path = '/users/' . $this->owner . '/events/public'; + + $this->client->expects($this->once()) + ->method('get') + ->with($path) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getByUserPublic($this->owner), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getUserOrg method + * + * @return void + */ + public function testGetUserOrg() + { + $path = '/users/' . $this->owner . '/events/orgs/' . $this->repo; + + $this->client->expects($this->once()) + ->method('get') + ->with($path) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getUserOrg($this->owner, $this->repo), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/Activity/FeedsTest.php b/Tests/Package/Activity/FeedsTest.php index bb8c8d2e..173c7010 100644 --- a/Tests/Package/Activity/FeedsTest.php +++ b/Tests/Package/Activity/FeedsTest.php @@ -1,4 +1,5 @@ object = new Feeds($this->options, $this->client); - } - - /** - * Tests the getFeeds method - * - * @return void - */ - public function testGetFeeds() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/feeds') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getFeeds(), - $this->equalTo(json_decode($this->response->body)) - ); - } + /** + * @var Feeds Object under test. + * @since 1.4.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.4.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Feeds($this->options, $this->client); + } + + /** + * Tests the getFeeds method + * + * @return void + */ + public function testGetFeeds() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/feeds') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getFeeds(), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/Activity/NotificationsTest.php b/Tests/Package/Activity/NotificationsTest.php index ade094b7..66d06e7b 100644 --- a/Tests/Package/Activity/NotificationsTest.php +++ b/Tests/Package/Activity/NotificationsTest.php @@ -1,4 +1,5 @@ object = new Notifications($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::getList() - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/notifications?all=1&participating=1&since=2005-08-17T00:00:00+00:00&before=2005-08-17T00:00:00+00:00', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList(true, true, new \DateTime('2005-8-17', new \DateTimeZone('UTC')), new \DateTime('2005-8-17', new \DateTimeZone('UTC'))), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::getListRepository() - * - * @return void - */ - public function testGetListRepository() - { - $args = 'all=1&participating=1&since=2005-08-17T00:00:00+00:00&before=2005-08-17T00:00:00+00:00'; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{owner}/{repo}/notifications?' . $args, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListRepository( - '{owner}', - '{repo}', - true, - true, - new \DateTime('2005-8-17', new \DateTimeZone('UTC')), - new \DateTime('2005-8-17', new \DateTimeZone('UTC')) - ), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::markRead() - * - * @return void - */ - public function testMarkRead() - { - $this->response->code = 205; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('put') - ->with('/notifications', '{"unread":true,"read":true}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->markRead(), - $this->equalTo($this->response->body) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::markRead() - * - * @return void - */ - public function testMarkReadLastRead() - { - $this->response->code = 205; - $this->response->body = ''; - - $date = new \DateTime('1966-09-14', new \DateTimeZone('UTC')); - $data = '{"unread":true,"read":true,"last_read_at":"1966-09-14T00:00:00+00:00"}'; - - $this->client->expects($this->once()) - ->method('put') - ->with('/notifications', $data, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->markRead(true, true, $date), - $this->equalTo($this->response->body) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::markReadRepository() - * - * @return void - */ - public function testMarkReadRepository() - { - $this->response->code = 205; - $this->response->body = ''; - - $data = '{"unread":true,"read":true}'; - - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-platform/notifications', $data, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->markReadRepository('joomla', 'joomla-platform', true, true), - $this->equalTo($this->response->body) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::markReadRepository() - * - * @return void - */ - public function testMarkReadRepositoryLastRead() - { - $this->response->code = 205; - $this->response->body = ''; - - $date = new \DateTime('1966-09-14', new \DateTimeZone('UTC')); - $data = '{"unread":true,"read":true,"last_read_at":"1966-09-14T00:00:00+00:00"}'; - - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-platform/notifications', $data, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->markReadRepository('joomla', 'joomla-platform', true, true, $date), - $this->equalTo($this->response->body) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::viewThread() - * - * @return void - */ - public function testViewThread() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/notifications/threads/1', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->viewThread(1), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::markReadThread() - * - * @return void - */ - public function testMarkReadThread() - { - $this->response->code = 205; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/notifications/threads/1', '{"unread":true,"read":true}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->markReadThread(1), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::getThreadSubscription() - * - * @return void - */ - public function testGetThreadSubscription() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/notifications/threads/1/subscription', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getThreadSubscription(1), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::setThreadSubscription() - * - * @return void - */ - public function testSetThreadSubscription() - { - $this->client->expects($this->once()) - ->method('put') - ->with('/notifications/threads/1/subscription', '{"subscribed":true,"ignored":false}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->setThreadSubscription(1, true, false), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Notifications::deleteThreadSubscription() - * - * @return void - */ - public function testDeleteThreadSubscription() - { - $this->response->code = 204; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/notifications/threads/1/subscription', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->deleteThreadSubscription(1), - $this->equalTo(json_decode($this->response->body)) - ); - } + /** + * @var Notifications Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Notifications($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::getList() + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/notifications?all=1&participating=1&since=2005-08-17T00:00:00+00:00&before=2005-08-17T00:00:00+00:00', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList(true, true, new \DateTime('2005-8-17', new \DateTimeZone('UTC')), new \DateTime('2005-8-17', new \DateTimeZone('UTC'))), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::getListRepository() + * + * @return void + */ + public function testGetListRepository() + { + $args = 'all=1&participating=1&since=2005-08-17T00:00:00+00:00&before=2005-08-17T00:00:00+00:00'; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{owner}/{repo}/notifications?' . $args, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListRepository( + '{owner}', + '{repo}', + true, + true, + new \DateTime('2005-8-17', new \DateTimeZone('UTC')), + new \DateTime('2005-8-17', new \DateTimeZone('UTC')) + ), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::markRead() + * + * @return void + */ + public function testMarkRead() + { + $this->response->code = 205; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('put') + ->with('/notifications', '{"unread":true,"read":true}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->markRead(), + $this->equalTo($this->response->body) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::markRead() + * + * @return void + */ + public function testMarkReadLastRead() + { + $this->response->code = 205; + $this->response->body = ''; + + $date = new \DateTime('1966-09-14', new \DateTimeZone('UTC')); + $data = '{"unread":true,"read":true,"last_read_at":"1966-09-14T00:00:00+00:00"}'; + + $this->client->expects($this->once()) + ->method('put') + ->with('/notifications', $data, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->markRead(true, true, $date), + $this->equalTo($this->response->body) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::markReadRepository() + * + * @return void + */ + public function testMarkReadRepository() + { + $this->response->code = 205; + $this->response->body = ''; + + $data = '{"unread":true,"read":true}'; + + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/notifications', $data, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->markReadRepository('joomla', 'joomla-platform', true, true), + $this->equalTo($this->response->body) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::markReadRepository() + * + * @return void + */ + public function testMarkReadRepositoryLastRead() + { + $this->response->code = 205; + $this->response->body = ''; + + $date = new \DateTime('1966-09-14', new \DateTimeZone('UTC')); + $data = '{"unread":true,"read":true,"last_read_at":"1966-09-14T00:00:00+00:00"}'; + + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/notifications', $data, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->markReadRepository('joomla', 'joomla-platform', true, true, $date), + $this->equalTo($this->response->body) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::viewThread() + * + * @return void + */ + public function testViewThread() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/notifications/threads/1', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->viewThread(1), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::markReadThread() + * + * @return void + */ + public function testMarkReadThread() + { + $this->response->code = 205; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/notifications/threads/1', '{"unread":true,"read":true}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->markReadThread(1), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::getThreadSubscription() + * + * @return void + */ + public function testGetThreadSubscription() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/notifications/threads/1/subscription', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getThreadSubscription(1), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::setThreadSubscription() + * + * @return void + */ + public function testSetThreadSubscription() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/notifications/threads/1/subscription', '{"subscribed":true,"ignored":false}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->setThreadSubscription(1, true, false), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Notifications::deleteThreadSubscription() + * + * @return void + */ + public function testDeleteThreadSubscription() + { + $this->response->code = 204; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/notifications/threads/1/subscription', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->deleteThreadSubscription(1), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/Activity/StarringTest.php b/Tests/Package/Activity/StarringTest.php index cc8b6dc9..c1c4cf8f 100644 --- a/Tests/Package/Activity/StarringTest.php +++ b/Tests/Package/Activity/StarringTest.php @@ -1,4 +1,5 @@ object = new Starring($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Starring::getList() - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/stargazers', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Starring::getRepositories() - * - * @return void - */ - public function testGetRepositories() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/user/starred?sort=created&direction=desc', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getRepositories(), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Starring::getRepositories() - * - * @return void - */ - public function testGetRepositoriesWithName() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/users/{user}/starred?sort=created&direction=desc', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getRepositories('{user}'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Starring::getRepositories() - * - * Invalid sort option - * - * @return void - */ - public function testGetRepositoriesInvalidSort() - { - $this->expectException(\InvalidArgumentException::class); - - $this->object->getRepositories('', 'invalid'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Starring::getRepositories() - * - * Invalid direction option - * - * @return void - */ - public function testGetRepositoriesInvalidDirection() - { - $this->expectException(\InvalidArgumentException::class); - - $this->object->getRepositories('', 'created', 'invalid'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Starring::check() - * - * @return void - */ - public function testCheck() - { - $this->response->code = 204; - $this->response->body = true; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/starred/joomla/joomla-platform', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Starring::check() - * - * @return void - */ - public function testCheckFalse() - { - $this->response->code = 404; - $this->response->body = false; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/starred/joomla/joomla-platform', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Starring::check() - * - * @return void - */ - public function testCheckUnexpected() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - $this->response->body = false; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/starred/joomla/joomla-platform', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Starring::star() - * - * @return void - */ - public function testStar() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('put') - ->with('/user/starred/joomla/joomla-platform', '', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->star('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity\Starring::unstar() - * - * @return void - */ - public function testUnstar() - { - $this->response->code = 204; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/user/starred/joomla/joomla-platform', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->unstar('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } + /** + * @var Starring Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Starring($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Starring::getList() + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/stargazers', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Starring::getRepositories() + * + * @return void + */ + public function testGetRepositories() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/starred?sort=created&direction=desc', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getRepositories(), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Starring::getRepositories() + * + * @return void + */ + public function testGetRepositoriesWithName() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/{user}/starred?sort=created&direction=desc', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getRepositories('{user}'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Starring::getRepositories() + * + * Invalid sort option + * + * @return void + */ + public function testGetRepositoriesInvalidSort() + { + $this->expectException(\InvalidArgumentException::class); + + $this->object->getRepositories('', 'invalid'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Starring::getRepositories() + * + * Invalid direction option + * + * @return void + */ + public function testGetRepositoriesInvalidDirection() + { + $this->expectException(\InvalidArgumentException::class); + + $this->object->getRepositories('', 'created', 'invalid'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Starring::check() + * + * @return void + */ + public function testCheck() + { + $this->response->code = 204; + $this->response->body = true; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/starred/joomla/joomla-platform', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Starring::check() + * + * @return void + */ + public function testCheckFalse() + { + $this->response->code = 404; + $this->response->body = false; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/starred/joomla/joomla-platform', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Starring::check() + * + * @return void + */ + public function testCheckUnexpected() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + $this->response->body = false; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/starred/joomla/joomla-platform', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Starring::star() + * + * @return void + */ + public function testStar() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('put') + ->with('/user/starred/joomla/joomla-platform', '', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->star('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity\Starring::unstar() + * + * @return void + */ + public function testUnstar() + { + $this->response->code = 204; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/user/starred/joomla/joomla-platform', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->unstar('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/Activity/WatchingTest.php b/Tests/Package/Activity/WatchingTest.php index fca3fdef..5bc9b1e5 100644 --- a/Tests/Package/Activity/WatchingTest.php +++ b/Tests/Package/Activity/WatchingTest.php @@ -1,4 +1,5 @@ object = new Watching($this->options, $this->client); - } - - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/subscribers', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getRepositories method - * - * @return void - */ - public function testGetRepositories() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/user/subscriptions', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getRepositories(), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getRepositoriesUser method - * - * @return void - */ - public function testGetRepositoriesUser() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/users/joomla/subscriptions', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getRepositories('joomla'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getSubscription method - * - * @return void - */ - public function testGetSubscription() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/subscription', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getSubscription('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the setSubscription method - * - * @return void - */ - public function testSetSubscription() - { - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-platform/subscription', '{"subscribed":true,"ignored":false}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->setSubscription('joomla', 'joomla-platform', true, false), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the deleteSubscription method - * - * @return void - */ - public function testDeleteSubscription() - { - $this->response->code = 204; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/subscription', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->deleteSubscription('joomla', 'joomla-platform'), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the check method - * - * @return void - */ - public function testCheck() - { - $this->response->code = 204; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/subscriptions/joomla/joomla-platform', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla', 'joomla-platform'), - $this->equalTo(true) - ); - } - - /** - * Tests the checkFalse method - * - * @return void - */ - public function testCheckFalse() - { - $this->response->code = 404; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/subscriptions/joomla/joomla-platform', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla', 'joomla-platform'), - $this->equalTo(false) - ); - } - - /** - * Tests the checkUnexpected method - * - * @return void - */ - public function testCheckUnexpected() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/subscriptions/joomla/joomla-platform', array(), 0) - ->will($this->returnValue($this->response)); - - $this->object->check('joomla', 'joomla-platform'); - } - - /** - * Tests the watch method - * - * @return void - */ - public function testWatch() - { - $this->response->code = 204; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('put') - ->with('/user/subscriptions/joomla/joomla-platform', '', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->watch('joomla', 'joomla-platform'), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the unwatch method - * - * @return void - */ - public function testUnwatch() - { - $this->response->code = 204; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/user/subscriptions/joomla/joomla-platform', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->unwatch('joomla', 'joomla-platform'), - $this->equalTo($this->response->body) - ); - } + /** + * @var Watching Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Watching($this->options, $this->client); + } + + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/subscribers', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getRepositories method + * + * @return void + */ + public function testGetRepositories() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/subscriptions', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getRepositories(), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getRepositoriesUser method + * + * @return void + */ + public function testGetRepositoriesUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/joomla/subscriptions', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getRepositories('joomla'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getSubscription method + * + * @return void + */ + public function testGetSubscription() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/subscription', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getSubscription('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the setSubscription method + * + * @return void + */ + public function testSetSubscription() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/subscription', '{"subscribed":true,"ignored":false}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->setSubscription('joomla', 'joomla-platform', true, false), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the deleteSubscription method + * + * @return void + */ + public function testDeleteSubscription() + { + $this->response->code = 204; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/subscription', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->deleteSubscription('joomla', 'joomla-platform'), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the check method + * + * @return void + */ + public function testCheck() + { + $this->response->code = 204; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/subscriptions/joomla/joomla-platform', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla', 'joomla-platform'), + $this->equalTo(true) + ); + } + + /** + * Tests the checkFalse method + * + * @return void + */ + public function testCheckFalse() + { + $this->response->code = 404; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/subscriptions/joomla/joomla-platform', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla', 'joomla-platform'), + $this->equalTo(false) + ); + } + + /** + * Tests the checkUnexpected method + * + * @return void + */ + public function testCheckUnexpected() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/subscriptions/joomla/joomla-platform', [], 0) + ->will($this->returnValue($this->response)); + + $this->object->check('joomla', 'joomla-platform'); + } + + /** + * Tests the watch method + * + * @return void + */ + public function testWatch() + { + $this->response->code = 204; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('put') + ->with('/user/subscriptions/joomla/joomla-platform', '', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->watch('joomla', 'joomla-platform'), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the unwatch method + * + * @return void + */ + public function testUnwatch() + { + $this->response->code = 204; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/user/subscriptions/joomla/joomla-platform', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->unwatch('joomla', 'joomla-platform'), + $this->equalTo($this->response->body) + ); + } } diff --git a/Tests/Package/ActivityTest.php b/Tests/Package/ActivityTest.php index da2d6a54..8d56ef88 100644 --- a/Tests/Package/ActivityTest.php +++ b/Tests/Package/ActivityTest.php @@ -1,4 +1,5 @@ object = new Activity($this->options, $this->client); - } + $this->object = new Activity($this->options, $this->client); + } - /** - * Test method. - * - * @covers \Joomla\Github\Package\Activity::__construct() - * - * @return void - * - * @since 1.0 - */ - public function testConstruct() - { - // Dummy to make PHPUnit "happy" - self::assertEquals(true, true); - } + /** + * Test method. + * + * @covers \Joomla\Github\Package\Activity::__construct() + * + * @return void + * + * @since 1.0 + */ + public function testConstruct() + { + // Dummy to make PHPUnit "happy" + self::assertEquals(true, true); + } } diff --git a/Tests/Package/AuthorizationsTest.php b/Tests/Package/AuthorizationsTest.php index 5314e433..0a303f9a 100644 --- a/Tests/Package/AuthorizationsTest.php +++ b/Tests/Package/AuthorizationsTest.php @@ -1,4 +1,5 @@ object = new Authorization($this->options, $this->client); - } - - /** - * Tests the createAuthorisation method - * - * @return void - * - * @since 1.0 - */ - public function testCreate() - { - $this->response->code = 201; - - $authorisation = '{' - . '"scopes":["public_repo"],' - . '"note":"My test app",' - . '"note_url":"http:\/\/www.joomla.org"' - . '}'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/authorizations', $authorisation) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create(array('public_repo'), 'My test app', 'http://www.joomla.org'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the createAuthorisation method - simulated failure - * - * @return void - * - * @since 1.0 - */ - public function testCreateFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $authorisation = '{' - . '"scopes":["public_repo"],' - . '"note":"My test app",' - . '"note_url":"http:\/\/www.joomla.org"' - . '}'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/authorizations', $authorisation) - ->will($this->returnValue($this->response)); - - try - { - $this->object->create(array('public_repo'), 'My test app', 'http://www.joomla.org'); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the delete method - * - * @return void - * - * @since 1.0 - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/authorizations/42') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete(42), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the delete method - simulated failure - * - * @return void - * - * @since 1.0 - */ - public function testDeleteFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/authorizations/42') - ->will($this->returnValue($this->response)); - - try - { - $this->object->delete(42); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the deleteGrant method - * - * @return void - * - * @since 1.0 - */ - public function testDeleteGrant() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/authorizations/grants/42') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->deleteGrant(42), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the deleteGrant method - simulated failure - * - * @return void - * - * @since 1.0 - */ - public function testDeleteGrantFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/authorizations/grants/42') - ->will($this->returnValue($this->response)); - - try - { - $this->object->deleteGrant(42); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the editAuthorisation method - Add scopes - * - * @return void - * - * @since 1.0 - */ - public function testEditAddScopes() - { - $authorisation = '{' - . '"add_scopes":["public_repo","gist"],' - . '"note":"My test app",' - . '"note_url":"http:\/\/www.joomla.org"' - . '}'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/authorizations/42', $authorisation) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit(42, array(), array('public_repo', 'gist'), array(), 'My test app', 'http://www.joomla.org'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the editAuthorisation method - Remove scopes - * - * @return void - * - * @since 1.0 - */ - public function testEditRemoveScopes() - { - $authorisation = '{' - . '"remove_scopes":["public_repo","gist"],' - . '"note":"My test app",' - . '"note_url":"http:\/\/www.joomla.org"' - . '}'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/authorizations/42', $authorisation) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit(42, array(), array(), array('public_repo', 'gist'), 'My test app', 'http://www.joomla.org'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the editAuthorisation method - Scopes param - * - * @return void - * - * @since 1.0 - */ - public function testEditScopes() - { - $authorisation = '{' - . '"scopes":["public_repo","gist"],' - . '"note":"My test app",' - . '"note_url":"http:\/\/www.joomla.org"' - . '}'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/authorizations/42', $authorisation) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit(42, array('public_repo', 'gist'), array(), array(), 'My test app', 'http://www.joomla.org'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the editAuthorisation method - simulated failure - * - * @return void - * - * @since 1.0 - */ - public function testEditFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $authorisation = '{' - . '"add_scopes":["public_repo","gist"],' - . '"note":"My test app",' - . '"note_url":"http:\/\/www.joomla.org"' - . '}'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/authorizations/42', $authorisation) - ->will($this->returnValue($this->response)); - - try - { - $this->object->edit(42, array(), array('public_repo', 'gist'), array(), 'My test app', 'http://www.joomla.org'); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the editAuthorisation method - too many scope params - * - * @return void - * - * @since 1.0 - */ - public function testEditTooManyScopes() - { - $this->expectException(\RuntimeException::class); - - $this->object->edit(42, array(), array('public_repo', 'gist'), array('public_repo', 'gist'), 'My test app', 'http://www.joomla.org'); - } - - /** - * Tests the get method - * - * @return void - * - * @since 1.0 - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/authorizations/42') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get(42), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the get method - failure - * - * @return void - * - * @since 1.0 - */ - public function testGetFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/authorizations/42') - ->will($this->returnValue($this->response)); - - $this->object->get(42); - } - - /** - * Tests the getGrant method - * - * @return void - * - * @since 1.0 - */ - public function testGetGrant() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/authorizations/grants/42') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getGrant(42), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getGrant method - failure - * - * @return void - * - * @since 1.0 - */ - public function testGetGrantFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/authorizations/grants/42') - ->will($this->returnValue($this->response)); - - $this->object->getGrant(42); - } - - /** - * Tests the getList method - * - * @return void - * - * @since 1.0 - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/authorizations') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getList method - failure - * - * @return void - * - * @since 1.0 - */ - public function testGetListFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/authorizations') - ->will($this->returnValue($this->response)); - - $this->object->getList(); - } - - /** - * Tests the getListGrants method - * - * @return void - * - * @since 1.0 - */ - public function testGetListGrants() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/authorizations/grants') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListGrants(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getListGrants method - failure - * - * @return void - * - * @since 1.0 - */ - public function testGetListGrantsFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/authorizations/grants') - ->will($this->returnValue($this->response)); - - $this->object->getListGrants(); - } - - /** - * Tests the getRateLimit method - * - * @return void - * - * @since 1.0 - */ - public function testGetRateLimit() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/rate_limit') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getRateLimit(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getRateLimit method for an unlimited user. - * - * @return void - * - * @since 1.0 - */ - public function testGetRateLimitUnlimited() - { - $this->response->code = 404; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('get') - ->with('/rate_limit') - ->will($this->returnValue($this->response)); - - $this->assertFalse($this->object->getRateLimit()->limit, 'The limit should be false for unlimited'); - } - - /** - * Tests the getRateLimit method - failure - * - * @return void - * - * @since 1.0 - */ - public function testGetRateLimitFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/rate_limit') - ->will($this->returnValue($this->response)); - - $this->object->getRateLimit(); - } - - /** - * Tests the getAuthorizationLink method - * - * @return void - */ - public function testGetAuthorizationLink() - { - $this->response->code = 200; - $this->response->body = 'https://github.com/login/oauth/authorize?client_id=12345' - . '&redirect_uri=aaa&scope=bbb&state=ccc'; - - $this->assertThat( - $this->object->getAuthorizationLink('12345', 'aaa', 'bbb', 'ccc'), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the requestToken method - * - * @return void - */ - public function testRequestToken() - { - $this->response->code = 200; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('post') - ->with('https://github.com/login/oauth/access_token') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc'), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the requestTokenJson method - * - * @return void - */ - public function testRequestTokenJson() - { - $this->response->code = 200; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('post') - ->with('https://github.com/login/oauth/access_token') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc', 'json'), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the requestTokenXml method - * - * @return void - */ - public function testRequestTokenXml() - { - $this->response->code = 200; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('post') - ->with('https://github.com/login/oauth/access_token') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc', 'xml'), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the requestTokenInvalidFormat method - * - * @return void - */ - public function testRequestTokenInvalidFormat() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 200; - $this->response->body = ''; - - $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc', 'invalid'); - } - - /** - * Tests the revokeGrantForApplication method - * - * @return void - * - * @since 1.0 - */ - public function testRevokeGrantForApplication() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/applications/42/grants/1a2b3c') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->revokeGrantForApplication(42, '1a2b3c'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the revokeGrantForApplication method - failure - * - * @return void - * - * @since 1.0 - */ - public function testRevokeGrantForApplicationFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/applications/42/grants/1a2b3c') - ->will($this->returnValue($this->response)); - - $this->object->revokeGrantForApplication(42, '1a2b3c'); - } + /** + * @var Authorization + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @return void + * + * @since 1.0 + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Authorization($this->options, $this->client); + } + + /** + * Tests the createAuthorisation method + * + * @return void + * + * @since 1.0 + */ + public function testCreate() + { + $this->response->code = 201; + + $authorisation = '{' + . '"scopes":["public_repo"],' + . '"note":"My test app",' + . '"note_url":"http:\/\/www.joomla.org"' + . '}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/authorizations', $authorisation) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create(['public_repo'], 'My test app', 'http://www.joomla.org'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the createAuthorisation method - simulated failure + * + * @return void + * + * @since 1.0 + */ + public function testCreateFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $authorisation = '{' + . '"scopes":["public_repo"],' + . '"note":"My test app",' + . '"note_url":"http:\/\/www.joomla.org"' + . '}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/authorizations', $authorisation) + ->will($this->returnValue($this->response)); + + try { + $this->object->create(['public_repo'], 'My test app', 'http://www.joomla.org'); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the delete method + * + * @return void + * + * @since 1.0 + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/authorizations/42') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete(42), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the delete method - simulated failure + * + * @return void + * + * @since 1.0 + */ + public function testDeleteFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/authorizations/42') + ->will($this->returnValue($this->response)); + + try { + $this->object->delete(42); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the deleteGrant method + * + * @return void + * + * @since 1.0 + */ + public function testDeleteGrant() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/authorizations/grants/42') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->deleteGrant(42), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the deleteGrant method - simulated failure + * + * @return void + * + * @since 1.0 + */ + public function testDeleteGrantFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/authorizations/grants/42') + ->will($this->returnValue($this->response)); + + try { + $this->object->deleteGrant(42); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the editAuthorisation method - Add scopes + * + * @return void + * + * @since 1.0 + */ + public function testEditAddScopes() + { + $authorisation = '{' + . '"add_scopes":["public_repo","gist"],' + . '"note":"My test app",' + . '"note_url":"http:\/\/www.joomla.org"' + . '}'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/authorizations/42', $authorisation) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit(42, [], ['public_repo', 'gist'], [], 'My test app', 'http://www.joomla.org'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the editAuthorisation method - Remove scopes + * + * @return void + * + * @since 1.0 + */ + public function testEditRemoveScopes() + { + $authorisation = '{' + . '"remove_scopes":["public_repo","gist"],' + . '"note":"My test app",' + . '"note_url":"http:\/\/www.joomla.org"' + . '}'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/authorizations/42', $authorisation) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit(42, [], [], ['public_repo', 'gist'], 'My test app', 'http://www.joomla.org'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the editAuthorisation method - Scopes param + * + * @return void + * + * @since 1.0 + */ + public function testEditScopes() + { + $authorisation = '{' + . '"scopes":["public_repo","gist"],' + . '"note":"My test app",' + . '"note_url":"http:\/\/www.joomla.org"' + . '}'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/authorizations/42', $authorisation) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit(42, ['public_repo', 'gist'], [], [], 'My test app', 'http://www.joomla.org'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the editAuthorisation method - simulated failure + * + * @return void + * + * @since 1.0 + */ + public function testEditFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $authorisation = '{' + . '"add_scopes":["public_repo","gist"],' + . '"note":"My test app",' + . '"note_url":"http:\/\/www.joomla.org"' + . '}'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/authorizations/42', $authorisation) + ->will($this->returnValue($this->response)); + + try { + $this->object->edit(42, [], ['public_repo', 'gist'], [], 'My test app', 'http://www.joomla.org'); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the editAuthorisation method - too many scope params + * + * @return void + * + * @since 1.0 + */ + public function testEditTooManyScopes() + { + $this->expectException(\RuntimeException::class); + + $this->object->edit(42, [], ['public_repo', 'gist'], ['public_repo', 'gist'], 'My test app', 'http://www.joomla.org'); + } + + /** + * Tests the get method + * + * @return void + * + * @since 1.0 + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/authorizations/42') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get(42), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the get method - failure + * + * @return void + * + * @since 1.0 + */ + public function testGetFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/authorizations/42') + ->will($this->returnValue($this->response)); + + $this->object->get(42); + } + + /** + * Tests the getGrant method + * + * @return void + * + * @since 1.0 + */ + public function testGetGrant() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/authorizations/grants/42') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getGrant(42), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getGrant method - failure + * + * @return void + * + * @since 1.0 + */ + public function testGetGrantFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/authorizations/grants/42') + ->will($this->returnValue($this->response)); + + $this->object->getGrant(42); + } + + /** + * Tests the getList method + * + * @return void + * + * @since 1.0 + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/authorizations') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getList method - failure + * + * @return void + * + * @since 1.0 + */ + public function testGetListFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/authorizations') + ->will($this->returnValue($this->response)); + + $this->object->getList(); + } + + /** + * Tests the getListGrants method + * + * @return void + * + * @since 1.0 + */ + public function testGetListGrants() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/authorizations/grants') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListGrants(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getListGrants method - failure + * + * @return void + * + * @since 1.0 + */ + public function testGetListGrantsFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/authorizations/grants') + ->will($this->returnValue($this->response)); + + $this->object->getListGrants(); + } + + /** + * Tests the getRateLimit method + * + * @return void + * + * @since 1.0 + */ + public function testGetRateLimit() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/rate_limit') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getRateLimit(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getRateLimit method for an unlimited user. + * + * @return void + * + * @since 1.0 + */ + public function testGetRateLimitUnlimited() + { + $this->response->code = 404; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('get') + ->with('/rate_limit') + ->will($this->returnValue($this->response)); + + $this->assertFalse($this->object->getRateLimit()->limit, 'The limit should be false for unlimited'); + } + + /** + * Tests the getRateLimit method - failure + * + * @return void + * + * @since 1.0 + */ + public function testGetRateLimitFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/rate_limit') + ->will($this->returnValue($this->response)); + + $this->object->getRateLimit(); + } + + /** + * Tests the getAuthorizationLink method + * + * @return void + */ + public function testGetAuthorizationLink() + { + $this->response->code = 200; + $this->response->body = 'https://github.com/login/oauth/authorize?client_id=12345' + . '&redirect_uri=aaa&scope=bbb&state=ccc'; + + $this->assertThat( + $this->object->getAuthorizationLink('12345', 'aaa', 'bbb', 'ccc'), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the requestToken method + * + * @return void + */ + public function testRequestToken() + { + $this->response->code = 200; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('post') + ->with('https://github.com/login/oauth/access_token') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc'), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the requestTokenJson method + * + * @return void + */ + public function testRequestTokenJson() + { + $this->response->code = 200; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('post') + ->with('https://github.com/login/oauth/access_token') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc', 'json'), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the requestTokenXml method + * + * @return void + */ + public function testRequestTokenXml() + { + $this->response->code = 200; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('post') + ->with('https://github.com/login/oauth/access_token') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc', 'xml'), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the requestTokenInvalidFormat method + * + * @return void + */ + public function testRequestTokenInvalidFormat() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 200; + $this->response->body = ''; + + $this->object->requestToken('12345', 'aaa', 'bbb', 'ccc', 'invalid'); + } + + /** + * Tests the revokeGrantForApplication method + * + * @return void + * + * @since 1.0 + */ + public function testRevokeGrantForApplication() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/applications/42/grants/1a2b3c') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->revokeGrantForApplication(42, '1a2b3c'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the revokeGrantForApplication method - failure + * + * @return void + * + * @since 1.0 + */ + public function testRevokeGrantForApplicationFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/applications/42/grants/1a2b3c') + ->will($this->returnValue($this->response)); + + $this->object->revokeGrantForApplication(42, '1a2b3c'); + } } diff --git a/Tests/Package/Data/BlobsTest.php b/Tests/Package/Data/BlobsTest.php index f84a2330..856fabd5 100644 --- a/Tests/Package/Data/BlobsTest.php +++ b/Tests/Package/Data/BlobsTest.php @@ -1,4 +1,5 @@ object = new Blobs($this->options, $this->client); - } + $this->object = new Blobs($this->options, $this->client); + } - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/git/blobs/12345', array(), 0) - ->will($this->returnValue($this->response)); + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/git/blobs/12345', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', '12345'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', '12345'), + $this->equalTo(json_decode($this->response->body)) + ); + } - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/git/blobs', '{"content":"Hello w\u00f6rld","encoding":"utf-8"}', array(), 0) - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/git/blobs', '{"content":"Hello w\u00f6rld","encoding":"utf-8"}', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 'Hello wörld'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', 'Hello wörld'), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/Data/CommitsTest.php b/Tests/Package/Data/CommitsTest.php index c9f129e0..21b8f959 100644 --- a/Tests/Package/Data/CommitsTest.php +++ b/Tests/Package/Data/CommitsTest.php @@ -1,4 +1,5 @@ object = new Commits($this->options, $this->client); - } + $this->object = new Commits($this->options, $this->client); + } - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/git/commits/12345', array(), 0) - ->will($this->returnValue($this->response)); + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/git/commits/12345', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', '12345'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', '12345'), + $this->equalTo(json_decode($this->response->body)) + ); + } - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/git/commits', '{"message":"My Message","tree":"12345","parents":[]}', array(), 0) - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/git/commits', '{"message":"My Message","tree":"12345","parents":[]}', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 'My Message', '12345'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', 'My Message', '12345'), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/Data/RefsTest.php b/Tests/Package/Data/RefsTest.php index ce2ba7e9..36af0dc5 100755 --- a/Tests/Package/Data/RefsTest.php +++ b/Tests/Package/Data/RefsTest.php @@ -1,4 +1,5 @@ object = new Refs($this->options, $this->client); - } - - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/git/refs/heads/master') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 'heads/master'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the get method - * - * @return void - */ - public function testGetFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/git/refs/heads/master') - ->will($this->returnValue($this->response)); - - $this->object->get('joomla', 'joomla-platform', 'heads/master'); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - // Build the request data. - $data = json_encode( - array( - 'ref' => '/ref/heads/myhead', - 'sha' => 'This is the sha' - ) - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/git/refs', $data) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', '/ref/heads/myhead', 'This is the sha'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the create method - failure - * - * @return void - */ - public function testCreateFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 501; - $this->response->body = $this->errorString; - - // Build the request data. - $data = json_encode( - array( - 'ref' => '/ref/heads/myhead', - 'sha' => 'This is the sha' - ) - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/git/refs', $data) - ->will($this->returnValue($this->response)); - - $this->object->create('joomla', 'joomla-platform', '/ref/heads/myhead', 'This is the sha'); - } - - /** - * Tests the edit method - * - * @return void - */ - public function testEdit() - { - // Build the request data. - $data = json_encode( - array( - 'force' => true, - 'sha' => 'This is the sha' - ) - ); - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/git/refs/heads/master', $data) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-platform', 'heads/master', 'This is the sha', true), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the edit method - failure - * - * @return void - */ - public function testEditFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - // Build the request data. - $data = json_encode( - array( - 'sha' => 'This is the sha' - ) - ); - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/git/refs/heads/master', $data) - ->will($this->returnValue($this->response)); - - $this->object->edit('joomla', 'joomla-platform', 'heads/master', 'This is the sha'); - } - - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/git/refs') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getList method with a namespace - * - * @return void - */ - public function testGetListWithNamespace() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/git/refs/tags') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform', 'tags'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getList method - failure - * - * @return void - */ - public function testGetListFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/git/refs') - ->will($this->returnValue($this->response)); - - $this->object->getList('joomla', 'joomla-platform'); - } - - /** - * Tests the delete method - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - $this->response->body = ''; - - $ref = 'refs/heads/sc/featureA'; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/git/refs/' . $ref) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 'joomla-platform', $ref), - $this->equalTo('') - ); - } - - /** - * Tests the delete method - failure - * - * @return void - */ - public function testDeleteFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $ref = 'refs/heads/sc/featureA'; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/git/refs/' . $ref) - ->will($this->returnValue($this->response)); - - $this->object->delete('joomla', 'joomla-platform', $ref); - } + /** + * @var Refs Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Refs($this->options, $this->client); + } + + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/git/refs/heads/master') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 'heads/master'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the get method + * + * @return void + */ + public function testGetFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/git/refs/heads/master') + ->will($this->returnValue($this->response)); + + $this->object->get('joomla', 'joomla-platform', 'heads/master'); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + // Build the request data. + $data = json_encode( + [ + 'ref' => '/ref/heads/myhead', + 'sha' => 'This is the sha', + ] + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/git/refs', $data) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', '/ref/heads/myhead', 'This is the sha'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the create method - failure + * + * @return void + */ + public function testCreateFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 501; + $this->response->body = $this->errorString; + + // Build the request data. + $data = json_encode( + [ + 'ref' => '/ref/heads/myhead', + 'sha' => 'This is the sha', + ] + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/git/refs', $data) + ->will($this->returnValue($this->response)); + + $this->object->create('joomla', 'joomla-platform', '/ref/heads/myhead', 'This is the sha'); + } + + /** + * Tests the edit method + * + * @return void + */ + public function testEdit() + { + // Build the request data. + $data = json_encode( + [ + 'force' => true, + 'sha' => 'This is the sha', + ] + ); + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/git/refs/heads/master', $data) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit('joomla', 'joomla-platform', 'heads/master', 'This is the sha', true), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the edit method - failure + * + * @return void + */ + public function testEditFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + // Build the request data. + $data = json_encode( + [ + 'sha' => 'This is the sha', + ] + ); + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/git/refs/heads/master', $data) + ->will($this->returnValue($this->response)); + + $this->object->edit('joomla', 'joomla-platform', 'heads/master', 'This is the sha'); + } + + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/git/refs') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getList method with a namespace + * + * @return void + */ + public function testGetListWithNamespace() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/git/refs/tags') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform', 'tags'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getList method - failure + * + * @return void + */ + public function testGetListFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/git/refs') + ->will($this->returnValue($this->response)); + + $this->object->getList('joomla', 'joomla-platform'); + } + + /** + * Tests the delete method + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + $this->response->body = ''; + + $ref = 'refs/heads/sc/featureA'; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/git/refs/' . $ref) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 'joomla-platform', $ref), + $this->equalTo('') + ); + } + + /** + * Tests the delete method - failure + * + * @return void + */ + public function testDeleteFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $ref = 'refs/heads/sc/featureA'; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/git/refs/' . $ref) + ->will($this->returnValue($this->response)); + + $this->object->delete('joomla', 'joomla-platform', $ref); + } } diff --git a/Tests/Package/Data/TagsTest.php b/Tests/Package/Data/TagsTest.php index 02a4dec4..f3861735 100644 --- a/Tests/Package/Data/TagsTest.php +++ b/Tests/Package/Data/TagsTest.php @@ -1,4 +1,5 @@ object = new Tags($this->options, $this->client); - } + $this->object = new Tags($this->options, $this->client); + } - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/git/tags/12345', array(), 0) - ->will($this->returnValue($this->response)); + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/git/tags/12345', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', '12345'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', '12345'), + $this->equalTo(json_decode($this->response->body)) + ); + } - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; - $data = '{"tag":"0.1","message":"Message","object":"12345","type":"commit","tagger":' - . '{"name":"elkuku","email":"email@example.com","date":"123456789"}}'; - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/git/tags', $data, array(), 0) - ->will($this->returnValue($this->response)); + $data = '{"tag":"0.1","message":"Message","object":"12345","type":"commit","tagger":' + . '{"name":"elkuku","email":"email@example.com","date":"123456789"}}'; + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/git/tags', $data, [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', '0.1', 'Message', '12345', 'commit', 'elkuku', 'email@example.com', '123456789'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', '0.1', 'Message', '12345', 'commit', 'elkuku', 'email@example.com', '123456789'), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/Data/TreesTest.php b/Tests/Package/Data/TreesTest.php index 5f66eac5..f3459cfd 100644 --- a/Tests/Package/Data/TreesTest.php +++ b/Tests/Package/Data/TreesTest.php @@ -1,4 +1,5 @@ object = new Trees($this->options, $this->client); - } + $this->object = new Trees($this->options, $this->client); + } - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/git/trees/12345', array(), 0) - ->will($this->returnValue($this->response)); + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/git/trees/12345', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', '12345'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', '12345'), + $this->equalTo(json_decode($this->response->body)) + ); + } - /** - * Tests the getRecursively method - * - * @return void - */ - public function testGetRecursively() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/git/trees/12345?recursive=1', array(), 0) - ->will($this->returnValue($this->response)); + /** + * Tests the getRecursively method + * + * @return void + */ + public function testGetRecursively() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/git/trees/12345?recursive=1', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getRecursively('joomla', 'joomla-platform', '12345'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->getRecursively('joomla', 'joomla-platform', '12345'), + $this->equalTo(json_decode($this->response->body)) + ); + } - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/git/trees', '{"tree":"12345","base_tree":"678"}', array(), 0) - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/git/trees', '{"tree":"12345","base_tree":"678"}', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', '12345', '678'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', '12345', '678'), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/DataTest.php b/Tests/Package/DataTest.php index 5594c18a..95a997dc 100644 --- a/Tests/Package/DataTest.php +++ b/Tests/Package/DataTest.php @@ -1,4 +1,5 @@ object = new Data($this->options, $this->client); - } + $this->object = new Data($this->options, $this->client); + } - /** - * Test method. - * - * @covers \Joomla\Github\Package\Data::__construct() - * - * @return void - * - * @since 1.0 - */ - public function testConstruct() - { - // Dummy to make PHPUnit "happy" - self::assertEquals(true, true); - } + /** + * Test method. + * + * @covers \Joomla\Github\Package\Data::__construct() + * + * @return void + * + * @since 1.0 + */ + public function testConstruct() + { + // Dummy to make PHPUnit "happy" + self::assertEquals(true, true); + } } diff --git a/Tests/Package/EmojisTest.php b/Tests/Package/EmojisTest.php index 59685a55..0ad9dbd7 100755 --- a/Tests/Package/EmojisTest.php +++ b/Tests/Package/EmojisTest.php @@ -1,4 +1,5 @@ object = new Emojis($this->options, $this->client); - } + $this->object = new Emojis($this->options, $this->client); + } - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/emojis') - ->will($this->returnValue($this->response)); + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/emojis') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getList(), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->getList(), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the getList method - simulated failure - * - * @return void - */ - public function testGetListFailure() - { - $exception = false; + /** + * Tests the getList method - simulated failure + * + * @return void + */ + public function testGetListFailure() + { + $exception = false; - $this->response->code = 500; - $this->response->body = $this->errorString; + $this->response->code = 500; + $this->response->body = $this->errorString; - $this->client->expects($this->once()) - ->method('get') - ->with('/emojis') - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('get') + ->with('/emojis') + ->will($this->returnValue($this->response)); - try - { - $this->object->getList(); - } - catch (\DomainException $e) - { - $exception = true; + try { + $this->object->getList(); + } catch (\DomainException $e) { + $exception = true; - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } - $this->assertTrue($exception); - } + $this->assertTrue($exception); + } } diff --git a/Tests/Package/GistsTest.php b/Tests/Package/GistsTest.php index dde9503f..26f5454d 100755 --- a/Tests/Package/GistsTest.php +++ b/Tests/Package/GistsTest.php @@ -1,4 +1,5 @@ object = new Gists($this->options, $this->client); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - // Build the request data. - $data = json_encode( - array( - 'files' => array( - 'file2.txt' => array('content' => 'This is the second file') - ), - 'public' => true, - 'description' => 'This is a gist' - ) - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/gists', $data) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create( - array( - 'file2.txt' => 'This is the second file' - ), - true, - 'This is a gist' - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the create method loading file content from a file - * - * @return void - */ - public function testCreateGistFromFile() - { - $this->response->code = 201; - - // Build the request data. - $data = json_encode( - array( - 'files' => array( - 'gittest' => array('content' => 'GistContent' . PHP_EOL) - ), - 'public' => true, - 'description' => 'This is a gist' - ) - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/gists', $data) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create( - array( - __DIR__ . '/../data/gittest' - ), - true, - 'This is a gist' - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the create method loading file content from a file - file does not exist - * - * @return void - */ - public function testCreateGistFromFileNotFound() - { - $this->expectException(\InvalidArgumentException::class); - - $this->response->code = 501; - - $this->object->create( - array( - '/file/not/found' - ), - true, - 'This is a gist' - ); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreateFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - // Build the request data. - $data = json_encode( - array('files' => array(), 'public' => true, 'description' => 'This is a gist') - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/gists', $data) - ->will($this->returnValue($this->response)); - - try - { - $this->object->create(array(), true, 'This is a gist'); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the createComment method - simulated failure - * - * @return void - */ - public function testCreateComment() - { - $this->response->code = 201; - - $gist = new \stdClass; - $gist->body = 'My Insightful Comment'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/gists/523/comments', json_encode($gist)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->comments->create(523, 'My Insightful Comment'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the createComment method - simulated failure - * - * @return void - */ - public function testCreateCommentFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $gist = new \stdClass; - $gist->body = 'My Insightful Comment'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/gists/523/comments', json_encode($gist)) - ->will($this->returnValue($this->response)); - - try - { - $this->object->comments->create(523, 'My Insightful Comment'); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the delete method - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/gists/254') - ->will($this->returnValue($this->response)); - - $this->object->delete(254); - } - - /** - * Tests the delete method - simulated failure - * - * @return void - */ - public function testDeleteFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/gists/254') - ->will($this->returnValue($this->response)); - - try - { - $this->object->delete(254); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the deleteComment method - * - * @return void - */ - public function testDeleteComment() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/gists/comments/254') - ->will($this->returnValue($this->response)); - - $this->object->comments->delete(254); - } - - /** - * Tests the deleteComment method - simulated failure - * - * @return void - */ - public function testDeleteCommentFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/gists/comments/254') - ->will($this->returnValue($this->response)); - - try - { - $this->object->comments->delete(254); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the edit method - * - * @return void - */ - public function testEdit() - { - // Build the request data. - $data = json_encode( - array( - 'description' => 'This is a gist', - 'public' => true, - 'files' => array( - 'file1.txt' => array('content' => 'This is the first file'), - 'file2.txt' => array('content' => 'This is the second file') - ) - ) - ); - - $this->client->expects($this->once()) - ->method('patch') - ->with('/gists/512', $data) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit( - 512, - array( - 'file1.txt' => 'This is the first file', - 'file2.txt' => 'This is the second file' - ), - true, - 'This is a gist' - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the edit method - simulated failure - * - * @return void - */ - public function testEditFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - // Build the request data. - $data = json_encode( - array( - 'description' => 'This is a gist', - 'public' => true, - 'files' => array( - 'file1.txt' => array('content' => 'This is the first file'), - 'file2.txt' => array('content' => 'This is the second file') - ) - ) - ); - - $this->client->expects($this->once()) - ->method('patch') - ->with('/gists/512', $data) - ->will($this->returnValue($this->response)); - - try - { - $this->object->edit( - 512, - array( - 'file1.txt' => 'This is the first file', - 'file2.txt' => 'This is the second file' - ), - true, - 'This is a gist' - ); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the editComment method - * - * @return void - */ - public function testEditComment() - { - $gist = new \stdClass; - $gist->body = 'This comment is now even more insightful'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/gists/comments/523', json_encode($gist)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->comments->edit(523, 'This comment is now even more insightful'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the editComment method - simulated failure - * - * @return void - */ - public function testEditCommentFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $gist = new \stdClass; - $gist->body = 'This comment is now even more insightful'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/gists/comments/523', json_encode($gist)) - ->will($this->returnValue($this->response)); - - try - { - $this->object->comments->edit(523, 'This comment is now even more insightful'); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the fork method - * - * @return void - */ - public function testFork() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/gists/523/forks') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->fork(523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the fork method - simulated failure - * - * @return void - */ - public function testForkFailure() - { - $exception = false; - - $this->response->code = 501; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('post') - ->with('/gists/523/forks') - ->will($this->returnValue($this->response)); - - try - { - $this->object->fork(523); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get(523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the get method - simulated failure - * - * @return void - */ - public function testGetFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523') - ->will($this->returnValue($this->response)); - - try - { - $this->object->get(523); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the getComment method - * - * @return void - */ - public function testGetComment() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/comments/523') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->comments->get(523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getComment method - simulated failure - * - * @return void - */ - public function testGetCommentFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/comments/523') - ->will($this->returnValue($this->response)); - - try - { - $this->object->comments->get(523); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the getComments method - * - * @return void - */ - public function testGetComments() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/comments') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->comments->getList(523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getComments method - simulated failure - * - * @return void - */ - public function testGetCommentsFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/comments') - ->will($this->returnValue($this->response)); - - try - { - $this->object->comments->getList(523); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the getCommitList method - * - * @return void - */ - public function testGetCommitList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/commits') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getCommitList(523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getCommitList method - simulated failure - * - * @return void - */ - public function testGetCommitListFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/commits') - ->will($this->returnValue($this->response)); - - try - { - $this->object->getCommitList(523); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the getForkList method - * - * @return void - */ - public function testGetForkList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/forks') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getForkList(523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getForkList method - simulated failure - * - * @return void - */ - public function testGetForkListFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/forks') - ->will($this->returnValue($this->response)); - - try - { - $this->object->getForkList(523); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/gists') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getList method - simulated failure - * - * @return void - */ - public function testGetListFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists') - ->will($this->returnValue($this->response)); - - try - { - $this->object->getList(); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the getListByUser method - * - * @return void - */ - public function testGetListByUser() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/users/joomla/gists') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListByUser('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getListByUser method - simulated failure - * - * @return void - */ - public function testGetListByUserFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/users/joomla/gists') - ->will($this->returnValue($this->response)); - - try - { - $this->object->getListByUser('joomla'); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the getListPublic method - * - * @return void - */ - public function testGetListPublic() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/public') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListPublic(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getListPublic method - simulated failure - * - * @return void - */ - public function testGetListPublicFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/public') - ->will($this->returnValue($this->response)); - - try - { - $this->object->getListPublic(); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the getListStarred method - * - * @return void - */ - public function testGetListStarred() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/starred') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListStarred(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getListStarred method - simulated failure - * - * @return void - */ - public function testGetListStarredFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/starred') - ->will($this->returnValue($this->response)); - - try - { - $this->object->getListStarred(); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the getRevision method - * - * @return void - */ - public function testGetRevision() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/a1b2c3') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getRevision(523, 'a1b2c3'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getRevision method - simulated failure - * - * @return void - */ - public function testGetRevisionFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/a1b2c3') - ->will($this->returnValue($this->response)); - - try - { - $this->object->getRevision(523, 'a1b2c3'); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the isStarred method when the gist has been starred - * - * @return void - */ - public function testIsStarredTrue() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/star') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->isStarred(523), - $this->equalTo(true) - ); - } - - /** - * Tests the isStarred method when the gist has not been starred - * - * @return void - */ - public function testIsStarredFalse() - { - $this->response->code = 404; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/star') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->isStarred(523), - $this->equalTo(false) - ); - } - - /** - * Tests the isStarred method expecting a failure response - * - * @return void - */ - public function testIsStarredFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gists/523/star') - ->will($this->returnValue($this->response)); - - try - { - $this->object->isStarred(523); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the star method - * - * @return void - */ - public function testStar() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('put') - ->with('/gists/523/star', '') - ->will($this->returnValue($this->response)); - - $this->object->star(523); - } - - /** - * Tests the star method - simulated failure - * - * @return void - */ - public function testStarFailure() - { - $exception = false; - - $this->response->code = 504; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('put') - ->with('/gists/523/star', '') - ->will($this->returnValue($this->response)); - - try - { - $this->object->star(523); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Tests the unstar method - * - * @return void - */ - public function testUnstar() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/gists/523/star') - ->will($this->returnValue($this->response)); - - $this->object->unstar(523); - } - - /** - * Tests the unstar method - simulated failure - * - * @return void - */ - public function testUnstarFailure() - { - $exception = false; - - $this->response->code = 504; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/gists/523/star') - ->will($this->returnValue($this->response)); - - try - { - $this->object->unstar(523); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } + /** + * @var Gists + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Gists($this->options, $this->client); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + // Build the request data. + $data = json_encode( + [ + 'files' => [ + 'file2.txt' => ['content' => 'This is the second file'], + ], + 'public' => true, + 'description' => 'This is a gist', + ] + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/gists', $data) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create( + [ + 'file2.txt' => 'This is the second file', + ], + true, + 'This is a gist' + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the create method loading file content from a file + * + * @return void + */ + public function testCreateGistFromFile() + { + $this->response->code = 201; + + // Build the request data. + $data = json_encode( + [ + 'files' => [ + 'gittest' => ['content' => 'GistContent' . PHP_EOL], + ], + 'public' => true, + 'description' => 'This is a gist', + ] + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/gists', $data) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create( + [ + __DIR__ . '/../data/gittest', + ], + true, + 'This is a gist' + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the create method loading file content from a file - file does not exist + * + * @return void + */ + public function testCreateGistFromFileNotFound() + { + $this->expectException(\InvalidArgumentException::class); + + $this->response->code = 501; + + $this->object->create( + [ + '/file/not/found', + ], + true, + 'This is a gist' + ); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreateFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + // Build the request data. + $data = json_encode( + ['files' => [], 'public' => true, 'description' => 'This is a gist'] + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/gists', $data) + ->will($this->returnValue($this->response)); + + try { + $this->object->create([], true, 'This is a gist'); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the createComment method - simulated failure + * + * @return void + */ + public function testCreateComment() + { + $this->response->code = 201; + + $gist = new \stdClass(); + $gist->body = 'My Insightful Comment'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/gists/523/comments', json_encode($gist)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->comments->create(523, 'My Insightful Comment'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the createComment method - simulated failure + * + * @return void + */ + public function testCreateCommentFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $gist = new \stdClass(); + $gist->body = 'My Insightful Comment'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/gists/523/comments', json_encode($gist)) + ->will($this->returnValue($this->response)); + + try { + $this->object->comments->create(523, 'My Insightful Comment'); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the delete method + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/gists/254') + ->will($this->returnValue($this->response)); + + $this->object->delete(254); + } + + /** + * Tests the delete method - simulated failure + * + * @return void + */ + public function testDeleteFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/gists/254') + ->will($this->returnValue($this->response)); + + try { + $this->object->delete(254); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the deleteComment method + * + * @return void + */ + public function testDeleteComment() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/gists/comments/254') + ->will($this->returnValue($this->response)); + + $this->object->comments->delete(254); + } + + /** + * Tests the deleteComment method - simulated failure + * + * @return void + */ + public function testDeleteCommentFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/gists/comments/254') + ->will($this->returnValue($this->response)); + + try { + $this->object->comments->delete(254); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the edit method + * + * @return void + */ + public function testEdit() + { + // Build the request data. + $data = json_encode( + [ + 'description' => 'This is a gist', + 'public' => true, + 'files' => [ + 'file1.txt' => ['content' => 'This is the first file'], + 'file2.txt' => ['content' => 'This is the second file'], + ], + ] + ); + + $this->client->expects($this->once()) + ->method('patch') + ->with('/gists/512', $data) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit( + 512, + [ + 'file1.txt' => 'This is the first file', + 'file2.txt' => 'This is the second file', + ], + true, + 'This is a gist' + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the edit method - simulated failure + * + * @return void + */ + public function testEditFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + // Build the request data. + $data = json_encode( + [ + 'description' => 'This is a gist', + 'public' => true, + 'files' => [ + 'file1.txt' => ['content' => 'This is the first file'], + 'file2.txt' => ['content' => 'This is the second file'], + ], + ] + ); + + $this->client->expects($this->once()) + ->method('patch') + ->with('/gists/512', $data) + ->will($this->returnValue($this->response)); + + try { + $this->object->edit( + 512, + [ + 'file1.txt' => 'This is the first file', + 'file2.txt' => 'This is the second file', + ], + true, + 'This is a gist' + ); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the editComment method + * + * @return void + */ + public function testEditComment() + { + $gist = new \stdClass(); + $gist->body = 'This comment is now even more insightful'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/gists/comments/523', json_encode($gist)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->comments->edit(523, 'This comment is now even more insightful'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the editComment method - simulated failure + * + * @return void + */ + public function testEditCommentFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $gist = new \stdClass(); + $gist->body = 'This comment is now even more insightful'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/gists/comments/523', json_encode($gist)) + ->will($this->returnValue($this->response)); + + try { + $this->object->comments->edit(523, 'This comment is now even more insightful'); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the fork method + * + * @return void + */ + public function testFork() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/gists/523/forks') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->fork(523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the fork method - simulated failure + * + * @return void + */ + public function testForkFailure() + { + $exception = false; + + $this->response->code = 501; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('post') + ->with('/gists/523/forks') + ->will($this->returnValue($this->response)); + + try { + $this->object->fork(523); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get(523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the get method - simulated failure + * + * @return void + */ + public function testGetFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523') + ->will($this->returnValue($this->response)); + + try { + $this->object->get(523); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the getComment method + * + * @return void + */ + public function testGetComment() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/comments/523') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->comments->get(523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getComment method - simulated failure + * + * @return void + */ + public function testGetCommentFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/comments/523') + ->will($this->returnValue($this->response)); + + try { + $this->object->comments->get(523); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the getComments method + * + * @return void + */ + public function testGetComments() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/comments') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->comments->getList(523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getComments method - simulated failure + * + * @return void + */ + public function testGetCommentsFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/comments') + ->will($this->returnValue($this->response)); + + try { + $this->object->comments->getList(523); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the getCommitList method + * + * @return void + */ + public function testGetCommitList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/commits') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getCommitList(523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getCommitList method - simulated failure + * + * @return void + */ + public function testGetCommitListFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/commits') + ->will($this->returnValue($this->response)); + + try { + $this->object->getCommitList(523); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the getForkList method + * + * @return void + */ + public function testGetForkList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/forks') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getForkList(523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getForkList method - simulated failure + * + * @return void + */ + public function testGetForkListFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/forks') + ->will($this->returnValue($this->response)); + + try { + $this->object->getForkList(523); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/gists') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getList method - simulated failure + * + * @return void + */ + public function testGetListFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists') + ->will($this->returnValue($this->response)); + + try { + $this->object->getList(); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the getListByUser method + * + * @return void + */ + public function testGetListByUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/joomla/gists') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListByUser('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getListByUser method - simulated failure + * + * @return void + */ + public function testGetListByUserFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/users/joomla/gists') + ->will($this->returnValue($this->response)); + + try { + $this->object->getListByUser('joomla'); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the getListPublic method + * + * @return void + */ + public function testGetListPublic() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/public') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListPublic(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getListPublic method - simulated failure + * + * @return void + */ + public function testGetListPublicFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/public') + ->will($this->returnValue($this->response)); + + try { + $this->object->getListPublic(); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the getListStarred method + * + * @return void + */ + public function testGetListStarred() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/starred') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListStarred(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getListStarred method - simulated failure + * + * @return void + */ + public function testGetListStarredFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/starred') + ->will($this->returnValue($this->response)); + + try { + $this->object->getListStarred(); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the getRevision method + * + * @return void + */ + public function testGetRevision() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/a1b2c3') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getRevision(523, 'a1b2c3'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getRevision method - simulated failure + * + * @return void + */ + public function testGetRevisionFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/a1b2c3') + ->will($this->returnValue($this->response)); + + try { + $this->object->getRevision(523, 'a1b2c3'); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the isStarred method when the gist has been starred + * + * @return void + */ + public function testIsStarredTrue() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/star') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->isStarred(523), + $this->equalTo(true) + ); + } + + /** + * Tests the isStarred method when the gist has not been starred + * + * @return void + */ + public function testIsStarredFalse() + { + $this->response->code = 404; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/star') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->isStarred(523), + $this->equalTo(false) + ); + } + + /** + * Tests the isStarred method expecting a failure response + * + * @return void + */ + public function testIsStarredFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gists/523/star') + ->will($this->returnValue($this->response)); + + try { + $this->object->isStarred(523); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the star method + * + * @return void + */ + public function testStar() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('put') + ->with('/gists/523/star', '') + ->will($this->returnValue($this->response)); + + $this->object->star(523); + } + + /** + * Tests the star method - simulated failure + * + * @return void + */ + public function testStarFailure() + { + $exception = false; + + $this->response->code = 504; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('put') + ->with('/gists/523/star', '') + ->will($this->returnValue($this->response)); + + try { + $this->object->star(523); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Tests the unstar method + * + * @return void + */ + public function testUnstar() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/gists/523/star') + ->will($this->returnValue($this->response)); + + $this->object->unstar(523); + } + + /** + * Tests the unstar method - simulated failure + * + * @return void + */ + public function testUnstarFailure() + { + $exception = false; + + $this->response->code = 504; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/gists/523/star') + ->will($this->returnValue($this->response)); + + try { + $this->object->unstar(523); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } } diff --git a/Tests/Package/GitignoreTest.php b/Tests/Package/GitignoreTest.php index 536523eb..8a9de29d 100644 --- a/Tests/Package/GitignoreTest.php +++ b/Tests/Package/GitignoreTest.php @@ -1,4 +1,5 @@ object = new Gitignore($this->options, $this->client); - } - - /** - * Tests the getList method. - * - * @return void - * - * @since 1.0 - */ - public function testGetList() - { - $this->response->code = 200; - $this->response->body = '[ + /** + * @var Gitignore + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Gitignore($this->options, $this->client); + } + + /** + * Tests the getList method. + * + * @return void + * + * @since 1.0 + */ + public function testGetList() + { + $this->response->code = 200; + $this->response->body = '[ "Actionscript", "Android", "AppceleratorTitanium", @@ -56,55 +57,55 @@ public function testGetList() "C++" ]'; - $this->client->expects($this->once()) - ->method('get') - ->with('/gitignore/templates', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList(), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the get method. - * - * @return void - * - * @since 1.0 - */ - public function testGet() - { - $this->response->code = 200; - $this->response->body = '{ + $this->client->expects($this->once()) + ->method('get') + ->with('/gitignore/templates', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList(), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the get method. + * + * @return void + * + * @since 1.0 + */ + public function testGet() + { + $this->response->code = 200; + $this->response->body = '{ "name": "C", "source": "# Object files\n*.o\n\n# Libraries\n*.lib\n*.a\n\n# Shared objects (inc. Windows DLLs)\n' - . '*.dll\n*.so\n*.so.*\n*.dylib\n\n# Executables\n*.exe\n*.out\n*.app\n" + . '*.dll\n*.so\n*.so.*\n*.dylib\n\n# Executables\n*.exe\n*.out\n*.app\n" }'; - $this->client->expects($this->once()) - ->method('get') - ->with('/gitignore/templates/C', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('C'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the get method with raw return data. - * - * @return void - * - * @since 1.0 - */ - public function testGetRaw() - { - $this->response->code = 200; - $this->response->body = '# Object files + $this->client->expects($this->once()) + ->method('get') + ->with('/gitignore/templates/C', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('C'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the get method with raw return data. + * + * @return void + * + * @since 1.0 + */ + public function testGetRaw() + { + $this->response->code = 200; + $this->response->body = '# Object files *.o # Libraries @@ -123,38 +124,38 @@ public function testGetRaw() *.app '; - $this->client->expects($this->once()) - ->method('get') - ->with('/gitignore/templates/C', array('Accept' => 'application/vnd.github.raw+json'), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('C', true), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the get method with failure. - * - * @since 1.0 - * @return void - */ - public function testGetFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 404; - $this->response->body = '{"message":"Not found"}'; - - $this->client->expects($this->once()) - ->method('get') - ->with('/gitignore/templates/X', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('X'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->client->expects($this->once()) + ->method('get') + ->with('/gitignore/templates/C', ['Accept' => 'application/vnd.github.raw+json'], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('C', true), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the get method with failure. + * + * @since 1.0 + * @return void + */ + public function testGetFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 404; + $this->response->body = '{"message":"Not found"}'; + + $this->client->expects($this->once()) + ->method('get') + ->with('/gitignore/templates/X', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('X'), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/GraphqlTest.php b/Tests/Package/GraphqlTest.php index d36e017a..6538debb 100644 --- a/Tests/Package/GraphqlTest.php +++ b/Tests/Package/GraphqlTest.php @@ -1,4 +1,5 @@ object = new Graphql($this->options, $this->client); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 200; - $this->response->body = $this->sampleString; - - // Build the query. - $query = 'foo'; - - // Build the request data. - $data = array( - 'query' => $query, - ); - - // Build the headers. - $headers = array( - 'Accept' => 'application/vnd.github.v4+json', - 'Content-Type' => 'application/json', - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/graphql', json_encode($data), $headers) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->execute($query), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreateFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - // Build the query. - $query = 'foo'; - - // Build the request data. - $data = array( - 'query' => $query, - ); - - // Build the headers. - $headers = array( - 'Accept' => 'application/vnd.github.v4+json', - 'Content-Type' => 'application/json', - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/graphql', json_encode($data), $headers) - ->will($this->returnValue($this->response)); - - try - { - $this->object->execute($query); - $this->fail('Exception not thrown'); - } - catch (\DomainException $e) - { - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - } + /** + * @var Graphql + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Graphql($this->options, $this->client); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 200; + $this->response->body = $this->sampleString; + + // Build the query. + $query = 'foo'; + + // Build the request data. + $data = [ + 'query' => $query, + ]; + + // Build the headers. + $headers = [ + 'Accept' => 'application/vnd.github.v4+json', + 'Content-Type' => 'application/json', + ]; + + $this->client->expects($this->once()) + ->method('post') + ->with('/graphql', json_encode($data), $headers) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->execute($query), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreateFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + // Build the query. + $query = 'foo'; + + // Build the request data. + $data = [ + 'query' => $query, + ]; + + // Build the headers. + $headers = [ + 'Accept' => 'application/vnd.github.v4+json', + 'Content-Type' => 'application/json', + ]; + + $this->client->expects($this->once()) + ->method('post') + ->with('/graphql', json_encode($data), $headers) + ->will($this->returnValue($this->response)); + + try { + $this->object->execute($query); + $this->fail('Exception not thrown'); + } catch (\DomainException $e) { + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + } } diff --git a/Tests/Package/Issues/AssigneesTest.php b/Tests/Package/Issues/AssigneesTest.php index b17450b1..6ce15ec9 100644 --- a/Tests/Package/Issues/AssigneesTest.php +++ b/Tests/Package/Issues/AssigneesTest.php @@ -1,4 +1,5 @@ object = new Assignees($this->options, $this->client); - } - - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->response->code = 200; - $this->response->body = '[ + /** + * @var Assignees Object under test. + * @since 1.0 + */ + protected $object; + + /** + * @var string + * @since 1.0 + */ + protected $owner = 'joomla'; + + /** + * @var string + * @since 1.0 + */ + protected $repo = 'joomla-framework'; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Assignees($this->options, $this->client); + } + + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->response->code = 200; + $this->response->body = '[ { "login": "octocat", "id": 1, @@ -67,109 +68,109 @@ public function testGetList() } ]'; - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/' . $this->owner . '/' . $this->repo . '/assignees', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList($this->owner, $this->repo), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getList method - * Response: - * If the given assignee login belongs to an assignee for the repository, - * a 204 header with no content is returned. - * Otherwise a 404 status code is returned. - * - * @return void - */ - public function testCheck() - { - $this->response->code = 204; - $this->response->body = ''; - - $assignee = 'elkuku'; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/' . $this->owner . '/' . $this->repo . '/assignees/' . $assignee, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check($this->owner, $this->repo, $assignee), - $this->equalTo(true) - ); - } - - /** - * Tests the getList method with a negative response - * Response: - * If the given assignee login belongs to an assignee for the repository, - * a 204 header with no content is returned. - * Otherwise a 404 status code is returned. - * - * @return void - */ - public function testCheckNo() - { - $this->response->code = 404; - $this->response->body = ''; - - $assignee = 'elkuku'; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/' . $this->owner . '/' . $this->repo . '/assignees/' . $assignee, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check($this->owner, $this->repo, $assignee), - $this->equalTo(false) - ); - } - - /** - * Tests the getList method with a negative response - * Response: - * If the given assignee login belongs to an assignee for the repository, - * a 204 header with no content is returned. - * Otherwise a 404 status code is returned. - * - * @return void - */ - public function testCheckException() - { - $this->expectException(\DomainException::class); - - $this->response->code = 666; - $this->response->body = ''; - - $assignee = 'elkuku'; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/' . $this->owner . '/' . $this->repo . '/assignees/' . $assignee, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check($this->owner, $this->repo, $assignee), - $this->equalTo(false) - ); - } - - /** - * Tests the add method - * - * @return void - */ - public function testAdd() - { - $this->response->code = 201; - $this->response->body = '[ + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/' . $this->owner . '/' . $this->repo . '/assignees', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList($this->owner, $this->repo), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getList method + * Response: + * If the given assignee login belongs to an assignee for the repository, + * a 204 header with no content is returned. + * Otherwise a 404 status code is returned. + * + * @return void + */ + public function testCheck() + { + $this->response->code = 204; + $this->response->body = ''; + + $assignee = 'elkuku'; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/' . $this->owner . '/' . $this->repo . '/assignees/' . $assignee, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check($this->owner, $this->repo, $assignee), + $this->equalTo(true) + ); + } + + /** + * Tests the getList method with a negative response + * Response: + * If the given assignee login belongs to an assignee for the repository, + * a 204 header with no content is returned. + * Otherwise a 404 status code is returned. + * + * @return void + */ + public function testCheckNo() + { + $this->response->code = 404; + $this->response->body = ''; + + $assignee = 'elkuku'; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/' . $this->owner . '/' . $this->repo . '/assignees/' . $assignee, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check($this->owner, $this->repo, $assignee), + $this->equalTo(false) + ); + } + + /** + * Tests the getList method with a negative response + * Response: + * If the given assignee login belongs to an assignee for the repository, + * a 204 header with no content is returned. + * Otherwise a 404 status code is returned. + * + * @return void + */ + public function testCheckException() + { + $this->expectException(\DomainException::class); + + $this->response->code = 666; + $this->response->body = ''; + + $assignee = 'elkuku'; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/' . $this->owner . '/' . $this->repo . '/assignees/' . $assignee, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check($this->owner, $this->repo, $assignee), + $this->equalTo(false) + ); + } + + /** + * Tests the add method + * + * @return void + */ + public function testAdd() + { + $this->response->code = 201; + $this->response->body = '[ { "login": "octocat", "id": 1, @@ -179,26 +180,26 @@ public function testAdd() } ]'; - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/' . $this->owner . '/' . $this->repo . '/issues/123/assignees', json_encode(array('assignees' => array('joomla')))) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->add($this->owner, $this->repo, 123, array('joomla')), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the remove method - * - * @return void - */ - public function testRemove() - { - $this->response->code = 200; - $this->response->body = '[ + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/' . $this->owner . '/' . $this->repo . '/issues/123/assignees', json_encode(['assignees' => ['joomla']])) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->add($this->owner, $this->repo, 123, ['joomla']), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the remove method + * + * @return void + */ + public function testRemove() + { + $this->response->code = 200; + $this->response->body = '[ { "login": "octocat", "id": 1, @@ -208,14 +209,14 @@ public function testRemove() } ]'; - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/' . $this->owner . '/' . $this->repo . '/issues/123/assignees', [], null, json_encode(['assignees' => ['joomla']])) - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/' . $this->owner . '/' . $this->repo . '/issues/123/assignees', [], null, json_encode(['assignees' => ['joomla']])) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->remove($this->owner, $this->repo, 123, array('joomla')), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->remove($this->owner, $this->repo, 123, ['joomla']), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/Issues/CommentsTest.php b/Tests/Package/Issues/CommentsTest.php index 184baf65..86e13639 100644 --- a/Tests/Package/Issues/CommentsTest.php +++ b/Tests/Package/Issues/CommentsTest.php @@ -1,4 +1,5 @@ object = new Comments($this->options, $this->client); - } - - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues/1/comments', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform', '1'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getRepositoryList method - * - * @return void - */ - public function testGetRepositoryList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues/comments?sort=created&direction=asc', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getRepositoryList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getRepositoryListInvalidSort method - * - * @return void - */ - public function testGetRepositoryListInvalidSort() - { - $this->expectException(\UnexpectedValueException::class); - - $this->object->getRepositoryList('joomla', 'joomla-platform', 'invalid'); - } - - /** - * Tests the getRepositoryListInvalidDirection method - * - * @return void - */ - public function testGetRepositoryListInvalidDirection() - { - $this->expectException(\UnexpectedValueException::class); - - $this->object->getRepositoryList('joomla', 'joomla-platform', 'created', 'invalid'); - } - - /** - * Tests the getRepositoryListSince method - * - * @return void - */ - public function testGetRepositoryListSince() - { - $date = new \DateTime('1966-09-15 12:34:56', new \DateTimeZone('UTC')); - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues/comments?sort=created&direction=asc&since=1966-09-15T12:34:56+00:00', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getRepositoryList('joomla', 'joomla-platform', 'created', 'asc', $date), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues/comments/1', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 1), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the edit method - * - * @return void - */ - public function testEdit() - { - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/issues/comments/1', '{"body":"Hello"}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-platform', 1, 'Hello'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/issues/1/comments', '{"body":"Hello"}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 1, 'Hello'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the delete method - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/issues/comments/1', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 'joomla-platform', 1), - $this->equalTo(true) - ); - } + /** + * @var Comments Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Comments($this->options, $this->client); + } + + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues/1/comments', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform', '1'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getRepositoryList method + * + * @return void + */ + public function testGetRepositoryList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues/comments?sort=created&direction=asc', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getRepositoryList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getRepositoryListInvalidSort method + * + * @return void + */ + public function testGetRepositoryListInvalidSort() + { + $this->expectException(\UnexpectedValueException::class); + + $this->object->getRepositoryList('joomla', 'joomla-platform', 'invalid'); + } + + /** + * Tests the getRepositoryListInvalidDirection method + * + * @return void + */ + public function testGetRepositoryListInvalidDirection() + { + $this->expectException(\UnexpectedValueException::class); + + $this->object->getRepositoryList('joomla', 'joomla-platform', 'created', 'invalid'); + } + + /** + * Tests the getRepositoryListSince method + * + * @return void + */ + public function testGetRepositoryListSince() + { + $date = new \DateTime('1966-09-15 12:34:56', new \DateTimeZone('UTC')); + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues/comments?sort=created&direction=asc&since=1966-09-15T12:34:56+00:00', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getRepositoryList('joomla', 'joomla-platform', 'created', 'asc', $date), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues/comments/1', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 1), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the edit method + * + * @return void + */ + public function testEdit() + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/issues/comments/1', '{"body":"Hello"}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit('joomla', 'joomla-platform', 1, 'Hello'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/issues/1/comments', '{"body":"Hello"}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', 1, 'Hello'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the delete method + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/issues/comments/1', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 'joomla-platform', 1), + $this->equalTo(true) + ); + } } diff --git a/Tests/Package/Issues/EventsTest.php b/Tests/Package/Issues/EventsTest.php index 7347cde3..b0a4189c 100644 --- a/Tests/Package/Issues/EventsTest.php +++ b/Tests/Package/Issues/EventsTest.php @@ -1,4 +1,5 @@ object = new Events($this->options, $this->client); - } + $this->object = new Events($this->options, $this->client); + } - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues/1/events', array(), 0) - ->will($this->returnValue($this->response)); + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues/1/events', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform', '1'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform', '1'), + $this->equalTo(json_decode($this->response->body)) + ); + } - /** - * Tests the getListRepository method - * - * @return void - */ - public function testGetListRepository() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues/1/comments', array(), 0) - ->will($this->returnValue($this->response)); + /** + * Tests the getListRepository method + * + * @return void + */ + public function testGetListRepository() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues/1/comments', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getListRepository('joomla', 'joomla-platform', '1'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->getListRepository('joomla', 'joomla-platform', '1'), + $this->equalTo(json_decode($this->response->body)) + ); + } - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues/events/1', array(), 0) - ->will($this->returnValue($this->response)); + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues/events/1', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', '1'), - $this->equalTo(json_decode($this->response->body)) - ); - } + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', '1'), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/Issues/LabelsTest.php b/Tests/Package/Issues/LabelsTest.php index 4593795f..400e62ea 100644 --- a/Tests/Package/Issues/LabelsTest.php +++ b/Tests/Package/Issues/LabelsTest.php @@ -1,4 +1,5 @@ object = new Labels($this->options, $this->client); - } - - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/labels', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/labels/1', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', '1'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/labels', '{"name":"foobar","color":"red"}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 'foobar', 'red'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the createFailure method - * - * @return void - */ - public function testCreateFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 404; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/labels', '{"name":"foobar","color":"red"}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 'foobar', 'red'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the update method - * - * @return void - */ - public function testUpdate() - { - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/labels/foobar', '{"name":"boofaz","color":"red"}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->update('joomla', 'joomla-platform', 'foobar', 'boofaz', 'red'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the delete method - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/labels/foobar', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 'joomla-platform', 'foobar'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getListByIssue method - * - * @return void - */ - public function testGetListByIssue() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues/1/labels', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListByIssue('joomla', 'joomla-platform', 1), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the add method - * - * @return void - */ - public function testAdd() - { - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/issues/1/labels', '["A","B"]', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->add('joomla', 'joomla-platform', 1, array('A', 'B')), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the removeFromIssue method - * - * @return void - */ - public function testRemoveFromIssue() - { - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/issues/1/labels/foobar', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->removeFromIssue('joomla', 'joomla-platform', 1, 'foobar'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the replace method - * - * @return void - */ - public function testReplace() - { - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-platform/issues/1/labels', '["A","B"]', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->replace('joomla', 'joomla-platform', 1, array('A', 'B')), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the removeAllFromIssue method - * - * @return void - */ - public function testRemoveAllFromIssue() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/issues/1/labels', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->removeAllFromIssue('joomla', 'joomla-platform', 1), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getListByMilestone method - * - * @return void - */ - public function testGetListByMilestone() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/milestones/1/labels', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListByMilestone('joomla', 'joomla-platform', 1), - $this->equalTo(json_decode($this->response->body)) - ); - } + /** + * @var Labels Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Labels($this->options, $this->client); + } + + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/labels', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/labels/1', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', '1'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/labels', '{"name":"foobar","color":"red"}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', 'foobar', 'red'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the createFailure method + * + * @return void + */ + public function testCreateFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 404; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/labels', '{"name":"foobar","color":"red"}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', 'foobar', 'red'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the update method + * + * @return void + */ + public function testUpdate() + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/labels/foobar', '{"name":"boofaz","color":"red"}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->update('joomla', 'joomla-platform', 'foobar', 'boofaz', 'red'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the delete method + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/labels/foobar', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 'joomla-platform', 'foobar'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getListByIssue method + * + * @return void + */ + public function testGetListByIssue() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues/1/labels', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListByIssue('joomla', 'joomla-platform', 1), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the add method + * + * @return void + */ + public function testAdd() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/issues/1/labels', '["A","B"]', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->add('joomla', 'joomla-platform', 1, ['A', 'B']), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the removeFromIssue method + * + * @return void + */ + public function testRemoveFromIssue() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/issues/1/labels/foobar', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->removeFromIssue('joomla', 'joomla-platform', 1, 'foobar'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the replace method + * + * @return void + */ + public function testReplace() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/issues/1/labels', '["A","B"]', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->replace('joomla', 'joomla-platform', 1, ['A', 'B']), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the removeAllFromIssue method + * + * @return void + */ + public function testRemoveAllFromIssue() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/issues/1/labels', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->removeAllFromIssue('joomla', 'joomla-platform', 1), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getListByMilestone method + * + * @return void + */ + public function testGetListByMilestone() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/milestones/1/labels', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListByMilestone('joomla', 'joomla-platform', 1), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/Issues/MilestonesTest.php b/Tests/Package/Issues/MilestonesTest.php index d42a67de..e1fbcee1 100644 --- a/Tests/Package/Issues/MilestonesTest.php +++ b/Tests/Package/Issues/MilestonesTest.php @@ -1,4 +1,5 @@ object = new Milestones($this->options, $this->client); - } - - /** - * Tests the create method - * - * @return void - * - * @since 1.0 - */ - public function testCreate() - { - $this->response->code = 201; - - $milestone = '{' - . '"title":"My Milestone","state":"open","description":"This milestone is impossible","due_on":"2012-12-25T20:09:31Z"' - . '}'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/milestones', $milestone) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 'My Milestone', 'open', 'This milestone is impossible', '2012-12-25T20:09:31Z'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the create method - failure - * - * @return void - * - * @since 12.3 - */ - public function testCreateFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 501; - $this->response->body = $this->errorString; - - $milestone = '{' - . '"title":"My Milestone","state":"open","description":"This milestone is impossible","due_on":"2012-12-25T20:09:31Z"' - . '}'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/milestones', $milestone) - ->will($this->returnValue($this->response)); - - $this->object->create('joomla', 'joomla-platform', 'My Milestone', 'open', 'This milestone is impossible', '2012-12-25T20:09:31Z'); - } - - /** - * Tests the edit method - * - * @return void - * - * @since 12.3 - */ - public function testEdit() - { - $milestone = new \stdClass; - $milestone->state = 'closed'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/milestones/523', json_encode($milestone)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-platform', 523, null, 'closed'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the edit method with all parameters - * - * @return void - * - * @since 12.3 - */ - public function testEditAllParameters() - { - $milestone = '{' - . '"title":"{title}","state":"closed","description":"{description}","due_on":"2012-12-25T20:09:31Z"' - . '}'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/{user}/{repo}/milestones/523', $milestone) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('{user}', '{repo}', 523, '{title}', 'closed', '{description}', - '2012-12-25T20:09:31Z' - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the edit method - failure - * - * @return void - * - * @since 12.3 - */ - public function testEditFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $milestone = new \stdClass; - $milestone->state = 'closed'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/milestones/523', json_encode($milestone)) - ->will($this->returnValue($this->response)); - - $this->object->edit('joomla', 'joomla-platform', 523, null, 'closed'); - } - - /** - * Tests the get method - * - * @return void - * - * @since 12.3 - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/milestones/523') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the get method - failure - * - * @return void - * - * @since 12.3 - */ - public function testGetFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/milestones/523') - ->will($this->returnValue($this->response)); - - $this->object->get('joomla', 'joomla-platform', 523); - } - - /** - * Tests the getList method - * - * @return void - * - * @since 12.3 - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/milestones?state=open&sort=due_date&direction=desc') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getList method - failure - * - * @return void - * - * @since 12.3 - */ - public function testGetListFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/milestones?state=open&sort=due_date&direction=desc') - ->will($this->returnValue($this->response)); - - $this->object->getList('joomla', 'joomla-platform'); - } - - /** - * Tests the delete method - * - * @return void - * - * @since 12.3 - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/milestones/254') - ->will($this->returnValue($this->response)); - - $this->object->delete('joomla', 'joomla-platform', 254); - } - - /** - * Tests the delete method - failure - * - * @return void - * - * @since 12.3 - */ - public function testDeleteFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 504; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/milestones/254') - ->will($this->returnValue($this->response)); - - $this->object->delete('joomla', 'joomla-platform', 254); - } + /** + * @var Milestones Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Milestones($this->options, $this->client); + } + + /** + * Tests the create method + * + * @return void + * + * @since 1.0 + */ + public function testCreate() + { + $this->response->code = 201; + + $milestone = '{' + . '"title":"My Milestone","state":"open","description":"This milestone is impossible","due_on":"2012-12-25T20:09:31Z"' + . '}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/milestones', $milestone) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', 'My Milestone', 'open', 'This milestone is impossible', '2012-12-25T20:09:31Z'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the create method - failure + * + * @return void + * + * @since 12.3 + */ + public function testCreateFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 501; + $this->response->body = $this->errorString; + + $milestone = '{' + . '"title":"My Milestone","state":"open","description":"This milestone is impossible","due_on":"2012-12-25T20:09:31Z"' + . '}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/milestones', $milestone) + ->will($this->returnValue($this->response)); + + $this->object->create('joomla', 'joomla-platform', 'My Milestone', 'open', 'This milestone is impossible', '2012-12-25T20:09:31Z'); + } + + /** + * Tests the edit method + * + * @return void + * + * @since 12.3 + */ + public function testEdit() + { + $milestone = new \stdClass(); + $milestone->state = 'closed'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/milestones/523', json_encode($milestone)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit('joomla', 'joomla-platform', 523, null, 'closed'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the edit method with all parameters + * + * @return void + * + * @since 12.3 + */ + public function testEditAllParameters() + { + $milestone = '{' + . '"title":"{title}","state":"closed","description":"{description}","due_on":"2012-12-25T20:09:31Z"' + . '}'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/{user}/{repo}/milestones/523', $milestone) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit( + '{user}', + '{repo}', + 523, + '{title}', + 'closed', + '{description}', + '2012-12-25T20:09:31Z' + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the edit method - failure + * + * @return void + * + * @since 12.3 + */ + public function testEditFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $milestone = new \stdClass(); + $milestone->state = 'closed'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/milestones/523', json_encode($milestone)) + ->will($this->returnValue($this->response)); + + $this->object->edit('joomla', 'joomla-platform', 523, null, 'closed'); + } + + /** + * Tests the get method + * + * @return void + * + * @since 12.3 + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/milestones/523') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the get method - failure + * + * @return void + * + * @since 12.3 + */ + public function testGetFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/milestones/523') + ->will($this->returnValue($this->response)); + + $this->object->get('joomla', 'joomla-platform', 523); + } + + /** + * Tests the getList method + * + * @return void + * + * @since 12.3 + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/milestones?state=open&sort=due_date&direction=desc') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getList method - failure + * + * @return void + * + * @since 12.3 + */ + public function testGetListFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/milestones?state=open&sort=due_date&direction=desc') + ->will($this->returnValue($this->response)); + + $this->object->getList('joomla', 'joomla-platform'); + } + + /** + * Tests the delete method + * + * @return void + * + * @since 12.3 + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/milestones/254') + ->will($this->returnValue($this->response)); + + $this->object->delete('joomla', 'joomla-platform', 254); + } + + /** + * Tests the delete method - failure + * + * @return void + * + * @since 12.3 + */ + public function testDeleteFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 504; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/milestones/254') + ->will($this->returnValue($this->response)); + + $this->object->delete('joomla', 'joomla-platform', 254); + } } diff --git a/Tests/Package/IssuesTest.php b/Tests/Package/IssuesTest.php index 466e7750..5272c0f3 100755 --- a/Tests/Package/IssuesTest.php +++ b/Tests/Package/IssuesTest.php @@ -1,4 +1,5 @@ object = new Issues($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::create() - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $issue = new \stdClass; - $issue->title = '{title}'; - $issue->milestone = '{milestone}'; - $issue->labels = ['{label1}']; - $issue->body = '{body}'; - $issue->assignee = '{assignee}'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/{user}/{repo}/issues', json_encode($issue)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('{user}', '{repo}', '{title}', '{body}', '{assignee}', '{milestone}', array('{label1}')), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::create() - * - * @return void - */ - public function testCreate2() - { - $this->response->code = 201; - - $issue = new \stdClass; - $issue->title = '{title}'; - $issue->milestone = '{milestone}'; - $issue->labels = array('{label1}'); - $issue->body = '{body}'; - $issue->assignees = array('{assignee1}'); - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/{user}/{repo}/issues', json_encode($issue)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('{user}', '{repo}', '{title}', '{body}', null, '{milestone}', array('{label1}'), array('{assignee1}')), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::create() - * - * Failure - * - * @return void - */ - public function testCreateFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 501; - $this->response->body = $this->errorString; - - $issue = new \stdClass; - $issue->title = '{title}'; - $issue->milestone = '{milestone}'; - $issue->labels = array('{label1}'); - $issue->body = '{body}'; - $issue->assignee = '{assignee}'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/{user}/{repo}/issues', json_encode($issue)) - ->will($this->returnValue($this->response)); - - $this->object->create('{user}', '{repo}', '{title}', '{body}', '{assignee}', '{milestone}', array('{label1}')); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::create() - * - * @return void - */ - public function testCreateComment() - { - $this->response->code = 201; - - $issue = new \stdClass; - $issue->body = 'My Insightful Comment'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/issues/523/comments', json_encode($issue)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->comments->create('joomla', 'joomla-platform', 523, 'My Insightful Comment'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::create() - * - * Failure - * - * @return void - */ - public function testCreateCommentFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 501; - $this->response->body = $this->errorString; - - $issue = new \stdClass; - $issue->body = 'My Insightful Comment'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/issues/523/comments', json_encode($issue)) - ->will($this->returnValue($this->response)); - - $this->object->comments->create('joomla', 'joomla-platform', 523, 'My Insightful Comment'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::edit() - * - * @return void - */ - public function testEdit() - { - $issue = new \stdClass; - $issue->title = 'My issue'; - $issue->body = 'These are my changes - please review them'; - $issue->state = 'Closed'; - $issue->assignee = 'JoeAssignee'; - $issue->milestone = '12.2'; - $issue->labels = array('Fixed'); - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/issues/523', json_encode($issue)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-platform', 523, 'Closed', 'My issue', 'These are my changes - please review them', - 'JoeAssignee', '12.2', array('Fixed') - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::edit() - * - * Failure - * - * @return void - */ - public function testEditFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $issue = new \stdClass; - $issue->title = 'My issue'; - $issue->body = 'These are my changes - please review them'; - $issue->state = 'Closed'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/issues/523', json_encode($issue)) - ->will($this->returnValue($this->response)); - - $this->object->edit('joomla', 'joomla-platform', 523, 'Closed', 'My issue', 'These are my changes - please review them'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::get() - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues/523') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::get() - * - * Failure - * - * @return void - */ - public function testGetFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues/523') - ->will($this->returnValue($this->response)); - - $this->object->get('joomla', 'joomla-platform', 523); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::getList() - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/issues') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::getList() - * - * @return void - */ - public function testGetListAll() - { - $since = new \DateTime('January 1, 2012 12:12:12', new \DateTimeZone('UTC')); - - $this->client->expects($this->once()) - ->method('get') - ->with('/issues?filter={filter}&state={state}&labels={labels}&sort={sort}&direction={direction}&since=2012-01-01T12:12:12+0000') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('{filter}', '{state}', '{labels}', '{sort}', '{direction}', $since), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::getList() - * - * Failure - * - * @return void - */ - public function testGetListFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/issues') - ->will($this->returnValue($this->response)); - - $this->object->getList(); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::getListByRepository() - * - * @return void - */ - public function testGetListByRepository() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListByRepository('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::getListByRepository() - * - * With all parameters - * - * @return void - */ - public function testGetListByRepositoryAll() - { - $date = new \DateTime('January 1, 2012 12:12:12', new \DateTimeZone('UTC')); - - $this->client->expects($this->once()) - ->method('get') - ->with( - '/repos/joomla/joomla-platform/issues?milestone=25&state=closed&assignee=none&' . - 'mentioned=joomla-jenkins&labels=bug&sort=created&direction=asc&since=2012-01-01T12:12:12+00:00' - ) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListByRepository( - 'joomla', - 'joomla-platform', - '25', - 'closed', - 'none', - 'joomla-jenkins', - 'bug', - 'created', - 'asc', - $date - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::getListByRepository() - * - * Failure - * - * @return void - */ - public function testGetListByRepositoryFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/issues') - ->will($this->returnValue($this->response)); - - $this->object->getListByRepository('joomla', 'joomla-platform'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::lock() - * - * @return void - */ - public function testLock() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-platform/issues/523/lock') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->lock('joomla', 'joomla-platform', 523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::lock() - * - * Failure - * - * @return void - */ - public function testLockFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-platform/issues/523/lock') - ->will($this->returnValue($this->response)); - - $this->object->lock('joomla', 'joomla-platform', 523); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::unlock() - * - * @return void - */ - public function testUnlock() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/issues/523/lock') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->unlock('joomla', 'joomla-platform', 523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Issues::unlock() - * - * Failure - * - * @return void - */ - public function testUnlockFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/issues/523/lock') - ->will($this->returnValue($this->response)); - - $this->object->unlock('joomla', 'joomla-platform', 523); - } + /** + * @var Issues Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Issues($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::create() + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $issue = new \stdClass(); + $issue->title = '{title}'; + $issue->milestone = '{milestone}'; + $issue->labels = ['{label1}']; + $issue->body = '{body}'; + $issue->assignee = '{assignee}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/{user}/{repo}/issues', json_encode($issue)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('{user}', '{repo}', '{title}', '{body}', '{assignee}', '{milestone}', ['{label1}']), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::create() + * + * @return void + */ + public function testCreate2() + { + $this->response->code = 201; + + $issue = new \stdClass(); + $issue->title = '{title}'; + $issue->milestone = '{milestone}'; + $issue->labels = ['{label1}']; + $issue->body = '{body}'; + $issue->assignees = ['{assignee1}']; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/{user}/{repo}/issues', json_encode($issue)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('{user}', '{repo}', '{title}', '{body}', null, '{milestone}', ['{label1}'], ['{assignee1}']), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::create() + * + * Failure + * + * @return void + */ + public function testCreateFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 501; + $this->response->body = $this->errorString; + + $issue = new \stdClass(); + $issue->title = '{title}'; + $issue->milestone = '{milestone}'; + $issue->labels = ['{label1}']; + $issue->body = '{body}'; + $issue->assignee = '{assignee}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/{user}/{repo}/issues', json_encode($issue)) + ->will($this->returnValue($this->response)); + + $this->object->create('{user}', '{repo}', '{title}', '{body}', '{assignee}', '{milestone}', ['{label1}']); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::create() + * + * @return void + */ + public function testCreateComment() + { + $this->response->code = 201; + + $issue = new \stdClass(); + $issue->body = 'My Insightful Comment'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/issues/523/comments', json_encode($issue)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->comments->create('joomla', 'joomla-platform', 523, 'My Insightful Comment'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::create() + * + * Failure + * + * @return void + */ + public function testCreateCommentFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 501; + $this->response->body = $this->errorString; + + $issue = new \stdClass(); + $issue->body = 'My Insightful Comment'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/issues/523/comments', json_encode($issue)) + ->will($this->returnValue($this->response)); + + $this->object->comments->create('joomla', 'joomla-platform', 523, 'My Insightful Comment'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::edit() + * + * @return void + */ + public function testEdit() + { + $issue = new \stdClass(); + $issue->title = 'My issue'; + $issue->body = 'These are my changes - please review them'; + $issue->state = 'Closed'; + $issue->assignee = 'JoeAssignee'; + $issue->milestone = '12.2'; + $issue->labels = ['Fixed']; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/issues/523', json_encode($issue)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit( + 'joomla', + 'joomla-platform', + 523, + 'Closed', + 'My issue', + 'These are my changes - please review them', + 'JoeAssignee', + '12.2', + ['Fixed'] + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::edit() + * + * Failure + * + * @return void + */ + public function testEditFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $issue = new \stdClass(); + $issue->title = 'My issue'; + $issue->body = 'These are my changes - please review them'; + $issue->state = 'Closed'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/issues/523', json_encode($issue)) + ->will($this->returnValue($this->response)); + + $this->object->edit('joomla', 'joomla-platform', 523, 'Closed', 'My issue', 'These are my changes - please review them'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::get() + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues/523') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::get() + * + * Failure + * + * @return void + */ + public function testGetFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues/523') + ->will($this->returnValue($this->response)); + + $this->object->get('joomla', 'joomla-platform', 523); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::getList() + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/issues') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::getList() + * + * @return void + */ + public function testGetListAll() + { + $since = new \DateTime('January 1, 2012 12:12:12', new \DateTimeZone('UTC')); + + $this->client->expects($this->once()) + ->method('get') + ->with('/issues?filter={filter}&state={state}&labels={labels}&sort={sort}&direction={direction}&since=2012-01-01T12:12:12+0000') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('{filter}', '{state}', '{labels}', '{sort}', '{direction}', $since), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::getList() + * + * Failure + * + * @return void + */ + public function testGetListFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/issues') + ->will($this->returnValue($this->response)); + + $this->object->getList(); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::getListByRepository() + * + * @return void + */ + public function testGetListByRepository() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListByRepository('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::getListByRepository() + * + * With all parameters + * + * @return void + */ + public function testGetListByRepositoryAll() + { + $date = new \DateTime('January 1, 2012 12:12:12', new \DateTimeZone('UTC')); + + $this->client->expects($this->once()) + ->method('get') + ->with( + '/repos/joomla/joomla-platform/issues?milestone=25&state=closed&assignee=none&' . + 'mentioned=joomla-jenkins&labels=bug&sort=created&direction=asc&since=2012-01-01T12:12:12+00:00' + ) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListByRepository( + 'joomla', + 'joomla-platform', + '25', + 'closed', + 'none', + 'joomla-jenkins', + 'bug', + 'created', + 'asc', + $date + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::getListByRepository() + * + * Failure + * + * @return void + */ + public function testGetListByRepositoryFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/issues') + ->will($this->returnValue($this->response)); + + $this->object->getListByRepository('joomla', 'joomla-platform'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::lock() + * + * @return void + */ + public function testLock() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/issues/523/lock') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->lock('joomla', 'joomla-platform', 523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::lock() + * + * Failure + * + * @return void + */ + public function testLockFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/issues/523/lock') + ->will($this->returnValue($this->response)); + + $this->object->lock('joomla', 'joomla-platform', 523); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::unlock() + * + * @return void + */ + public function testUnlock() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/issues/523/lock') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->unlock('joomla', 'joomla-platform', 523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Issues::unlock() + * + * Failure + * + * @return void + */ + public function testUnlockFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/issues/523/lock') + ->will($this->returnValue($this->response)); + + $this->object->unlock('joomla', 'joomla-platform', 523); + } } diff --git a/Tests/Package/MarkdownTest.php b/Tests/Package/MarkdownTest.php index 95ca4689..8f31d555 100644 --- a/Tests/Package/MarkdownTest.php +++ b/Tests/Package/MarkdownTest.php @@ -1,4 +1,5 @@ object = new Markdown($this->options, $this->client); - } - - /** - * Tests the render method - * - * @return void - */ - public function testRender() - { - $this->response->code = 200; - $this->response->body = '

Hello world github/linguist#1 cool, ' - . 'and #1!

'; - - $text = 'Hello world github/linguist#1 **cool**, and #1!'; - $mode = 'gfm'; - $context = 'github/gollum'; - - $data = str_replace( - '\\/', '/', json_encode( - array( - 'text' => $text, - 'mode' => $mode, - 'context' => $context - ) - ) - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/markdown', $data, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->render($text, $mode, $context), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the renderInvalidMode method - * - * @return void - */ - public function testRenderInvalidMode() - { - $this->expectException(\InvalidArgumentException::class); - - $this->assertThat( - $this->object->render('', 'xxx', 'github/gollum'), - $this->equalTo('') - ); - } - - /** - * Tests the renderFailure method - * - * @return void - */ - public function testRenderFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 404; - $this->response->body = ''; - - $text = 'Hello world github/linguist#1 **cool**, and #1!'; - $mode = 'gfm'; - $context = 'github/gollum'; - - $data = str_replace( - '\\/', '/', json_encode( - array( - 'text' => $text, - 'mode' => $mode, - 'context' => $context - ) - ) - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/markdown', $data, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->render($text, $mode, $context), - $this->equalTo('') - ); - } + /** + * @var Markdown + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Markdown($this->options, $this->client); + } + + /** + * Tests the render method + * + * @return void + */ + public function testRender() + { + $this->response->code = 200; + $this->response->body = '

Hello world github/linguist#1 cool, ' + . 'and #1!

'; + + $text = 'Hello world github/linguist#1 **cool**, and #1!'; + $mode = 'gfm'; + $context = 'github/gollum'; + + $data = str_replace( + '\\/', + '/', + json_encode( + [ + 'text' => $text, + 'mode' => $mode, + 'context' => $context, + ] + ) + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/markdown', $data, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->render($text, $mode, $context), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the renderInvalidMode method + * + * @return void + */ + public function testRenderInvalidMode() + { + $this->expectException(\InvalidArgumentException::class); + + $this->assertThat( + $this->object->render('', 'xxx', 'github/gollum'), + $this->equalTo('') + ); + } + + /** + * Tests the renderFailure method + * + * @return void + */ + public function testRenderFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 404; + $this->response->body = ''; + + $text = 'Hello world github/linguist#1 **cool**, and #1!'; + $mode = 'gfm'; + $context = 'github/gollum'; + + $data = str_replace( + '\\/', + '/', + json_encode( + [ + 'text' => $text, + 'mode' => $mode, + 'context' => $context, + ] + ) + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/markdown', $data, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->render($text, $mode, $context), + $this->equalTo('') + ); + } } diff --git a/Tests/Package/MetaTest.php b/Tests/Package/MetaTest.php index f0964b4c..dd20ee95 100644 --- a/Tests/Package/MetaTest.php +++ b/Tests/Package/MetaTest.php @@ -1,4 +1,5 @@ object = new Meta($this->options, $this->client); - } + $this->object = new Meta($this->options, $this->client); + } - /** - * Tests the getMeta method - * - * @return void - * - * @since 1.0 - */ - public function testGetMeta() - { - $decodedResponse = new \stdClass; - $decodedResponse->hooks = array('127.0.0.1/32', '192.168.1.1/32', '10.10.1.1/27'); - $decodedResponse->git = array('127.0.0.1/32'); + /** + * Tests the getMeta method + * + * @return void + * + * @since 1.0 + */ + public function testGetMeta() + { + $decodedResponse = new \stdClass(); + $decodedResponse->hooks = ['127.0.0.1/32', '192.168.1.1/32', '10.10.1.1/27']; + $decodedResponse->git = ['127.0.0.1/32']; - $this->client->expects($this->once()) - ->method('get') - ->with('/meta') - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('get') + ->with('/meta') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getMeta(), - $this->equalTo($decodedResponse) - ); - } + $this->assertThat( + $this->object->getMeta(), + $this->equalTo($decodedResponse) + ); + } - /** - * Tests the getMeta method - failure - * - * @return void - * - * @since 1.0 - */ - public function testGetMetaFailure() - { - $this->expectException(\DomainException::class); + /** + * Tests the getMeta method - failure + * + * @return void + * + * @since 1.0 + */ + public function testGetMetaFailure() + { + $this->expectException(\DomainException::class); - $this->response->code = 500; - $this->response->body = $this->errorString; + $this->response->code = 500; + $this->response->body = $this->errorString; - $this->client->expects($this->once()) - ->method('get') - ->with('/meta') - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('get') + ->with('/meta') + ->will($this->returnValue($this->response)); - $this->object->getMeta(); - } + $this->object->getMeta(); + } } diff --git a/Tests/Package/Orgs/HooksTest.php b/Tests/Package/Orgs/HooksTest.php index 6ad1b4ff..b99201f5 100644 --- a/Tests/Package/Orgs/HooksTest.php +++ b/Tests/Package/Orgs/HooksTest.php @@ -1,4 +1,5 @@ object = new Hooks($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Hooks::getList() - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/hooks') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Hooks::get() - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/hooks/123') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 123), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Hooks::create() - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/orgs/joomla/hooks') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', '{url}', 'json', '{secret}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Hooks::create() - * - * @return void - */ - public function testCreateFailure() - { - $this->expectException(\UnexpectedValueException::class); - $this->expectExceptionMessage('Content type must be either "form" or "json"'); - - $this->object->create('joomla', '{url}', '{invalid}'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Hooks::create() - * - * @return void - */ - public function testCreateInvalidEvent() - { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Your events array contains an unauthorized event.'); - - $this->object->create('{org}', '{url}', 'form', null, false, ['{invalid}']); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Hooks::edit() - * - * @return void - */ - public function testEdit() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/orgs/{org}/hooks') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('{org}', '{url}', 'json', '{secret}', 1, array('create'), true), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Hooks::edit() - * - * @return void - */ - public function testEditFailure() - { - $this->expectException(\UnexpectedValueException::class); - - $this->object->edit('{org}', '{url}', '{invalid}'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Hooks::edit() - * - * @return void - */ - public function testEditFailure2() - { - $this->expectException(\RuntimeException::class); - - $this->object->edit('{org}', '{url}', 'json', '{secret}', 1, array('{invalid}')); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Hooks::ping() - * - * @return void - */ - public function testPing() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('post') - ->with('/orgs/{org}/hooks/123/pings') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->ping('{org}', 123), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Hooks::delete() - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/orgs/joomla/hooks/123') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 123), - $this->equalTo(json_decode($this->sampleString)) - ); - } + /** + * @var Hooks + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Hooks($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Hooks::getList() + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/hooks') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Hooks::get() + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/hooks/123') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 123), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Hooks::create() + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/hooks') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', '{url}', 'json', '{secret}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Hooks::create() + * + * @return void + */ + public function testCreateFailure() + { + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('Content type must be either "form" or "json"'); + + $this->object->create('joomla', '{url}', '{invalid}'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Hooks::create() + * + * @return void + */ + public function testCreateInvalidEvent() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Your events array contains an unauthorized event.'); + + $this->object->create('{org}', '{url}', 'form', null, false, ['{invalid}']); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Hooks::edit() + * + * @return void + */ + public function testEdit() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/{org}/hooks') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit('{org}', '{url}', 'json', '{secret}', 1, ['create'], true), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Hooks::edit() + * + * @return void + */ + public function testEditFailure() + { + $this->expectException(\UnexpectedValueException::class); + + $this->object->edit('{org}', '{url}', '{invalid}'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Hooks::edit() + * + * @return void + */ + public function testEditFailure2() + { + $this->expectException(\RuntimeException::class); + + $this->object->edit('{org}', '{url}', 'json', '{secret}', 1, ['{invalid}']); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Hooks::ping() + * + * @return void + */ + public function testPing() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/{org}/hooks/123/pings') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->ping('{org}', 123), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Hooks::delete() + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/hooks/123') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 123), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Orgs/MembersTest.php b/Tests/Package/Orgs/MembersTest.php index bed70da6..604ed50a 100644 --- a/Tests/Package/Orgs/MembersTest.php +++ b/Tests/Package/Orgs/MembersTest.php @@ -1,4 +1,5 @@ object = new Members($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::getList() - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/members') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::getList() - * - * @return void - */ - public function testGetListNotAMember() - { - $this->response->code = 302; - - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/members') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla'), - $this->equalTo(false) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::getList() - * - * @return void - */ - public function testGetListUnexpected() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/members') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::check() - * - * @return void - */ - public function testCheck() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla', 'elkuku'), - $this->equalTo(true) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::check() - * - * @return void - */ - public function testCheckNoMember() - { - $this->response->code = 404; - - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla', 'elkuku'), - $this->equalTo(false) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::check() - * - * @return void - */ - public function testCheckRequesterNoMember() - { - $this->response->code = 302; - - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla', 'elkuku'), - $this->equalTo(false) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::check() - * - * @return void - */ - public function testCheckUnexpectedr() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla', 'elkuku'), - $this->equalTo(false) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::remove() - * - * @return void - */ - public function testRemove() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/orgs/joomla/members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->remove('joomla', 'elkuku'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::getListPublic() - * - * @return void - */ - public function testGetListPublic() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/public_members') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListPublic('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::checkPublic() - * - * @return void - */ - public function testCheckPublic() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/public_members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->checkPublic('joomla', 'elkuku'), - $this->equalTo(true) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::checkPublic() - * - * @return void - */ - public function testCheckPublicNo() - { - $this->response->code = 404; - - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/public_members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->checkPublic('joomla', 'elkuku'), - $this->equalTo(false) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::checkPublic() - * - * @return void - */ - public function testCheckPublicUnexpected() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/public_members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->checkPublic('joomla', 'elkuku'), - $this->equalTo(false) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::publicize() - * - * @return void - */ - public function testPublicize() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('put') - ->with('/orgs/joomla/public_members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->publicize('joomla', 'elkuku'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::conceal() - * - * @return void - */ - public function testConceal() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/orgs/joomla/public_members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->conceal('joomla', 'elkuku'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::getMembership() - * - * @return void - */ - public function testGetMembership() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/{org}/memberships/{user}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getMembership('{org}', '{user}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::updateMembership() - * - * @return void - */ - public function testUpdateMembership() - { - $this->client->expects($this->once()) - ->method('put') - ->with('/orgs/{org}/memberships/{user}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->updateMembership('{org}', '{user}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::updateMembership() - * - * @return void - */ - public function testUpdateMembershipInvalidRole() - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage("The user's role must be: member, admin"); - - $this->object->updateMembership('{org}', '{user}', 'INVALID'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::removeMembership() - * - * @return void - */ - public function testRemoveMembership() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/orgs/{org}/memberships/{user}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->removeMembership('{org}', '{user}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::listMemberships() - * - * @return void - */ - public function testListMemberships() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/user/memberships/orgs') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->listMemberships(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::listOrganizationMembership() - * - * @return void - */ - public function testListOrganizationMemberships() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/user/memberships/orgs/{org}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->listOrganizationMembership('{org}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::editOrganizationMembership() - * - * @return void - */ - public function testEditOrganizationMemberships() - { - $this->client->expects($this->once()) - ->method('patch') - ->with('/user/memberships/orgs/{org}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->editOrganizationMembership('{org}', 'active'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Members::editOrganizationMembership() - * - * @return void - */ - public function testEditOrganizationMembershipsInvalidState() - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('The state must be "active".'); - - $this->object->editOrganizationMembership('{org}', 'INVALID'); - } + /** + * @var Members + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Members($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::getList() + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::getList() + * + * @return void + */ + public function testGetListNotAMember() + { + $this->response->code = 302; + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla'), + $this->equalTo(false) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::getList() + * + * @return void + */ + public function testGetListUnexpected() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::check() + * + * @return void + */ + public function testCheck() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla', 'elkuku'), + $this->equalTo(true) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::check() + * + * @return void + */ + public function testCheckNoMember() + { + $this->response->code = 404; + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla', 'elkuku'), + $this->equalTo(false) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::check() + * + * @return void + */ + public function testCheckRequesterNoMember() + { + $this->response->code = 302; + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla', 'elkuku'), + $this->equalTo(false) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::check() + * + * @return void + */ + public function testCheckUnexpectedr() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla', 'elkuku'), + $this->equalTo(false) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::remove() + * + * @return void + */ + public function testRemove() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->remove('joomla', 'elkuku'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::getListPublic() + * + * @return void + */ + public function testGetListPublic() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/public_members') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListPublic('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::checkPublic() + * + * @return void + */ + public function testCheckPublic() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/public_members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->checkPublic('joomla', 'elkuku'), + $this->equalTo(true) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::checkPublic() + * + * @return void + */ + public function testCheckPublicNo() + { + $this->response->code = 404; + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/public_members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->checkPublic('joomla', 'elkuku'), + $this->equalTo(false) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::checkPublic() + * + * @return void + */ + public function testCheckPublicUnexpected() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/public_members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->checkPublic('joomla', 'elkuku'), + $this->equalTo(false) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::publicize() + * + * @return void + */ + public function testPublicize() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/public_members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->publicize('joomla', 'elkuku'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::conceal() + * + * @return void + */ + public function testConceal() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/public_members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->conceal('joomla', 'elkuku'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::getMembership() + * + * @return void + */ + public function testGetMembership() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/{org}/memberships/{user}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getMembership('{org}', '{user}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::updateMembership() + * + * @return void + */ + public function testUpdateMembership() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/{org}/memberships/{user}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->updateMembership('{org}', '{user}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::updateMembership() + * + * @return void + */ + public function testUpdateMembershipInvalidRole() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage("The user's role must be: member, admin"); + + $this->object->updateMembership('{org}', '{user}', 'INVALID'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::removeMembership() + * + * @return void + */ + public function testRemoveMembership() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/{org}/memberships/{user}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->removeMembership('{org}', '{user}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::listMemberships() + * + * @return void + */ + public function testListMemberships() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/memberships/orgs') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->listMemberships(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::listOrganizationMembership() + * + * @return void + */ + public function testListOrganizationMemberships() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/memberships/orgs/{org}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->listOrganizationMembership('{org}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::editOrganizationMembership() + * + * @return void + */ + public function testEditOrganizationMemberships() + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/user/memberships/orgs/{org}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->editOrganizationMembership('{org}', 'active'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Members::editOrganizationMembership() + * + * @return void + */ + public function testEditOrganizationMembershipsInvalidState() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The state must be "active".'); + + $this->object->editOrganizationMembership('{org}', 'INVALID'); + } } diff --git a/Tests/Package/Orgs/TeamsTest.php b/Tests/Package/Orgs/TeamsTest.php index 7ab71b99..2e55c2dd 100644 --- a/Tests/Package/Orgs/TeamsTest.php +++ b/Tests/Package/Orgs/TeamsTest.php @@ -1,4 +1,5 @@ object = new Teams($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::getList() - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/teams') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::get() - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get(123), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::create() - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/orgs/joomla/teams') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'TheTeam', array('joomla-platform'), 'admin'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::create() - * - * @return void - */ - public function testCreateWrongPermission() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 201; - - $this->object->create('joomla', 'TheTeam', array('joomla-platform'), 'invalid'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::edit() - * - * @return void - */ - public function testEdit() - { - $this->client->expects($this->once()) - ->method('patch') - ->with('/teams/123') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit(123, 'TheTeam', 'admin'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::edit() - * - * @return void - */ - public function testEditWrongPermission() - { - $this->expectException(\UnexpectedValueException::class); - - $this->object->edit(123, 'TheTeam', 'invalid'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::delete() - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/teams/123') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete(123), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::getListMembers() - * - * @return void - */ - public function testGetListMembers() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/members') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListMembers(123), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::isMember() - * - * @deprecated - * - * @return void - */ - public function testIsMember() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->isMember(123, 'elkuku'), - $this->equalTo(json_decode(true)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::isMember() - * - * @deprecated - * - * @return void - */ - public function testIsMemberNo() - { - $this->response->code = 404; - - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->isMember(123, 'elkuku'), - $this->equalTo(json_decode(false)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::isMember() - * - * @deprecated - * - * @return void - */ - public function testIsMemberUnexpected() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->isMember(123, 'elkuku'), - $this->equalTo(json_decode(true)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::addMember() - * - * @deprecated - * - * @return void - */ - public function testAddMember() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('put') - ->with('/teams/123/members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->addMember(123, 'elkuku'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::removeMember() - * - * @deprecated - * - * @return void - */ - public function testRemoveMember() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/teams/123/members/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->removeMember(123, 'elkuku'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::getListRepos() - * - * @return void - */ - public function testGetListRepos() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/repos') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListRepos(123), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::checkRepo() - * - * @return void - */ - public function testCheckRepo() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/repos/joomla/cms') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->checkRepo(123, 'joomla', 'cms'), - $this->equalTo(true) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::checkRepo() - * - * @return void - */ - public function testCheckRepoNo() - { - $this->response->code = 404; - - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/repos/joomla/cms') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->checkRepo(123, 'joomla', 'cms'), - $this->equalTo(false) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::checkRepo() - * - * @return void - */ - public function testCheckRepoUnexpected() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/repos/joomla/cms') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->checkRepo(123, 'joomla', 'cms'), - $this->equalTo(true) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::addRepo() - * - * @return void - */ - public function testAddRepo() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('put') - ->with('/teams/123/repos/joomla/joomla-platform') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->addRepo(123, 'joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::removeRepo() - * - * @return void - */ - public function testRemoveRepo() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/teams/123/repos/joomla/joomla-platform') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->removeRepo(123, 'joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::getTeamMembership() - * - * @return void - */ - public function testGetTeamMemberships() - { - $this->response->code = 200; - $this->response->body = '{"state":"TEST"}'; - - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/memberships/{user}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getTeamMembership(123, '{user}'), - $this->equalTo('TEST') - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::getTeamMembership() - * - * Response if user is not a member - * - * @return void - */ - public function testGetTeamMembershipsFailure1() - { - $this->response->code = 404; - $this->response->body = '{"state":"TEST"}'; - - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/memberships/{user}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getTeamMembership(123, '{user}'), - $this->equalTo(false) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::getTeamMembership() - * - * Unexpected Response - * - * @return void - */ - public function testGetTeamMembershipsFailure2() - { - $this->expectException(\UnexpectedValueException::class); - $this->expectExceptionMessage('Unexpected response code: 666'); - - $this->response->code = 666; - $this->response->body = '{"state":"TEST"}'; - - $this->client->expects($this->once()) - ->method('get') - ->with('/teams/123/memberships/{user}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getTeamMembership(123, '{user}'), - $this->equalTo('TEST') - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::addTeamMembership() - * - * @return void - */ - public function testAddTeamMemberships() - { - $this->client->expects($this->once()) - ->method('put') - ->with('/teams/123/memberships/{user}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->addTeamMembership(123, '{user}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::addTeamMembership() - * - * Invalid role - * - * @return void - */ - public function testAddTeamMembershipsFailure() - { - $this->expectException(\UnexpectedValueException::class); - $this->expectExceptionMessage('Roles must be either "member" or "maintainer".'); - - $this->object->addTeamMembership(123, '{user}', 'INVALID'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::removeTeamMembership() - * - * @return void - */ - public function testRemoveTeamMemberships() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/teams/123/memberships/{user}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->removeTeamMembership(123, '{user}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Orgs\Teams::getUserTeams() - * - * @return void - */ - public function testGetUserTeams() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/user/teams') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getUserTeams(), - $this->equalTo(json_decode($this->sampleString)) - ); - } + /** + * @var Teams + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Teams($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::getList() + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/teams') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::get() + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get(123), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::create() + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/teams') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'TheTeam', ['joomla-platform'], 'admin'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::create() + * + * @return void + */ + public function testCreateWrongPermission() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 201; + + $this->object->create('joomla', 'TheTeam', ['joomla-platform'], 'invalid'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::edit() + * + * @return void + */ + public function testEdit() + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/teams/123') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit(123, 'TheTeam', 'admin'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::edit() + * + * @return void + */ + public function testEditWrongPermission() + { + $this->expectException(\UnexpectedValueException::class); + + $this->object->edit(123, 'TheTeam', 'invalid'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::delete() + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/teams/123') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete(123), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::getListMembers() + * + * @return void + */ + public function testGetListMembers() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/members') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListMembers(123), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::isMember() + * + * @deprecated + * + * @return void + */ + public function testIsMember() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->isMember(123, 'elkuku'), + $this->equalTo(json_decode(true)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::isMember() + * + * @deprecated + * + * @return void + */ + public function testIsMemberNo() + { + $this->response->code = 404; + + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->isMember(123, 'elkuku'), + $this->equalTo(json_decode(false)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::isMember() + * + * @deprecated + * + * @return void + */ + public function testIsMemberUnexpected() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->isMember(123, 'elkuku'), + $this->equalTo(json_decode(true)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::addMember() + * + * @deprecated + * + * @return void + */ + public function testAddMember() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('put') + ->with('/teams/123/members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->addMember(123, 'elkuku'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::removeMember() + * + * @deprecated + * + * @return void + */ + public function testRemoveMember() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/teams/123/members/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->removeMember(123, 'elkuku'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::getListRepos() + * + * @return void + */ + public function testGetListRepos() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/repos') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListRepos(123), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::checkRepo() + * + * @return void + */ + public function testCheckRepo() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/repos/joomla/cms') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->checkRepo(123, 'joomla', 'cms'), + $this->equalTo(true) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::checkRepo() + * + * @return void + */ + public function testCheckRepoNo() + { + $this->response->code = 404; + + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/repos/joomla/cms') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->checkRepo(123, 'joomla', 'cms'), + $this->equalTo(false) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::checkRepo() + * + * @return void + */ + public function testCheckRepoUnexpected() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/repos/joomla/cms') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->checkRepo(123, 'joomla', 'cms'), + $this->equalTo(true) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::addRepo() + * + * @return void + */ + public function testAddRepo() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('put') + ->with('/teams/123/repos/joomla/joomla-platform') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->addRepo(123, 'joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::removeRepo() + * + * @return void + */ + public function testRemoveRepo() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/teams/123/repos/joomla/joomla-platform') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->removeRepo(123, 'joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::getTeamMembership() + * + * @return void + */ + public function testGetTeamMemberships() + { + $this->response->code = 200; + $this->response->body = '{"state":"TEST"}'; + + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/memberships/{user}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getTeamMembership(123, '{user}'), + $this->equalTo('TEST') + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::getTeamMembership() + * + * Response if user is not a member + * + * @return void + */ + public function testGetTeamMembershipsFailure1() + { + $this->response->code = 404; + $this->response->body = '{"state":"TEST"}'; + + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/memberships/{user}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getTeamMembership(123, '{user}'), + $this->equalTo(false) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::getTeamMembership() + * + * Unexpected Response + * + * @return void + */ + public function testGetTeamMembershipsFailure2() + { + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('Unexpected response code: 666'); + + $this->response->code = 666; + $this->response->body = '{"state":"TEST"}'; + + $this->client->expects($this->once()) + ->method('get') + ->with('/teams/123/memberships/{user}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getTeamMembership(123, '{user}'), + $this->equalTo('TEST') + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::addTeamMembership() + * + * @return void + */ + public function testAddTeamMemberships() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/teams/123/memberships/{user}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->addTeamMembership(123, '{user}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::addTeamMembership() + * + * Invalid role + * + * @return void + */ + public function testAddTeamMembershipsFailure() + { + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('Roles must be either "member" or "maintainer".'); + + $this->object->addTeamMembership(123, '{user}', 'INVALID'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::removeTeamMembership() + * + * @return void + */ + public function testRemoveTeamMemberships() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/teams/123/memberships/{user}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->removeTeamMembership(123, '{user}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Orgs\Teams::getUserTeams() + * + * @return void + */ + public function testGetUserTeams() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/teams') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getUserTeams(), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/OrgsTest.php b/Tests/Package/OrgsTest.php index 2c992e51..28acae74 100644 --- a/Tests/Package/OrgsTest.php +++ b/Tests/Package/OrgsTest.php @@ -1,4 +1,5 @@ object = new Orgs($this->options, $this->client); - } + $this->object = new Orgs($this->options, $this->client); + } - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/users/joomla/orgs') - ->will($this->returnValue($this->response)); + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/joomla/orgs') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getList('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->getList('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla') - ->will($this->returnValue($this->response)); + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->get('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->get('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the edit method - * - * @return void - */ - public function testEdit() - { - $this->client->expects($this->once()) - ->method('patch') - ->with('/orgs/joomla') - ->will($this->returnValue($this->response)); + /** + * Tests the edit method + * + * @return void + */ + public function testEdit() + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/orgs/joomla') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->edit('joomla', 'email@example.com'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->edit('joomla', 'email@example.com'), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Pulls/CommentsTest.php b/Tests/Package/Pulls/CommentsTest.php index 0c0e96cf..faaea5a5 100644 --- a/Tests/Package/Pulls/CommentsTest.php +++ b/Tests/Package/Pulls/CommentsTest.php @@ -1,4 +1,5 @@ object = new Comments($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Pulls\Comments::create() - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - $data = '{"body":"The Body","commit_id":"123abc","path":"a\/b\/c","position":456}'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/pulls/1/comments', $data, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 1, 'The Body', '123abc', 'a/b/c', 456), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Pulls\Comments::createReply() - * - * @return void - */ - public function testCreateReply() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/pulls/1/comments', '{"body":"The Body","in_reply_to":456}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->createReply('joomla', 'joomla-platform', 1, 'The Body', 456), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Pulls\Comments::delete() - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/pulls/comments/456', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 'joomla-platform', 456), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Pulls\Comments::edit() - * - * @return void - */ - public function testEdit() - { - $this->response->code = 200; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/pulls/comments/456', '{"body":"Hello"}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-platform', 456, 'Hello'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Pulls\Comments::get() - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/comments/456', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 456), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Pulls\Comments::getList() - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/456/comments', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform', 456), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Pulls\Comments::getListForRepo() - * - * @return void - */ - public function testGetListForRepo() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{user}/{repo}/pulls/comments', [], 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListForRepo('{user}', '{repo}'), - $this->equalTo(json_decode($this->response->body)) - ); - } + /** + * @var Comments + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Comments($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Pulls\Comments::create() + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + $data = '{"body":"The Body","commit_id":"123abc","path":"a\/b\/c","position":456}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/pulls/1/comments', $data, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', 1, 'The Body', '123abc', 'a/b/c', 456), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Pulls\Comments::createReply() + * + * @return void + */ + public function testCreateReply() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/pulls/1/comments', '{"body":"The Body","in_reply_to":456}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->createReply('joomla', 'joomla-platform', 1, 'The Body', 456), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Pulls\Comments::delete() + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/pulls/comments/456', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 'joomla-platform', 456), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Pulls\Comments::edit() + * + * @return void + */ + public function testEdit() + { + $this->response->code = 200; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/pulls/comments/456', '{"body":"Hello"}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit('joomla', 'joomla-platform', 456, 'Hello'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Pulls\Comments::get() + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/comments/456', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 456), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Pulls\Comments::getList() + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/456/comments', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform', 456), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Pulls\Comments::getListForRepo() + * + * @return void + */ + public function testGetListForRepo() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{user}/{repo}/pulls/comments', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListForRepo('{user}', '{repo}'), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/PullsTest.php b/Tests/Package/PullsTest.php index 11d5bfe0..e75bd4d6 100755 --- a/Tests/Package/PullsTest.php +++ b/Tests/Package/PullsTest.php @@ -1,4 +1,5 @@ object = new Pulls($this->options, $this->client); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $pull = new \stdClass; - $pull->title = 'My Pull Request'; - $pull->base = 'staging'; - $pull->head = 'joomla-jenkins:mychanges'; - $pull->body = 'These are my changes - please review them'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/pulls', json_encode($pull)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 'My Pull Request', 'staging', 'joomla-jenkins:mychanges', - 'These are my changes - please review them' - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the create method - failure - * - * @return void - */ - public function testCreateFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 501; - $this->response->body = $this->errorString; - - $pull = new \stdClass; - $pull->title = 'My Pull Request'; - $pull->base = 'staging'; - $pull->head = 'joomla-jenkins:mychanges'; - $pull->body = 'These are my changes - please review them'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/pulls', json_encode($pull)) - ->will($this->returnValue($this->response)); - - $this->object->create('joomla', 'joomla-platform', 'My Pull Request', 'staging', 'joomla-jenkins:mychanges', - 'These are my changes - please review them' - ); - } - - /** - * Tests the createFromIssue method - * - * @return void - */ - public function testCreateFromIssue() - { - $this->response->code = 201; - - $pull = new \stdClass; - $pull->issue = 254; - $pull->base = 'staging'; - $pull->head = 'joomla-jenkins:mychanges'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/pulls', json_encode($pull)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->createFromIssue('joomla', 'joomla-platform', 254, 'staging', 'joomla-jenkins:mychanges'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the createFromIssue method - failure - * - * @return void - */ - public function testCreateFromIssueFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 501; - $this->response->body = $this->errorString; - - $pull = new \stdClass; - $pull->issue = 254; - $pull->base = 'staging'; - $pull->head = 'joomla-jenkins:mychanges'; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/pulls', json_encode($pull)) - ->will($this->returnValue($this->response)); - - $this->object->createFromIssue('joomla', 'joomla-platform', 254, 'staging', 'joomla-jenkins:mychanges'); - } - - /** - * Tests the edit method - * - * @return void - */ - public function testEdit() - { - $pull = new \stdClass; - $pull->title = 'My Pull Request'; - $pull->body = 'These are my changes - please review them'; - $pull->state = 'Closed'; - $pull->base = 'new'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/pulls/523', json_encode($pull)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-platform', 523, 'My Pull Request', 'These are my changes - please review them', 'Closed', 'new'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the edit method - failure - * - * @return void - */ - public function testEditFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $pull = new \stdClass; - $pull->title = 'My Pull Request'; - $pull->body = 'These are my changes - please review them'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/pulls/523', json_encode($pull)) - ->will($this->returnValue($this->response)); - - $this->object->edit('joomla', 'joomla-platform', 523, 'My Pull Request', 'These are my changes - please review them'); - } - - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/523') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the get method - failure - * - * @return void - */ - public function testGetFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/523') - ->will($this->returnValue($this->response)); - - $this->object->get('joomla', 'joomla-platform', 523); - } - - /** - * Tests the getCommits method - * - * @return void - */ - public function testGetCommits() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/523/commits') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getCommits('joomla', 'joomla-platform', 523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getCommits method - failure - * - * @return void - */ - public function testGetCommitsFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/523/commits') - ->will($this->returnValue($this->response)); - - $this->object->getCommits('joomla', 'joomla-platform', 523); - } - - /** - * Tests the getFiles method - * - * @return void - */ - public function testGetFiles() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/523/files') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getFiles('joomla', 'joomla-platform', 523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getFiles method - failure - * - * @return void - */ - public function testGetFilesFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/523/files') - ->will($this->returnValue($this->response)); - - $this->object->getFiles('joomla', 'joomla-platform', 523); - } - - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls?state=closed') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform', 'closed'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getList method - failure - * - * @return void - */ - public function testGetListFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls') - ->will($this->returnValue($this->response)); - - $this->object->getList('joomla', 'joomla-platform'); - } - - /** - * Tests the isMerged method when the pull request has been merged - * - * @return void - */ - public function testIsMergedTrue() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/523/merge') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->isMerged('joomla', 'joomla-platform', 523), - $this->equalTo(true) - ); - } - - /** - * Tests the isMerged method when the pull request has not been merged - * - * @return void - */ - public function testIsMergedFalse() - { - $this->response->code = 404; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/523/merge') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->isMerged('joomla', 'joomla-platform', 523), - $this->equalTo(false) - ); - } - - /** - * Tests the isMerged method when the request fails - * - * @return void - */ - public function testIsMergedFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 504; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/pulls/523/merge') - ->will($this->returnValue($this->response)); - - $this->object->isMerged('joomla', 'joomla-platform', 523); - } - - /** - * Tests the merge method - * - * @return void - */ - public function testMerge() - { - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-platform/pulls/523/merge') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->merge('joomla', 'joomla-platform', 523), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the merge method - failure - * - * @return void - */ - public function testMergeFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-platform/pulls/523/merge') - ->will($this->returnValue($this->response)); - - $this->object->merge('joomla', 'joomla-platform', 523); - } + /** + * @var Pulls + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Pulls($this->options, $this->client); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $pull = new \stdClass(); + $pull->title = 'My Pull Request'; + $pull->base = 'staging'; + $pull->head = 'joomla-jenkins:mychanges'; + $pull->body = 'These are my changes - please review them'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/pulls', json_encode($pull)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create( + 'joomla', + 'joomla-platform', + 'My Pull Request', + 'staging', + 'joomla-jenkins:mychanges', + 'These are my changes - please review them' + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the create method - failure + * + * @return void + */ + public function testCreateFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 501; + $this->response->body = $this->errorString; + + $pull = new \stdClass(); + $pull->title = 'My Pull Request'; + $pull->base = 'staging'; + $pull->head = 'joomla-jenkins:mychanges'; + $pull->body = 'These are my changes - please review them'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/pulls', json_encode($pull)) + ->will($this->returnValue($this->response)); + + $this->object->create( + 'joomla', + 'joomla-platform', + 'My Pull Request', + 'staging', + 'joomla-jenkins:mychanges', + 'These are my changes - please review them' + ); + } + + /** + * Tests the createFromIssue method + * + * @return void + */ + public function testCreateFromIssue() + { + $this->response->code = 201; + + $pull = new \stdClass(); + $pull->issue = 254; + $pull->base = 'staging'; + $pull->head = 'joomla-jenkins:mychanges'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/pulls', json_encode($pull)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->createFromIssue('joomla', 'joomla-platform', 254, 'staging', 'joomla-jenkins:mychanges'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the createFromIssue method - failure + * + * @return void + */ + public function testCreateFromIssueFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 501; + $this->response->body = $this->errorString; + + $pull = new \stdClass(); + $pull->issue = 254; + $pull->base = 'staging'; + $pull->head = 'joomla-jenkins:mychanges'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/pulls', json_encode($pull)) + ->will($this->returnValue($this->response)); + + $this->object->createFromIssue('joomla', 'joomla-platform', 254, 'staging', 'joomla-jenkins:mychanges'); + } + + /** + * Tests the edit method + * + * @return void + */ + public function testEdit() + { + $pull = new \stdClass(); + $pull->title = 'My Pull Request'; + $pull->body = 'These are my changes - please review them'; + $pull->state = 'Closed'; + $pull->base = 'new'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/pulls/523', json_encode($pull)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit('joomla', 'joomla-platform', 523, 'My Pull Request', 'These are my changes - please review them', 'Closed', 'new'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the edit method - failure + * + * @return void + */ + public function testEditFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $pull = new \stdClass(); + $pull->title = 'My Pull Request'; + $pull->body = 'These are my changes - please review them'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/pulls/523', json_encode($pull)) + ->will($this->returnValue($this->response)); + + $this->object->edit('joomla', 'joomla-platform', 523, 'My Pull Request', 'These are my changes - please review them'); + } + + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/523') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the get method - failure + * + * @return void + */ + public function testGetFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/523') + ->will($this->returnValue($this->response)); + + $this->object->get('joomla', 'joomla-platform', 523); + } + + /** + * Tests the getCommits method + * + * @return void + */ + public function testGetCommits() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/523/commits') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getCommits('joomla', 'joomla-platform', 523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getCommits method - failure + * + * @return void + */ + public function testGetCommitsFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/523/commits') + ->will($this->returnValue($this->response)); + + $this->object->getCommits('joomla', 'joomla-platform', 523); + } + + /** + * Tests the getFiles method + * + * @return void + */ + public function testGetFiles() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/523/files') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getFiles('joomla', 'joomla-platform', 523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getFiles method - failure + * + * @return void + */ + public function testGetFilesFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/523/files') + ->will($this->returnValue($this->response)); + + $this->object->getFiles('joomla', 'joomla-platform', 523); + } + + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls?state=closed') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform', 'closed'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getList method - failure + * + * @return void + */ + public function testGetListFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls') + ->will($this->returnValue($this->response)); + + $this->object->getList('joomla', 'joomla-platform'); + } + + /** + * Tests the isMerged method when the pull request has been merged + * + * @return void + */ + public function testIsMergedTrue() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/523/merge') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->isMerged('joomla', 'joomla-platform', 523), + $this->equalTo(true) + ); + } + + /** + * Tests the isMerged method when the pull request has not been merged + * + * @return void + */ + public function testIsMergedFalse() + { + $this->response->code = 404; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/523/merge') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->isMerged('joomla', 'joomla-platform', 523), + $this->equalTo(false) + ); + } + + /** + * Tests the isMerged method when the request fails + * + * @return void + */ + public function testIsMergedFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 504; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/pulls/523/merge') + ->will($this->returnValue($this->response)); + + $this->object->isMerged('joomla', 'joomla-platform', 523); + } + + /** + * Tests the merge method + * + * @return void + */ + public function testMerge() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/pulls/523/merge') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->merge('joomla', 'joomla-platform', 523), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the merge method - failure + * + * @return void + */ + public function testMergeFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/pulls/523/merge') + ->will($this->returnValue($this->response)); + + $this->object->merge('joomla', 'joomla-platform', 523); + } } diff --git a/Tests/Package/Repositories/BranchesTest.php b/Tests/Package/Repositories/BranchesTest.php index 51ca67e5..c9899301 100644 --- a/Tests/Package/Repositories/BranchesTest.php +++ b/Tests/Package/Repositories/BranchesTest.php @@ -1,4 +1,5 @@ object = new Branches($this->options, $this->client); - } + $this->object = new Branches($this->options, $this->client); + } - /** - * Tests the GetList method. - * - * @covers \Joomla\Github\Package\Repositories\Branches::getList() - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{owner}/{repo}/branches') - ->will($this->returnValue($this->response)); + /** + * Tests the GetList method. + * + * @covers \Joomla\Github\Package\Repositories\Branches::getList() + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{owner}/{repo}/branches') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getList('{owner}', '{repo}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->getList('{owner}', '{repo}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the Get method. - * - * @covers \Joomla\Github\Package\Repositories\Branches::get() - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{owner}/{repo}/branches/{branch}') - ->will($this->returnValue($this->response)); + /** + * Tests the Get method. + * + * @covers \Joomla\Github\Package\Repositories\Branches::get() + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{owner}/{repo}/branches/{branch}') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->get('{owner}', '{repo}', '{branch}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->get('{owner}', '{repo}', '{branch}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Repositories/CollaboratorsTest.php b/Tests/Package/Repositories/CollaboratorsTest.php index 045572fb..d365db94 100644 --- a/Tests/Package/Repositories/CollaboratorsTest.php +++ b/Tests/Package/Repositories/CollaboratorsTest.php @@ -1,4 +1,5 @@ object = new Collaborators($this->options, $this->client); - } - - /** - * Tests the GetList method. - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/collaborators') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-framework'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Get method. - * - * @return void - */ - public function testGet() - { - $this->response->code = 204; - $this->response->body = true; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/collaborators/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-framework', 'elkuku'), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the GetNegative method. - * - * @return void - */ - public function testGetNegative() - { - $this->response->code = 404; - $this->response->body = false; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/collaborators/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-framework', 'elkuku'), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the GetUnexpected method. - * - * @return void - */ - public function testGetUnexpected() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - $this->response->body = null; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/collaborators/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-framework', 'elkuku'), - $this->equalTo($this->response->body) - ); - } - - /** - * Tests the Add method. - * - * @return void - */ - public function testAdd() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-framework/collaborators/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->add('joomla', 'joomla-framework', 'elkuku'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Remove method. - * - * @return void - */ - public function testRemove() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-framework/collaborators/elkuku') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->remove('joomla', 'joomla-framework', 'elkuku'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + /** + * @var Collaborators + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Collaborators($this->options, $this->client); + } + + /** + * Tests the GetList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/collaborators') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-framework'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Get method. + * + * @return void + */ + public function testGet() + { + $this->response->code = 204; + $this->response->body = true; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/collaborators/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-framework', 'elkuku'), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the GetNegative method. + * + * @return void + */ + public function testGetNegative() + { + $this->response->code = 404; + $this->response->body = false; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/collaborators/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-framework', 'elkuku'), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the GetUnexpected method. + * + * @return void + */ + public function testGetUnexpected() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + $this->response->body = null; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/collaborators/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-framework', 'elkuku'), + $this->equalTo($this->response->body) + ); + } + + /** + * Tests the Add method. + * + * @return void + */ + public function testAdd() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-framework/collaborators/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->add('joomla', 'joomla-framework', 'elkuku'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Remove method. + * + * @return void + */ + public function testRemove() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-framework/collaborators/elkuku') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->remove('joomla', 'joomla-framework', 'elkuku'), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Repositories/CommentsTest.php b/Tests/Package/Repositories/CommentsTest.php index ecfada10..0b6e441d 100644 --- a/Tests/Package/Repositories/CommentsTest.php +++ b/Tests/Package/Repositories/CommentsTest.php @@ -1,4 +1,5 @@ object = new Comments($this->options, $this->client); - } - - /** - * Tests the GetListRepository method. - * - * @return void - */ - public function testGetListRepository() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/comments') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListRepository('joomla', 'joomla-framework'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the GetList method. - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/commits/123/comments') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-framework', '123'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Get method. - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/comments/123') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-framework', 123), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Edit method. - * - * @return void - */ - public function testEdit() - { - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-framework/comments/123') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-framework', 123, 'My Comment'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Delete method. - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-framework/comments/123') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 'joomla-framework', 123), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Create method. - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-framework/commits/123abc/comments') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-framework', '123abc', 'My Comment', 456, 'path/file.php', 789), - $this->equalTo(json_decode($this->sampleString)) - ); - } + /** + * @var Comments + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Comments($this->options, $this->client); + } + + /** + * Tests the GetListRepository method. + * + * @return void + */ + public function testGetListRepository() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/comments') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListRepository('joomla', 'joomla-framework'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the GetList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/commits/123/comments') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-framework', '123'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Get method. + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/comments/123') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-framework', 123), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Edit method. + * + * @return void + */ + public function testEdit() + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-framework/comments/123') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit('joomla', 'joomla-framework', 123, 'My Comment'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Delete method. + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-framework/comments/123') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 'joomla-framework', 123), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Create method. + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-framework/commits/123abc/comments') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-framework', '123abc', 'My Comment', 456, 'path/file.php', 789), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Repositories/CommitsTest.php b/Tests/Package/Repositories/CommitsTest.php index 50999a97..1d7e9065 100644 --- a/Tests/Package/Repositories/CommitsTest.php +++ b/Tests/Package/Repositories/CommitsTest.php @@ -1,4 +1,5 @@ object = new Commits($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Commits::get() - * - * @return void - * - * @since 1.0 - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/commits/abc1234') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 'abc1234'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Commits::get() - * - * @return void - * - * @since 1.0 - */ - public function testGetFailure() - { - $this->expectException(\DomainException::class); - $this->expectExceptionMessage('Generic Error'); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/commits/abc1234') - ->will($this->returnValue($this->response)); - - $this->object->get('joomla', 'joomla-platform', 'abc1234'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Commits::getList() - * - * @return void - * - * @since 1.0 - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/commits') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Commits::getList() - * - * @return void - * - * @since 1.0 - */ - public function testGetListFailure() - { - $this->expectException(\DomainException::class); - $this->expectExceptionMessage('Generic Error'); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/commits') - ->will($this->returnValue($this->response)); - - $this->object->getList('joomla', 'joomla-platform'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Commits::compare() - * - * @return void - * - * @since 1.0 - */ - public function testCompare() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/compare/123abc...456def') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->compare('joomla', 'joomla-platform', '123abc', '456def'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Commits::getSha() - * - * @return void - * - * @since 1.0 - */ - public function testgetSha() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{user}/{repo}/commits/{ref}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getSha('{user}', '{repo}', '{ref}'), - $this->equalTo($this->sampleString) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Commits::getSha() - * - * @return void - * - * @since 1.0 - */ - public function testgetShaFailure() - { - $this->expectException(UnexpectedResponseException::class); - $this->expectExceptionMessage('Invalid response received from GitHub.'); - - $this->response->code = 666; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{user}/{repo}/commits/{ref}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getSha('{user}', '{repo}', '{ref}'), - $this->equalTo($this->sampleString) - ); - } + /** + * @var Commits Object under test. + * @since 12.1 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @return void + * + * @since 1.0 + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Commits($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Commits::get() + * + * @return void + * + * @since 1.0 + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/commits/abc1234') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 'abc1234'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Commits::get() + * + * @return void + * + * @since 1.0 + */ + public function testGetFailure() + { + $this->expectException(\DomainException::class); + $this->expectExceptionMessage('Generic Error'); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/commits/abc1234') + ->will($this->returnValue($this->response)); + + $this->object->get('joomla', 'joomla-platform', 'abc1234'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Commits::getList() + * + * @return void + * + * @since 1.0 + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/commits') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Commits::getList() + * + * @return void + * + * @since 1.0 + */ + public function testGetListFailure() + { + $this->expectException(\DomainException::class); + $this->expectExceptionMessage('Generic Error'); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/commits') + ->will($this->returnValue($this->response)); + + $this->object->getList('joomla', 'joomla-platform'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Commits::compare() + * + * @return void + * + * @since 1.0 + */ + public function testCompare() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/compare/123abc...456def') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->compare('joomla', 'joomla-platform', '123abc', '456def'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Commits::getSha() + * + * @return void + * + * @since 1.0 + */ + public function testgetSha() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{user}/{repo}/commits/{ref}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getSha('{user}', '{repo}', '{ref}'), + $this->equalTo($this->sampleString) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Commits::getSha() + * + * @return void + * + * @since 1.0 + */ + public function testgetShaFailure() + { + $this->expectException(UnexpectedResponseException::class); + $this->expectExceptionMessage('Invalid response received from GitHub.'); + + $this->response->code = 666; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{user}/{repo}/commits/{ref}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getSha('{user}', '{repo}', '{ref}'), + $this->equalTo($this->sampleString) + ); + } } diff --git a/Tests/Package/Repositories/ContentsTest.php b/Tests/Package/Repositories/ContentsTest.php index b95917b2..a3467a42 100644 --- a/Tests/Package/Repositories/ContentsTest.php +++ b/Tests/Package/Repositories/ContentsTest.php @@ -1,4 +1,5 @@ object = new Contents($this->options, $this->client); - } - - /** - * Tests the GetReadme method. - * - * @return void - */ - public function testGetReadme() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/readme') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getReadme('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the GetReadmeRef method. - * - * @return void - */ - public function testGetReadmeRef() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/readme?ref=123abc') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getReadme('joomla', 'joomla-platform', '123abc'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Get method. - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/contents/path/to/file.php') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 'path/to/file.php'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the GetRef method. - * - * @return void - */ - public function testGetRef() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/contents/path/to/file.php?ref=123abc') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 'path/to/file.php', '123abc'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the GetArchiveLink method. - * - * @return void - */ - public function testGetArchiveLink() - { - $this->response->code = 302; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/zipball') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getArchiveLink('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the GetArchiveLinkRef method. - * - * @return void - */ - public function testGetArchiveLinkRef() - { - $this->response->code = 302; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/zipball?ref=123abc') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getArchiveLink('joomla', 'joomla-platform', 'zipball', '123abc'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the GetArchiveLinkInvalidFormat method. - * - * @return void - */ - public function testGetArchiveLinkInvalidFormat() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 302; - - $this->object->getArchiveLink('joomla', 'joomla-platform', 'invalid'); - } - - /** - * Tests the create method. - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-platform/contents/src/foo') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create( - 'joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def', 'xxbranch', - 'eddieajau', 'eddieajau@example.com', 'elkuku', 'elkuku@example.com' - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the create method with missing author e-mail. - * - * @return void - */ - public function testCreateFail1() - { - $this->expectException(\UnexpectedValueException::class); - - $this->object->create( - 'joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def', 'xxbranch', - 'eddieajau', '', 'elkuku', 'elkuku@example.com' - ); - } - - /** - * Tests the create method with missing committer e-mail. - * - * @return void - */ - public function testCreateFail2() - { - $this->expectException(\UnexpectedValueException::class); - - $this->object->create( - 'joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def', 'xxbranch', - 'eddieajau', 'eddieajau@example.com', 'elkuku', '' - ); - } - - /** - * Tests the update method. - * - * @return void - */ - public function testUpdate() - { - $this->client->expects($this->once()) - ->method('put') - ->with('/repos/joomla/joomla-platform/contents/src/foo') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->update( - 'joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def', 'abcd1234', 'xxbranch', - 'eddieajau', 'eddieajau@example.com', 'elkuku', 'elkuku@example.com' - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the update method with missing author e-mail. - * - * @return void - */ - public function testUpdateFail1() - { - $this->expectException(\UnexpectedValueException::class); - - $this->object->update( - 'joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def', 'abcd1234', 'xxbranch', - 'eddieajau', '', 'elkuku', 'elkuku@example.com' - ); - } - - /** - * Tests the update method with missing committer e-mail. - * - * @return void - */ - public function testUpdateFail2() - { - $this->expectException(\UnexpectedValueException::class); - - $this->object->update( - 'joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def', 'abcd1234', 'xxbranch', - 'eddieajau', 'eddieajau@example.com', 'elkuku', '' - ); - } - - /** - * Tests the delete method. - * - * @return void - */ - public function testDelete() - { - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/contents/src/foo') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete( - 'joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def', 'xxbranch', - 'eddieajau', 'eddieajau@example.com', 'elkuku', 'elkuku@example.com' - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the delete method with missing author e-mail. - * - * @return void - */ - public function testDeleteFail1() - { - $this->expectException(\UnexpectedValueException::class); - - $this->object->delete( - 'joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def', 'xxbranch', - 'eddieajau', '', 'elkuku', 'elkuku@example.com' - ); - } - - /** - * Tests the update method with missing committer e-mail. - * - * @return void - */ - public function testDeleteFail2() - { - $this->expectException(\UnexpectedValueException::class); - - $this->object->delete( - 'joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def', 'xxbranch', - 'eddieajau', 'eddieajau@example.com', 'elkuku', '' - ); - } + /** + * @var Contents + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Contents($this->options, $this->client); + } + + /** + * Tests the GetReadme method. + * + * @return void + */ + public function testGetReadme() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/readme') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getReadme('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the GetReadmeRef method. + * + * @return void + */ + public function testGetReadmeRef() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/readme?ref=123abc') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getReadme('joomla', 'joomla-platform', '123abc'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Get method. + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/contents/path/to/file.php') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 'path/to/file.php'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the GetRef method. + * + * @return void + */ + public function testGetRef() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/contents/path/to/file.php?ref=123abc') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 'path/to/file.php', '123abc'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the GetArchiveLink method. + * + * @return void + */ + public function testGetArchiveLink() + { + $this->response->code = 302; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/zipball') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getArchiveLink('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the GetArchiveLinkRef method. + * + * @return void + */ + public function testGetArchiveLinkRef() + { + $this->response->code = 302; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/zipball?ref=123abc') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getArchiveLink('joomla', 'joomla-platform', 'zipball', '123abc'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the GetArchiveLinkInvalidFormat method. + * + * @return void + */ + public function testGetArchiveLinkInvalidFormat() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 302; + + $this->object->getArchiveLink('joomla', 'joomla-platform', 'invalid'); + } + + /** + * Tests the create method. + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/contents/src/foo') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create( + 'joomla', + 'joomla-platform', + 'src/foo', + 'my Message', + 'ABC123def', + 'xxbranch', + 'eddieajau', + 'eddieajau@example.com', + 'elkuku', + 'elkuku@example.com' + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the create method with missing author e-mail. + * + * @return void + */ + public function testCreateFail1() + { + $this->expectException(\UnexpectedValueException::class); + + $this->object->create( + 'joomla', + 'joomla-platform', + 'src/foo', + 'my Message', + 'ABC123def', + 'xxbranch', + 'eddieajau', + '', + 'elkuku', + 'elkuku@example.com' + ); + } + + /** + * Tests the create method with missing committer e-mail. + * + * @return void + */ + public function testCreateFail2() + { + $this->expectException(\UnexpectedValueException::class); + + $this->object->create( + 'joomla', + 'joomla-platform', + 'src/foo', + 'my Message', + 'ABC123def', + 'xxbranch', + 'eddieajau', + 'eddieajau@example.com', + 'elkuku', + '' + ); + } + + /** + * Tests the update method. + * + * @return void + */ + public function testUpdate() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/contents/src/foo') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->update( + 'joomla', + 'joomla-platform', + 'src/foo', + 'my Message', + 'ABC123def', + 'abcd1234', + 'xxbranch', + 'eddieajau', + 'eddieajau@example.com', + 'elkuku', + 'elkuku@example.com' + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the update method with missing author e-mail. + * + * @return void + */ + public function testUpdateFail1() + { + $this->expectException(\UnexpectedValueException::class); + + $this->object->update( + 'joomla', + 'joomla-platform', + 'src/foo', + 'my Message', + 'ABC123def', + 'abcd1234', + 'xxbranch', + 'eddieajau', + '', + 'elkuku', + 'elkuku@example.com' + ); + } + + /** + * Tests the update method with missing committer e-mail. + * + * @return void + */ + public function testUpdateFail2() + { + $this->expectException(\UnexpectedValueException::class); + + $this->object->update( + 'joomla', + 'joomla-platform', + 'src/foo', + 'my Message', + 'ABC123def', + 'abcd1234', + 'xxbranch', + 'eddieajau', + 'eddieajau@example.com', + 'elkuku', + '' + ); + } + + /** + * Tests the delete method. + * + * @return void + */ + public function testDelete() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/contents/src/foo') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete( + 'joomla', + 'joomla-platform', + 'src/foo', + 'my Message', + 'ABC123def', + 'xxbranch', + 'eddieajau', + 'eddieajau@example.com', + 'elkuku', + 'elkuku@example.com' + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the delete method with missing author e-mail. + * + * @return void + */ + public function testDeleteFail1() + { + $this->expectException(\UnexpectedValueException::class); + + $this->object->delete( + 'joomla', + 'joomla-platform', + 'src/foo', + 'my Message', + 'ABC123def', + 'xxbranch', + 'eddieajau', + '', + 'elkuku', + 'elkuku@example.com' + ); + } + + /** + * Tests the update method with missing committer e-mail. + * + * @return void + */ + public function testDeleteFail2() + { + $this->expectException(\UnexpectedValueException::class); + + $this->object->delete( + 'joomla', + 'joomla-platform', + 'src/foo', + 'my Message', + 'ABC123def', + 'xxbranch', + 'eddieajau', + 'eddieajau@example.com', + 'elkuku', + '' + ); + } } diff --git a/Tests/Package/Repositories/DeploymentsTest.php b/Tests/Package/Repositories/DeploymentsTest.php index eccf027f..a135b672 100644 --- a/Tests/Package/Repositories/DeploymentsTest.php +++ b/Tests/Package/Repositories/DeploymentsTest.php @@ -1,4 +1,5 @@ object = new Deployments($this->options, $this->client); - } - - /** - * Tests the GetList method. - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{owner}/{repo}/deployments?sha={sha}&ref={ref}&task={task}&environment={environment}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('{owner}', '{repo}', '{sha}', '{ref}', '{task}', '{environment}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Create method. - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/{owner}/{repo}/deployments') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create( - '{owner}', '{repo}', '{ref}', '{task}', '{automerge}', array('{requiredContexts}'), - '{payload}', '{environment}', '{description}' - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Create method. - * - * @return void - */ - public function testCreateMergeConflict() - { - $this->expectException(\RuntimeException::class); - - $this->response->code = 409; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/{owner}/{repo}/deployments') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('{owner}', '{repo}', '{ref}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Create method. - * - * @return void - */ - public function testCreateFailure() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/{owner}/{repo}/deployments') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('{owner}', '{repo}', '{ref}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the GetDeploymentStatuses method. - * - * @return void - */ - public function testGetDeploymentStatuses() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{owner}/{repo}/deployments/123/statuses') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getDeploymentStatuses('{owner}', '{repo}', 123), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the CreateStatus method. - * - * @return void - */ - public function testCreateStatus() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/{owner}/{repo}/deployments/123/statuses') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->createStatus('{owner}', '{repo}', 123, 'success', '{targetUrl}', '{description}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the CreateStatus method. - * - * @return void - */ - public function testCreateStatusFailure() - { - $this->expectException(\InvalidArgumentException::class); - - $this->object->createStatus('{owner}', '{repo}', 123, '{invalid}'); - } + /** + * @var Deployments + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Deployments($this->options, $this->client); + } + + /** + * Tests the GetList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{owner}/{repo}/deployments?sha={sha}&ref={ref}&task={task}&environment={environment}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('{owner}', '{repo}', '{sha}', '{ref}', '{task}', '{environment}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Create method. + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/{owner}/{repo}/deployments') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create( + '{owner}', + '{repo}', + '{ref}', + '{task}', + '{automerge}', + ['{requiredContexts}'], + '{payload}', + '{environment}', + '{description}' + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Create method. + * + * @return void + */ + public function testCreateMergeConflict() + { + $this->expectException(\RuntimeException::class); + + $this->response->code = 409; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/{owner}/{repo}/deployments') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('{owner}', '{repo}', '{ref}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Create method. + * + * @return void + */ + public function testCreateFailure() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/{owner}/{repo}/deployments') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('{owner}', '{repo}', '{ref}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the GetDeploymentStatuses method. + * + * @return void + */ + public function testGetDeploymentStatuses() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{owner}/{repo}/deployments/123/statuses') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getDeploymentStatuses('{owner}', '{repo}', 123), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the CreateStatus method. + * + * @return void + */ + public function testCreateStatus() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/{owner}/{repo}/deployments/123/statuses') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->createStatus('{owner}', '{repo}', 123, 'success', '{targetUrl}', '{description}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the CreateStatus method. + * + * @return void + */ + public function testCreateStatusFailure() + { + $this->expectException(\InvalidArgumentException::class); + + $this->object->createStatus('{owner}', '{repo}', 123, '{invalid}'); + } } diff --git a/Tests/Package/Repositories/DownloadsTest.php b/Tests/Package/Repositories/DownloadsTest.php index 953f8511..9e11539e 100644 --- a/Tests/Package/Repositories/DownloadsTest.php +++ b/Tests/Package/Repositories/DownloadsTest.php @@ -1,4 +1,5 @@ object = new Downloads($this->options, $this->client); - } - - /** - * Tests the GetList method. - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/downloads') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Get method. - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/downloads/123abc') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', '123abc'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Create method. - * - * @return void - */ - public function testCreate() - { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('The GitHub API no longer supports creating downloads. The Releases API should be used instead.'); - - $this->object->create('joomla', 'joomla-platform', 'aaa.zip', 1234, 'Description', 'content_type'); - } - - /** - * Tests the Upload method. - * - * @return void - */ - public function testUpload() - { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('The GitHub API no longer supports creating downloads. The Releases API should be used instead.'); - - $this->object->upload( - 'joomla', 'joomla-platform', 123, 'a/b/aaa.zip', 'acl', 201, 'aaa.zip', '123abc', '123abc', '123abc', 'content_type', '@aaa.zip' - ); - } - - /** - * Tests the Delete method. - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/downloads/123') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 'joomla-platform', 123), - $this->equalTo(json_decode($this->sampleString)) - ); - } + /** + * @var Downloads + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Downloads($this->options, $this->client); + } + + /** + * Tests the GetList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/downloads') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Get method. + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/downloads/123abc') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', '123abc'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Create method. + * + * @return void + */ + public function testCreate() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('The GitHub API no longer supports creating downloads. The Releases API should be used instead.'); + + $this->object->create('joomla', 'joomla-platform', 'aaa.zip', 1234, 'Description', 'content_type'); + } + + /** + * Tests the Upload method. + * + * @return void + */ + public function testUpload() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('The GitHub API no longer supports creating downloads. The Releases API should be used instead.'); + + $this->object->upload( + 'joomla', + 'joomla-platform', + 123, + 'a/b/aaa.zip', + 'acl', + 201, + 'aaa.zip', + '123abc', + '123abc', + '123abc', + 'content_type', + '@aaa.zip' + ); + } + + /** + * Tests the Delete method. + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/downloads/123') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 'joomla-platform', 123), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Repositories/ForksTest.php b/Tests/Package/Repositories/ForksTest.php index 255c5ef1..a6b6cc20 100755 --- a/Tests/Package/Repositories/ForksTest.php +++ b/Tests/Package/Repositories/ForksTest.php @@ -1,4 +1,5 @@ object = new Forks($this->options, $this->client); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 202; - - // Build the request data. - $data = json_encode( - array( - 'org' => 'jenkins-jools' - ) - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/forks', $data) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 'jenkins-jools'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the create method - failure - * - * @return void - */ - public function testCreateFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - // Build the request data. - $data = json_encode( - array() - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/forks', $data) - ->will($this->returnValue($this->response)); - - $this->object->create('joomla', 'joomla-platform', ''); - } - - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/forks') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getList method - failure - * - * @return void - */ - public function testGetListFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/forks') - ->will($this->returnValue($this->response)); - - $this->object->getList('joomla', 'joomla-platform'); - } + /** + * @var Forks Object under test. + * @since 11.4 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Forks($this->options, $this->client); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 202; + + // Build the request data. + $data = json_encode( + [ + 'org' => 'jenkins-jools', + ] + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/forks', $data) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', 'jenkins-jools'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the create method - failure + * + * @return void + */ + public function testCreateFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + // Build the request data. + $data = json_encode( + [] + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/forks', $data) + ->will($this->returnValue($this->response)); + + $this->object->create('joomla', 'joomla-platform', ''); + } + + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/forks') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getList method - failure + * + * @return void + */ + public function testGetListFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/forks') + ->will($this->returnValue($this->response)); + + $this->object->getList('joomla', 'joomla-platform'); + } } diff --git a/Tests/Package/Repositories/HooksTest.php b/Tests/Package/Repositories/HooksTest.php index 83f8b6af..4bf84c16 100644 --- a/Tests/Package/Repositories/HooksTest.php +++ b/Tests/Package/Repositories/HooksTest.php @@ -1,4 +1,5 @@ object = new Hooks($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::create() - * - * @return void - * - * @since 1.0 - */ - public function testCreate() - { - $this->response->code = 201; - - $hook = new \stdClass; - $hook->name = 'acunote'; - $hook->config = array('token' => '123456789'); - $hook->events = array('push', 'public'); - $hook->active = true; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/hooks', json_encode($hook)) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 'acunote', array('token' => '123456789'), array('push', 'public')), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::create() - * - * @return void - * - * @since 1.0 - */ - public function testCreateFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $hook = new \stdClass; - $hook->name = 'acunote'; - $hook->config = array('token' => '123456789'); - $hook->events = array('push', 'public'); - $hook->active = true; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/hooks', json_encode($hook)) - ->will($this->returnValue($this->response)); - - try - { - $this->object->create('joomla', 'joomla-platform', 'acunote', array('token' => '123456789'), array('push', 'public')); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::create() - * - * Unauthorised event - * - * @return void - * - * @since 1.0 - */ - public function testCreateUnauthorisedEvent() - { - $this->expectException(\RuntimeException::class); - - $this->object->create('joomla', 'joomla-platform', 'acunote', array('token' => '123456789'), array('push', 'faker')); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::delete() - * - * @return void - * - * @since 1.0 - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/hooks/42') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 'joomla-platform', 42), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::delete() - * - * Simulated failure - * - * @return void - * - * @since 1.0 - */ - public function testDeleteFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/hooks/42') - ->will($this->returnValue($this->response)); - - try - { - $this->object->delete('joomla', 'joomla-platform', 42); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::edit() - * - * @return void - * - * @since 1.0 - */ - public function testEdit() - { - $hook = '{' - . '"name":"acunote","config":{"token":"123456789"},"events":["push","public"],' - . '"add_events":["watch"],"remove_events":["watch"],"active":true' - . '}'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/hooks/42', $hook) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-platform', 42, 'acunote', array('token' => '123456789'), - array('push', 'public'), array('watch'), array('watch') - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::edit() - * - * Simulated failure - * - * @return void - * - * @since 1.0 - */ - public function testEditFailure() - { - $exception = false; - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $hook = '{' - . '"name":"acunote","config":{"token":"123456789"},"events":["push","public"],' - . '"add_events":["watch"],"remove_events":["watch"],"active":true' - . '}'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/hooks/42', $hook) - ->will($this->returnValue($this->response)); - - try - { - $this->object->edit('joomla', 'joomla-platform', 42, 'acunote', array('token' => '123456789'), - array('push', 'public'), array('watch'), array('watch') - ); - } - catch (\DomainException $e) - { - $exception = true; - - $this->assertThat( - $e->getMessage(), - $this->equalTo(json_decode($this->errorString)->message) - ); - } - - $this->assertTrue($exception); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::edit() - * - * Unauthorised event - * - * @return void - * - * @since 1.0 - */ - public function testEditUnauthorisedEvent() - { - $this->expectException(\RuntimeException::class); - - $this->object->edit('joomla', 'joomla-platform', 42, 'acunote', array('token' => '123456789'), array('invalid')); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::edit() - * - * Unauthorised event - * - * @return void - * - * @since 1.0 - */ - public function testEditUnauthorisedAddEvent() - { - $this->expectException(\RuntimeException::class); - - $this->object->edit('joomla', 'joomla-platform', 42, 'acunote', array('token' => '123456789'), array('push'), array('invalid')); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::edit() - * - * Unauthorised event - * - * @return void - * - * @since 1.0 - */ - public function testEditUnauthorisedRemoveEvent() - { - $this->expectException(\RuntimeException::class); - - $this->object->edit( - 'joomla', 'joomla-platform', 42, 'acunote', array('token' => '123456789'), array('push'), array('push'), array('invalid') - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::get() - * - * @return void - * - * @since 1.0 - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/hooks/42') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 42), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::get() - * - * Failure - * - * @return void - * - * @since 1.0 - */ - public function testGetFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/hooks/42') - ->will($this->returnValue($this->response)); - - $this->object->get('joomla', 'joomla-platform', 42); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::getList() - * - * @return void - * - * @since 1.0 - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/hooks') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::getList() - * - * Failure - * - * @return void - * - * @since 1.0 - */ - public function testGetListFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/hooks') - ->will($this->returnValue($this->response)); - - $this->object->getList('joomla', 'joomla-platform'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::test() - * - * @return void - * - * @since 1.0 - */ - public function testTest() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/hooks/42/test') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->test('joomla', 'joomla-platform', 42), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::test() - * - * Failure - * - * @return void - * - * @since 1.0 - */ - public function testTestFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/hooks/42/test') - ->will($this->returnValue($this->response)); - - $this->object->test('joomla', 'joomla-platform', 42); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Hooks::ping() - * - * @return void - * - * @since 1.0 - */ - public function testPing() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/{user}/{repo}/hooks/42/pings') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->ping('{user}', '{repo}', 42), - $this->equalTo(json_decode($this->sampleString)) - ); - } + /** + * @var Hooks Object under test. + * @since 12.3 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @return void + * + * @since 1.0 + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Hooks($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::create() + * + * @return void + * + * @since 1.0 + */ + public function testCreate() + { + $this->response->code = 201; + + $hook = new \stdClass(); + $hook->name = 'acunote'; + $hook->config = ['token' => '123456789']; + $hook->events = ['push', 'public']; + $hook->active = true; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/hooks', json_encode($hook)) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', 'acunote', ['token' => '123456789'], ['push', 'public']), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::create() + * + * @return void + * + * @since 1.0 + */ + public function testCreateFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $hook = new \stdClass(); + $hook->name = 'acunote'; + $hook->config = ['token' => '123456789']; + $hook->events = ['push', 'public']; + $hook->active = true; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/hooks', json_encode($hook)) + ->will($this->returnValue($this->response)); + + try { + $this->object->create('joomla', 'joomla-platform', 'acunote', ['token' => '123456789'], ['push', 'public']); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::create() + * + * Unauthorised event + * + * @return void + * + * @since 1.0 + */ + public function testCreateUnauthorisedEvent() + { + $this->expectException(\RuntimeException::class); + + $this->object->create('joomla', 'joomla-platform', 'acunote', ['token' => '123456789'], ['push', 'faker']); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::delete() + * + * @return void + * + * @since 1.0 + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/hooks/42') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 'joomla-platform', 42), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::delete() + * + * Simulated failure + * + * @return void + * + * @since 1.0 + */ + public function testDeleteFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/hooks/42') + ->will($this->returnValue($this->response)); + + try { + $this->object->delete('joomla', 'joomla-platform', 42); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::edit() + * + * @return void + * + * @since 1.0 + */ + public function testEdit() + { + $hook = '{' + . '"name":"acunote","config":{"token":"123456789"},"events":["push","public"],' + . '"add_events":["watch"],"remove_events":["watch"],"active":true' + . '}'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/hooks/42', $hook) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit( + 'joomla', + 'joomla-platform', + 42, + 'acunote', + ['token' => '123456789'], + ['push', 'public'], + ['watch'], + ['watch'] + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::edit() + * + * Simulated failure + * + * @return void + * + * @since 1.0 + */ + public function testEditFailure() + { + $exception = false; + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $hook = '{' + . '"name":"acunote","config":{"token":"123456789"},"events":["push","public"],' + . '"add_events":["watch"],"remove_events":["watch"],"active":true' + . '}'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/hooks/42', $hook) + ->will($this->returnValue($this->response)); + + try { + $this->object->edit( + 'joomla', + 'joomla-platform', + 42, + 'acunote', + ['token' => '123456789'], + ['push', 'public'], + ['watch'], + ['watch'] + ); + } catch (\DomainException $e) { + $exception = true; + + $this->assertThat( + $e->getMessage(), + $this->equalTo(json_decode($this->errorString)->message) + ); + } + + $this->assertTrue($exception); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::edit() + * + * Unauthorised event + * + * @return void + * + * @since 1.0 + */ + public function testEditUnauthorisedEvent() + { + $this->expectException(\RuntimeException::class); + + $this->object->edit('joomla', 'joomla-platform', 42, 'acunote', ['token' => '123456789'], ['invalid']); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::edit() + * + * Unauthorised event + * + * @return void + * + * @since 1.0 + */ + public function testEditUnauthorisedAddEvent() + { + $this->expectException(\RuntimeException::class); + + $this->object->edit('joomla', 'joomla-platform', 42, 'acunote', ['token' => '123456789'], ['push'], ['invalid']); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::edit() + * + * Unauthorised event + * + * @return void + * + * @since 1.0 + */ + public function testEditUnauthorisedRemoveEvent() + { + $this->expectException(\RuntimeException::class); + + $this->object->edit( + 'joomla', + 'joomla-platform', + 42, + 'acunote', + ['token' => '123456789'], + ['push'], + ['push'], + ['invalid'] + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::get() + * + * @return void + * + * @since 1.0 + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/hooks/42') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 42), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::get() + * + * Failure + * + * @return void + * + * @since 1.0 + */ + public function testGetFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/hooks/42') + ->will($this->returnValue($this->response)); + + $this->object->get('joomla', 'joomla-platform', 42); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::getList() + * + * @return void + * + * @since 1.0 + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/hooks') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::getList() + * + * Failure + * + * @return void + * + * @since 1.0 + */ + public function testGetListFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/hooks') + ->will($this->returnValue($this->response)); + + $this->object->getList('joomla', 'joomla-platform'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::test() + * + * @return void + * + * @since 1.0 + */ + public function testTest() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/hooks/42/test') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->test('joomla', 'joomla-platform', 42), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::test() + * + * Failure + * + * @return void + * + * @since 1.0 + */ + public function testTestFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/hooks/42/test') + ->will($this->returnValue($this->response)); + + $this->object->test('joomla', 'joomla-platform', 42); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Hooks::ping() + * + * @return void + * + * @since 1.0 + */ + public function testPing() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/{user}/{repo}/hooks/42/pings') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->ping('{user}', '{repo}', 42), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Repositories/KeysTest.php b/Tests/Package/Repositories/KeysTest.php index eb3e5cba..0243ff46 100644 --- a/Tests/Package/Repositories/KeysTest.php +++ b/Tests/Package/Repositories/KeysTest.php @@ -1,4 +1,5 @@ object = new Keys($this->options, $this->client); - } - - /** - * Tests the GetList method. - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/keys') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Get method. - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/keys/1') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', 1), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Create method. - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/keys') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', 'email@example.com', '123abc'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Edit method. - * - * @return void - */ - public function testEdit() - { - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/keys/1') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-platform', 1, 'email@example.com', '123abc'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Delete method. - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - $this->response->body = true; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/keys/1') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 'joomla-platform', 1), - $this->equalTo($this->response->body) - ); - } + /** + * @var Keys + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Keys($this->options, $this->client); + } + + /** + * Tests the GetList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/keys') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Get method. + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/keys/1') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', 1), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Create method. + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/keys') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', 'email@example.com', '123abc'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Edit method. + * + * @return void + */ + public function testEdit() + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/keys/1') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit('joomla', 'joomla-platform', 1, 'email@example.com', '123abc'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Delete method. + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + $this->response->body = true; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/keys/1') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 'joomla-platform', 1), + $this->equalTo($this->response->body) + ); + } } diff --git a/Tests/Package/Repositories/MergingTest.php b/Tests/Package/Repositories/MergingTest.php index 128535da..c7e0c3f1 100644 --- a/Tests/Package/Repositories/MergingTest.php +++ b/Tests/Package/Repositories/MergingTest.php @@ -1,4 +1,5 @@ object = new Merging($this->options, $this->client); - } - - /** - * Tests the Perform method. - * - * @return void - */ - public function testPerform() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/merges') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->perform('joomla', 'joomla-platform', '123', '456', 'My Message'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Perform method. - * - * @return void - */ - public function testPerformNoOp() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/merges') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->perform('joomla', 'joomla-platform', '123', '456', 'My Message'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Perform method. - * - * @return void - */ - public function testPerformMissing() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 404; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/merges') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->perform('joomla', 'joomla-platform', '123', '456', 'My Message'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Perform method. - * - * @return void - */ - public function testPerformConflict() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 409; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/merges') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->perform('joomla', 'joomla-platform', '123', '456', 'My Message'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the Perform method. - * - * @return void - */ - public function testPerformUnexpected() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/merges') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->perform('joomla', 'joomla-platform', '123', '456', 'My Message'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + /** + * @var Merging + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Merging($this->options, $this->client); + } + + /** + * Tests the Perform method. + * + * @return void + */ + public function testPerform() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/merges') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->perform('joomla', 'joomla-platform', '123', '456', 'My Message'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Perform method. + * + * @return void + */ + public function testPerformNoOp() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/merges') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->perform('joomla', 'joomla-platform', '123', '456', 'My Message'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Perform method. + * + * @return void + */ + public function testPerformMissing() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 404; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/merges') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->perform('joomla', 'joomla-platform', '123', '456', 'My Message'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Perform method. + * + * @return void + */ + public function testPerformConflict() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 409; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/merges') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->perform('joomla', 'joomla-platform', '123', '456', 'My Message'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the Perform method. + * + * @return void + */ + public function testPerformUnexpected() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/merges') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->perform('joomla', 'joomla-platform', '123', '456', 'My Message'), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Repositories/PagesTest.php b/Tests/Package/Repositories/PagesTest.php index 242de302..e723d41d 100644 --- a/Tests/Package/Repositories/PagesTest.php +++ b/Tests/Package/Repositories/PagesTest.php @@ -16,77 +16,77 @@ */ class PagesTest extends GitHubTestCase { - /** - * @var Pages - */ - protected $object; + /** + * @var Pages + */ + protected $object; - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @since 1.0 - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); - $this->object = new Pages($this->options, $this->client); - } + $this->object = new Pages($this->options, $this->client); + } - /** - * Tests the GetInfo method. - * - * @return void - */ - public function testGetInfo() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{owner}/{repo}/pages') - ->will($this->returnValue($this->response)); + /** + * Tests the GetInfo method. + * + * @return void + */ + public function testGetInfo() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{owner}/{repo}/pages') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getInfo('{owner}', '{repo}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->getInfo('{owner}', '{repo}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the GetList method. - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{owner}/{repo}/pages/builds') - ->will($this->returnValue($this->response)); + /** + * Tests the GetList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{owner}/{repo}/pages/builds') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getList('{owner}', '{repo}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->getList('{owner}', '{repo}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the GetLatest method. - * - * @return void - */ - public function testGetLatest() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{owner}/{repo}/pages/builds/latest') - ->will($this->returnValue($this->response)); + /** + * Tests the GetLatest method. + * + * @return void + */ + public function testGetLatest() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{owner}/{repo}/pages/builds/latest') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getLatest('{owner}', '{repo}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->getLatest('{owner}', '{repo}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Repositories/ReleasesTest.php b/Tests/Package/Repositories/ReleasesTest.php index 9d4c27e7..ddd596c8 100644 --- a/Tests/Package/Repositories/ReleasesTest.php +++ b/Tests/Package/Repositories/ReleasesTest.php @@ -16,272 +16,271 @@ */ class ReleasesTest extends GitHubTestCase { - /** - * @var Releases Object under test. - * @since 1.0 - */ - protected $object; - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @since 1.0 - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); - - $this->object = new Releases($this->options, $this->client); - } - - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/releases/12345', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-platform', '12345'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $data = '{"tag_name":"0.1","target_commitish":"targetCommitish","name":"master","body":"New release","draft":false,"prerelease":false}'; - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/releases', $data, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', '0.1', 'targetCommitish', 'master', 'New release', false, false), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the create method with failure. - * - * @return void - */ - public function testCreateFailure() - { - $this->response->code = 201; - - $data = '{"tag_name":"0.1","target_commitish":"targetCommitish","name":"master","body":"New release","draft":false,"prerelease":false}'; - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/releases', $data, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla', 'joomla-platform', '0.1', 'targetCommitish', 'master', 'New release', false, false), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the edit method. - * - * @return void - */ - public function testEdit() - { - $releaseId = 123; - - $data = '{"tag_name":"tagName","target_commitish":"targetCommitish","name":"name","body":"body","draft":"draft","prerelease":"preRelease"}'; - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/releases/' . $releaseId, $data, array(), 0) - - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-platform', $releaseId, 'tagName', 'targetCommitish', 'name', 'body', 'draft', 'preRelease'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getList method. - * - * @return void - */ - public function testGetList() - { - $this->response->code = 200; - $this->response->body = '[{"tag_name":"1"},{"tag_name":"2"}]'; - - $releases = array(); - - foreach (json_decode($this->response->body) as $i => $release) - { - $releases[$i + 1] = $release; - } - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/releases', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform'), - $this->equalTo($releases) - ); - } - - /** - * Tests the delete method - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/releases/123') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 'joomla-platform', '123'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getLatest method. - * - * @return void - */ - public function testGetLatest() - { - $this->response->code = 200; - $this->response->body = '[]'; - - $releases = array(); - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/releases/latest', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getLatest('joomla', 'joomla-platform'), - $this->equalTo($releases) - ); - } - - /** - * Tests the getByTag method - * - * @return void - */ - public function testGetByTag() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/releases/tags/{tag}', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getByTag('joomla', 'joomla-platform', '{tag}'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getListAssets method - * - * @return void - */ - public function testGetListAssets() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/releases/123/assets', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListAssets('joomla', 'joomla-platform', 123), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getAsset method - * - * @return void - */ - public function testGetAsset() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/releases/assets/123', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getAsset('joomla', 'joomla-platform', 123), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the editAsset method - * - * @return void - */ - public function testEditAsset() - { - $data = '{"name":"{name}","label":"{label}"}'; - - $this->client->expects($this->once()) - ->method('patch') - ->with('/repos/joomla/joomla-platform/releases/assets/123', $data, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->editAsset('joomla', 'joomla-platform', 123, '{name}', '{label}'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the deleteAsset method - * - * @return void - */ - public function testDeleteAsset() - { - $this->response->code = 204; - $this->response->body = true; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-platform/releases/assets/123', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->deleteAsset('joomla', 'joomla-platform', 123), - $this->equalTo($this->response->body) - ); - } + /** + * @var Releases Object under test. + * @since 1.0 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Releases($this->options, $this->client); + } + + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/releases/12345', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-platform', '12345'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $data = '{"tag_name":"0.1","target_commitish":"targetCommitish","name":"master","body":"New release","draft":false,"prerelease":false}'; + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/releases', $data, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', '0.1', 'targetCommitish', 'master', 'New release', false, false), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the create method with failure. + * + * @return void + */ + public function testCreateFailure() + { + $this->response->code = 201; + + $data = '{"tag_name":"0.1","target_commitish":"targetCommitish","name":"master","body":"New release","draft":false,"prerelease":false}'; + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/releases', $data, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla', 'joomla-platform', '0.1', 'targetCommitish', 'master', 'New release', false, false), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the edit method. + * + * @return void + */ + public function testEdit() + { + $releaseId = 123; + + $data = '{"tag_name":"tagName","target_commitish":"targetCommitish","name":"name","body":"body","draft":"draft","prerelease":"preRelease"}'; + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/releases/' . $releaseId, $data, [], 0) + + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit('joomla', 'joomla-platform', $releaseId, 'tagName', 'targetCommitish', 'name', 'body', 'draft', 'preRelease'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getList method. + * + * @return void + */ + public function testGetList() + { + $this->response->code = 200; + $this->response->body = '[{"tag_name":"1"},{"tag_name":"2"}]'; + + $releases = []; + + foreach (json_decode($this->response->body) as $i => $release) { + $releases[$i + 1] = $release; + } + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/releases', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform'), + $this->equalTo($releases) + ); + } + + /** + * Tests the delete method + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/releases/123') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 'joomla-platform', '123'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getLatest method. + * + * @return void + */ + public function testGetLatest() + { + $this->response->code = 200; + $this->response->body = '[]'; + + $releases = []; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/releases/latest', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getLatest('joomla', 'joomla-platform'), + $this->equalTo($releases) + ); + } + + /** + * Tests the getByTag method + * + * @return void + */ + public function testGetByTag() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/releases/tags/{tag}', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getByTag('joomla', 'joomla-platform', '{tag}'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getListAssets method + * + * @return void + */ + public function testGetListAssets() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/releases/123/assets', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListAssets('joomla', 'joomla-platform', 123), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getAsset method + * + * @return void + */ + public function testGetAsset() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/releases/assets/123', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getAsset('joomla', 'joomla-platform', 123), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the editAsset method + * + * @return void + */ + public function testEditAsset() + { + $data = '{"name":"{name}","label":"{label}"}'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/releases/assets/123', $data, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->editAsset('joomla', 'joomla-platform', 123, '{name}', '{label}'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the deleteAsset method + * + * @return void + */ + public function testDeleteAsset() + { + $this->response->code = 204; + $this->response->body = true; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/releases/assets/123', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->deleteAsset('joomla', 'joomla-platform', 123), + $this->equalTo($this->response->body) + ); + } } diff --git a/Tests/Package/Repositories/StatisticsTest.php b/Tests/Package/Repositories/StatisticsTest.php index c4baa718..4aefac24 100644 --- a/Tests/Package/Repositories/StatisticsTest.php +++ b/Tests/Package/Repositories/StatisticsTest.php @@ -16,136 +16,136 @@ */ class StatisticsTest extends GitHubTestCase { - /** - * @var Statistics Object under test. - * @since 11.4 - */ - protected $object; - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @access protected - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); - - $this->object = new Statistics($this->options, $this->client); - } - - /** - * Tests the getListContributors method. - * - * @return void - */ - public function testContributors() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/stats/contributors') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListContributors('joomla', 'joomla-framework'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getActivityData method. - * - * @return void - */ - public function testActivity() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/stats/commit_activity') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getActivityData('joomla', 'joomla-framework'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getCodeFrequency method. - * - * @return void - */ - public function testFrequency() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/stats/code_frequency') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getCodeFrequency('joomla', 'joomla-framework'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getParticipation method. - * - * @return void - */ - public function testParticipation() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/stats/participation') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getParticipation('joomla', 'joomla-framework'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getPunchCard method. - * - * @return void - */ - public function testPunchCard() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/stats/punch_card') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getPunchCard('joomla', 'joomla-framework'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the ProcessResponse method with failure. - * - * @return void - */ - public function testProcessResponse202() - { - $this->expectException(\DomainException::class); - - $this->response->code = 202; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-framework/stats/punch_card') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getPunchCard('joomla', 'joomla-framework'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + /** + * @var Statistics Object under test. + * @since 11.4 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Statistics($this->options, $this->client); + } + + /** + * Tests the getListContributors method. + * + * @return void + */ + public function testContributors() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/stats/contributors') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListContributors('joomla', 'joomla-framework'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getActivityData method. + * + * @return void + */ + public function testActivity() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/stats/commit_activity') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getActivityData('joomla', 'joomla-framework'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getCodeFrequency method. + * + * @return void + */ + public function testFrequency() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/stats/code_frequency') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getCodeFrequency('joomla', 'joomla-framework'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getParticipation method. + * + * @return void + */ + public function testParticipation() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/stats/participation') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getParticipation('joomla', 'joomla-framework'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getPunchCard method. + * + * @return void + */ + public function testPunchCard() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/stats/punch_card') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getPunchCard('joomla', 'joomla-framework'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the ProcessResponse method with failure. + * + * @return void + */ + public function testProcessResponse202() + { + $this->expectException(\DomainException::class); + + $this->response->code = 202; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-framework/stats/punch_card') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getPunchCard('joomla', 'joomla-framework'), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Repositories/StatusesTest.php b/Tests/Package/Repositories/StatusesTest.php index 02e6d16d..d51a6b73 100755 --- a/Tests/Package/Repositories/StatusesTest.php +++ b/Tests/Package/Repositories/StatusesTest.php @@ -18,180 +18,180 @@ */ class StatusesTest extends GitHubTestCase { - /** - * @var Statuses Object under test. - * @since 11.4 - */ - protected $object; - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @access protected - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); - - $this->object = new Statuses($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Statuses::create() - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - // Build the request data. - $data = json_encode( - array( - 'state' => 'success', - 'target_url' => 'http://example.com/my_url', - 'description' => 'Success is the only option - failure is not.', - 'context' => 'Joomla/Test' - ) - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e', $data) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create( - 'joomla', - 'joomla-platform', - '6dcb09b5b57875f334f61aebed695e2e4193db5e', - 'success', - 'http://example.com/my_url', - 'Success is the only option - failure is not.', - 'Joomla/Test' - ), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Statuses::create() - * - * Failure - * - * @return void - */ - public function testCreateFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 501; - $this->response->body = $this->errorString; - - // Build the request data. - $data = json_encode( - array( - 'state' => 'pending' - ) - ); - - $this->client->expects($this->once()) - ->method('post') - ->with('/repos/joomla/joomla-platform/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e', $data) - ->will($this->returnValue($this->response)); - - $this->object->create('joomla', 'joomla-platform', '6dcb09b5b57875f334f61aebed695e2e4193db5e', 'pending'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Statuses::create() - * - * Failure - * - * @return void - */ - public function testCreateInvalidState() - { - $this->expectException(\InvalidArgumentException::class); - - $this->response->code = 501; - $this->response->body = $this->errorString; - - $this->object->create('joomla', 'joomla-platform', '6dcb09b5b57875f334f61aebed695e2e4193db5e', 'INVALID'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Statuses::getList() - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla', 'joomla-platform', '6dcb09b5b57875f334f61aebed695e2e4193db5e'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Statuses::getList() - * - * Failure - * - * @return void - */ - public function testGetListFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 500; - $this->response->body = $this->errorString; - - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-platform/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e') - ->will($this->returnValue($this->response)); - - $this->object->getList('joomla', 'joomla-platform', '6dcb09b5b57875f334f61aebed695e2e4193db5e'); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Repositories\Statuses::getCombinedStatus() - * - * @return void - * - * @since 1.0 - */ - public function testGetCombinedStatus() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/{user}/{repo}/commits/{sha}/status') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getCombinedStatus('{user}', '{repo}', '{sha}'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + /** + * @var Statuses Object under test. + * @since 11.4 + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Statuses($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Statuses::create() + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + // Build the request data. + $data = json_encode( + [ + 'state' => 'success', + 'target_url' => 'http://example.com/my_url', + 'description' => 'Success is the only option - failure is not.', + 'context' => 'Joomla/Test', + ] + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e', $data) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create( + 'joomla', + 'joomla-platform', + '6dcb09b5b57875f334f61aebed695e2e4193db5e', + 'success', + 'http://example.com/my_url', + 'Success is the only option - failure is not.', + 'Joomla/Test' + ), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Statuses::create() + * + * Failure + * + * @return void + */ + public function testCreateFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 501; + $this->response->body = $this->errorString; + + // Build the request data. + $data = json_encode( + [ + 'state' => 'pending', + ] + ); + + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e', $data) + ->will($this->returnValue($this->response)); + + $this->object->create('joomla', 'joomla-platform', '6dcb09b5b57875f334f61aebed695e2e4193db5e', 'pending'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Statuses::create() + * + * Failure + * + * @return void + */ + public function testCreateInvalidState() + { + $this->expectException(\InvalidArgumentException::class); + + $this->response->code = 501; + $this->response->body = $this->errorString; + + $this->object->create('joomla', 'joomla-platform', '6dcb09b5b57875f334f61aebed695e2e4193db5e', 'INVALID'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Statuses::getList() + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla', 'joomla-platform', '6dcb09b5b57875f334f61aebed695e2e4193db5e'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Statuses::getList() + * + * Failure + * + * @return void + */ + public function testGetListFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 500; + $this->response->body = $this->errorString; + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e') + ->will($this->returnValue($this->response)); + + $this->object->getList('joomla', 'joomla-platform', '6dcb09b5b57875f334f61aebed695e2e4193db5e'); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Repositories\Statuses::getCombinedStatus() + * + * @return void + * + * @since 1.0 + */ + public function testGetCombinedStatus() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/{user}/{repo}/commits/{sha}/status') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getCombinedStatus('{user}', '{repo}', '{sha}'), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/RepositoriesTest.php b/Tests/Package/RepositoriesTest.php index a392afa4..667d5f0c 100644 --- a/Tests/Package/RepositoriesTest.php +++ b/Tests/Package/RepositoriesTest.php @@ -16,358 +16,362 @@ */ class RepositoriesTest extends GitHubTestCase { - /** - * @var Repositories - */ - protected $object; - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @since 1.0 - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); - - $this->object = new Repositories($this->options, $this->client); - } - - /** - * Tests the GetListOwn method. - * - * @return void - */ - public function testGetListOwn() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/user/repos?type=all&sort=full_name&direction=asc', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListOwn(), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the GetListOwn method. - * - * @return void - */ - public function testGetListOwnInvalidType() - { - $this->expectException(\RuntimeException::class); - - $this->object->getListOwn('INVALID'); - } - - /** - * Tests the GetListOwn method. - * - * @return void - */ - public function testGetListOwnInvalidSortField() - { - $this->expectException(\RuntimeException::class); - - $this->object->getListOwn('all', 'INVALID'); - } - - /** - * Tests the GetListOwn method. - * - * @return void - */ - public function testGetListOwnInvalidSortOrder() - { - $this->expectException(\RuntimeException::class); - - $this->object->getListOwn('all', 'full_name', 'INVALID'); - } - - /** - * Tests the GetListUser method. - * - * @return void - */ - public function testGetListUser() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/users/joomla/repos?type=all&sort=full_name&direction=asc', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListUser('joomla'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the GetListUser method. - * - * @return void - */ - public function testGetListUserInvalidType() - { - $this->expectException(\RuntimeException::class); - - $this->object->getListUser('joomla', 'INVALID'); - } - - /** - * Tests the GetListUser method. - * - * @return void - */ - public function testGetListUserInvalidSortField() - { - $this->expectException(\RuntimeException::class); - - $this->object->getListUser('joomla', 'all', 'INVALID'); - } - - /** - * Tests the GetListUser method. - * - * @return void - */ - public function testGetListUserInvalidSortOrder() - { - $this->expectException(\RuntimeException::class); - - $this->object->getListUser('joomla', 'all', 'full_name', 'INVALID'); - } - - /** - * Tests the GetListOrg method. - * - * @return void - */ - public function testGetListOrg() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/orgs/joomla/repos?type=all', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListOrg('joomla'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the GetList method. - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repositories', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList(), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the Create method. - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/user/repos', - '{"name":"joomla-test","description":"","homepage":"","private":false,"has_issues":false,' - . '"has_wiki":false,"has_downloads":false,"team_id":0,"auto_init":false,"gitignore_template":""}', - array(), 0 - ) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla-test'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the Create method. - * - * @return void - */ - public function testCreateWithOrg() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/orgs/joomla.org/repos', - '{"name":"joomla-test","description":"","homepage":"","private":false,"has_issues":false,' - . '"has_wiki":false,"has_downloads":false,"team_id":0,"auto_init":false,"gitignore_template":""}', - array(), 0 - ) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('joomla-test', 'joomla.org'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the Get method. - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-cms', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla', 'joomla-cms'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the GetListOrg method. - * - * @return void - */ - public function testGetListOrgInvalidType() - { - $this->expectException(\RuntimeException::class); - - $this->object->getListOrg('joomla', 'INVALID'); - } - - /** - * Tests the Edit method. - * - * @return void - */ - public function testEdit() - { - $this->client->expects($this->once()) - ->method('patch') - ->with( - '/repos/joomla/joomla-test', - '{"name":"joomla-test-1","description":"","homepage":"","private":' - . 'false,"has_issues":false,"has_wiki":false,"has_downloads":false,"default_branch":""}', - array() - ) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit('joomla', 'joomla-test', 'joomla-test-1'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the GetListContributors method. - * - * @return void - */ - public function testGetListContributors() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-cms/contributors', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListContributors('joomla', 'joomla-cms'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the GetListLanguages method. - * - * @return void - */ - public function testGetListLanguages() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-cms/languages', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListLanguages('joomla', 'joomla-cms'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the GetListTeams method. - * - * @return void - */ - public function testGetListTeams() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-cms/teams', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListTeams('joomla', 'joomla-cms'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the GetListTags method. - * - * @return void - */ - public function testGetListTags() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/repos/joomla/joomla-cms/tags', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListTags('joomla', 'joomla-cms'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the Delete method. - * - * @return void - */ - public function testDelete() - { - $this->client->expects($this->once()) - ->method('delete') - ->with('/repos/joomla/joomla-cms', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete('joomla', 'joomla-cms'), - $this->equalTo(json_decode($this->response->body)) - ); - } + /** + * @var Repositories + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Repositories($this->options, $this->client); + } + + /** + * Tests the GetListOwn method. + * + * @return void + */ + public function testGetListOwn() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/repos?type=all&sort=full_name&direction=asc', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListOwn(), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the GetListOwn method. + * + * @return void + */ + public function testGetListOwnInvalidType() + { + $this->expectException(\RuntimeException::class); + + $this->object->getListOwn('INVALID'); + } + + /** + * Tests the GetListOwn method. + * + * @return void + */ + public function testGetListOwnInvalidSortField() + { + $this->expectException(\RuntimeException::class); + + $this->object->getListOwn('all', 'INVALID'); + } + + /** + * Tests the GetListOwn method. + * + * @return void + */ + public function testGetListOwnInvalidSortOrder() + { + $this->expectException(\RuntimeException::class); + + $this->object->getListOwn('all', 'full_name', 'INVALID'); + } + + /** + * Tests the GetListUser method. + * + * @return void + */ + public function testGetListUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/joomla/repos?type=all&sort=full_name&direction=asc', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListUser('joomla'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the GetListUser method. + * + * @return void + */ + public function testGetListUserInvalidType() + { + $this->expectException(\RuntimeException::class); + + $this->object->getListUser('joomla', 'INVALID'); + } + + /** + * Tests the GetListUser method. + * + * @return void + */ + public function testGetListUserInvalidSortField() + { + $this->expectException(\RuntimeException::class); + + $this->object->getListUser('joomla', 'all', 'INVALID'); + } + + /** + * Tests the GetListUser method. + * + * @return void + */ + public function testGetListUserInvalidSortOrder() + { + $this->expectException(\RuntimeException::class); + + $this->object->getListUser('joomla', 'all', 'full_name', 'INVALID'); + } + + /** + * Tests the GetListOrg method. + * + * @return void + */ + public function testGetListOrg() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/repos?type=all', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListOrg('joomla'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the GetList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repositories', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList(), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the Create method. + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with( + '/user/repos', + '{"name":"joomla-test","description":"","homepage":"","private":false,"has_issues":false,' + . '"has_wiki":false,"has_downloads":false,"team_id":0,"auto_init":false,"gitignore_template":""}', + [], + 0 + ) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla-test'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the Create method. + * + * @return void + */ + public function testCreateWithOrg() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with( + '/orgs/joomla.org/repos', + '{"name":"joomla-test","description":"","homepage":"","private":false,"has_issues":false,' + . '"has_wiki":false,"has_downloads":false,"team_id":0,"auto_init":false,"gitignore_template":""}', + [], + 0 + ) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('joomla-test', 'joomla.org'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the Get method. + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-cms', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla', 'joomla-cms'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the GetListOrg method. + * + * @return void + */ + public function testGetListOrgInvalidType() + { + $this->expectException(\RuntimeException::class); + + $this->object->getListOrg('joomla', 'INVALID'); + } + + /** + * Tests the Edit method. + * + * @return void + */ + public function testEdit() + { + $this->client->expects($this->once()) + ->method('patch') + ->with( + '/repos/joomla/joomla-test', + '{"name":"joomla-test-1","description":"","homepage":"","private":' + . 'false,"has_issues":false,"has_wiki":false,"has_downloads":false,"default_branch":""}', + [] + ) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit('joomla', 'joomla-test', 'joomla-test-1'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the GetListContributors method. + * + * @return void + */ + public function testGetListContributors() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-cms/contributors', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListContributors('joomla', 'joomla-cms'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the GetListLanguages method. + * + * @return void + */ + public function testGetListLanguages() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-cms/languages', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListLanguages('joomla', 'joomla-cms'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the GetListTeams method. + * + * @return void + */ + public function testGetListTeams() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-cms/teams', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListTeams('joomla', 'joomla-cms'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the GetListTags method. + * + * @return void + */ + public function testGetListTags() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-cms/tags', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListTags('joomla', 'joomla-cms'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the Delete method. + * + * @return void + */ + public function testDelete() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-cms', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete('joomla', 'joomla-cms'), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/SearchTest.php b/Tests/Package/SearchTest.php index 1a2d5954..ea8c758f 100644 --- a/Tests/Package/SearchTest.php +++ b/Tests/Package/SearchTest.php @@ -16,107 +16,107 @@ */ class SearchTest extends GitHubTestCase { - /** - * @var Search - */ - protected $object; + /** + * @var Search + */ + protected $object; - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @since 1.0 - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); - $this->object = new Search($this->options, $this->client); - } + $this->object = new Search($this->options, $this->client); + } - /** - * Tests the issues method - * - * @return void - */ - public function testIssues() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/legacy/issues/search/joomla/joomla-platform/open/github') - ->will($this->returnValue($this->response)); + /** + * Tests the issues method + * + * @return void + */ + public function testIssues() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/legacy/issues/search/joomla/joomla-platform/open/github') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->issues('joomla', 'joomla-platform', 'open', 'github'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->issues('joomla', 'joomla-platform', 'open', 'github'), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the issuesInvalidState method - * - * @return void - */ - public function testIssuesInvalidState() - { - $this->expectException(\UnexpectedValueException::class); + /** + * Tests the issuesInvalidState method + * + * @return void + */ + public function testIssuesInvalidState() + { + $this->expectException(\UnexpectedValueException::class); - $this->object->issues('joomla', 'joomla-platform', 'invalid', 'github'); - } + $this->object->issues('joomla', 'joomla-platform', 'invalid', 'github'); + } - /** - * Tests the repositories method - * - * @return void - */ - public function testRepositories() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/legacy/repos/search/joomla') - ->will($this->returnValue($this->response)); + /** + * Tests the repositories method + * + * @return void + */ + public function testRepositories() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/legacy/repos/search/joomla') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->repositories('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->repositories('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the users method - * - * @return void - */ - public function testUsers() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/legacy/user/search/joomla') - ->will($this->returnValue($this->response)); + /** + * Tests the users method + * + * @return void + */ + public function testUsers() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/legacy/user/search/joomla') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->users('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->users('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the email method - * - * @return void - */ - public function testEmail() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/legacy/user/email/email@joomla') - ->will($this->returnValue($this->response)); + /** + * Tests the email method + * + * @return void + */ + public function testEmail() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/legacy/user/email/email@joomla') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->email('email@joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->email('email@joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Users/EmailsTest.php b/Tests/Package/Users/EmailsTest.php index c1d466e6..a08eda6b 100644 --- a/Tests/Package/Users/EmailsTest.php +++ b/Tests/Package/Users/EmailsTest.php @@ -16,81 +16,81 @@ */ class EmailsTest extends GitHubTestCase { - /** - * @var Emails - */ - protected $object; + /** + * @var Emails + */ + protected $object; - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @since 1.0 - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); - $this->object = new Emails($this->options, $this->client); - } + $this->object = new Emails($this->options, $this->client); + } - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/user/emails') - ->will($this->returnValue($this->response)); + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/emails') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->getList(), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->getList(), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the add method - * - * @return void - */ - public function testAdd() - { - $this->response->code = 201; + /** + * Tests the add method + * + * @return void + */ + public function testAdd() + { + $this->response->code = 201; - $this->client->expects($this->once()) - ->method('post') - ->with('/user/emails') - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('post') + ->with('/user/emails') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->add('email@example.com'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->add('email@example.com'), + $this->equalTo(json_decode($this->sampleString)) + ); + } - /** - * Tests the delete method - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; + /** + * Tests the delete method + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; - $this->client->expects($this->once()) - ->method('delete') - ->with('/user/emails') - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('delete') + ->with('/user/emails') + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->delete('email@example.com'), - $this->equalTo(json_decode($this->sampleString)) - ); - } + $this->assertThat( + $this->object->delete('email@example.com'), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/Users/FollowersTest.php b/Tests/Package/Users/FollowersTest.php index 6bad291b..00a1c247 100644 --- a/Tests/Package/Users/FollowersTest.php +++ b/Tests/Package/Users/FollowersTest.php @@ -18,305 +18,305 @@ */ class FollowersTest extends GitHubTestCase { - /** - * @var Followers - */ - protected $object; - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @since 1.0 - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); - - $this->object = new Followers($this->options, $this->client); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::getList() - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/user/followers') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::getList() - * - * @return void - */ - public function testGetListWithUser() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/users/joomla/followers') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::getListFollowedBy() - * - * @return void - */ - public function testGetListFollowedBy() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/user/following') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListFollowedBy(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::getListFollowedBy() - * - * @return void - */ - public function testGetListFollowedByWithUser() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/users/joomla/following') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListFollowedBy('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::check() - * - * You are following this user - * - * @return void - */ - public function testCheck() - { - $this->response->code = 204; - $this->response->body = true; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/following/joomla') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla'), - $this->equalTo($this->response->body) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::check() - * - * You are not following this user - * - * @return void - */ - public function testCheckNo() - { - $this->response->code = 404; - $this->response->body = false; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/following/joomla') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla'), - $this->equalTo($this->response->body) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::check() - * - * @return void - */ - public function testCheckUnexpected() - { - $this->expectException(\UnexpectedValueException::class); - - $this->response->code = 666; - $this->response->body = false; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/following/joomla') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->check('joomla'), - $this->equalTo($this->response->body) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::follow() - * - * @return void - */ - public function testFollow() - { - $this->response->code = 204; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('put') - ->with('/user/following/joomla') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->follow('joomla'), - $this->equalTo($this->response->body) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::unfollow() - * - * @return void - */ - public function testUnfollow() - { - $this->response->code = 204; - $this->response->body = ''; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/user/following/joomla') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->unfollow('joomla'), - $this->equalTo($this->response->body) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::checkUserFollowing() - * - * User is following the target - * - * @return void - * - * @since 1.0 - */ - public function testCheckUserFollowing() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/{user}/following/{target}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->checkUserFollowing('{user}', '{target}'), - $this->equalTo(true) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::checkUserFollowing() - * - * User is not following the target - * - * @return void - * - * @since 1.0 - */ - public function testCheckUserFollowingNot() - { - $this->response->code = 404; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/{user}/following/{target}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->checkUserFollowing('{user}', '{target}'), - $this->equalTo(false) - ); - } - - /** - * Test method. - * - * @covers \Joomla\Github\Package\Users\Followers::checkUserFollowing() - * - * // Unexpected response - * - * @return void - * - * @since 1.0 - */ - public function testCheckUserFollowingUnexpected() - { - $this->expectException(\UnexpectedValueException::class); - $this->expectExceptionMessage('Unexpected response code: 666'); - - $this->response->code = 666; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user/{user}/following/{target}') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->checkUserFollowing('{user}', '{target}'), - $this->equalTo(true) - ); - } + /** + * @var Followers + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Followers($this->options, $this->client); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::getList() + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/followers') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::getList() + * + * @return void + */ + public function testGetListWithUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/joomla/followers') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::getListFollowedBy() + * + * @return void + */ + public function testGetListFollowedBy() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/following') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListFollowedBy(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::getListFollowedBy() + * + * @return void + */ + public function testGetListFollowedByWithUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/joomla/following') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListFollowedBy('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::check() + * + * You are following this user + * + * @return void + */ + public function testCheck() + { + $this->response->code = 204; + $this->response->body = true; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/following/joomla') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla'), + $this->equalTo($this->response->body) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::check() + * + * You are not following this user + * + * @return void + */ + public function testCheckNo() + { + $this->response->code = 404; + $this->response->body = false; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/following/joomla') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla'), + $this->equalTo($this->response->body) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::check() + * + * @return void + */ + public function testCheckUnexpected() + { + $this->expectException(\UnexpectedValueException::class); + + $this->response->code = 666; + $this->response->body = false; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/following/joomla') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->check('joomla'), + $this->equalTo($this->response->body) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::follow() + * + * @return void + */ + public function testFollow() + { + $this->response->code = 204; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('put') + ->with('/user/following/joomla') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->follow('joomla'), + $this->equalTo($this->response->body) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::unfollow() + * + * @return void + */ + public function testUnfollow() + { + $this->response->code = 204; + $this->response->body = ''; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/user/following/joomla') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->unfollow('joomla'), + $this->equalTo($this->response->body) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::checkUserFollowing() + * + * User is following the target + * + * @return void + * + * @since 1.0 + */ + public function testCheckUserFollowing() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/{user}/following/{target}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->checkUserFollowing('{user}', '{target}'), + $this->equalTo(true) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::checkUserFollowing() + * + * User is not following the target + * + * @return void + * + * @since 1.0 + */ + public function testCheckUserFollowingNot() + { + $this->response->code = 404; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/{user}/following/{target}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->checkUserFollowing('{user}', '{target}'), + $this->equalTo(false) + ); + } + + /** + * Test method. + * + * @covers \Joomla\Github\Package\Users\Followers::checkUserFollowing() + * + * // Unexpected response + * + * @return void + * + * @since 1.0 + */ + public function testCheckUserFollowingUnexpected() + { + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('Unexpected response code: 666'); + + $this->response->code = 666; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user/{user}/following/{target}') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->checkUserFollowing('{user}', '{target}'), + $this->equalTo(true) + ); + } } diff --git a/Tests/Package/Users/KeysTest.php b/Tests/Package/Users/KeysTest.php index a7548890..a5be5862 100644 --- a/Tests/Package/Users/KeysTest.php +++ b/Tests/Package/Users/KeysTest.php @@ -16,135 +16,135 @@ */ class KeysTest extends GitHubTestCase { - /** - * @var Keys - */ - protected $object; - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @since 1.0 - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); - - $this->object = new Keys($this->options, $this->client); - } - - /** - * Tests the getListUser method - * - * @return void - */ - public function testGetListUser() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/users/joomla/keys') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getListUser('joomla'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the getList method - * - * @return void - */ - public function testGetList() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/users/keys') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList(), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the get method - * - * @return void - */ - public function testGet() - { - $this->client->expects($this->once()) - ->method('get') - ->with('/users/keys/1') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get(1), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the create method - * - * @return void - */ - public function testCreate() - { - $this->response->code = 201; - - $this->client->expects($this->once()) - ->method('post') - ->with('/users/keys') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->create('email@example.com', '12345'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the edit method - * - * @return void - */ - public function testEdit() - { - $this->client->expects($this->once()) - ->method('patch') - ->with('/users/keys/1') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit(1, 'email@example.com', '12345'), - $this->equalTo(json_decode($this->sampleString)) - ); - } - - /** - * Tests the delete method - * - * @return void - */ - public function testDelete() - { - $this->response->code = 204; - - $this->client->expects($this->once()) - ->method('delete') - ->with('/users/keys/1') - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->delete(1), - $this->equalTo(json_decode($this->sampleString)) - ); - } + /** + * @var Keys + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Keys($this->options, $this->client); + } + + /** + * Tests the getListUser method + * + * @return void + */ + public function testGetListUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/joomla/keys') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getListUser('joomla'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the getList method + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/keys') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList(), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the get method + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/keys/1') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get(1), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the create method + * + * @return void + */ + public function testCreate() + { + $this->response->code = 201; + + $this->client->expects($this->once()) + ->method('post') + ->with('/users/keys') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->create('email@example.com', '12345'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the edit method + * + * @return void + */ + public function testEdit() + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/users/keys/1') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit(1, 'email@example.com', '12345'), + $this->equalTo(json_decode($this->sampleString)) + ); + } + + /** + * Tests the delete method + * + * @return void + */ + public function testDelete() + { + $this->response->code = 204; + + $this->client->expects($this->once()) + ->method('delete') + ->with('/users/keys/1') + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->delete(1), + $this->equalTo(json_decode($this->sampleString)) + ); + } } diff --git a/Tests/Package/UsersTest.php b/Tests/Package/UsersTest.php index 5b7f52c1..83845b1c 100755 --- a/Tests/Package/UsersTest.php +++ b/Tests/Package/UsersTest.php @@ -16,35 +16,35 @@ */ class UsersTest extends GitHubTestCase { - /** - * @var Users - */ - protected $object; - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @access protected - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); - - $this->object = new Users($this->options, $this->client); - } - - /** - * Tests the getUser method - * - * @return void - */ - public function testGet() - { - $this->response->code = 200; - $this->response->body = '{ + /** + * @var Users + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @access protected + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + + $this->object = new Users($this->options, $this->client); + } + + /** + * Tests the getUser method + * + * @return void + */ + public function testGet() + { + $this->response->code = 200; + $this->response->body = '{ "login": "octocat", "id": 1, "avatar_url": "https://github.com/images/error/octocat_happy.gif", @@ -66,49 +66,49 @@ public function testGet() "type": "User" }'; - $this->client->expects($this->once()) - ->method('get') - ->with('/users/joomla', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('joomla'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getUser method with failure - * - * @return void - */ - public function testGetFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 404; - $this->response->body = '{"message":"Not Found"}'; - - $this->client->expects($this->once()) - ->method('get') - ->with('/users/nonexistentuser', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->get('nonexistentuser'), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getAuthenticatedUser method - * - * @return void - */ - public function testGetAuthenticatedUser() - { - $this->response->code = 200; - $this->response->body = '{ + $this->client->expects($this->once()) + ->method('get') + ->with('/users/joomla', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('joomla'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getUser method with failure + * + * @return void + */ + public function testGetFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 404; + $this->response->body = '{"message":"Not Found"}'; + + $this->client->expects($this->once()) + ->method('get') + ->with('/users/nonexistentuser', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->get('nonexistentuser'), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getAuthenticatedUser method + * + * @return void + */ + public function testGetAuthenticatedUser() + { + $this->response->code = 200; + $this->response->body = '{ "login": "octocat", "id": 1, "avatar_url": "https://github.com/images/error/octocat_happy.gif", @@ -141,49 +141,49 @@ public function testGetAuthenticatedUser() } }'; - $this->client->expects($this->once()) - ->method('get') - ->with('/user', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getAuthenticatedUser(), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the GetAuthenticatedUser method with failure - * - * @return void - */ - public function testGetAuthenticatedUserFailure() - { - $this->expectException(\DomainException::class); - - $this->response->code = 401; - $this->response->body = '{"message":"Requires authentication"}'; - - $this->client->expects($this->once()) - ->method('get') - ->with('/user', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getAuthenticatedUser(), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getUsers method - * - * @return void - */ - public function testGetList() - { - $this->response->code = 200; - $this->response->body = '[ + $this->client->expects($this->once()) + ->method('get') + ->with('/user', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getAuthenticatedUser(), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the GetAuthenticatedUser method with failure + * + * @return void + */ + public function testGetAuthenticatedUserFailure() + { + $this->expectException(\DomainException::class); + + $this->response->code = 401; + $this->response->body = '{"message":"Requires authentication"}'; + + $this->client->expects($this->once()) + ->method('get') + ->with('/user', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getAuthenticatedUser(), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getUsers method + * + * @return void + */ + public function testGetList() + { + $this->response->code = 200; + $this->response->body = '[ { "login": "octocat", "id": 1, @@ -201,34 +201,34 @@ public function testGetList() } ]'; - $this->client->expects($this->once()) - ->method('get') - ->with('/users', array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->getList(), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getUsers method - * - * @return void - */ - public function testEdit() - { - $name = 'monalisa octocat'; - $email = 'octocat@github.com'; - $blog = 'https =>//github.com/blog'; - $company = 'GitHub'; - $location = 'San Francisco'; - $hireable = true; - $bio = 'There once...'; - - $this->response->code = 200; - $this->response->body = '{ + $this->client->expects($this->once()) + ->method('get') + ->with('/users', [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->getList(), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getUsers method + * + * @return void + */ + public function testEdit() + { + $name = 'monalisa octocat'; + $email = 'octocat@github.com'; + $blog = 'https =>//github.com/blog'; + $company = 'GitHub'; + $location = 'San Francisco'; + $hireable = true; + $bio = 'There once...'; + + $this->response->code = 200; + $this->response->body = '{ "login": "octocat", "id": 1, "avatar_url": "https://github.com/images/error/octocat_happy.gif", @@ -261,71 +261,71 @@ public function testEdit() } }'; - $input = json_encode( - array( - 'name' => $name, - 'email' => $email, - 'blog' => $blog, - 'company' => $company, - 'location' => $location, - 'hireable' => $hireable, - 'bio' => $bio - ) - ); - - $this->client->expects($this->once()) - ->method('patch') - ->with('/user', $input, array(), 0) - ->will($this->returnValue($this->response)); - - $this->assertThat( - $this->object->edit($name, $email, $blog, $company, $location, $hireable, $bio), - $this->equalTo(json_decode($this->response->body)) - ); - } - - /** - * Tests the getUsers method - * - * @return void - */ - public function testEditFailure() - { - $this->expectException(\DomainException::class); - - $name = 'monalisa octocat'; - $email = 'octocat@github.com'; - $blog = 'https =>//github.com/blog'; - $company = 'GitHub'; - $location = 'San Francisco'; - $hireable = true; - $bio = 'There once...'; - - $this->response->code = 404; - $this->response->body = $this->errorString; - - $input = json_encode( - array( - 'name' => $name, - 'email' => $email, - 'blog' => $blog, - 'company' => $company, - 'location' => $location, - 'hireable' => $hireable, - 'bio' => $bio - ) - ); - - $this->client->expects($this->once()) - ->method('patch') - ->with('/user', $input, array(), 0) - ->will($this->returnValue($this->response)); - - // $this->object->edit($name, $email, $blog, $company, $location, $hireable, $bio); - - $this->assertThat( - $this->object->edit($name, $email, $blog, $company, $location, $hireable, $bio), - $this->equalTo(json_decode($this->response->body)) - ); - } + $input = json_encode( + [ + 'name' => $name, + 'email' => $email, + 'blog' => $blog, + 'company' => $company, + 'location' => $location, + 'hireable' => $hireable, + 'bio' => $bio, + ] + ); + + $this->client->expects($this->once()) + ->method('patch') + ->with('/user', $input, [], 0) + ->will($this->returnValue($this->response)); + + $this->assertThat( + $this->object->edit($name, $email, $blog, $company, $location, $hireable, $bio), + $this->equalTo(json_decode($this->response->body)) + ); + } + + /** + * Tests the getUsers method + * + * @return void + */ + public function testEditFailure() + { + $this->expectException(\DomainException::class); + + $name = 'monalisa octocat'; + $email = 'octocat@github.com'; + $blog = 'https =>//github.com/blog'; + $company = 'GitHub'; + $location = 'San Francisco'; + $hireable = true; + $bio = 'There once...'; + + $this->response->code = 404; + $this->response->body = $this->errorString; + + $input = json_encode( + [ + 'name' => $name, + 'email' => $email, + 'blog' => $blog, + 'company' => $company, + 'location' => $location, + 'hireable' => $hireable, + 'bio' => $bio, + ] + ); + + $this->client->expects($this->once()) + ->method('patch') + ->with('/user', $input, [], 0) + ->will($this->returnValue($this->response)); + + // $this->object->edit($name, $email, $blog, $company, $location, $hireable, $bio); + + $this->assertThat( + $this->object->edit($name, $email, $blog, $company, $location, $hireable, $bio), + $this->equalTo(json_decode($this->response->body)) + ); + } } diff --git a/Tests/Package/ZenTest.php b/Tests/Package/ZenTest.php index a68cfeba..f7ab1943 100644 --- a/Tests/Package/ZenTest.php +++ b/Tests/Package/ZenTest.php @@ -16,67 +16,67 @@ */ class ZenTest extends GitHubTestCase { - /** - * @var Zen - */ - protected $object; + /** + * @var Zen + */ + protected $object; - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @since 1.0 - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); - $this->object = new Zen($this->options, $this->client); - } + $this->object = new Zen($this->options, $this->client); + } - /** - * Tests the Get method. - * - * @return void - */ - public function testGet() - { - $this->response->code = 200; - $this->response->body = 'My Zen'; + /** + * Tests the Get method. + * + * @return void + */ + public function testGet() + { + $this->response->code = 200; + $this->response->body = 'My Zen'; - $this->client->expects($this->once()) - ->method('get') - ->with('/zen', array(), 0) - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('get') + ->with('/zen', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->get(), - $this->equalTo($this->response->body) - ); - } + $this->assertThat( + $this->object->get(), + $this->equalTo($this->response->body) + ); + } - /** - * Tests the Get method. - * - * @return void - */ - public function testGetFailure() - { - $this->expectException(\RuntimeException::class); + /** + * Tests the Get method. + * + * @return void + */ + public function testGetFailure() + { + $this->expectException(\RuntimeException::class); - $this->response->code = 400; - $this->response->body = 'My Zen'; + $this->response->code = 400; + $this->response->body = 'My Zen'; - $this->client->expects($this->once()) - ->method('get') - ->with('/zen', array(), 0) - ->will($this->returnValue($this->response)); + $this->client->expects($this->once()) + ->method('get') + ->with('/zen', [], 0) + ->will($this->returnValue($this->response)); - $this->assertThat( - $this->object->get(), - $this->equalTo($this->response->body) - ); - } + $this->assertThat( + $this->object->get(), + $this->equalTo($this->response->body) + ); + } } diff --git a/Tests/PackageTest.php b/Tests/PackageTest.php index 717375d1..c86e3c42 100755 --- a/Tests/PackageTest.php +++ b/Tests/PackageTest.php @@ -16,53 +16,53 @@ */ class PackageTest extends GitHubTestCase { - /** - * @var Github Object under test. - * @since 1.0 - */ - protected $object; + /** + * @var Github Object under test. + * @since 1.0 + */ + protected $object; - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @return void - * - * @since 1.0 - */ - protected function setUp(): void - { - parent::setUp(); + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @return void + * + * @since 1.0 + */ + protected function setUp(): void + { + parent::setUp(); - $this->object = new Github($this->options, $this->client); - } + $this->object = new Github($this->options, $this->client); + } - /** - * Tests the magic __get method - * - * @return void - * - * @since 1.0 - */ - public function testGet() - { - $this->assertThat( - $this->object->repositories->forks, - $this->isInstanceOf('Joomla\Github\Package\Repositories\Forks') - ); - } + /** + * Tests the magic __get method + * + * @return void + * + * @since 1.0 + */ + public function testGet() + { + $this->assertThat( + $this->object->repositories->forks, + $this->isInstanceOf('Joomla\Github\Package\Repositories\Forks') + ); + } - /** - * Tests the magic __get method with an invalid parameter. - * - * @return void - * - * @since 1.0 - */ - public function testGetInvalid() - { - $this->expectException(\InvalidArgumentException::class); + /** + * Tests the magic __get method with an invalid parameter. + * + * @return void + * + * @since 1.0 + */ + public function testGetInvalid() + { + $this->expectException(\InvalidArgumentException::class); - $this->object->repositories->INVALID; - } + $this->object->repositories->INVALID; + } } diff --git a/Tests/Stub/GitHubTestCase.php b/Tests/Stub/GitHubTestCase.php index f6c4b1ae..c4c91664 100644 --- a/Tests/Stub/GitHubTestCase.php +++ b/Tests/Stub/GitHubTestCase.php @@ -16,59 +16,59 @@ */ abstract class GitHubTestCase extends TestCase { - /** - * @var Registry Options for the GitHub object. - * @since 1.0 - */ - protected $options; + /** + * @var Registry Options for the GitHub object. + * @since 1.0 + */ + protected $options; - /** - * @var \PHPUnit_Framework_MockObject_MockObject Mock client object. - * @since 1.0 - */ - protected $client; + /** + * @var \PHPUnit_Framework_MockObject_MockObject Mock client object. + * @since 1.0 + */ + protected $client; - /** - * @var \Joomla\Http\Response Mock response object. - * @since 1.0 - */ - protected $response; + /** + * @var \Joomla\Http\Response Mock response object. + * @since 1.0 + */ + protected $response; - /** - * @var string Sample JSON string. - * @since 12.3 - */ - protected $sampleString = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; + /** + * @var string Sample JSON string. + * @since 12.3 + */ + protected $sampleString = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; - /** - * @var string Sample JSON error message. - * @since 12.3 - */ - protected $errorString = '{"message": "Generic Error"}'; + /** + * @var string Sample JSON error message. + * @since 12.3 + */ + protected $errorString = '{"message": "Generic Error"}'; - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - * - * @since 1.0 - * - * @return void - */ - protected function setUp(): void - { - parent::setUp(); + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @since 1.0 + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); - $this->options = new Registry; + $this->options = new Registry(); - $this->client = $this->getMockBuilder('\\Joomla\\Http\\Http') - ->setMethods(array('get', 'post', 'delete', 'patch', 'put')) - ->getMock(); + $this->client = $this->getMockBuilder('\\Joomla\\Http\\Http') + ->setMethods(['get', 'post', 'delete', 'patch', 'put']) + ->getMock(); - $this->response = $this->getMockBuilder('\\Joomla\\Http\\Response') - ->getMock(); + $this->response = $this->getMockBuilder('\\Joomla\\Http\\Response') + ->getMock(); - // Set a default response - $this->response->code = 200; - $this->response->body = $this->sampleString; - } + // Set a default response + $this->response->code = 200; + $this->response->body = $this->sampleString; + } } diff --git a/Tests/Stub/ObjectMock.php b/Tests/Stub/ObjectMock.php index 8ddcb1cd..fe40373e 100644 --- a/Tests/Stub/ObjectMock.php +++ b/Tests/Stub/ObjectMock.php @@ -15,23 +15,23 @@ */ class ObjectMock extends AbstractGithubObject { - // @codingStandardsIgnoreStart - /** - * Method to build and return a full request URL for the request. This method will - * add appropriate pagination details if necessary and also prepend the API url - * to have a complete URL for the request. - * - * @param string $path URL to inflect - * @param integer $page Page to request - * @param integer $limit Number of results to return per page - * - * @return string The request URL. - * - * @since 1.0 - */ - public function fetchUrl($path, $page = 0, $limit = 0) - { - return parent::fetchUrl($path, $page, $limit); - } - // @codingStandardsIgnoreEnd + // @codingStandardsIgnoreStart + /** + * Method to build and return a full request URL for the request. This method will + * add appropriate pagination details if necessary and also prepend the API url + * to have a complete URL for the request. + * + * @param string $path URL to inflect + * @param integer $page Page to request + * @param integer $limit Number of results to return per page + * + * @return string The request URL. + * + * @since 1.0 + */ + public function fetchUrl($path, $page = 0, $limit = 0) + { + return parent::fetchUrl($path, $page, $limit); + } + // @codingStandardsIgnoreEnd } diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php index ddfe6fa4..f87308fe 100644 --- a/Tests/bootstrap.php +++ b/Tests/bootstrap.php @@ -1,6 +1,7 @@ - + + + This is a PSR-12 compliant ruleset for Joomla. + + src + Tests + + */Tests/Stubs/* + + - + - - */.github/* - */.travis/* - - - */vendor/* - - - - - - - - - - - + + + diff --git a/src/AbstractGithubObject.php b/src/AbstractGithubObject.php index 2fbe7725..b283fce4 100644 --- a/src/AbstractGithubObject.php +++ b/src/AbstractGithubObject.php @@ -1,4 +1,5 @@ options = $options ?: new Registry; - $this->client = $client ?: (new HttpFactory)->getHttp($this->options); - - $this->package = \get_class($this); - $this->package = substr($this->package, strrpos($this->package, '\\') + 1); - } - - /** - * Method to build and return a full request URL for the request. This method will - * add appropriate pagination details if necessary and also prepend the API url - * to have a complete URL for the request. - * - * @param string $path URL to inflect - * @param integer $page Page to request - * @param integer $limit Number of results to return per page - * - * @return Uri - * - * @since 1.0 - */ - protected function fetchUrl($path, $page = 0, $limit = 0) - { - // Get a new Uri object focusing the api url and given path. - $uri = new Uri($this->options->get('api.url') . $path); - - if ($this->options->get('gh.token', false)) - { - // Use oAuth authentication - $headers = $this->client->getOption('headers', array()); - - if (!isset($headers['Authorization'])) - { - $headers['Authorization'] = 'token ' . $this->options->get('gh.token'); - $this->client->setOption('headers', $headers); - } - } - else - { - // Use basic authentication - if ($this->options->get('api.username', false)) - { - $uri->setUser($this->options->get('api.username')); - } - - if ($this->options->get('api.password', false)) - { - $uri->setPass($this->options->get('api.password')); - } - } - - // If we have a defined page number add it to the JUri object. - if ($page > 0) - { - $uri->setVar('page', (int) $page); - } - - // If we have a defined items per page add it to the JUri object. - if ($limit > 0) - { - $uri->setVar('per_page', (int) $limit); - } - - return $uri; - } - - /** - * Process the response and decode it. - * - * @param Response $response The response. - * @param integer $expectedCode The expected "good" code. - * - * @return mixed - * - * @since 1.0 - * @throws UnexpectedResponseException - */ - protected function processResponse(Response $response, $expectedCode = 200) - { - // Validate the response code. - if ($response->code != $expectedCode) - { - // Decode the error response and throw an exception. - $error = json_decode($response->body); - $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; - - throw new UnexpectedResponseException($response, $message, $response->code); - } - - return json_decode($response->body); - } + /** + * Options for the GitHub object. + * + * @var Registry + * @since 1.0 + */ + protected $options; + + /** + * The HTTP client object to use in sending HTTP requests. + * + * @var BaseHttp + * @since 1.0 + */ + protected $client; + + /** + * The package the object resides in + * + * @var string + * @since 1.0 + */ + protected $package = ''; + + /** + * Array containing the allowed hook events + * + * @var array + * @since 1.5.2 + * @link https://developer.github.com/webhooks/#events + * @note From 1.4.0 to 1.5.1 this was named $events, it was renamed due to naming conflicts with package subclasses + */ + protected $hookEvents = [ + '*', + 'commit_comment', + 'create', + 'delete', + 'deployment', + 'deployment_status', + 'fork', + 'gollum', + 'issue_comment', + 'issues', + 'member', + 'membership', + 'page_build', + 'public', + 'pull_request_review_comment', + 'pull_request', + 'push', + 'repository', + 'release', + 'status', + 'team_add', + 'watch', + ]; + + /** + * Constructor. + * + * @param ?Registry $options GitHub options object. + * @param ?BaseHttp $client The HTTP client object. + * + * @since 1.0 + */ + public function __construct(Registry $options = null, BaseHttp $client = null) + { + $this->options = $options ?: new Registry(); + $this->client = $client ?: (new HttpFactory())->getHttp($this->options); + + $this->package = \get_class($this); + $this->package = substr($this->package, strrpos($this->package, '\\') + 1); + } + + /** + * Method to build and return a full request URL for the request. This method will + * add appropriate pagination details if necessary and also prepend the API url + * to have a complete URL for the request. + * + * @param string $path URL to inflect + * @param integer $page Page to request + * @param integer $limit Number of results to return per page + * + * @return Uri + * + * @since 1.0 + */ + protected function fetchUrl($path, $page = 0, $limit = 0) + { + // Get a new Uri object focusing the api url and given path. + $uri = new Uri($this->options->get('api.url') . $path); + + if ($this->options->get('gh.token', false)) { + // Use oAuth authentication + $headers = $this->client->getOption('headers', []); + + if (!isset($headers['Authorization'])) { + $headers['Authorization'] = 'token ' . $this->options->get('gh.token'); + $this->client->setOption('headers', $headers); + } + } else { + // Use basic authentication + if ($this->options->get('api.username', false)) { + $uri->setUser($this->options->get('api.username')); + } + + if ($this->options->get('api.password', false)) { + $uri->setPass($this->options->get('api.password')); + } + } + + // If we have a defined page number add it to the JUri object. + if ($page > 0) { + $uri->setVar('page', (int) $page); + } + + // If we have a defined items per page add it to the JUri object. + if ($limit > 0) { + $uri->setVar('per_page', (int) $limit); + } + + return $uri; + } + + /** + * Process the response and decode it. + * + * @param Response $response The response. + * @param integer $expectedCode The expected "good" code. + * + * @return mixed + * + * @since 1.0 + * @throws UnexpectedResponseException + */ + protected function processResponse(Response $response, $expectedCode = 200) + { + // Validate the response code. + if ($response->code != $expectedCode) { + // Decode the error response and throw an exception. + $error = json_decode($response->body); + $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; + + throw new UnexpectedResponseException($response, $message, $response->code); + } + + return json_decode($response->body); + } } diff --git a/src/AbstractPackage.php b/src/AbstractPackage.php index 98ce329c..71266b7c 100644 --- a/src/AbstractPackage.php +++ b/src/AbstractPackage.php @@ -1,4 +1,5 @@ package = \get_class($this); - $this->package = substr($this->package, strrpos($this->package, '\\') + 1); - } + $this->package = \get_class($this); + $this->package = substr($this->package, strrpos($this->package, '\\') + 1); + } - /** - * Magic method to lazily create API objects - * - * @param string $name Name of property to retrieve - * - * @since 1.0 - * @throws \InvalidArgumentException - * - * @return AbstractPackage GitHub API package object. - */ - public function __get($name) - { - $class = '\\Joomla\\Github\\Package\\' . $this->package . '\\' . ucfirst($name); + /** + * Magic method to lazily create API objects + * + * @param string $name Name of property to retrieve + * + * @since 1.0 + * @throws \InvalidArgumentException + * + * @return AbstractPackage GitHub API package object. + */ + public function __get($name) + { + $class = '\\Joomla\\Github\\Package\\' . $this->package . '\\' . ucfirst($name); - if (class_exists($class) == false) - { - throw new \InvalidArgumentException( - sprintf( - 'Argument %1$s produced an invalid class name: %2$s in package %3$s', - $name, $class, $this->package - ) - ); - } + if (class_exists($class) == false) { + throw new \InvalidArgumentException( + sprintf( + 'Argument %1$s produced an invalid class name: %2$s in package %3$s', + $name, + $class, + $this->package + ) + ); + } - if (isset($this->$name) == false) - { - $this->$name = new $class($this->options, $this->client); - } + if (isset($this->$name) == false) { + $this->$name = new $class($this->options, $this->client); + } - return $this->$name; - } + return $this->$name; + } } diff --git a/src/Github.php b/src/Github.php index 636b1a9d..632c54e4 100644 --- a/src/Github.php +++ b/src/Github.php @@ -1,4 +1,5 @@ options = $options ?: new Registry; - - // Setup the default user agent if not already set. - if (!$this->getOption('userAgent')) - { - $this->setOption('userAgent', 'JGitHub/2.0'); - } - - // Setup the default API url if not already set. - if (!$this->getOption('api.url')) - { - $this->setOption('api.url', 'https://api.github.com'); - } - - $this->client = $client ?: (new HttpFactory)->getHttp($this->options); - } - - /** - * Magic method to lazily create API objects - * - * @param string $name Name of property to retrieve - * - * @return AbstractGithubObject GitHub API object (gists, issues, pulls, etc). - * - * @since 1.0 - * @throws \InvalidArgumentException If $name is not a valid sub class. - */ - public function __get($name) - { - $class = 'Joomla\\Github\\Package\\' . ucfirst($name); - - if (class_exists($class)) - { - if (isset($this->$name) == false) - { - $this->$name = new $class($this->options, $this->client); - } - - return $this->$name; - } - - throw new \InvalidArgumentException(sprintf('Argument %s produced an invalid class name: %s', $name, $class)); - } - - /** - * Get an option from the GitHub instance. - * - * @param string $key The name of the option to get. - * - * @return mixed The option value. - * - * @since 1.0 - */ - public function getOption($key) - { - return isset($this->options[$key]) ? $this->options[$key] : null; - } - - /** - * Set an option for the GitHub instance. - * - * @param string $key The name of the option to set. - * @param mixed $value The option value to set. - * - * @return GitHub This object for method chaining. - * - * @since 1.0 - */ - public function setOption($key, $value) - { - $this->options[$key] = $value; - - return $this; - } + /** + * Options for the GitHub object. + * + * @var Registry + * @since 1.0 + */ + protected $options; + + /** + * The HTTP client object to use in sending HTTP requests. + * + * @var Http + * @since 1.0 + */ + protected $client; + + /** + * Constructor. + * + * @param ?Registry $options GitHub options object. + * @param ?Http $client The HTTP client object. + * + * @since 1.0 + */ + public function __construct(Registry $options = null, Http $client = null) + { + $this->options = $options ?: new Registry(); + + // Setup the default user agent if not already set. + if (!$this->getOption('userAgent')) { + $this->setOption('userAgent', 'JGitHub/2.0'); + } + + // Setup the default API url if not already set. + if (!$this->getOption('api.url')) { + $this->setOption('api.url', 'https://api.github.com'); + } + + $this->client = $client ?: (new HttpFactory())->getHttp($this->options); + } + + /** + * Magic method to lazily create API objects + * + * @param string $name Name of property to retrieve + * + * @return AbstractGithubObject GitHub API object (gists, issues, pulls, etc). + * + * @since 1.0 + * @throws \InvalidArgumentException If $name is not a valid sub class. + */ + public function __get($name) + { + $class = 'Joomla\\Github\\Package\\' . ucfirst($name); + + if (class_exists($class)) { + if (isset($this->$name) == false) { + $this->$name = new $class($this->options, $this->client); + } + + return $this->$name; + } + + throw new \InvalidArgumentException(sprintf('Argument %s produced an invalid class name: %s', $name, $class)); + } + + /** + * Get an option from the GitHub instance. + * + * @param string $key The name of the option to get. + * + * @return mixed The option value. + * + * @since 1.0 + */ + public function getOption($key) + { + return isset($this->options[$key]) ? $this->options[$key] : null; + } + + /** + * Set an option for the GitHub instance. + * + * @param string $key The name of the option to set. + * @param mixed $value The option value to set. + * + * @return GitHub This object for method chaining. + * + * @since 1.0 + */ + public function setOption($key, $value) + { + $this->options[$key] = $value; + + return $this; + } } diff --git a/src/Package/Activity.php b/src/Package/Activity.php index 328da0b7..cafc6de7 100644 --- a/src/Package/Activity.php +++ b/src/Package/Activity.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List repository events. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function getRepository($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/events'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List issue events for a repository. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function getIssue($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/issues/events'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List public events for a network of repositories. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function getNetwork($owner, $repo) - { - // Build the request path. - $path = '/networks/' . $owner . '/' . $repo . '/events'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List public events for an organization. - * - * @param string $org Organization. - * - * @return object - * - * @since 1.0 - */ - public function getOrg($org) - { - // Build the request path. - $path = '/orgs/' . $org . '/events'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List events that a user has received. - * - * These are events that you’ve received by watching repos and following users. - * If you are authenticated as the given user, you will see private events. - * Otherwise, you’ll only see public events. - * - * @param string $user User name. - * - * @return object - * - * @since 1.0 - */ - public function getUser($user) - { - // Build the request path. - $path = '/users/' . $user . '/received_events'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List public events that a user has received. - * - * @param string $user User name. - * - * @return object - * - * @since 1.0 - */ - public function getUserPublic($user) - { - // Build the request path. - $path = '/users/' . $user . '/received_events/public'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List events performed by a user. - * - * If you are authenticated as the given user, you will see your private events. - * Otherwise, you’ll only see public events. - * - * @param string $user User name. - * - * @return object - * - * @since 1.0 - */ - public function getByUser($user) - { - // Build the request path. - $path = '/users/' . $user . '/events'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List public events performed by a user. - * - * @param string $user User name. - * - * @return object - * - * @since 1.0 - */ - public function getByUserPublic($user) - { - // Build the request path. - $path = '/users/' . $user . '/events/public'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List events for an organization. - * - * This is the user’s organization dashboard. - * You must be authenticated as the user to view this. - * - * @param string $user User name. - * @param string $org Organisation. - * - * @return object - * - * @since 1.0 - */ - public function getUserOrg($user, $org) - { - // Build the request path. - $path = '/users/' . $user . '/events/orgs/' . $org; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + /** + * List public events. + * + * @return object + * + * @since 1.0 + */ + public function getPublic() + { + // Build the request path. + $path = '/events'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List repository events. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function getRepository($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/events'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List issue events for a repository. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function getIssue($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/events'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List public events for a network of repositories. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function getNetwork($owner, $repo) + { + // Build the request path. + $path = '/networks/' . $owner . '/' . $repo . '/events'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List public events for an organization. + * + * @param string $org Organization. + * + * @return object + * + * @since 1.0 + */ + public function getOrg($org) + { + // Build the request path. + $path = '/orgs/' . $org . '/events'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List events that a user has received. + * + * These are events that you’ve received by watching repos and following users. + * If you are authenticated as the given user, you will see private events. + * Otherwise, you’ll only see public events. + * + * @param string $user User name. + * + * @return object + * + * @since 1.0 + */ + public function getUser($user) + { + // Build the request path. + $path = '/users/' . $user . '/received_events'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List public events that a user has received. + * + * @param string $user User name. + * + * @return object + * + * @since 1.0 + */ + public function getUserPublic($user) + { + // Build the request path. + $path = '/users/' . $user . '/received_events/public'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List events performed by a user. + * + * If you are authenticated as the given user, you will see your private events. + * Otherwise, you’ll only see public events. + * + * @param string $user User name. + * + * @return object + * + * @since 1.0 + */ + public function getByUser($user) + { + // Build the request path. + $path = '/users/' . $user . '/events'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List public events performed by a user. + * + * @param string $user User name. + * + * @return object + * + * @since 1.0 + */ + public function getByUserPublic($user) + { + // Build the request path. + $path = '/users/' . $user . '/events/public'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List events for an organization. + * + * This is the user’s organization dashboard. + * You must be authenticated as the user to view this. + * + * @param string $user User name. + * @param string $org Organisation. + * + * @return object + * + * @since 1.0 + */ + public function getUserOrg($user, $org) + { + // Build the request path. + $path = '/users/' . $user . '/events/orgs/' . $org; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } } diff --git a/src/Package/Activity/Feeds.php b/src/Package/Activity/Feeds.php index decdee49..2ee6ba07 100644 --- a/src/Package/Activity/Feeds.php +++ b/src/Package/Activity/Feeds.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } } diff --git a/src/Package/Activity/Notifications.php b/src/Package/Activity/Notifications.php index dc8c6e99..7b228b2f 100644 --- a/src/Package/Activity/Notifications.php +++ b/src/Package/Activity/Notifications.php @@ -1,4 +1,5 @@ fetchUrl($path); - - if ($all) - { - $uri->setVar('all', 1); - } - - if ($participating) - { - $uri->setVar('participating', 1); - } - - if ($since) - { - $uri->setVar('since', $since->format(\DateTime::RFC3339)); - } - - if ($before) - { - $uri->setVar('before', $before->format(\DateTime::RFC3339)); - } - - return $this->processResponse($this->client->get($uri)); - } - - /** - * List your notifications in a repository. - * - * List all notifications for the current user. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * @param boolean $all True to show notifications marked as read. - * @param boolean $participating True to show only notifications in which the user is directly participating or mentioned. - * @param \DateTimeInterface $since Only show notifications updated after the given time. - * @param \DateTimeInterface $before Only show notifications updated before the given time. - * - * @return object - * - * @since 1.0 - */ - public function getListRepository($owner, $repo, $all = true, $participating = true, \DateTimeInterface $since = null, - \DateTimeInterface $before = null - ) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/notifications'; - - $uri = $this->fetchUrl($path); - - if ($all) - { - $uri->setVar('all', 1); - } - - if ($participating) - { - $uri->setVar('participating', 1); - } - - if ($since) - { - $uri->setVar('since', $since->format(\DateTime::RFC3339)); - } - - if ($before) - { - $uri->setVar('before', $before->format(\DateTime::RFC3339)); - } - - return $this->processResponse($this->client->get($uri)); - } - - /** - * Mark as read. - * - * Marking a notification as “read” removes it from the default view on GitHub.com. - * - * @param boolean $unread Changes the unread status of the threads. - * @param boolean $read Inverse of “unread”. - * @param \DateTimeInterface $lastReadAt Describes the last point that notifications were checked. - * Anything updated since this time will not be updated. Default: Now. Expected in ISO 8601 format. - * - * @return object - * - * @since 1.0 - */ - public function markRead($unread = true, $read = true, \DateTimeInterface $lastReadAt = null) - { - // Build the request path. - $path = '/notifications'; - - $data = [ - 'unread' => $unread, - 'read' => $read, - ]; - - if ($lastReadAt) - { - $data['last_read_at'] = $lastReadAt->format(\DateTime::RFC3339); - } - - return $this->processResponse( - $this->client->put($this->fetchUrl($path), json_encode($data)), - 205 - ); - } - - /** - * Mark notifications as read in a repository. - * - * Marking all notifications in a repository as “read” removes them from the default view on GitHub.com. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * @param boolean $unread Changes the unread status of the threads. - * @param boolean $read Inverse of “unread”. - * @param \DateTimeInterface $lastReadAt Describes the last point that notifications were checked. - * Anything updated since this time will not be updated. Default: Now. Expected in ISO 8601 format. - * - * @return object - * - * @since 1.0 - */ - public function markReadRepository($owner, $repo, $unread, $read, \DateTimeInterface $lastReadAt = null) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/notifications'; - - $data = [ - 'unread' => $unread, - 'read' => $read, - ]; - - if ($lastReadAt) - { - $data['last_read_at'] = $lastReadAt->format(\DateTime::RFC3339); - } - - return $this->processResponse( - $this->client->put($this->fetchUrl($path), json_encode($data)), - 205 - ); - } - - /** - * View a single thread. - * - * @param integer $id The thread id. - * - * @return object - * - * @since 1.0 - */ - public function viewThread($id) - { - // Build the request path. - $path = '/notifications/threads/' . $id; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Mark a thread as read. - * - * @param integer $id The thread id. - * @param boolean $unread Changes the unread status of the threads. - * @param boolean $read Inverse of “unread”. - * - * @return object - * - * @since 1.0 - */ - public function markReadThread($id, $unread = true, $read = true) - { - // Build the request path. - $path = '/notifications/threads/' . $id; - - $data = [ - 'unread' => $unread, - 'read' => $read, - ]; - - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), json_encode($data)), - 205 - ); - } - - /** - * Get a Thread Subscription. - * - * This checks to see if the current user is subscribed to a thread. - * You can also get a Repository subscription. - * - * @param integer $id The thread id. - * - * @return object - * - * @since 1.0 - */ - public function getThreadSubscription($id) - { - // Build the request path. - $path = '/notifications/threads/' . $id . '/subscription'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Set a Thread Subscription. - * - * This lets you subscribe to a thread, or ignore it. Subscribing to a thread is unnecessary - * if the user is already subscribed to the repository. Ignoring a thread will mute all - * future notifications (until you comment or get @mentioned). - * - * @param integer $id The thread id. - * @param boolean $subscribed Determines if notifications should be received from this thread. - * @param boolean $ignored Determines if all notifications should be blocked from this thread. - * - * @return object - * - * @since 1.0 - */ - public function setThreadSubscription($id, $subscribed, $ignored) - { - // Build the request path. - $path = '/notifications/threads/' . $id . '/subscription'; - - $data = [ - 'subscribed' => $subscribed, - 'ignored' => $ignored, - ]; - - return $this->processResponse( - $this->client->put($this->fetchUrl($path), json_encode($data)) - ); - } - - /** - * Delete a Thread Subscription. - * - * @param integer $id The thread id. - * - * @return object - * - * @since 1.0 - */ - public function deleteThreadSubscription($id) - { - // Build the request path. - $path = '/notifications/threads/' . $id . '/subscription'; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + /** + * List your notifications. + * + * List all notifications for the current user, grouped by repository. + * + * @param boolean $all True to show notifications marked as read. + * @param boolean $participating True to show only notifications in which the user is directly participating or mentioned. + * @param ?\DateTimeInterface $since Only show notifications updated after the given time. + * @param ?\DateTimeInterface $before Only show notifications updated before the given time. + * + * @return object + * + * @since 1.0 + */ + public function getList($all = true, $participating = true, \DateTimeInterface $since = null, \DateTimeInterface $before = null) + { + // Build the request path. + $path = '/notifications'; + + $uri = $this->fetchUrl($path); + + if ($all) { + $uri->setVar('all', 1); + } + + if ($participating) { + $uri->setVar('participating', 1); + } + + if ($since) { + $uri->setVar('since', $since->format(\DateTime::RFC3339)); + } + + if ($before) { + $uri->setVar('before', $before->format(\DateTime::RFC3339)); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * List your notifications in a repository. + * + * List all notifications for the current user. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * @param boolean $all True to show notifications marked as read. + * @param boolean $participating True to show only notifications in which the user is directly participating or mentioned. + * @param ?\DateTimeInterface $since Only show notifications updated after the given time. + * @param ?\DateTimeInterface $before Only show notifications updated before the given time. + * + * @return object + * + * @since 1.0 + */ + public function getListRepository( + $owner, + $repo, + $all = true, + $participating = true, + \DateTimeInterface $since = null, + \DateTimeInterface $before = null + ) { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/notifications'; + + $uri = $this->fetchUrl($path); + + if ($all) { + $uri->setVar('all', 1); + } + + if ($participating) { + $uri->setVar('participating', 1); + } + + if ($since) { + $uri->setVar('since', $since->format(\DateTime::RFC3339)); + } + + if ($before) { + $uri->setVar('before', $before->format(\DateTime::RFC3339)); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * Mark as read. + * + * Marking a notification as “read” removes it from the default view on GitHub.com. + * + * @param boolean $unread Changes the unread status of the threads. + * @param boolean $read Inverse of “unread”. + * @param ?\DateTimeInterface $lastReadAt Describes the last point that notifications were checked. + * Anything updated since this time will not be updated. Default: Now. Expected in ISO 8601 format. + * + * @return object + * + * @since 1.0 + */ + public function markRead($unread = true, $read = true, \DateTimeInterface $lastReadAt = null) + { + // Build the request path. + $path = '/notifications'; + + $data = [ + 'unread' => $unread, + 'read' => $read, + ]; + + if ($lastReadAt) { + $data['last_read_at'] = $lastReadAt->format(\DateTime::RFC3339); + } + + return $this->processResponse( + $this->client->put($this->fetchUrl($path), json_encode($data)), + 205 + ); + } + + /** + * Mark notifications as read in a repository. + * + * Marking all notifications in a repository as “read” removes them from the default view on GitHub.com. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * @param boolean $unread Changes the unread status of the threads. + * @param boolean $read Inverse of “unread”. + * @param ?\DateTimeInterface $lastReadAt Describes the last point that notifications were checked. + * Anything updated since this time will not be updated. Default: Now. Expected in ISO 8601 format. + * + * @return object + * + * @since 1.0 + */ + public function markReadRepository($owner, $repo, $unread, $read, \DateTimeInterface $lastReadAt = null) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/notifications'; + + $data = [ + 'unread' => $unread, + 'read' => $read, + ]; + + if ($lastReadAt) { + $data['last_read_at'] = $lastReadAt->format(\DateTime::RFC3339); + } + + return $this->processResponse( + $this->client->put($this->fetchUrl($path), json_encode($data)), + 205 + ); + } + + /** + * View a single thread. + * + * @param integer $id The thread id. + * + * @return object + * + * @since 1.0 + */ + public function viewThread($id) + { + // Build the request path. + $path = '/notifications/threads/' . $id; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Mark a thread as read. + * + * @param integer $id The thread id. + * @param boolean $unread Changes the unread status of the threads. + * @param boolean $read Inverse of “unread”. + * + * @return object + * + * @since 1.0 + */ + public function markReadThread($id, $unread = true, $read = true) + { + // Build the request path. + $path = '/notifications/threads/' . $id; + + $data = [ + 'unread' => $unread, + 'read' => $read, + ]; + + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), json_encode($data)), + 205 + ); + } + + /** + * Get a Thread Subscription. + * + * This checks to see if the current user is subscribed to a thread. + * You can also get a Repository subscription. + * + * @param integer $id The thread id. + * + * @return object + * + * @since 1.0 + */ + public function getThreadSubscription($id) + { + // Build the request path. + $path = '/notifications/threads/' . $id . '/subscription'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Set a Thread Subscription. + * + * This lets you subscribe to a thread, or ignore it. Subscribing to a thread is unnecessary + * if the user is already subscribed to the repository. Ignoring a thread will mute all + * future notifications (until you comment or get @mentioned). + * + * @param integer $id The thread id. + * @param boolean $subscribed Determines if notifications should be received from this thread. + * @param boolean $ignored Determines if all notifications should be blocked from this thread. + * + * @return object + * + * @since 1.0 + */ + public function setThreadSubscription($id, $subscribed, $ignored) + { + // Build the request path. + $path = '/notifications/threads/' . $id . '/subscription'; + + $data = [ + 'subscribed' => $subscribed, + 'ignored' => $ignored, + ]; + + return $this->processResponse( + $this->client->put($this->fetchUrl($path), json_encode($data)) + ); + } + + /** + * Delete a Thread Subscription. + * + * @param integer $id The thread id. + * + * @return object + * + * @since 1.0 + */ + public function deleteThreadSubscription($id) + { + // Build the request path. + $path = '/notifications/threads/' . $id . '/subscription'; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } } diff --git a/src/Package/Activity/Starring.php b/src/Package/Activity/Starring.php index 286278ce..4908dbea 100644 --- a/src/Package/Activity/Starring.php +++ b/src/Package/Activity/Starring.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List repositories being starred. - * - * List repositories being starred by a user. - * - * @param string $user User name. - * @param string $sort One of `created` (when the repository was starred) or `updated` (when it was last pushed to). - * @param string $direction One of `asc` (ascending) or `desc` (descending). - * - * @return object - * - * @since 1.0 - * @throws \InvalidArgumentException - */ - public function getRepositories($user = '', $sort = 'created', $direction = 'desc') - { - $allowedSort = ['created', 'updated']; - $allowedDir = ['asc', 'desc']; - - if (!\in_array($sort, $allowedSort)) - { - throw new \InvalidArgumentException( - sprintf( - 'The sorting value is invalid. Allowed values are: %s', - implode(', ', $allowedSort) - ) - ); - } - - if (!\in_array($direction, $allowedDir)) - { - throw new \InvalidArgumentException( - sprintf( - 'The direction value is invalid. Allowed values are: %s', - implode(', ', $allowedDir) - ) - ); - } - - // Build the request path. - $path = ($user) - ? '/users/' . $user . '/starred' - : '/user/starred'; - - $uri = $this->fetchUrl($path); - $uri->setVar('sort', $sort); - $uri->setVar('direction', $direction); - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * Check if you are starring a repository. - * - * Requires for the user to be authenticated. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return boolean - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function check($owner, $repo) - { - // Build the request path. - $path = '/user/starred/' . $owner . '/' . $repo; - - $response = $this->client->get($this->fetchUrl($path)); - - switch ($response->code) - { - case '204' : - // This repository is watched by you. - return true; - - case '404' : - // This repository is not watched by you. - return false; - } - - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - - /** - * Star a repository. - * - * Requires for the user to be authenticated. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function star($owner, $repo) - { - // Build the request path. - $path = '/user/starred/' . $owner . '/' . $repo; - - return $this->processResponse( - $this->client->put($this->fetchUrl($path), ''), - 204 - ); - } - - /** - * Unstar a repository. - * - * Requires for the user to be authenticated. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function unstar($owner, $repo) - { - // Build the request path. - $path = '/user/starred/' . $owner . '/' . $repo; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + /** + * List Stargazers. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return mixed + * + * @since 1.0 + */ + public function getList($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/stargazers'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List repositories being starred. + * + * List repositories being starred by a user. + * + * @param string $user User name. + * @param string $sort One of `created` (when the repository was starred) or `updated` (when it was last pushed to). + * @param string $direction One of `asc` (ascending) or `desc` (descending). + * + * @return object + * + * @since 1.0 + * @throws \InvalidArgumentException + */ + public function getRepositories($user = '', $sort = 'created', $direction = 'desc') + { + $allowedSort = ['created', 'updated']; + $allowedDir = ['asc', 'desc']; + + if (!\in_array($sort, $allowedSort)) { + throw new \InvalidArgumentException( + sprintf( + 'The sorting value is invalid. Allowed values are: %s', + implode(', ', $allowedSort) + ) + ); + } + + if (!\in_array($direction, $allowedDir)) { + throw new \InvalidArgumentException( + sprintf( + 'The direction value is invalid. Allowed values are: %s', + implode(', ', $allowedDir) + ) + ); + } + + // Build the request path. + $path = ($user) + ? '/users/' . $user . '/starred' + : '/user/starred'; + + $uri = $this->fetchUrl($path); + $uri->setVar('sort', $sort); + $uri->setVar('direction', $direction); + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * Check if you are starring a repository. + * + * Requires for the user to be authenticated. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return boolean + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function check($owner, $repo) + { + // Build the request path. + $path = '/user/starred/' . $owner . '/' . $repo; + + $response = $this->client->get($this->fetchUrl($path)); + + switch ($response->code) { + case '204': + // This repository is watched by you. + return true; + + case '404': + // This repository is not watched by you. + return false; + } + + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + + /** + * Star a repository. + * + * Requires for the user to be authenticated. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function star($owner, $repo) + { + // Build the request path. + $path = '/user/starred/' . $owner . '/' . $repo; + + return $this->processResponse( + $this->client->put($this->fetchUrl($path), ''), + 204 + ); + } + + /** + * Unstar a repository. + * + * Requires for the user to be authenticated. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function unstar($owner, $repo) + { + // Build the request path. + $path = '/user/starred/' . $owner . '/' . $repo; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } } diff --git a/src/Package/Activity/Watching.php b/src/Package/Activity/Watching.php index 30e57543..663c7b86 100644 --- a/src/Package/Activity/Watching.php +++ b/src/Package/Activity/Watching.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List repositories being watched. - * - * List repositories being watched by a user. - * - * @param string $user User name. - * - * @return mixed - * - * @since 1.0 - */ - public function getRepositories($user = '') - { - // Build the request path. - $path = ($user) - ? '/users/' . $user . '/subscriptions' - : '/user/subscriptions'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Get a Repository Subscription. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function getSubscription($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/subscription'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Set a Repository Subscription. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * @param boolean $subscribed Determines if notifications should be received from this thread. - * @param boolean $ignored Determines if all notifications should be blocked from this thread. - * - * @return object - * - * @since 1.0 - */ - public function setSubscription($owner, $repo, $subscribed, $ignored) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/subscription'; - - $data = [ - 'subscribed' => $subscribed, - 'ignored' => $ignored, - ]; - - return $this->processResponse( - $this->client->put($this->fetchUrl($path), json_encode($data)) - ); - } - - /** - * Delete a Repository Subscription. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function deleteSubscription($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/subscription'; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * Check if you are watching a repository (LEGACY). - * - * Requires for the user to be authenticated. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return boolean - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function check($owner, $repo) - { - // Build the request path. - $path = '/user/subscriptions/' . $owner . '/' . $repo; - - $response = $this->client->get($this->fetchUrl($path)); - - switch ($response->code) - { - case '204' : - // This repository is watched by you. - return true; - - case '404' : - // This repository is not watched by you. - return false; - } - - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - - /** - * Watch a repository (LEGACY). - * - * Requires for the user to be authenticated. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function watch($owner, $repo) - { - // Build the request path. - $path = '/user/subscriptions/' . $owner . '/' . $repo; - - return $this->processResponse( - $this->client->put($this->fetchUrl($path), ''), - 204 - ); - } - - /** - * Stop watching a repository (LEGACY). - * - * Requires for the user to be authenticated. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function unwatch($owner, $repo) - { - // Build the request path. - $path = '/user/subscriptions/' . $owner . '/' . $repo; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + /** + * List watchers + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return mixed + * + * @since 1.0 + */ + public function getList($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/subscribers'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List repositories being watched. + * + * List repositories being watched by a user. + * + * @param string $user User name. + * + * @return mixed + * + * @since 1.0 + */ + public function getRepositories($user = '') + { + // Build the request path. + $path = ($user) + ? '/users/' . $user . '/subscriptions' + : '/user/subscriptions'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Get a Repository Subscription. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function getSubscription($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/subscription'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Set a Repository Subscription. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * @param boolean $subscribed Determines if notifications should be received from this thread. + * @param boolean $ignored Determines if all notifications should be blocked from this thread. + * + * @return object + * + * @since 1.0 + */ + public function setSubscription($owner, $repo, $subscribed, $ignored) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/subscription'; + + $data = [ + 'subscribed' => $subscribed, + 'ignored' => $ignored, + ]; + + return $this->processResponse( + $this->client->put($this->fetchUrl($path), json_encode($data)) + ); + } + + /** + * Delete a Repository Subscription. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function deleteSubscription($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/subscription'; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * Check if you are watching a repository (LEGACY). + * + * Requires for the user to be authenticated. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return boolean + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function check($owner, $repo) + { + // Build the request path. + $path = '/user/subscriptions/' . $owner . '/' . $repo; + + $response = $this->client->get($this->fetchUrl($path)); + + switch ($response->code) { + case '204': + // This repository is watched by you. + return true; + + case '404': + // This repository is not watched by you. + return false; + } + + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + + /** + * Watch a repository (LEGACY). + * + * Requires for the user to be authenticated. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function watch($owner, $repo) + { + // Build the request path. + $path = '/user/subscriptions/' . $owner . '/' . $repo; + + return $this->processResponse( + $this->client->put($this->fetchUrl($path), ''), + 204 + ); + } + + /** + * Stop watching a repository (LEGACY). + * + * Requires for the user to be authenticated. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function unwatch($owner, $repo) + { + // Build the request path. + $path = '/user/subscriptions/' . $owner . '/' . $repo; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } } diff --git a/src/Package/Authorization.php b/src/Package/Authorization.php index d9b141eb..57318703 100644 --- a/src/Package/Authorization.php +++ b/src/Package/Authorization.php @@ -1,4 +1,5 @@ $scopes, 'note' => $note, 'note_url' => $url] - ); - - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } - - /** - * Method to delete an authorization - * - * @param integer $id ID of the authorization to delete - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function delete($id) - { - // Build the request path. - $path = '/authorizations/' . $id; - - // Send the request. - return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); - } - - /** - * Delete a grant - * - * Deleting an OAuth application's grant will also delete all OAuth tokens associated with the application for your user. - * - * @param integer $id ID of the authorization to delete - * - * @return object - * - * @since 1.5.0 - * @throws \DomainException - */ - public function deleteGrant($id) - { - // Build the request path. - $path = '/authorizations/grants/' . $id; - - // Send the request. - return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); - } - - /** - * Method to edit an authorization. - * - * @param integer $id ID of the authorization to edit - * @param array $scopes Replaces the authorization scopes with these. - * @param array $addScopes A list of scopes to add to this authorization. - * @param array $removeScopes A list of scopes to remove from this authorization. - * @param string $note A note to remind you what the OAuth token is for. - * @param string $url A URL to remind you what app the OAuth token is for. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - * @throws \RuntimeException - */ - public function edit($id, array $scopes = [], array $addScopes = [], array $removeScopes = [], $note = '', $url = '') - { - // Check if more than one scopes array contains data - $scopesCount = 0; - $scope = ''; - $scopeData = ''; - - if (!empty($scopes)) - { - $scope = 'scopes'; - $scopeData = $scopes; - $scopesCount++; - } - - if (!empty($addScopes)) - { - $scope = 'add_scopes'; - $scopeData = $addScopes; - $scopesCount++; - } - - if (!empty($removeScopes)) - { - $scope = 'remove_scopes'; - $scopeData = $removeScopes; - $scopesCount++; - } - - // Only allowed to send data for one scope parameter - if ($scopesCount >= 2) - { - throw new \RuntimeException('You can only send one scope key in this request.'); - } - - // Build the request path. - $path = '/authorizations/' . $id; - - $data = json_encode( - [ - $scope => $scopeData, - 'note' => $note, - 'note_url' => $url, - ] - ); - - // Send the request. - return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); - } - - /** - * Method to get details about an authorised application for the authenticated user. - * - * @param integer $id ID of the authorization to retrieve - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function get($id) - { - // Build the request path. - $path = '/authorizations/' . $id; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Get a single grant - * - * @param integer $id ID of the authorization to retrieve - * - * @return object - * - * @since 1.5.0 - * @throws \DomainException - */ - public function getGrant($id) - { - // Build the request path. - $path = '/authorizations/grants/' . $id; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Method to get the authorised applications for the authenticated user. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function getList() - { - // Build the request path. - $path = '/authorizations'; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * List your grants. - * - * You can use this API to list the set of OAuth applications that have been granted access to your account. - * - * @return object - * - * @since 1.5.0 - * @throws \DomainException - */ - public function getListGrants() - { - // Build the request path. - $path = '/authorizations/grants'; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Method to get the rate limit for the authenticated user. - * - * @return object Returns an object with the properties of `limit` and `remaining`. If there is no limit, the - * `limit` property will be false. - * - * @since 1.0 - * @throws UnexpectedResponseException - */ - public function getRateLimit() - { - // Build the request path. - $path = '/rate_limit'; - - // Send the request. - $response = $this->client->get($this->fetchUrl($path)); - - // Validate the response code. - if ($response->code != 200) - { - if ($response->code == 404) - { - // Unlimited rate for Github Enterprise sites and trusted users. - return (object) ['limit' => false, 'remaining' => null]; - } - - // Decode the error response and throw an exception. - $error = json_decode($response->body); - - throw new UnexpectedResponseException($response, $error->message, $response->code); - } - - return json_decode($response->body); - } - - /** - * 1. Request authorization on GitHub. - * - * @param string $clientId The client ID you received from GitHub when you registered. - * @param string $redirectUri URL in your app where users will be sent after authorization. - * @param string $scope Comma separated list of scopes. - * @param string $state An unguessable random string. It is used to protect against cross-site request forgery attacks. - * - * @return string - * - * @since 1.0 - */ - public function getAuthorizationLink($clientId, $redirectUri = '', $scope = '', $state = '') - { - $uri = new Uri('https://github.com/login/oauth/authorize'); - - $uri->setVar('client_id', $clientId); - - if ($redirectUri) - { - $uri->setVar('redirect_uri', urlencode($redirectUri)); - } - - if ($scope) - { - $uri->setVar('scope', $scope); - } - - if ($state) - { - $uri->setVar('state', $state); - } - - return (string) $uri; - } - - /** - * 2. Request the access token. - * - * @param string $clientId The client ID you received from GitHub when you registered. - * @param string $clientSecret The client secret you received from GitHub when you registered. - * @param string $code The code you received as a response to Step 1. - * @param string $redirectUri URL in your app where users will be sent after authorization. - * @param string $format The response format (json, xml, ). - * - * @return string - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function requestToken($clientId, $clientSecret, $code, $redirectUri = '', $format = '') - { - $uri = 'https://github.com/login/oauth/access_token'; - - $data = [ - 'client_id' => $clientId, - 'client_secret' => $clientSecret, - 'code' => $code, - ]; - - if ($redirectUri) - { - $data['redirect_uri'] = $redirectUri; - } - - $headers = []; - - switch ($format) - { - case 'json' : - $headers['Accept'] = 'application/json'; - - break; - - case 'xml' : - $headers['Accept'] = 'application/xml'; - - break; - - default : - if ($format) - { - throw new \UnexpectedValueException('Invalid format'); - } - - break; - } - - // Send the request. - return $this->processResponse( - $this->client->post($uri, $data, $headers), - 200 - ); - } - - /** - * Revoke a grant for an application - * - * OAuth application owners can revoke a grant for their OAuth application and a specific user. - * - * @param integer $clientId The application client ID - * @param integer $accessToken The access token to revoke - * - * @return object - * - * @since 1.5.0 - * @throws \DomainException - */ - public function revokeGrantForApplication($clientId, $accessToken) - { - // Build the request path. - $path = "/applications/$clientId/grants/$accessToken"; - - // Send the request. - return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); - } + /** + * Method to create an authorization. + * + * @param array $scopes A list of scopes that this authorization is in. + * @param string $note A note to remind you what the OAuth token is for. + * @param string $url A URL to remind you what app the OAuth token is for. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function create(array $scopes = [], $note = '', $url = '') + { + // Build the request path. + $path = '/authorizations'; + + $data = json_encode( + ['scopes' => $scopes, 'note' => $note, 'note_url' => $url] + ); + + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Method to delete an authorization + * + * @param integer $id ID of the authorization to delete + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function delete($id) + { + // Build the request path. + $path = '/authorizations/' . $id; + + // Send the request. + return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } + + /** + * Delete a grant + * + * Deleting an OAuth application's grant will also delete all OAuth tokens associated with the application for your user. + * + * @param integer $id ID of the authorization to delete + * + * @return object + * + * @since 1.5.0 + * @throws \DomainException + */ + public function deleteGrant($id) + { + // Build the request path. + $path = '/authorizations/grants/' . $id; + + // Send the request. + return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } + + /** + * Method to edit an authorization. + * + * @param integer $id ID of the authorization to edit + * @param array $scopes Replaces the authorization scopes with these. + * @param array $addScopes A list of scopes to add to this authorization. + * @param array $removeScopes A list of scopes to remove from this authorization. + * @param string $note A note to remind you what the OAuth token is for. + * @param string $url A URL to remind you what app the OAuth token is for. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + * @throws \RuntimeException + */ + public function edit($id, array $scopes = [], array $addScopes = [], array $removeScopes = [], $note = '', $url = '') + { + // Check if more than one scopes array contains data + $scopesCount = 0; + $scope = ''; + $scopeData = ''; + + if (!empty($scopes)) { + $scope = 'scopes'; + $scopeData = $scopes; + $scopesCount++; + } + + if (!empty($addScopes)) { + $scope = 'add_scopes'; + $scopeData = $addScopes; + $scopesCount++; + } + + if (!empty($removeScopes)) { + $scope = 'remove_scopes'; + $scopeData = $removeScopes; + $scopesCount++; + } + + // Only allowed to send data for one scope parameter + if ($scopesCount >= 2) { + throw new \RuntimeException('You can only send one scope key in this request.'); + } + + // Build the request path. + $path = '/authorizations/' . $id; + + $data = json_encode( + [ + $scope => $scopeData, + 'note' => $note, + 'note_url' => $url, + ] + ); + + // Send the request. + return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); + } + + /** + * Method to get details about an authorised application for the authenticated user. + * + * @param integer $id ID of the authorization to retrieve + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function get($id) + { + // Build the request path. + $path = '/authorizations/' . $id; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Get a single grant + * + * @param integer $id ID of the authorization to retrieve + * + * @return object + * + * @since 1.5.0 + * @throws \DomainException + */ + public function getGrant($id) + { + // Build the request path. + $path = '/authorizations/grants/' . $id; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Method to get the authorised applications for the authenticated user. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getList() + { + // Build the request path. + $path = '/authorizations'; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * List your grants. + * + * You can use this API to list the set of OAuth applications that have been granted access to your account. + * + * @return object + * + * @since 1.5.0 + * @throws \DomainException + */ + public function getListGrants() + { + // Build the request path. + $path = '/authorizations/grants'; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Method to get the rate limit for the authenticated user. + * + * @return object Returns an object with the properties of `limit` and `remaining`. If there is no limit, the + * `limit` property will be false. + * + * @since 1.0 + * @throws UnexpectedResponseException + */ + public function getRateLimit() + { + // Build the request path. + $path = '/rate_limit'; + + // Send the request. + $response = $this->client->get($this->fetchUrl($path)); + + // Validate the response code. + if ($response->code != 200) { + if ($response->code == 404) { + // Unlimited rate for Github Enterprise sites and trusted users. + return (object) ['limit' => false, 'remaining' => null]; + } + + // Decode the error response and throw an exception. + $error = json_decode($response->body); + + throw new UnexpectedResponseException($response, $error->message, $response->code); + } + + return json_decode($response->body); + } + + /** + * 1. Request authorization on GitHub. + * + * @param string $clientId The client ID you received from GitHub when you registered. + * @param string $redirectUri URL in your app where users will be sent after authorization. + * @param string $scope Comma separated list of scopes. + * @param string $state An unguessable random string. It is used to protect against cross-site request forgery attacks. + * + * @return string + * + * @since 1.0 + */ + public function getAuthorizationLink($clientId, $redirectUri = '', $scope = '', $state = '') + { + $uri = new Uri('https://github.com/login/oauth/authorize'); + + $uri->setVar('client_id', $clientId); + + if ($redirectUri) { + $uri->setVar('redirect_uri', urlencode($redirectUri)); + } + + if ($scope) { + $uri->setVar('scope', $scope); + } + + if ($state) { + $uri->setVar('state', $state); + } + + return (string) $uri; + } + + /** + * 2. Request the access token. + * + * @param string $clientId The client ID you received from GitHub when you registered. + * @param string $clientSecret The client secret you received from GitHub when you registered. + * @param string $code The code you received as a response to Step 1. + * @param string $redirectUri URL in your app where users will be sent after authorization. + * @param string $format The response format (json, xml, ). + * + * @return string + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function requestToken($clientId, $clientSecret, $code, $redirectUri = '', $format = '') + { + $uri = 'https://github.com/login/oauth/access_token'; + + $data = [ + 'client_id' => $clientId, + 'client_secret' => $clientSecret, + 'code' => $code, + ]; + + if ($redirectUri) { + $data['redirect_uri'] = $redirectUri; + } + + $headers = []; + + switch ($format) { + case 'json': + $headers['Accept'] = 'application/json'; + + break; + + case 'xml': + $headers['Accept'] = 'application/xml'; + + break; + + default: + if ($format) { + throw new \UnexpectedValueException('Invalid format'); + } + + break; + } + + // Send the request. + return $this->processResponse( + $this->client->post($uri, $data, $headers), + 200 + ); + } + + /** + * Revoke a grant for an application + * + * OAuth application owners can revoke a grant for their OAuth application and a specific user. + * + * @param integer $clientId The application client ID + * @param integer $accessToken The access token to revoke + * + * @return object + * + * @since 1.5.0 + * @throws \DomainException + */ + public function revokeGrantForApplication($clientId, $accessToken) + { + // Build the request path. + $path = "/applications/$clientId/grants/$accessToken"; + + // Send the request. + return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } } diff --git a/src/Package/Data.php b/src/Package/Data.php index bda466da..ef75c5a7 100644 --- a/src/Package/Data.php +++ b/src/Package/Data.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Create a Blob. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * @param string $content The content of the blob. - * @param string $encoding The encoding to use. - * - * @return object - * - * @since 1.0 - */ - public function create($owner, $repo, $content, $encoding = 'utf-8') - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/git/blobs'; + /** + * Create a Blob. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * @param string $content The content of the blob. + * @param string $encoding The encoding to use. + * + * @return object + * + * @since 1.0 + */ + public function create($owner, $repo, $content, $encoding = 'utf-8') + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/git/blobs'; - $data = [ - 'content' => $content, - 'encoding' => $encoding, - ]; + $data = [ + 'content' => $content, + 'encoding' => $encoding, + ]; - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($data)), - 201 - ); - } + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($data)), + 201 + ); + } } diff --git a/src/Package/Data/Commits.php b/src/Package/Data/Commits.php index a3cb2725..1f488dc3 100644 --- a/src/Package/Data/Commits.php +++ b/src/Package/Data/Commits.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Create a Commit. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $message The commit message. - * @param string $tree SHA of the tree object this commit points to. - * @param array $parents Array of the SHAs of the commits that were the parents of this commit. - * If omitted or empty, the commit will be written as a root commit. - * For a single parent, an array of one SHA should be provided. - * For a merge commit, an array of more than one should be provided. - * - * @since 1.0 - * - * @return object - */ - public function create($owner, $repo, $message, $tree, array $parents = []) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/git/commits'; + /** + * Create a Commit. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $message The commit message. + * @param string $tree SHA of the tree object this commit points to. + * @param array $parents Array of the SHAs of the commits that were the parents of this commit. + * If omitted or empty, the commit will be written as a root commit. + * For a single parent, an array of one SHA should be provided. + * For a merge commit, an array of more than one should be provided. + * + * @since 1.0 + * + * @return object + */ + public function create($owner, $repo, $message, $tree, array $parents = []) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/git/commits'; - $data = json_encode( - ['message' => $message, 'tree' => $tree, 'parents' => $parents] - ); + $data = json_encode( + ['message' => $message, 'tree' => $tree, 'parents' => $parents] + ); - // Send the request. - return $this->processResponse( - $response = $this->client->post($this->fetchUrl($path), $data), - 201 - ); - } + // Send the request. + return $this->processResponse( + $response = $this->client->post($this->fetchUrl($path), $data), + 201 + ); + } } diff --git a/src/Package/Data/Refs.php b/src/Package/Data/Refs.php index 18cf6333..02111536 100644 --- a/src/Package/Data/Refs.php +++ b/src/Package/Data/Refs.php @@ -1,4 +1,5 @@ processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Get all References. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $namespace Optional sub-namespace to limit the returned references. - * @param integer $page Page to request - * @param integer $limit Number of results to return per page - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function getList($user, $repo, $namespace = '', $page = 0, $limit = 0) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/git/refs'; - - if (!empty($namespace)) - { - $path .= '/' . $namespace; - } - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); - } - - /** - * Create a Reference. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $ref The name of the fully qualified reference. - * @param string $sha The SHA1 value to set this reference to. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function create($user, $repo, $ref, $sha) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/git/refs'; - - // Build the request data. - $data = json_encode( - [ - 'ref' => $ref, - 'sha' => $sha, - ] - ); - - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } - - /** - * Update a Reference. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $ref The reference to update. - * @param string $sha The SHA1 value to set the reference to. - * @param boolean $force Whether the update should be forced. Default to false. - * - * @return object - * - * @since 1.0 - * @throws DomainException - */ - public function edit($user, $repo, $ref, $sha, $force = false) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/git/refs/' . $ref; - - // Create the data object. - $data = new \stdClass; - - // If a title is set add it to the data object. - if ($force) - { - $data->force = true; - } - - $data->sha = $sha; - - // Encode the request data. - $data = json_encode($data); - - // Send the request. - return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); - } - - /** - * Delete a Reference - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $ref The reference to update. - * - * @return object - * - * @since 1.0 - */ - public function delete($owner, $repo, $ref) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/git/refs/' . $ref; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + /** + * Get a Reference. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $ref The reference to get. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function get($user, $repo, $ref) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/git/refs/' . $ref; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Get all References. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $namespace Optional sub-namespace to limit the returned references. + * @param integer $page Page to request + * @param integer $limit Number of results to return per page + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getList($user, $repo, $namespace = '', $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/git/refs'; + + if (!empty($namespace)) { + $path .= '/' . $namespace; + } + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } + + /** + * Create a Reference. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $ref The name of the fully qualified reference. + * @param string $sha The SHA1 value to set this reference to. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function create($user, $repo, $ref, $sha) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/git/refs'; + + // Build the request data. + $data = json_encode( + [ + 'ref' => $ref, + 'sha' => $sha, + ] + ); + + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Update a Reference. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $ref The reference to update. + * @param string $sha The SHA1 value to set the reference to. + * @param boolean $force Whether the update should be forced. Default to false. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function edit($user, $repo, $ref, $sha, $force = false) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/git/refs/' . $ref; + + // Create the data object. + $data = new \stdClass(); + + // If a title is set add it to the data object. + if ($force) { + $data->force = true; + } + + $data->sha = $sha; + + // Encode the request data. + $data = json_encode($data); + + // Send the request. + return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); + } + + /** + * Delete a Reference + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $ref The reference to update. + * + * @return object + * + * @since 1.0 + */ + public function delete($owner, $repo, $ref) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/git/refs/' . $ref; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } } diff --git a/src/Package/Data/Tags.php b/src/Package/Data/Tags.php index 76287210..558b79bf 100644 --- a/src/Package/Data/Tags.php +++ b/src/Package/Data/Tags.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Create a Tag Object - * - * Note that creating a tag object does not create the reference that makes a tag in Git. - * If you want to create an annotated tag in Git, you have to do this call to create the tag object, - * and then create the refs/tags/[tag] reference. If you want to create a lightweight tag, - * you simply have to create the reference - this call would be unnecessary. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $tag The tag string. - * @param string $message The tag message. - * @param string $object The SHA of the git object this is tagging. - * @param string $type The type of the object we’re tagging. Normally this is a commit but it can also be a tree or a blob. - * @param string $taggerName The name of the author of the tag. - * @param string $taggerEmail The email of the author of the tag. - * @param string $taggerDate Timestamp of when this object was tagged. - * - * @return object - * - * @since 1.0 - */ - public function create($owner, $repo, $tag, $message, $object, $type, $taggerName, $taggerEmail, $taggerDate) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/git/tags'; + /** + * Create a Tag Object + * + * Note that creating a tag object does not create the reference that makes a tag in Git. + * If you want to create an annotated tag in Git, you have to do this call to create the tag object, + * and then create the refs/tags/[tag] reference. If you want to create a lightweight tag, + * you simply have to create the reference - this call would be unnecessary. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $tag The tag string. + * @param string $message The tag message. + * @param string $object The SHA of the git object this is tagging. + * @param string $type The type of the object we’re tagging. Normally this is a commit but it can also be a tree or a blob. + * @param string $taggerName The name of the author of the tag. + * @param string $taggerEmail The email of the author of the tag. + * @param string $taggerDate Timestamp of when this object was tagged. + * + * @return object + * + * @since 1.0 + */ + public function create($owner, $repo, $tag, $message, $object, $type, $taggerName, $taggerEmail, $taggerDate) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/git/tags'; - $data = [ - 'tag' => $tag, - 'message' => $message, - 'object' => $object, - 'type' => $type, - 'tagger' => [ - 'name' => $taggerName, - 'email' => $taggerEmail, - 'date' => $taggerDate, - ], - ]; + $data = [ + 'tag' => $tag, + 'message' => $message, + 'object' => $object, + 'type' => $type, + 'tagger' => [ + 'name' => $taggerName, + 'email' => $taggerEmail, + 'date' => $taggerDate, + ], + ]; - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($data)), - 201 - ); - } + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($data)), + 201 + ); + } } diff --git a/src/Package/Data/Trees.php b/src/Package/Data/Trees.php index f94e0488..cc924767 100644 --- a/src/Package/Data/Trees.php +++ b/src/Package/Data/Trees.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Get a Tree Recursively - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $sha The SHA1 value to set the reference to. - * - * @since 1.0 - * - * @return object - */ - public function getRecursively($owner, $repo, $sha) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/git/trees/' . $sha . '?recursive=1'; + /** + * Get a Tree Recursively + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $sha The SHA1 value to set the reference to. + * + * @since 1.0 + * + * @return object + */ + public function getRecursively($owner, $repo, $sha) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/git/trees/' . $sha . '?recursive=1'; - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Create a Tree. - * - * The tree creation API will take nested entries as well. If both a tree and a nested path - * modifying that tree are specified, it will overwrite the contents of that tree with the - * new path contents and write a new tree out. - * - * Parameters for the tree: - * - * tree.path - * String of the file referenced in the tree - * tree.mode - * String of the file mode - one of 100644 for file (blob), 100755 for executable (blob), - * 040000 for subdirectory (tree), 160000 for submodule (commit) or 120000 for a blob - * that specifies the path of a symlink - * tree.type - * String of blob, tree, commit - * tree.sha - * String of SHA1 checksum ID of the object in the tree - * tree.content - * String of content you want this file to have - GitHub will write this blob out and use - * that SHA for this entry. Use either this or tree.sha - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param array $tree Array of Hash objects (of path, mode, type and sha) specifying a tree structure - * @param string $baseTree The SHA1 of the tree you want to update with new data. - * - * @return object - * - * @since 1.0 - */ - public function create($owner, $repo, $tree, $baseTree = '') - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/git/trees'; + /** + * Create a Tree. + * + * The tree creation API will take nested entries as well. If both a tree and a nested path + * modifying that tree are specified, it will overwrite the contents of that tree with the + * new path contents and write a new tree out. + * + * Parameters for the tree: + * + * tree.path + * String of the file referenced in the tree + * tree.mode + * String of the file mode - one of 100644 for file (blob), 100755 for executable (blob), + * 040000 for subdirectory (tree), 160000 for submodule (commit) or 120000 for a blob + * that specifies the path of a symlink + * tree.type + * String of blob, tree, commit + * tree.sha + * String of SHA1 checksum ID of the object in the tree + * tree.content + * String of content you want this file to have - GitHub will write this blob out and use + * that SHA for this entry. Use either this or tree.sha + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param array $tree Array of Hash objects (of path, mode, type and sha) specifying a tree structure + * @param string $baseTree The SHA1 of the tree you want to update with new data. + * + * @return object + * + * @since 1.0 + */ + public function create($owner, $repo, $tree, $baseTree = '') + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/git/trees'; - $data = []; + $data = []; - $data['tree'] = $tree; + $data['tree'] = $tree; - if ($baseTree) - { - $data['base_tree'] = $baseTree; - } + if ($baseTree) { + $data['base_tree'] = $baseTree; + } - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($data)), - 201 - ); - } + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($data)), + 201 + ); + } } diff --git a/src/Package/Emojis.php b/src/Package/Emojis.php index c5139e59..21a04ffc 100644 --- a/src/Package/Emojis.php +++ b/src/Package/Emojis.php @@ -1,4 +1,5 @@ processResponse($this->client->get($this->fetchUrl($path))); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } } diff --git a/src/Package/Gists.php b/src/Package/Gists.php index 5f1ad4f3..6b4473ba 100644 --- a/src/Package/Gists.php +++ b/src/Package/Gists.php @@ -1,4 +1,5 @@ $this->buildFileData((array) $files), - 'public' => (bool) $public, - 'description' => $description, - ] - ); - - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } - - /** - * Delete a gist. - * - * @param integer $gistId The gist number. - * - * @return void - * - * @since 1.0 - * @throws \DomainException - */ - public function delete($gistId) - { - // Build the request path. - $path = '/gists/' . (int) $gistId; - - // Send the request. - $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); - } - - /** - * Edit a gist. - * - * @param integer $gistId The gist number. - * @param mixed $files Either an array of file paths or a single file path as a string. - * @param boolean $public True if the gist should be public. - * @param string $description The description of the gist. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function edit($gistId, $files = null, $public = null, $description = null) - { - // Build the request path. - $path = '/gists/' . (int) $gistId; - - // Create the data object. - $data = new \stdClass; - - // If a description is set add it to the data object. - if (isset($description)) - { - $data->description = $description; - } - - // If the public flag is set add it to the data object. - if (isset($public)) - { - $data->public = $public; - } - - // If a state is set add it to the data object. - if (isset($files)) - { - $data->files = $this->buildFileData((array) $files); - } - - // Encode the request data. - $data = json_encode($data); - - // Send the request. - return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); - } - - /** - * Fork a gist. - * - * @param integer $gistId The gist number. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function fork($gistId) - { - // Build the request path. - $path = '/gists/' . (int) $gistId . '/forks'; - - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), ''), 201); - } - - /** - * Get a single gist. - * - * @param integer $gistId The gist number. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function get($gistId) - { - // Build the request path. - $path = '/gists/' . (int) $gistId; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * List gist commits. - * - * @param integer $gistId The gist number. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return array - * - * @since 1.4.0 - * @throws \DomainException - */ - public function getCommitList($gistId, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/gists/' . (int) $gistId . '/commits'; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); - } - - /** - * List gist forks. - * - * @param integer $gistId The gist number. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return array - * - * @since 1.4.0 - * @throws \DomainException - */ - public function getForkList($gistId, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/gists/' . (int) $gistId . '/forks'; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); - } - - /** - * List gists. - * - * If a user is authenticated it will return the user's gists, otherwise - * it will return all public gists. - * - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return array - * - * @since 1.0 - * @throws \DomainException - */ - public function getList($page = 0, $limit = 0) - { - // Build the request path. - $path = '/gists'; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); - } - - /** - * List a user’s gists. - * - * @param string $user The name of the GitHub user from which to list gists. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * @param \DateTime $since Only gists updated at or after this time are returned. - * - * @return array - * - * @since 1.0 - * @throws \DomainException - */ - public function getListByUser($user, $page = 0, $limit = 0, \DateTime $since = null) - { - // Build the request path. - $uri = $this->fetchUrl('/users/' . $user . '/gists', $page, $limit); - - if ($since) - { - $uri->setVar('since', $since->format(\DateTime::RFC3339)); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * List all public gists. - * - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * @param \DateTime $since Only gists updated at or after this time are returned. - * - * @return array - * - * @since 1.0 - * @throws \DomainException - */ - public function getListPublic($page = 0, $limit = 0, \DateTime $since = null) - { - // Build the request path. - $uri = $this->fetchUrl('/gists/public', $page, $limit); - - if ($since) - { - $uri->setVar('since', $since->format(\DateTime::RFC3339)); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * List starred gists. - * - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * @param \DateTime $since Only gists updated at or after this time are returned. - * - * @return array - * - * @since 1.0 - * @throws \DomainException - */ - public function getListStarred($page = 0, $limit = 0, \DateTime $since = null) - { - // Build the request path. - $uri = $this->fetchUrl('/gists/starred', $page, $limit); - - if ($since) - { - $uri->setVar('since', $since->format(\DateTime::RFC3339)); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * Get a specific revision of a gist. - * - * @param integer $gistId The gist number. - * @param string $sha The SHA for the revision to get. - * - * @return object - * - * @since 1.4.0 - * @throws \DomainException - */ - public function getRevision($gistId, $sha) - { - // Build the request path. - $path = '/gists/' . (int) $gistId . '/' . $sha; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Check if a gist is starred. - * - * @param integer $gistId The gist number. - * - * @return boolean True if gist is starred - * - * @since 1.0 - * @throws UnexpectedResponseException - */ - public function isStarred($gistId) - { - // Build the request path. - $path = '/gists/' . (int) $gistId . '/star'; - - // Send the request. - $response = $this->client->get($this->fetchUrl($path)); - - // Validate the response code. - if ($response->code == 204) - { - return true; - } - - if ($response->code == 404) - { - return false; - } - - // Decode the error response and throw an exception. - $error = json_decode($response->body); - $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; - - throw new UnexpectedResponseException($response, $message, $response->code); - } - - /** - * Star a gist. - * - * @param integer $gistId The gist number. - * - * @return void - * - * @since 1.0 - * @throws \DomainException - */ - public function star($gistId) - { - // Build the request path. - $path = '/gists/' . (int) $gistId . '/star'; - - // Send the request. - $this->processResponse($this->client->put($this->fetchUrl($path), ''), 204); - } - - /** - * Unstar a gist. - * - * @param integer $gistId The gist number. - * - * @return void - * - * @since 1.0 - * @throws \DomainException - */ - public function unstar($gistId) - { - // Build the request path. - $path = '/gists/' . (int) $gistId . '/star'; - - // Send the request. - $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); - } - - /** - * Method to fetch a data array for transmitting to the GitHub API for a list of files based on - * an input array of file paths or filename and content pairs. - * - * @param array $files The list of file paths or filenames and content. - * - * @return array - * - * @since 1.0 - * @throws \InvalidArgumentException - */ - protected function buildFileData(array $files) - { - $data = []; - - foreach ($files as $key => $file) - { - if (!is_numeric($key)) - { - // If the key isn't numeric, then we are dealing with a file whose content has been supplied - $data[$key] = ['content' => $file]; - } - elseif (!file_exists($file)) - { - // Otherwise, we have been given a path and we have to load the content - // Verify that the each file exists. - throw new \InvalidArgumentException('The file ' . $file . ' does not exist.'); - } - else - { - $data[basename($file)] = ['content' => file_get_contents($file)]; - } - } - - return $data; - } + /** + * Create a gist. + * + * @param mixed $files Either an array of file paths or a single file path as a string. + * @param boolean $public True if the gist should be public. + * @param string $description The optional description of the gist. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function create($files, $public = false, $description = null) + { + // Build the request path. + $path = '/gists'; + + // Build the request data. + $data = json_encode( + [ + 'files' => $this->buildFileData((array) $files), + 'public' => (bool) $public, + 'description' => $description, + ] + ); + + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Delete a gist. + * + * @param integer $gistId The gist number. + * + * @return void + * + * @since 1.0 + * @throws \DomainException + */ + public function delete($gistId) + { + // Build the request path. + $path = '/gists/' . (int) $gistId; + + // Send the request. + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } + + /** + * Edit a gist. + * + * @param integer $gistId The gist number. + * @param mixed $files Either an array of file paths or a single file path as a string. + * @param boolean $public True if the gist should be public. + * @param string $description The description of the gist. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function edit($gistId, $files = null, $public = null, $description = null) + { + // Build the request path. + $path = '/gists/' . (int) $gistId; + + // Create the data object. + $data = new \stdClass(); + + // If a description is set add it to the data object. + if (isset($description)) { + $data->description = $description; + } + + // If the public flag is set add it to the data object. + if (isset($public)) { + $data->public = $public; + } + + // If a state is set add it to the data object. + if (isset($files)) { + $data->files = $this->buildFileData((array) $files); + } + + // Encode the request data. + $data = json_encode($data); + + // Send the request. + return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); + } + + /** + * Fork a gist. + * + * @param integer $gistId The gist number. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function fork($gistId) + { + // Build the request path. + $path = '/gists/' . (int) $gistId . '/forks'; + + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), ''), 201); + } + + /** + * Get a single gist. + * + * @param integer $gistId The gist number. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function get($gistId) + { + // Build the request path. + $path = '/gists/' . (int) $gistId; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * List gist commits. + * + * @param integer $gistId The gist number. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array + * + * @since 1.4.0 + * @throws \DomainException + */ + public function getCommitList($gistId, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/gists/' . (int) $gistId . '/commits'; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } + + /** + * List gist forks. + * + * @param integer $gistId The gist number. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array + * + * @since 1.4.0 + * @throws \DomainException + */ + public function getForkList($gistId, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/gists/' . (int) $gistId . '/forks'; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } + + /** + * List gists. + * + * If a user is authenticated it will return the user's gists, otherwise + * it will return all public gists. + * + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array + * + * @since 1.0 + * @throws \DomainException + */ + public function getList($page = 0, $limit = 0) + { + // Build the request path. + $path = '/gists'; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } + + /** + * List a user’s gists. + * + * @param string $user The name of the GitHub user from which to list gists. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * @param ?\DateTime $since Only gists updated at or after this time are returned. + * + * @return array + * + * @since 1.0 + * @throws \DomainException + */ + public function getListByUser($user, $page = 0, $limit = 0, \DateTime $since = null) + { + // Build the request path. + $uri = $this->fetchUrl('/users/' . $user . '/gists', $page, $limit); + + if ($since) { + $uri->setVar('since', $since->format(\DateTime::RFC3339)); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * List all public gists. + * + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * @param ?\DateTime $since Only gists updated at or after this time are returned. + * + * @return array + * + * @since 1.0 + * @throws \DomainException + */ + public function getListPublic($page = 0, $limit = 0, \DateTime $since = null) + { + // Build the request path. + $uri = $this->fetchUrl('/gists/public', $page, $limit); + + if ($since) { + $uri->setVar('since', $since->format(\DateTime::RFC3339)); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * List starred gists. + * + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * @param ?\DateTime $since Only gists updated at or after this time are returned. + * + * @return array + * + * @since 1.0 + * @throws \DomainException + */ + public function getListStarred($page = 0, $limit = 0, \DateTime $since = null) + { + // Build the request path. + $uri = $this->fetchUrl('/gists/starred', $page, $limit); + + if ($since) { + $uri->setVar('since', $since->format(\DateTime::RFC3339)); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * Get a specific revision of a gist. + * + * @param integer $gistId The gist number. + * @param string $sha The SHA for the revision to get. + * + * @return object + * + * @since 1.4.0 + * @throws \DomainException + */ + public function getRevision($gistId, $sha) + { + // Build the request path. + $path = '/gists/' . (int) $gistId . '/' . $sha; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Check if a gist is starred. + * + * @param integer $gistId The gist number. + * + * @return boolean True if gist is starred + * + * @since 1.0 + * @throws UnexpectedResponseException + */ + public function isStarred($gistId) + { + // Build the request path. + $path = '/gists/' . (int) $gistId . '/star'; + + // Send the request. + $response = $this->client->get($this->fetchUrl($path)); + + // Validate the response code. + if ($response->code == 204) { + return true; + } + + if ($response->code == 404) { + return false; + } + + // Decode the error response and throw an exception. + $error = json_decode($response->body); + $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; + + throw new UnexpectedResponseException($response, $message, $response->code); + } + + /** + * Star a gist. + * + * @param integer $gistId The gist number. + * + * @return void + * + * @since 1.0 + * @throws \DomainException + */ + public function star($gistId) + { + // Build the request path. + $path = '/gists/' . (int) $gistId . '/star'; + + // Send the request. + $this->processResponse($this->client->put($this->fetchUrl($path), ''), 204); + } + + /** + * Unstar a gist. + * + * @param integer $gistId The gist number. + * + * @return void + * + * @since 1.0 + * @throws \DomainException + */ + public function unstar($gistId) + { + // Build the request path. + $path = '/gists/' . (int) $gistId . '/star'; + + // Send the request. + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } + + /** + * Method to fetch a data array for transmitting to the GitHub API for a list of files based on + * an input array of file paths or filename and content pairs. + * + * @param array $files The list of file paths or filenames and content. + * + * @return array + * + * @since 1.0 + * @throws \InvalidArgumentException + */ + protected function buildFileData(array $files) + { + $data = []; + + foreach ($files as $key => $file) { + if (!is_numeric($key)) { + // If the key isn't numeric, then we are dealing with a file whose content has been supplied + $data[$key] = ['content' => $file]; + } elseif (!file_exists($file)) { + // Otherwise, we have been given a path and we have to load the content + // Verify that the each file exists. + throw new \InvalidArgumentException('The file ' . $file . ' does not exist.'); + } else { + $data[basename($file)] = ['content' => file_get_contents($file)]; + } + } + + return $data; + } } diff --git a/src/Package/Gists/Comments.php b/src/Package/Gists/Comments.php index be012ae8..004cddd2 100644 --- a/src/Package/Gists/Comments.php +++ b/src/Package/Gists/Comments.php @@ -1,4 +1,5 @@ $body, - ] - ); + // Build the request data. + $data = json_encode( + [ + 'body' => $body, + ] + ); - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } - /** - * Delete a comment. - * - * @param integer $commentId The id of the comment to delete. - * - * @return void - * - * @since 1.0 - * @throws \DomainException - */ - public function delete($commentId) - { - // Build the request path. - $path = '/gists/comments/' . (int) $commentId; + /** + * Delete a comment. + * + * @param integer $commentId The id of the comment to delete. + * + * @return void + * + * @since 1.0 + * @throws \DomainException + */ + public function delete($commentId) + { + // Build the request path. + $path = '/gists/comments/' . (int) $commentId; - // Send the request. - $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); - } + // Send the request. + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } - /** - * Edit a comment. - * - * @param integer $commentId The id of the comment to update. - * @param string $body The new body text for the comment. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function edit($commentId, $body) - { - // Build the request path. - $path = '/gists/comments/' . (int) $commentId; + /** + * Edit a comment. + * + * @param integer $commentId The id of the comment to update. + * @param string $body The new body text for the comment. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function edit($commentId, $body) + { + // Build the request path. + $path = '/gists/comments/' . (int) $commentId; - // Build the request data. - $data = json_encode( - [ - 'body' => $body, - ] - ); + // Build the request data. + $data = json_encode( + [ + 'body' => $body, + ] + ); - // Send the request. - return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); - } + // Send the request. + return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); + } - /** - * Get a single comment. - * - * @param integer $commentId The comment id to get. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function get($commentId) - { - // Build the request path. - $path = '/gists/comments/' . (int) $commentId; + /** + * Get a single comment. + * + * @param integer $commentId The comment id to get. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function get($commentId) + { + // Build the request path. + $path = '/gists/comments/' . (int) $commentId; - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } - /** - * List comments on a gist. - * - * @param integer $gistId The gist number. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function getList($gistId, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/gists/' . (int) $gistId . '/comments'; + /** + * List comments on a gist. + * + * @param integer $gistId The gist number. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getList($gistId, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/gists/' . (int) $gistId . '/comments'; - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } } diff --git a/src/Package/Gitignore.php b/src/Package/Gitignore.php index 587c8a4c..e6b804be 100644 --- a/src/Package/Gitignore.php +++ b/src/Package/Gitignore.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Get a single template - * - * @param string $name The name of the template - * @param boolean $raw Raw output - * - * @return mixed|string - * - * @since 1.0 - * @throws UnexpectedResponseException - */ - public function get($name, $raw = false) - { - // Build the request path. - $path = '/gitignore/templates/' . $name; + /** + * Get a single template + * + * @param string $name The name of the template + * @param boolean $raw Raw output + * + * @return mixed|string + * + * @since 1.0 + * @throws UnexpectedResponseException + */ + public function get($name, $raw = false) + { + // Build the request path. + $path = '/gitignore/templates/' . $name; - $headers = []; + $headers = []; - if ($raw) - { - $headers['Accept'] = 'application/vnd.github.raw+json'; - } + if ($raw) { + $headers['Accept'] = 'application/vnd.github.raw+json'; + } - $response = $this->client->get($this->fetchUrl($path), $headers); + $response = $this->client->get($this->fetchUrl($path), $headers); - // Validate the response code. - if ($response->code != 200) - { - // Decode the error response and throw an exception. - $error = json_decode($response->body); - $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; + // Validate the response code. + if ($response->code != 200) { + // Decode the error response and throw an exception. + $error = json_decode($response->body); + $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; - throw new UnexpectedResponseException($response, $message, $response->code); - } + throw new UnexpectedResponseException($response, $message, $response->code); + } - return ($raw) ? $response->body : json_decode($response->body); - } + return ($raw) ? $response->body : json_decode($response->body); + } } diff --git a/src/Package/Graphql.php b/src/Package/Graphql.php index b840c45e..040865c7 100644 --- a/src/Package/Graphql.php +++ b/src/Package/Graphql.php @@ -1,4 +1,5 @@ 'application/vnd.github.v4+json', - 'Content-Type' => 'application/json', - ]; - - $data = [ - 'query' => $query, - ]; - - if (!empty($variables)) - { - $data['variables'] = $variables; - } - - // Send the request. - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($data), $headers) - ); - } + /** + * Execute a query against the GraphQL API. + * + * @param string $query The query to perform. + * @param array $variables An optional array of variables to include in the request. + * + * @return string + * + * @since 1.6.0 + */ + public function execute($query, array $variables = []) + { + // Build the request path. + $path = '/graphql'; + + $headers = [ + 'Accept' => 'application/vnd.github.v4+json', + 'Content-Type' => 'application/json', + ]; + + $data = [ + 'query' => $query, + ]; + + if (!empty($variables)) { + $data['variables'] = $variables; + } + + // Send the request. + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($data), $headers) + ); + } } diff --git a/src/Package/Issues.php b/src/Package/Issues.php index d20f1ee0..a82d2235 100644 --- a/src/Package/Issues.php +++ b/src/Package/Issues.php @@ -1,4 +1,5 @@ $title, - 'milestone' => $milestone, - 'labels' => $labels, - 'body' => $body, - ]; - - if (\is_string($assignee) && !empty($assignees)) - { - throw new \UnexpectedValueException('You cannot pass both assignee and assignees. Only one may be provided.'); - } - - if (!empty($assignees)) - { - $data['assignees'] = array_values($assignees); - } - elseif (\is_string($assignee)) - { - $data['assignee'] = $assignee; - } - - // Encode the request data. - $data = json_encode($data); - - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } - - /** - * Edit an issue. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $issueId The issue number. - * @param string $state The optional new state for the issue. [open, closed] - * @param string $title The title of the new issue. - * @param string $body The body text for the new issue. - * @param string $assignee The login for the GitHub user that this issue should be assigned to. - * @param integer $milestone The milestone to associate this issue with. - * @param array $labels The labels to associate with this issue. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function edit($user, $repo, $issueId, $state = null, $title = null, $body = null, $assignee = null, $milestone = null, array $labels = []) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId; - - // Create the data object. - $data = new \stdClass; - - // If a title is set add it to the data object. - if (isset($title)) - { - $data->title = $title; - } - - // If a body is set add it to the data object. - if (isset($body)) - { - $data->body = $body; - } - - // If a state is set add it to the data object. - if (isset($state)) - { - $data->state = $state; - } - - // If an assignee is set add it to the data object. - if (isset($assignee)) - { - $data->assignee = $assignee; - } - - // If a milestone is set add it to the data object. - if (isset($milestone)) - { - $data->milestone = $milestone; - } - - // If labels are set add them to the data object. - if (!empty($labels)) - { - // Ensure that we have a non-associative array. - if (isset($labels)) - { - $labels = array_values($labels); - } - - $data->labels = $labels; - } - - // Encode the request data. - $data = json_encode($data); - - // Send the request. - return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); - } - - /** - * Get a single issue. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $issueId The issue number. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function get($user, $repo, $issueId) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * List issues. - * - * @param string $filter The filter type: assigned, created, mentioned, subscribed. - * @param string $state The optional state to filter requests by. [open, closed] - * @param string $labels The list of comma separated Label names. Example: bug,ui,@high. - * @param string $sort The sort order: created, updated, comments, default: created. - * @param string $direction The list direction: asc or desc, default: desc. - * @param \DateTimeInterface $since Only issues updated at or after this time are returned. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function getList( - $filter = null, - $state = null, - $labels = null, - $sort = null, - $direction = null, - \DateTimeInterface $since = null, - $page = 0, - $limit = 0 - ) - { - // Build the request path. - $uri = $this->fetchUrl('/issues', $page, $limit); - - if ($filter) - { - $uri->setVar('filter', $filter); - } - - if ($state) - { - $uri->setVar('state', $state); - } - - if ($labels) - { - $uri->setVar('labels', $labels); - } - - if ($sort) - { - $uri->setVar('sort', $sort); - } - - if ($direction) - { - $uri->setVar('direction', $direction); - } - - if ($since) - { - $uri->setVar('since', $since->format(\DateTime::ISO8601)); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * List issues for a repository. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $milestone The milestone number, 'none', or *. - * @param string $state The optional state to filter requests by. [open, closed] - * @param string $assignee The assignee name, 'none', or *. - * @param string $mentioned The GitHub user name. - * @param string $labels The list of comma separated Label names. Example: bug,ui,@high. - * @param string $sort The sort order: created, updated, comments, default: created. - * @param string $direction The list direction: asc or desc, default: desc. - * @param \DateTimeInterface $since Only issues updated at or after this time are returned. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function getListByRepository( - $user, - $repo, - $milestone = null, - $state = null, - $assignee = null, - $mentioned = null, - $labels = null, - $sort = null, - $direction = null, - \DateTimeInterface $since = null, - $page = 0, - $limit = 0 - ) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/issues'; - - $uri = $this->fetchUrl($path, $page, $limit); - - if ($milestone) - { - $uri->setVar('milestone', $milestone); - } - - if ($state) - { - $uri->setVar('state', $state); - } - - if ($assignee) - { - $uri->setVar('assignee', $assignee); - } - - if ($mentioned) - { - $uri->setVar('mentioned', $mentioned); - } - - if ($labels) - { - $uri->setVar('labels', $labels); - } - - if ($sort) - { - $uri->setVar('sort', $sort); - } - - if ($direction) - { - $uri->setVar('direction', $direction); - } - - if ($since) - { - $uri->setVar('since', $since->format(\DateTime::RFC3339)); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * Lock an issue. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $issueId The issue number. - * - * @return object - * - * @since 1.4.0 - * @throws \DomainException - */ - public function lock($user, $repo, $issueId) - { - // Build the request path. - $path = "/repos/$user/$repo/issues/" . (int) $issueId . '/lock'; - - return $this->processResponse($this->client->put($this->fetchUrl($path), []), 204); - } - - /** - * Unlock an issue. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $issueId The issue number. - * - * @return object - * - * @since 1.4.0 - * @throws \DomainException - */ - public function unlock($user, $repo, $issueId) - { - // Build the request path. - $path = "/repos/$user/$repo/issues/" . (int) $issueId . '/lock'; - - return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); - } + /** + * Create an issue. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $title The title of the new issue. + * @param string $body The body text for the new issue. + * @param string $assignee The login for the GitHub user that this issue should be assigned to. + * @param integer $milestone The milestone to associate this issue with. + * @param string[] $labels The labels to associate with this issue. + * @param string[] $assignees The logins for GitHub users to assign to this issue. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function create($user, $repo, $title, $body = null, $assignee = null, $milestone = null, array $labels = [], array $assignees = []) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/issues'; + + // Ensure that we have a non-associative array. + if (!empty($labels)) { + $labels = array_values($labels); + } + + // Build the request data. + $data = [ + 'title' => $title, + 'milestone' => $milestone, + 'labels' => $labels, + 'body' => $body, + ]; + + if (\is_string($assignee) && !empty($assignees)) { + throw new \UnexpectedValueException('You cannot pass both assignee and assignees. Only one may be provided.'); + } + + if (!empty($assignees)) { + $data['assignees'] = array_values($assignees); + } elseif (\is_string($assignee)) { + $data['assignee'] = $assignee; + } + + // Encode the request data. + $data = json_encode($data); + + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Edit an issue. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $issueId The issue number. + * @param string $state The optional new state for the issue. [open, closed] + * @param string $title The title of the new issue. + * @param string $body The body text for the new issue. + * @param string $assignee The login for the GitHub user that this issue should be assigned to. + * @param integer $milestone The milestone to associate this issue with. + * @param array $labels The labels to associate with this issue. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function edit($user, $repo, $issueId, $state = null, $title = null, $body = null, $assignee = null, $milestone = null, array $labels = []) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId; + + // Create the data object. + $data = new \stdClass(); + + // If a title is set add it to the data object. + if (isset($title)) { + $data->title = $title; + } + + // If a body is set add it to the data object. + if (isset($body)) { + $data->body = $body; + } + + // If a state is set add it to the data object. + if (isset($state)) { + $data->state = $state; + } + + // If an assignee is set add it to the data object. + if (isset($assignee)) { + $data->assignee = $assignee; + } + + // If a milestone is set add it to the data object. + if (isset($milestone)) { + $data->milestone = $milestone; + } + + // If labels are set add them to the data object. + if (!empty($labels)) { + // Ensure that we have a non-associative array. + if (isset($labels)) { + $labels = array_values($labels); + } + + $data->labels = $labels; + } + + // Encode the request data. + $data = json_encode($data); + + // Send the request. + return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); + } + + /** + * Get a single issue. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $issueId The issue number. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function get($user, $repo, $issueId) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * List issues. + * + * @param string $filter The filter type: assigned, created, mentioned, subscribed. + * @param string $state The optional state to filter requests by. [open, closed] + * @param string $labels The list of comma separated Label names. Example: bug,ui,@high. + * @param string $sort The sort order: created, updated, comments, default: created. + * @param string $direction The list direction: asc or desc, default: desc. + * @param ?\DateTimeInterface $since Only issues updated at or after this time are returned. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getList( + $filter = null, + $state = null, + $labels = null, + $sort = null, + $direction = null, + \DateTimeInterface $since = null, + $page = 0, + $limit = 0 + ) { + // Build the request path. + $uri = $this->fetchUrl('/issues', $page, $limit); + + if ($filter) { + $uri->setVar('filter', $filter); + } + + if ($state) { + $uri->setVar('state', $state); + } + + if ($labels) { + $uri->setVar('labels', $labels); + } + + if ($sort) { + $uri->setVar('sort', $sort); + } + + if ($direction) { + $uri->setVar('direction', $direction); + } + + if ($since) { + $uri->setVar('since', $since->format(\DateTime::ISO8601)); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * List issues for a repository. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $milestone The milestone number, 'none', or *. + * @param string $state The optional state to filter requests by. [open, closed] + * @param string $assignee The assignee name, 'none', or *. + * @param string $mentioned The GitHub user name. + * @param string $labels The list of comma separated Label names. Example: bug,ui,@high. + * @param string $sort The sort order: created, updated, comments, default: created. + * @param string $direction The list direction: asc or desc, default: desc. + * @param ?\DateTimeInterface $since Only issues updated at or after this time are returned. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getListByRepository( + $user, + $repo, + $milestone = null, + $state = null, + $assignee = null, + $mentioned = null, + $labels = null, + $sort = null, + $direction = null, + \DateTimeInterface $since = null, + $page = 0, + $limit = 0 + ) { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/issues'; + + $uri = $this->fetchUrl($path, $page, $limit); + + if ($milestone) { + $uri->setVar('milestone', $milestone); + } + + if ($state) { + $uri->setVar('state', $state); + } + + if ($assignee) { + $uri->setVar('assignee', $assignee); + } + + if ($mentioned) { + $uri->setVar('mentioned', $mentioned); + } + + if ($labels) { + $uri->setVar('labels', $labels); + } + + if ($sort) { + $uri->setVar('sort', $sort); + } + + if ($direction) { + $uri->setVar('direction', $direction); + } + + if ($since) { + $uri->setVar('since', $since->format(\DateTime::RFC3339)); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * Lock an issue. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $issueId The issue number. + * + * @return object + * + * @since 1.4.0 + * @throws \DomainException + */ + public function lock($user, $repo, $issueId) + { + // Build the request path. + $path = "/repos/$user/$repo/issues/" . (int) $issueId . '/lock'; + + return $this->processResponse($this->client->put($this->fetchUrl($path), []), 204); + } + + /** + * Unlock an issue. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $issueId The issue number. + * + * @return object + * + * @since 1.4.0 + * @throws \DomainException + */ + public function unlock($user, $repo, $issueId) + { + // Build the request path. + $path = "/repos/$user/$repo/issues/" . (int) $issueId . '/lock'; + + return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } } diff --git a/src/Package/Issues/Assignees.php b/src/Package/Issues/Assignees.php index c189702d..cc628cca 100644 --- a/src/Package/Issues/Assignees.php +++ b/src/Package/Issues/Assignees.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Check assignee. - * - * You may check to see if a particular user is an assignee for a repository. - * If the given assignee login belongs to an assignee for the repository, a 204 header - * with no content is returned. - * Otherwise a 404 status code is returned. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $assignee The assignees login name. - * - * @return boolean - * - * @since 1.0 - * @throws \DomainException - */ - public function check($owner, $repo, $assignee) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/assignees/' . $assignee; - - try - { - $response = $this->client->get($this->fetchUrl($path)); - - if ($response->code == 204) - { - return true; - } - - throw new UnexpectedResponseException($response, 'Invalid response: ' . $response->code); - } - catch (\DomainException $e) - { - if (isset($response->code) && $response->code == 404) - { - return false; - } - - throw $e; - } - } - - /** - * Add assignees to an Issue - * - * This call adds the users passed in the assignees key (as their logins) to the issue. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $number The issue number to add assignees to. - * @param string[] $assignees The logins for GitHub users to assign to this issue. - * - * @return object - * - * @since 1.4.0 - * @throws \DomainException - */ - public function add($owner, $repo, $number, array $assignees) - { - // Build the request path. - $path = "/repos/$owner/$repo/issues/$number/assignees"; - - $data = json_encode( - [ - 'assignees' => $assignees, - ] - ); - - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } - - /** - * Remove assignees from an Issue - * - * This call removes the users passed in the assignees key (as their logins) from the issue. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $number The issue number to add assignees to. - * @param string[] $assignees The logins for GitHub users to assign to this issue. - * - * @return object - * - * @since 1.4.0 - * @throws \DomainException - */ - public function remove($owner, $repo, $number, array $assignees) - { - // Build the request path. - $path = "/repos/$owner/$repo/issues/$number/assignees"; - - $data = json_encode( - [ - 'assignees' => $assignees, - ] - ); - - return $this->processResponse($this->client->delete($this->fetchUrl($path), [], null, $data)); - } + /** + * List assignees. + * + * This call lists all the available assignees (owner + collaborators) to which issues may be assigned. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * + * @return object + * + * @since 1.0 + */ + public function getList($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/assignees'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Check assignee. + * + * You may check to see if a particular user is an assignee for a repository. + * If the given assignee login belongs to an assignee for the repository, a 204 header + * with no content is returned. + * Otherwise a 404 status code is returned. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $assignee The assignees login name. + * + * @return boolean + * + * @since 1.0 + * @throws \DomainException + */ + public function check($owner, $repo, $assignee) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/assignees/' . $assignee; + + try { + $response = $this->client->get($this->fetchUrl($path)); + + if ($response->code == 204) { + return true; + } + + throw new UnexpectedResponseException($response, 'Invalid response: ' . $response->code); + } catch (\DomainException $e) { + if (isset($response->code) && $response->code == 404) { + return false; + } + + throw $e; + } + } + + /** + * Add assignees to an Issue + * + * This call adds the users passed in the assignees key (as their logins) to the issue. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $number The issue number to add assignees to. + * @param string[] $assignees The logins for GitHub users to assign to this issue. + * + * @return object + * + * @since 1.4.0 + * @throws \DomainException + */ + public function add($owner, $repo, $number, array $assignees) + { + // Build the request path. + $path = "/repos/$owner/$repo/issues/$number/assignees"; + + $data = json_encode( + [ + 'assignees' => $assignees, + ] + ); + + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Remove assignees from an Issue + * + * This call removes the users passed in the assignees key (as their logins) from the issue. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $number The issue number to add assignees to. + * @param string[] $assignees The logins for GitHub users to assign to this issue. + * + * @return object + * + * @since 1.4.0 + * @throws \DomainException + */ + public function remove($owner, $repo, $number, array $assignees) + { + // Build the request path. + $path = "/repos/$owner/$repo/issues/$number/assignees"; + + $data = json_encode( + [ + 'assignees' => $assignees, + ] + ); + + return $this->processResponse($this->client->delete($this->fetchUrl($path), [], null, $data)); + } } diff --git a/src/Package/Issues/Comments.php b/src/Package/Issues/Comments.php index a61005ea..81ed9f8c 100644 --- a/src/Package/Issues/Comments.php +++ b/src/Package/Issues/Comments.php @@ -1,4 +1,5 @@ fetchUrl($path, $page, $limit); - - if ($since) - { - $uri->setVar('since', $since->format(\DateTime::RFC3339)); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * List comments in a repository. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $sort The sort field - created or updated. - * @param string $direction The sort order- asc or desc. Ignored without sort parameter. - * @param \DateTimeInterface $since Only comments updated at or after this time are returned. - * - * @return object - * - * @since 1.0 - * @throws \UnexpectedValueException - * @throws \DomainException - */ - public function getRepositoryList($owner, $repo, $sort = 'created', $direction = 'asc', \DateTimeInterface $since = null) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/issues/comments'; - - if (\in_array($sort, ['created', 'updated']) == false) - { - throw new \UnexpectedValueException( - sprintf( - '%1$s - sort field must be "created" or "updated"', __METHOD__ - ) - ); - } - - if (\in_array($direction, ['asc', 'desc']) == false) - { - throw new \UnexpectedValueException( - sprintf( - '%1$s - direction field must be "asc" or "desc"', __METHOD__ - ) - ); - } - - $uri = $this->fetchUrl($path); - $uri->setVar('sort', $sort); - $uri->setVar('direction', $direction); - - if ($since) - { - $uri->setVar('since', $since->format(\DateTime::RFC3339)); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * Get a single comment. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id The comment id. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function get($owner, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/issues/comments/' . (int) $id; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Edit a comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $commentId The id of the comment to update. - * @param string $body The new body text for the comment. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function edit($user, $repo, $commentId, $body) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/issues/comments/' . (int) $commentId; - - // Build the request data. - $data = json_encode( - [ - 'body' => $body, - ] - ); - - // Send the request. - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), $data) - ); - } - - /** - * Create a comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $issueId The issue number. - * @param string $body The comment body text. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function create($user, $repo, $issueId, $body) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId . '/comments'; - - // Build the request data. - $data = json_encode( - [ - 'body' => $body, - ] - ); - - // Send the request. - return $this->processResponse( - $this->client->post($this->fetchUrl($path), $data), - 201 - ); - } - - /** - * Delete a comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $commentId The id of the comment to delete. - * - * @return boolean - * - * @since 1.0 - * @throws \DomainException - */ - public function delete($user, $repo, $commentId) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/issues/comments/' . (int) $commentId; - - // Send the request. - $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - - return true; - } + /** + * List comments on an issue. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $issueId The issue number. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * @param ?\DateTimeInterface $since Only comments updated at or after this time are returned. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getList($owner, $repo, $issueId, $page = 0, $limit = 0, \DateTimeInterface $since = null) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/' . (int) $issueId . '/comments'; + + $uri = $this->fetchUrl($path, $page, $limit); + + if ($since) { + $uri->setVar('since', $since->format(\DateTime::RFC3339)); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * List comments in a repository. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $sort The sort field - created or updated. + * @param string $direction The sort order- asc or desc. Ignored without sort parameter. + * @param ?\DateTimeInterface $since Only comments updated at or after this time are returned. + * + * @return object + * + * @since 1.0 + * @throws \UnexpectedValueException + * @throws \DomainException + */ + public function getRepositoryList($owner, $repo, $sort = 'created', $direction = 'asc', \DateTimeInterface $since = null) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/comments'; + + if (\in_array($sort, ['created', 'updated']) == false) { + throw new \UnexpectedValueException( + sprintf( + '%1$s - sort field must be "created" or "updated"', + __METHOD__ + ) + ); + } + + if (\in_array($direction, ['asc', 'desc']) == false) { + throw new \UnexpectedValueException( + sprintf( + '%1$s - direction field must be "asc" or "desc"', + __METHOD__ + ) + ); + } + + $uri = $this->fetchUrl($path); + $uri->setVar('sort', $sort); + $uri->setVar('direction', $direction); + + if ($since) { + $uri->setVar('since', $since->format(\DateTime::RFC3339)); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * Get a single comment. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id The comment id. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function get($owner, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/comments/' . (int) $id; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Edit a comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $commentId The id of the comment to update. + * @param string $body The new body text for the comment. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function edit($user, $repo, $commentId, $body) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/issues/comments/' . (int) $commentId; + + // Build the request data. + $data = json_encode( + [ + 'body' => $body, + ] + ); + + // Send the request. + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), $data) + ); + } + + /** + * Create a comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $issueId The issue number. + * @param string $body The comment body text. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function create($user, $repo, $issueId, $body) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId . '/comments'; + + // Build the request data. + $data = json_encode( + [ + 'body' => $body, + ] + ); + + // Send the request. + return $this->processResponse( + $this->client->post($this->fetchUrl($path), $data), + 201 + ); + } + + /** + * Delete a comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $commentId The id of the comment to delete. + * + * @return boolean + * + * @since 1.0 + * @throws \DomainException + */ + public function delete($user, $repo, $commentId) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/issues/comments/' . (int) $commentId; + + // Send the request. + $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + + return true; + } } diff --git a/src/Package/Issues/Events.php b/src/Package/Issues/Events.php index 204f3c38..ccf163ff 100644 --- a/src/Package/Issues/Events.php +++ b/src/Package/Issues/Events.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path, $page, $limit)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path, $page, $limit)) + ); + } - /** - * List events for a repository. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $issueId The issue number. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return object - */ - public function getListRepository($owner, $repo, $issueId, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/issues/' . (int) $issueId . '/comments'; + /** + * List events for a repository. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $issueId The issue number. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + */ + public function getListRepository($owner, $repo, $issueId, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/' . (int) $issueId . '/comments'; - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path, $page, $limit)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path, $page, $limit)) + ); + } - /** - * Get a single event. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id The event number. - * - * @return object - */ - public function get($owner, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/issues/events/' . (int) $id; + /** + * Get a single event. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id The event number. + * + * @return object + */ + public function get($owner, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/events/' . (int) $id; - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } } diff --git a/src/Package/Issues/Labels.php b/src/Package/Issues/Labels.php index e2f2187b..f47aae60 100644 --- a/src/Package/Issues/Labels.php +++ b/src/Package/Issues/Labels.php @@ -1,4 +1,5 @@ processResponse( - $response = $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $response = $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Get a single label. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $name The label name to get. - * - * @return object - * - * @since 1.0 - */ - public function get($user, $repo, $name) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/labels/' . rawurlencode($name); + /** + * Get a single label. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $name The label name to get. + * + * @return object + * + * @since 1.0 + */ + public function get($user, $repo, $name) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/labels/' . rawurlencode($name); - // Send the request. - return $this->processResponse( - $response = $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $response = $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Create a label. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $name The label name. - * @param string $color The label color. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function create($owner, $repo, $name, $color) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/labels'; + /** + * Create a label. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $name The label name. + * @param string $color The label color. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function create($owner, $repo, $name, $color) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/labels'; - // Build the request data. - $data = json_encode( - [ - 'name' => $name, - 'color' => $color, - ] - ); + // Build the request data. + $data = json_encode( + [ + 'name' => $name, + 'color' => $color, + ] + ); - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } - /** - * Delete a label. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $name The label name. - * - * @return object - * - * @since 1.0 - */ - public function delete($owner, $repo, $name) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/labels/' . rawurlencode($name); + /** + * Delete a label. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $name The label name. + * + * @return object + * + * @since 1.0 + */ + public function delete($owner, $repo, $name) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/labels/' . rawurlencode($name); - // Send the request. - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + // Send the request. + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } - /** - * Update a label. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $label The label name. - * @param string $name The new label name. - * @param string $color The new label color. - * - * @return object - * - * @since 1.0 - */ - public function update($user, $repo, $label, $name, $color) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/labels/' . $label; + /** + * Update a label. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $label The label name. + * @param string $name The new label name. + * @param string $color The new label color. + * + * @return object + * + * @since 1.0 + */ + public function update($user, $repo, $label, $name, $color) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/labels/' . $label; - // Build the request data. - $data = json_encode( - [ - 'name' => $name, - 'color' => $color, - ] - ); + // Build the request data. + $data = json_encode( + [ + 'name' => $name, + 'color' => $color, + ] + ); - // Send the request. - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), $data) - ); - } + // Send the request. + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), $data) + ); + } - /** - * List labels on an issue. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $number The issue number. - * - * @return object - * - * @since 1.0 - */ - public function getListByIssue($owner, $repo, $number) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels'; + /** + * List labels on an issue. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $number The issue number. + * + * @return object + * + * @since 1.0 + */ + public function getListByIssue($owner, $repo, $number) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels'; - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Add labels to an issue. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $number The issue number. - * @param array $labels An array of labels to add. - * - * @return object - * - * @since 1.0 - */ - public function add($owner, $repo, $number, array $labels) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels'; + /** + * Add labels to an issue. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $number The issue number. + * @param array $labels An array of labels to add. + * + * @return object + * + * @since 1.0 + */ + public function add($owner, $repo, $number, array $labels) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels'; - // Send the request. - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($labels)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($labels)) + ); + } - /** - * Remove a label from an issue. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $number The issue number. - * @param string $name The name of the label to remove. - * - * @return object - * - * @since 1.0 - */ - public function removeFromIssue($owner, $repo, $number, $name) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels/' . rawurlencode($name); + /** + * Remove a label from an issue. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $number The issue number. + * @param string $name The name of the label to remove. + * + * @return object + * + * @since 1.0 + */ + public function removeFromIssue($owner, $repo, $number, $name) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels/' . rawurlencode($name); - // Send the request. - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)) + ); + } - /** - * Replace all labels for an issue. - * - * Sending an empty array ([]) will remove all Labels from the Issue. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $number The issue number. - * @param array $labels New labels - * - * @return object - * - * @since 1.0 - */ - public function replace($owner, $repo, $number, array $labels) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels'; + /** + * Replace all labels for an issue. + * + * Sending an empty array ([]) will remove all Labels from the Issue. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $number The issue number. + * @param array $labels New labels + * + * @return object + * + * @since 1.0 + */ + public function replace($owner, $repo, $number, array $labels) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels'; - // Send the request. - return $this->processResponse( - $this->client->put($this->fetchUrl($path), json_encode($labels)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->put($this->fetchUrl($path), json_encode($labels)) + ); + } - /** - * Remove all labels from an issue. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $number The issue number. - * - * @return object - * - * @since 1.0 - */ - public function removeAllFromIssue($owner, $repo, $number) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels'; + /** + * Remove all labels from an issue. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $number The issue number. + * + * @return object + * + * @since 1.0 + */ + public function removeAllFromIssue($owner, $repo, $number) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/issues/' . $number . '/labels'; - // Send the request. - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + // Send the request. + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } - /** - * Get labels for every issue in a milestone. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $number The issue number. - * - * @return object - * - * @since 1.0 - */ - public function getListByMilestone($owner, $repo, $number) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/milestones/' . $number . '/labels'; + /** + * Get labels for every issue in a milestone. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $number The issue number. + * + * @return object + * + * @since 1.0 + */ + public function getListByMilestone($owner, $repo, $number) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/milestones/' . $number . '/labels'; - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } } diff --git a/src/Package/Issues/Milestones.php b/src/Package/Issues/Milestones.php index 675c7b35..ca604911 100644 --- a/src/Package/Issues/Milestones.php +++ b/src/Package/Issues/Milestones.php @@ -1,4 +1,5 @@ fetchUrl($path, $page, $limit); - $uri->setVar('state', $state); - $uri->setVar('sort', $sort); - $uri->setVar('direction', $direction); - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * Get a single milestone. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $milestoneId The milestone id to get. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function get($user, $repo, $milestoneId) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Create a milestone. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $title The title of the milestone. - * @param string $state Can be open (default) or closed. - * @param string $description Optional description for milestone. - * @param string $dueOn The milestone due date. This is a timestamp in ISO 8601 format. - * - * @return object - * - * @note As of 2.0 the $dueOn parameter will be typehinted to a \DateTime object - * @since 1.0 - * @throws \DomainException - */ - public function create($user, $repo, $title, $state = null, $description = null, $dueOn = null) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/milestones'; - - // Build the request data. - $data = [ - 'title' => $title, - ]; - - if ($state !== null) - { - $data['state'] = $state; - } - - if ($description !== null) - { - $data['description'] = $description; - } - - if ($dueOn !== null) - { - $data['due_on'] = $dueOn; - } - - $data = json_encode($data); - - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } - - /** - * Update a milestone. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $milestoneId The id of the comment to update. - * @param integer $title Optional title of the milestone. - * @param string $state Can be open (default) or closed. - * @param string $description Optional description for milestone. - * @param string $dueOn The milestone due date. This is a timestamp in ISO 8601 format. - * - * @return object - * - * @note As of 2.0 the $dueOn parameter will be typehinted to a \DateTime object - * @since 1.0 - * @throws \DomainException - */ - public function edit($user, $repo, $milestoneId, $title = null, $state = null, $description = null, $dueOn = null) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId; - - // Build the request data. - $data = []; - - if ($title !== null) - { - $data['title'] = $title; - } - - if ($state !== null) - { - $data['state'] = $state; - } - - if ($description !== null) - { - $data['description'] = $description; - } - - if ($dueOn !== null) - { - $data['due_on'] = $dueOn; - } - - $data = json_encode($data); - - // Send the request. - return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); - } - - /** - * Delete a milestone. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $milestoneId The id of the milestone to delete. - * - * @return void - * - * @since 1.0 - * @throws \DomainException - */ - public function delete($user, $repo, $milestoneId) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId; - - // Send the request. - $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); - } + /** + * List milestones for a repository. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $state The milestone state to retrieved. Open (default) or closed. + * @param string $sort Sort can be due_date (default) or completeness. + * @param string $direction Direction is asc or desc (default). + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getList($user, $repo, $state = 'open', $sort = 'due_date', $direction = 'desc', $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/milestones'; + + $uri = $this->fetchUrl($path, $page, $limit); + $uri->setVar('state', $state); + $uri->setVar('sort', $sort); + $uri->setVar('direction', $direction); + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * Get a single milestone. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $milestoneId The milestone id to get. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function get($user, $repo, $milestoneId) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Create a milestone. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $title The title of the milestone. + * @param string $state Can be open (default) or closed. + * @param string $description Optional description for milestone. + * @param string $dueOn The milestone due date. This is a timestamp in ISO 8601 format. + * + * @return object + * + * @note As of 2.0 the $dueOn parameter will be typehinted to a \DateTime object + * @since 1.0 + * @throws \DomainException + */ + public function create($user, $repo, $title, $state = null, $description = null, $dueOn = null) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/milestones'; + + // Build the request data. + $data = [ + 'title' => $title, + ]; + + if ($state !== null) { + $data['state'] = $state; + } + + if ($description !== null) { + $data['description'] = $description; + } + + if ($dueOn !== null) { + $data['due_on'] = $dueOn; + } + + $data = json_encode($data); + + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Update a milestone. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $milestoneId The id of the comment to update. + * @param integer $title Optional title of the milestone. + * @param string $state Can be open (default) or closed. + * @param string $description Optional description for milestone. + * @param string $dueOn The milestone due date. This is a timestamp in ISO 8601 format. + * + * @return object + * + * @note As of 2.0 the $dueOn parameter will be typehinted to a \DateTime object + * @since 1.0 + * @throws \DomainException + */ + public function edit($user, $repo, $milestoneId, $title = null, $state = null, $description = null, $dueOn = null) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId; + + // Build the request data. + $data = []; + + if ($title !== null) { + $data['title'] = $title; + } + + if ($state !== null) { + $data['state'] = $state; + } + + if ($description !== null) { + $data['description'] = $description; + } + + if ($dueOn !== null) { + $data['due_on'] = $dueOn; + } + + $data = json_encode($data); + + // Send the request. + return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); + } + + /** + * Delete a milestone. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $milestoneId The id of the milestone to delete. + * + * @return void + * + * @since 1.0 + * @throws \DomainException + */ + public function delete($user, $repo, $milestoneId) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId; + + // Send the request. + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } } diff --git a/src/Package/Markdown.php b/src/Package/Markdown.php index dddc7bd6..08296148 100644 --- a/src/Package/Markdown.php +++ b/src/Package/Markdown.php @@ -1,4 +1,5 @@ $text, - 'mode' => $mode, - 'context' => $context, - ] - ) - ); + // Build the request data. + $data = str_replace( + '\\/', + '/', + json_encode( + [ + 'text' => $text, + 'mode' => $mode, + 'context' => $context, + ] + ) + ); - // Send the request. - $response = $this->client->post($this->fetchUrl($path), $data); + // Send the request. + $response = $this->client->post($this->fetchUrl($path), $data); - // Validate the response code. - if ($response->code != 200) - { - // Decode the error response and throw an exception. - $error = json_decode($response->body); - $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; + // Validate the response code. + if ($response->code != 200) { + // Decode the error response and throw an exception. + $error = json_decode($response->body); + $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; - throw new UnexpectedResponseException($response, $message, $response->code); - } + throw new UnexpectedResponseException($response, $message, $response->code); + } - return $response->body; - } + return $response->body; + } } diff --git a/src/Package/Meta.php b/src/Package/Meta.php index c4a2c083..6c699a33 100644 --- a/src/Package/Meta.php +++ b/src/Package/Meta.php @@ -1,4 +1,5 @@ processResponse($this->client->get($this->fetchUrl($path)), 200); - } + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } } diff --git a/src/Package/Orgs.php b/src/Package/Orgs.php index fa7d6aac..e48dcff1 100644 --- a/src/Package/Orgs.php +++ b/src/Package/Orgs.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Get an organization. - * - * @param string $org The organization name. - * - * @return object - * - * @since 1.0 - */ - public function get($org) - { - // Build the request path. - $path = '/orgs/' . $org; + /** + * Get an organization. + * + * @param string $org The organization name. + * + * @return object + * + * @since 1.0 + */ + public function get($org) + { + // Build the request path. + $path = '/orgs/' . $org; - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Edit an organization. - * - * @param string $org The organization name. - * @param string $billingEmail Billing email address. This address is not publicized. - * @param string $company The company name. - * @param string $email The email address. - * @param string $location The location name. - * @param string $name The name. - * - * @return object - * - * @since 1.0 - */ - public function edit($org, $billingEmail = '', $company = '', $email = '', $location = '', $name = '') - { - // Build the request path. - $path = '/orgs/' . $org; + /** + * Edit an organization. + * + * @param string $org The organization name. + * @param string $billingEmail Billing email address. This address is not publicized. + * @param string $company The company name. + * @param string $email The email address. + * @param string $location The location name. + * @param string $name The name. + * + * @return object + * + * @since 1.0 + */ + public function edit($org, $billingEmail = '', $company = '', $email = '', $location = '', $name = '') + { + // Build the request path. + $path = '/orgs/' . $org; - $args = ['billing_email', 'company', 'email', 'location', 'name']; + $args = ['billing_email', 'company', 'email', 'location', 'name']; - $data = []; + $data = []; - $fArgs = \func_get_args(); + $fArgs = \func_get_args(); - foreach ($args as $i => $arg) - { - if (array_key_exists($i + 1, $fArgs) && $fArgs[$i + 1]) - { - $data[$arg] = $fArgs[$i + 1]; - } - } + foreach ($args as $i => $arg) { + if (array_key_exists($i + 1, $fArgs) && $fArgs[$i + 1]) { + $data[$arg] = $fArgs[$i + 1]; + } + } - // Send the request. - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), $data) - ); - } + // Send the request. + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), $data) + ); + } } diff --git a/src/Package/Orgs/Hooks.php b/src/Package/Orgs/Hooks.php index 77491347..23dc959d 100644 --- a/src/Package/Orgs/Hooks.php +++ b/src/Package/Orgs/Hooks.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Get single hook. - * - * @param string $org The name of the organization. - * @param integer $id The hook id. - * - * @return object - * - * @since 1.4.0 - */ - public function get($org, $id) - { - // Build the request path. - $path = "/orgs/$org/hooks/" . (int) $id; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Create a hook. - * - * @param string $org The name of the organization. - * @param string $url The URL to which the payloads will be delivered. - * @param string $contentType The media type used to serialize the payloads. Supported values include "json" and "form". - * @param string $secret If provided, payloads will be delivered with an X-Hub-Signature header. - * The value of this header is computed as the - * [HMAC hex digest of the body, using the secret as the key][hub-signature]. - * @param boolean $insecureSsl Determines whether the SSL certificate of the host for url will be verified when delivering payloads. - * If false, verification is performed. If true, verification is not performed. - * @param array $events Determines what events the hook is triggered for. - * @param boolean $active Determines whether the hook is actually triggered on pushes. - * - * @return object - * - * @since 1.4.0 - * @throws \UnexpectedValueException - */ - public function create($org, $url, $contentType = 'form', $secret = null, $insecureSsl = false, array $events = ['push'], $active = true) - { - // Build the request path. - $path = "/orgs/$org/hooks"; - - if (\in_array($contentType, ['form', 'json']) == false) - { - throw new \UnexpectedValueException('Content type must be either "form" or "json".'); - } - - $config = [ - 'url' => $url, - 'content_type' => $contentType, - 'insecure_ssl' => (int) $insecureSsl, - ]; - - if ($secret) - { - $config['secret'] = $secret; - } - - $data = [ - 'name' => 'web', - 'active' => $active, - 'config' => (object) $config, - ]; - - if (!empty($events)) - { - // Check to ensure all events are in the allowed list - foreach ($events as $event) - { - if (!\in_array($event, $this->hookEvents)) - { - throw new \RuntimeException('Your events array contains an unauthorized event.'); - } - } - - $data['events'] = $events; - } - - return $this->processResponse( - $this->client->post($this->fetchUrl($path), $data), - 201 - ); - } - - /** - * Edit a hook. - * - * @param string $org The name of the organization. - * @param string $url The URL to which the payloads will be delivered. - * @param string $contentType The media type used to serialize the payloads. Supported values include "json" and "form". - * @param string $secret If provided, payloads will be delivered with an X-Hub-Signature header. - * The value of this header is computed as the - * [HMAC hex digest of the body, using the secret as the key][hub-signature]. - * @param boolean $insecureSsl Determines whether the SSL certificate of the host for url will be verified when delivering payloads. - * If false, verification is performed. If true, verification is not performed. - * @param array $events Determines what events the hook is triggered for. - * @param boolean $active Determines whether the hook is actually triggered on pushes. - * - * @return object - * - * @since 1.4.0 - * @throws \UnexpectedValueException - */ - public function edit($org, $url, $contentType = null, $secret = null, $insecureSsl = null, array $events = [], $active = null) - { - // Build the request path. - $path = "/orgs/$org/hooks"; - - $config = [ - 'url' => $url, - ]; - - if ($contentType) - { - if (\in_array($contentType, ['form', 'json']) == false) - { - throw new \UnexpectedValueException('Content type must be either "form" or "json".'); - } - - $config['content_type'] = $contentType; - } - - if ($insecureSsl !== null) - { - $config['insecure_ssl'] = (int) $insecureSsl; - } - - if ($secret) - { - $config['secret'] = $secret; - } - - $data = [ - 'config' => (object) $config, - ]; - - if ($active !== null) - { - $data['active'] = (bool) $active; - } - - if (!empty($events)) - { - // Check to ensure all events are in the allowed list - foreach ($events as $event) - { - if (!\in_array($event, $this->hookEvents)) - { - throw new \RuntimeException('Your events array contains an unauthorized event.'); - } - } - - $data['events'] = $events; - } - - return $this->processResponse( - $this->client->post($this->fetchUrl($path), $data), - 201 - ); - } - - /** - * Ping a hook. - * - * @param string $org The name of the organization - * @param integer $id ID of the hook to ping - * - * @return object - * - * @since 1.4.0 - * @throws \DomainException - */ - public function ping($org, $id) - { - // Build the request path. - $path = "/orgs/$org/hooks/$id/pings"; - - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode('')), - 204 - ); - } - - /** - * Delete a hook. - * - * @param string $org The name of the organization - * @param integer $id ID of the hook to delete - * - * @return object - * - * @since 1.4.0 - */ - public function delete($org, $id) - { - // Build the request path. - $path = "/orgs/$org/hooks/$id"; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + /** + * List hooks. + * + * @param string $org The name of the organization. + * + * @return object + * + * @since 1.4.0 + */ + public function getList($org) + { + // Build the request path. + $path = "/orgs/$org/hooks"; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Get single hook. + * + * @param string $org The name of the organization. + * @param integer $id The hook id. + * + * @return object + * + * @since 1.4.0 + */ + public function get($org, $id) + { + // Build the request path. + $path = "/orgs/$org/hooks/" . (int) $id; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Create a hook. + * + * @param string $org The name of the organization. + * @param string $url The URL to which the payloads will be delivered. + * @param string $contentType The media type used to serialize the payloads. Supported values include "json" and "form". + * @param string $secret If provided, payloads will be delivered with an X-Hub-Signature header. + * The value of this header is computed as the + * [HMAC hex digest of the body, using the secret as the key][hub-signature]. + * @param boolean $insecureSsl Determines whether the SSL certificate of the host for url will be verified when delivering payloads. + * If false, verification is performed. If true, verification is not performed. + * @param array $events Determines what events the hook is triggered for. + * @param boolean $active Determines whether the hook is actually triggered on pushes. + * + * @return object + * + * @since 1.4.0 + * @throws \UnexpectedValueException + */ + public function create($org, $url, $contentType = 'form', $secret = null, $insecureSsl = false, array $events = ['push'], $active = true) + { + // Build the request path. + $path = "/orgs/$org/hooks"; + + if (\in_array($contentType, ['form', 'json']) == false) { + throw new \UnexpectedValueException('Content type must be either "form" or "json".'); + } + + $config = [ + 'url' => $url, + 'content_type' => $contentType, + 'insecure_ssl' => (int) $insecureSsl, + ]; + + if ($secret) { + $config['secret'] = $secret; + } + + $data = [ + 'name' => 'web', + 'active' => $active, + 'config' => (object) $config, + ]; + + if (!empty($events)) { + // Check to ensure all events are in the allowed list + foreach ($events as $event) { + if (!\in_array($event, $this->hookEvents)) { + throw new \RuntimeException('Your events array contains an unauthorized event.'); + } + } + + $data['events'] = $events; + } + + return $this->processResponse( + $this->client->post($this->fetchUrl($path), $data), + 201 + ); + } + + /** + * Edit a hook. + * + * @param string $org The name of the organization. + * @param string $url The URL to which the payloads will be delivered. + * @param string $contentType The media type used to serialize the payloads. Supported values include "json" and "form". + * @param string $secret If provided, payloads will be delivered with an X-Hub-Signature header. + * The value of this header is computed as the + * [HMAC hex digest of the body, using the secret as the key][hub-signature]. + * @param boolean $insecureSsl Determines whether the SSL certificate of the host for url will be verified when delivering payloads. + * If false, verification is performed. If true, verification is not performed. + * @param array $events Determines what events the hook is triggered for. + * @param boolean $active Determines whether the hook is actually triggered on pushes. + * + * @return object + * + * @since 1.4.0 + * @throws \UnexpectedValueException + */ + public function edit($org, $url, $contentType = null, $secret = null, $insecureSsl = null, array $events = [], $active = null) + { + // Build the request path. + $path = "/orgs/$org/hooks"; + + $config = [ + 'url' => $url, + ]; + + if ($contentType) { + if (\in_array($contentType, ['form', 'json']) == false) { + throw new \UnexpectedValueException('Content type must be either "form" or "json".'); + } + + $config['content_type'] = $contentType; + } + + if ($insecureSsl !== null) { + $config['insecure_ssl'] = (int) $insecureSsl; + } + + if ($secret) { + $config['secret'] = $secret; + } + + $data = [ + 'config' => (object) $config, + ]; + + if ($active !== null) { + $data['active'] = (bool) $active; + } + + if (!empty($events)) { + // Check to ensure all events are in the allowed list + foreach ($events as $event) { + if (!\in_array($event, $this->hookEvents)) { + throw new \RuntimeException('Your events array contains an unauthorized event.'); + } + } + + $data['events'] = $events; + } + + return $this->processResponse( + $this->client->post($this->fetchUrl($path), $data), + 201 + ); + } + + /** + * Ping a hook. + * + * @param string $org The name of the organization + * @param integer $id ID of the hook to ping + * + * @return object + * + * @since 1.4.0 + * @throws \DomainException + */ + public function ping($org, $id) + { + // Build the request path. + $path = "/orgs/$org/hooks/$id/pings"; + + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode('')), + 204 + ); + } + + /** + * Delete a hook. + * + * @param string $org The name of the organization + * @param integer $id ID of the hook to delete + * + * @return object + * + * @since 1.4.0 + */ + public function delete($org, $id) + { + // Build the request path. + $path = "/orgs/$org/hooks/$id"; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } } diff --git a/src/Package/Orgs/Members.php b/src/Package/Orgs/Members.php index 023344a3..5799305a 100644 --- a/src/Package/Orgs/Members.php +++ b/src/Package/Orgs/Members.php @@ -1,4 +1,5 @@ client->get($this->fetchUrl($path)); - - switch ($response->code) - { - case 302 : - // Requester is not an organization member. - return false; - - case 200 : - return json_decode($response->body); - - default : - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - } - - /** - * Check membership. - * - * Check if a user is, publicly or privately, a member of the organization. - * - * @param string $org The name of the organization. - * @param string $user The name of the user. - * - * @throws \UnexpectedValueException - * @since 1.0 - * - * @return boolean - */ - public function check($org, $user) - { - // Build the request path. - $path = '/orgs/' . $org . '/members/' . $user; - - $response = $this->client->get($this->fetchUrl($path)); - - switch ($response->code) - { - case 204 : - // Requester is an organization member and user is a member. - return true; - - case 404 : - // Requester is an organization member and user is not a member. - // Requester is not an organization member and is inquiring about themselves. - return false; - - case 302 : - // Requester is not an organization member. - return false; - - default : - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - } - - /** - * Add a member. - * - * To add someone as a member to an org, you must add them to a team. - */ - - /** - * Remove a member. - * - * Removing a user from this list will remove them from all teams and they will no longer have - * any access to the organization’s repositories. - * - * @param string $org The name of the organization. - * @param string $user The name of the user. - * - * @since 1.0 - * - * @return object - */ - public function remove($org, $user) - { - // Build the request path. - $path = '/orgs/' . $org . '/members/' . $user; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * Public members list. - * - * Members of an organization can choose to have their membership publicized or not. - * - * @param string $org The name of the organization. - * - * @since 1.0 - * - * @return object - */ - public function getListPublic($org) - { - // Build the request path. - $path = '/orgs/' . $org . '/public_members'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Check public membership. - * - * @param string $org The name of the organization. - * @param string $user The name of the user. - * - * @throws \UnexpectedValueException - * @since 1.0 - * - * @return boolean - */ - public function checkPublic($org, $user) - { - // Build the request path. - $path = '/orgs/' . $org . '/public_members/' . $user; - - $response = $this->client->get($this->fetchUrl($path)); - - switch ($response->code) - { - case 204 : - // Response if user is a public member. - return true; - - case 404 : - // Response if user is not a public member. - return false; - - default : - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - } - - /** - * Publicize a user’s membership. - * - * @param string $org The name of the organization. - * @param string $user The name of the user. - * - * @since 1.0 - * - * @return object - */ - public function publicize($org, $user) - { - // Build the request path. - $path = '/orgs/' . $org . '/public_members/' . $user; - - return $this->processResponse( - $this->client->put($this->fetchUrl($path), ''), - 204 - ); - } - - /** - * Conceal a user’s membership. - * - * @param string $org The name of the organization. - * @param string $user The name of the user. - * - * @since 1.0 - * - * @return object - */ - public function conceal($org, $user) - { - // Build the request path. - $path = '/orgs/' . $org . '/public_members/' . $user; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * Get organization membership - * - * In order to get a user's membership with an organization, the authenticated user must be an organization owner. - * - * @param string $org The name of the organization. - * @param string $user The name of the user. - * - * @return object - * - * @since 1.4.0 - */ - public function getMembership($org, $user) - { - // Build the request path. - $path = '/orgs/' . $org . '/memberships/' . $user; - - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Add or update organization membership - * - * In order to create or update a user's membership with an organization, the authenticated user must be an organization owner. - * - * @param string $org The name of the organization. - * @param string $user The name of the user. - * @param string $role The role to give the user in the organization. Can be either 'member' or 'admin'. - * - * @return object - * - * @since 1.4.0 - */ - public function updateMembership($org, $user, $role = 'member') - { - $allowedRoles = ['member', 'admin']; - - if (!\in_array($role, $allowedRoles)) - { - throw new \InvalidArgumentException(sprintf("The user's role must be: %s", implode(', ', $allowedRoles))); - } - - // Build the request path. - $path = "/orgs/$org/memberships/$user"; - - $data = [ - 'role' => $role, - ]; - - return $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data))); - } - - /** - * Remove organization membership - * - * In order to remove a user's membership with an organization, the authenticated user must be an organization owner. - * - * @param string $org The name of the organization. - * @param string $user The name of the user. - * - * @return object - * - * @since 1.4.0 - */ - public function removeMembership($org, $user) - { - // Build the request path. - $path = '/orgs/' . $org . '/memberships/' . $user; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * List your organization memberships - * - * @return object - * - * @since 1.4.0 - */ - public function listMemberships() - { - // Build the request path. - $path = '/user/memberships/orgs'; - - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Get your organization membership - * - * @param string $org The name of the organization. - * - * @return object - * - * @since 1.4.0 - */ - public function listOrganizationMembership($org) - { - // Build the request path. - $path = '/user/memberships/orgs/' . $org; - - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Edit your organization membership - * - * @param string $org The name of the organization. - * @param string $state The state that the membership should be in. - * - * @return object - * - * @since 1.4.0 - */ - public function editOrganizationMembership($org, $state) - { - // The API only accepts $state == 'active' at present - if ($state != 'active') - { - throw new \InvalidArgumentException('The state must be "active".'); - } - - // Build the request path. - $path = '/user/memberships/orgs/' . $org; - - return $this->processResponse($this->client->patch($this->fetchUrl($path), ['state' => $state])); - } + /** + * Members list. + * + * List all users who are members of an organization. + * A member is a user that belongs to at least 1 team in the organization. + * If the authenticated user is also a member of this organization then + * both concealed and public members will be returned. + * If the requester is not a member of the organization the query will be + * redirected to the public members list. + * + * @param string $org The name of the organization. + * + * @throws \UnexpectedValueException + * @since 1.0 + * + * @return boolean|mixed + */ + public function getList($org) + { + // Build the request path. + $path = '/orgs/' . $org . '/members'; + + $response = $this->client->get($this->fetchUrl($path)); + + switch ($response->code) { + case 302: + // Requester is not an organization member. + return false; + + case 200: + return json_decode($response->body); + + default: + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + } + + /** + * Check membership. + * + * Check if a user is, publicly or privately, a member of the organization. + * + * @param string $org The name of the organization. + * @param string $user The name of the user. + * + * @throws \UnexpectedValueException + * @since 1.0 + * + * @return boolean + */ + public function check($org, $user) + { + // Build the request path. + $path = '/orgs/' . $org . '/members/' . $user; + + $response = $this->client->get($this->fetchUrl($path)); + + switch ($response->code) { + case 204: + // Requester is an organization member and user is a member. + return true; + + case 404: + // Requester is an organization member and user is not a member. + // Requester is not an organization member and is inquiring about themselves. + return false; + + case 302: + // Requester is not an organization member. + return false; + + default: + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + } + + /** + * Add a member. + * + * To add someone as a member to an org, you must add them to a team. + */ + + /** + * Remove a member. + * + * Removing a user from this list will remove them from all teams and they will no longer have + * any access to the organization’s repositories. + * + * @param string $org The name of the organization. + * @param string $user The name of the user. + * + * @since 1.0 + * + * @return object + */ + public function remove($org, $user) + { + // Build the request path. + $path = '/orgs/' . $org . '/members/' . $user; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * Public members list. + * + * Members of an organization can choose to have their membership publicized or not. + * + * @param string $org The name of the organization. + * + * @since 1.0 + * + * @return object + */ + public function getListPublic($org) + { + // Build the request path. + $path = '/orgs/' . $org . '/public_members'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Check public membership. + * + * @param string $org The name of the organization. + * @param string $user The name of the user. + * + * @throws \UnexpectedValueException + * @since 1.0 + * + * @return boolean + */ + public function checkPublic($org, $user) + { + // Build the request path. + $path = '/orgs/' . $org . '/public_members/' . $user; + + $response = $this->client->get($this->fetchUrl($path)); + + switch ($response->code) { + case 204: + // Response if user is a public member. + return true; + + case 404: + // Response if user is not a public member. + return false; + + default: + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + } + + /** + * Publicize a user’s membership. + * + * @param string $org The name of the organization. + * @param string $user The name of the user. + * + * @since 1.0 + * + * @return object + */ + public function publicize($org, $user) + { + // Build the request path. + $path = '/orgs/' . $org . '/public_members/' . $user; + + return $this->processResponse( + $this->client->put($this->fetchUrl($path), ''), + 204 + ); + } + + /** + * Conceal a user’s membership. + * + * @param string $org The name of the organization. + * @param string $user The name of the user. + * + * @since 1.0 + * + * @return object + */ + public function conceal($org, $user) + { + // Build the request path. + $path = '/orgs/' . $org . '/public_members/' . $user; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * Get organization membership + * + * In order to get a user's membership with an organization, the authenticated user must be an organization owner. + * + * @param string $org The name of the organization. + * @param string $user The name of the user. + * + * @return object + * + * @since 1.4.0 + */ + public function getMembership($org, $user) + { + // Build the request path. + $path = '/orgs/' . $org . '/memberships/' . $user; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Add or update organization membership + * + * In order to create or update a user's membership with an organization, the authenticated user must be an organization owner. + * + * @param string $org The name of the organization. + * @param string $user The name of the user. + * @param string $role The role to give the user in the organization. Can be either 'member' or 'admin'. + * + * @return object + * + * @since 1.4.0 + */ + public function updateMembership($org, $user, $role = 'member') + { + $allowedRoles = ['member', 'admin']; + + if (!\in_array($role, $allowedRoles)) { + throw new \InvalidArgumentException(sprintf("The user's role must be: %s", implode(', ', $allowedRoles))); + } + + // Build the request path. + $path = "/orgs/$org/memberships/$user"; + + $data = [ + 'role' => $role, + ]; + + return $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data))); + } + + /** + * Remove organization membership + * + * In order to remove a user's membership with an organization, the authenticated user must be an organization owner. + * + * @param string $org The name of the organization. + * @param string $user The name of the user. + * + * @return object + * + * @since 1.4.0 + */ + public function removeMembership($org, $user) + { + // Build the request path. + $path = '/orgs/' . $org . '/memberships/' . $user; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * List your organization memberships + * + * @return object + * + * @since 1.4.0 + */ + public function listMemberships() + { + // Build the request path. + $path = '/user/memberships/orgs'; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Get your organization membership + * + * @param string $org The name of the organization. + * + * @return object + * + * @since 1.4.0 + */ + public function listOrganizationMembership($org) + { + // Build the request path. + $path = '/user/memberships/orgs/' . $org; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Edit your organization membership + * + * @param string $org The name of the organization. + * @param string $state The state that the membership should be in. + * + * @return object + * + * @since 1.4.0 + */ + public function editOrganizationMembership($org, $state) + { + // The API only accepts $state == 'active' at present + if ($state != 'active') { + throw new \InvalidArgumentException('The state must be "active".'); + } + + // Build the request path. + $path = '/user/memberships/orgs/' . $org; + + return $this->processResponse($this->client->patch($this->fetchUrl($path), ['state' => $state])); + } } diff --git a/src/Package/Orgs/Teams.php b/src/Package/Orgs/Teams.php index d42885bc..a1640c91 100644 --- a/src/Package/Orgs/Teams.php +++ b/src/Package/Orgs/Teams.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Get team. - * - * @param integer $id The team id. - * - * @return object - * - * @since 1.0 - */ - public function get($id) - { - // Build the request path. - $path = '/teams/' . (int) $id; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Create team. - * - * In order to create a team, the authenticated user must be an owner of the organization. - * - * @param string $org The name of the organization. - * @param string $name The name of the team. - * @param array $repoNames Repository names. - * @param string $permission The permission. (Deprecated) - * pull - team members can pull, but not push to or administer these repositories. Default - * push - team members can pull and push, but not administer these repositories. - * admin - team members can pull, push and administer these repositories. - * - * @return object - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function create($org, $name, array $repoNames = [], $permission = '') - { - // Build the request path. - $path = '/orgs/' . $org . '/teams'; - - $data = [ - 'name' => $name, - ]; - - if ($repoNames) - { - $data['repo_names'] = $repoNames; - } - - if ($permission) - { - if (\in_array($permission, ['pull', 'push', 'admin']) == false) - { - throw new \UnexpectedValueException('Permissions must be either "pull", "push", or "admin".'); - } - - $data['permission'] = $permission; - } - - return $this->processResponse( - $this->client->post($this->fetchUrl($path), $data), - 201 - ); - } - - /** - * Edit team. - * - * In order to edit a team, the authenticated user must be an owner of the org that the team is associated with. - * - * @param integer $id The team id. - * @param string $name The name of the team. - * @param string $permission The permission. (Deprecated) - * pull - team members can pull, but not push to or administer these repositories. Default - * push - team members can pull and push, but not administer these repositories. - * admin - team members can pull, push and administer these repositories. - * - * @return object - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function edit($id, $name, $permission = '') - { - // Build the request path. - $path = '/teams/' . (int) $id; - - $data = [ - 'name' => $name, - ]; - - if ($permission) - { - if (\in_array($permission, ['pull', 'push', 'admin']) == false) - { - throw new \UnexpectedValueException('Permissions must be either "pull", "push", or "admin".'); - } - - $data['permission'] = $permission; - } - - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), $data) - ); - } - - /** - * Delete team. - * - * In order to delete a team, the authenticated user must be an owner of the org that the team is associated with. - * - * @param integer $id The team id. - * - * @return object - * - * @since 1.0 - */ - public function delete($id) - { - // Build the request path. - $path = '/teams/' . $id; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * List team members. - * - * In order to list members in a team, the authenticated user must be a member of the team. - * - * @param integer $id The team id. - * - * @return object - * - * @since 1.0 - */ - public function getListMembers($id) - { - // Build the request path. - $path = '/teams/' . $id . '/members'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Get team member. - * - * In order to get if a user is a member of a team, the authenticated user must be a member of the team. - * - * @param integer $id The team id. - * @param string $user The name of the user. - * - * @return boolean - * - * @since 1.0 - * @throws \UnexpectedValueException - * @deprecated Use getTeamMembership() instead - */ - public function isMember($id, $user) - { - // Build the request path. - $path = '/teams/' . $id . '/members/' . $user; - - $response = $this->client->get($this->fetchUrl($path)); - - switch ($response->code) - { - case 204 : - // Response if user is a member - return true; - - case 404 : - // Response if user is not a member - return false; - - default : - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - } - - /** - * Add team member. - * - * In order to add a user to a team, the authenticated user must have ‘admin’ permissions - * to the team or be an owner of the org that the team is associated with. - * - * @param integer $id The team id. - * @param string $user The name of the user. - * - * @return object - * - * @since 1.0 - * @deprecated Use addTeamMembership() instead - */ - public function addMember($id, $user) - { - // Build the request path. - $path = '/teams/' . $id . '/members/' . $user; - - return $this->processResponse( - $this->client->put($this->fetchUrl($path), ''), - 204 - ); - } - - /** - * Remove team member. - * - * In order to remove a user from a team, the authenticated user must have ‘admin’ permissions - * to the team or be an owner of the org that the team is associated with. - * NOTE: This does not delete the user, it just remove them from the team. - * - * @param integer $id The team id. - * @param string $user The name of the user. - * - * @return object - * - * @since 1.0 - * @deprecated Use removeTeamMembership() instead - */ - public function removeMember($id, $user) - { - // Build the request path. - $path = '/teams/' . $id . '/members/' . $user; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * Get team membership - * - * In order to get a user's membership with a team, the team must be visible to the authenticated user. - * - * @param integer $id The team id. - * @param string $user The name of the user. - * - * @return string|boolean The state the user's membership is in or boolean false if the user is not a member. - * - * @since 1.4.0 - * @throws \UnexpectedValueException - */ - public function getTeamMembership($id, $user) - { - // Build the request path. - $path = "/teams/$id/memberships/$user"; - - $response = $this->client->get($this->fetchUrl($path)); - - switch ($response->code) - { - case 200 : - // Response if user is an active member or pending membership - $body = json_decode($response->body); - - return $body->state; - - case 404 : - // Response if user is not a member - return false; - - default : - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - } - - /** - * Add team membership - * - * If the user is already a member of the team's organization, this endpoint will add the user to the team. - * In order to add a membership between an organization member and a team, the authenticated user must be - * an organization owner or a maintainer of the team. - * - * @param integer $id The team id. - * @param string $user The name of the user. - * @param string $role The role the user should have on the team. Can be either 'member' or 'maintainer'. - * - * @return object - * - * @since 1.4.0 - * @throws \UnexpectedValueException - */ - public function addTeamMembership($id, $user, $role = 'member') - { - // Build the request path. - $path = "/teams/$id/memberships/$user"; - - if (\in_array($role, ['member', 'maintainer']) == false) - { - throw new \UnexpectedValueException('Roles must be either "member" or "maintainer".'); - } - - $data = [ - 'role' => $role, - ]; - - return $this->processResponse($this->client->put($this->fetchUrl($path), $data)); - } - - /** - * Remove team membership - * - * In order to remove a membership between a user and a team, the authenticated user must have 'admin' permissions to the team - * or be an owner of the organization that the team is associated with. - * NOTE: This does not delete the user, it just removes their membership from the team. - * - * @param integer $id The team id. - * @param string $user The name of the user. - * - * @return object - * - * @since 1.4.0 - * @throws \UnexpectedValueException - */ - public function removeTeamMembership($id, $user) - { - // Build the request path. - $path = "/teams/$id/memberships/$user"; - - return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); - } - - /** - * List team repos. - * - * @param integer $id The team id. - * - * @return object - * - * @since 1.0 - */ - public function getListRepos($id) - { - // Build the request path. - $path = '/teams/' . $id . '/repos'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Check if a team manages a repository. - * - * @param integer $id The team id. - * @param string $owner The owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * - * @return boolean - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function checkRepo($id, $owner, $repo) - { - // Build the request path. - $path = '/teams/' . $id . '/repos/' . $owner . '/' . $repo; - - $response = $this->client->get($this->fetchUrl($path)); - - switch ($response->code) - { - case 204 : - // Response if repo is managed by this team. - return true; - - case 404 : - // Response if repo is not managed by this team. - return false; - - default : - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - } - - /** - * Add or update team repository. - * - * In order to add a repo to a team, the authenticated user must be an owner of the - * org that the team is associated with. Also, the repo must be owned by the organization, - * or a direct form of a repo owned by the organization. - * - * If you attempt to add a repo to a team that is not owned by the organization, you get: - * Status: 422 Unprocessable Entity - * - * @param integer $id The team id. - * @param string $org The name of the organization of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * - * @return object - * - * @since 1.0 - */ - public function addRepo($id, $org, $repo) - { - // Build the request path. - $path = '/teams/' . $id . '/repos/' . $org . '/' . $repo; - - return $this->processResponse( - $this->client->put($this->fetchUrl($path), ''), - 204 - ); - } - - /** - * Remove team repository. - * - * In order to remove a repo from a team, the authenticated user must be an owner - * of the org that the team is associated with. NOTE: This does not delete the - * repo, it just removes it from the team. - * - * @param integer $id The team id. - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * - * @return object - * - * @since 1.0 - */ - public function removeRepo($id, $owner, $repo) - { - // Build the request path. - $path = '/teams/' . (int) $id . '/repos/' . $owner . '/' . $repo; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * List user teams. - * - * List all of the teams across all of the organizations to which the authenticated user belongs. - * This method requires user, repo, or read:org scope when authenticating via OAuth. - * - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return object - * - * @since 1.4.0 - */ - public function getUserTeams($page = 0, $limit = 0) - { - // Build the request path. - $path = '/user/teams'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path, $page, $limit)) - ); - } + /** + * List teams. + * + * @param string $org The name of the organization. + * + * @return object + * + * @since 1.0 + */ + public function getList($org) + { + // Build the request path. + $path = '/orgs/' . $org . '/teams'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Get team. + * + * @param integer $id The team id. + * + * @return object + * + * @since 1.0 + */ + public function get($id) + { + // Build the request path. + $path = '/teams/' . (int) $id; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Create team. + * + * In order to create a team, the authenticated user must be an owner of the organization. + * + * @param string $org The name of the organization. + * @param string $name The name of the team. + * @param array $repoNames Repository names. + * @param string $permission The permission. (Deprecated) + * pull - team members can pull, but not push to or administer these repositories. Default + * push - team members can pull and push, but not administer these repositories. + * admin - team members can pull, push and administer these repositories. + * + * @return object + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function create($org, $name, array $repoNames = [], $permission = '') + { + // Build the request path. + $path = '/orgs/' . $org . '/teams'; + + $data = [ + 'name' => $name, + ]; + + if ($repoNames) { + $data['repo_names'] = $repoNames; + } + + if ($permission) { + if (\in_array($permission, ['pull', 'push', 'admin']) == false) { + throw new \UnexpectedValueException('Permissions must be either "pull", "push", or "admin".'); + } + + $data['permission'] = $permission; + } + + return $this->processResponse( + $this->client->post($this->fetchUrl($path), $data), + 201 + ); + } + + /** + * Edit team. + * + * In order to edit a team, the authenticated user must be an owner of the org that the team is associated with. + * + * @param integer $id The team id. + * @param string $name The name of the team. + * @param string $permission The permission. (Deprecated) + * pull - team members can pull, but not push to or administer these repositories. Default + * push - team members can pull and push, but not administer these repositories. + * admin - team members can pull, push and administer these repositories. + * + * @return object + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function edit($id, $name, $permission = '') + { + // Build the request path. + $path = '/teams/' . (int) $id; + + $data = [ + 'name' => $name, + ]; + + if ($permission) { + if (\in_array($permission, ['pull', 'push', 'admin']) == false) { + throw new \UnexpectedValueException('Permissions must be either "pull", "push", or "admin".'); + } + + $data['permission'] = $permission; + } + + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), $data) + ); + } + + /** + * Delete team. + * + * In order to delete a team, the authenticated user must be an owner of the org that the team is associated with. + * + * @param integer $id The team id. + * + * @return object + * + * @since 1.0 + */ + public function delete($id) + { + // Build the request path. + $path = '/teams/' . $id; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * List team members. + * + * In order to list members in a team, the authenticated user must be a member of the team. + * + * @param integer $id The team id. + * + * @return object + * + * @since 1.0 + */ + public function getListMembers($id) + { + // Build the request path. + $path = '/teams/' . $id . '/members'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Get team member. + * + * In order to get if a user is a member of a team, the authenticated user must be a member of the team. + * + * @param integer $id The team id. + * @param string $user The name of the user. + * + * @return boolean + * + * @since 1.0 + * @throws \UnexpectedValueException + * @deprecated Use getTeamMembership() instead + */ + public function isMember($id, $user) + { + // Build the request path. + $path = '/teams/' . $id . '/members/' . $user; + + $response = $this->client->get($this->fetchUrl($path)); + + switch ($response->code) { + case 204: + // Response if user is a member + return true; + + case 404: + // Response if user is not a member + return false; + + default: + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + } + + /** + * Add team member. + * + * In order to add a user to a team, the authenticated user must have ‘admin’ permissions + * to the team or be an owner of the org that the team is associated with. + * + * @param integer $id The team id. + * @param string $user The name of the user. + * + * @return object + * + * @since 1.0 + * @deprecated Use addTeamMembership() instead + */ + public function addMember($id, $user) + { + // Build the request path. + $path = '/teams/' . $id . '/members/' . $user; + + return $this->processResponse( + $this->client->put($this->fetchUrl($path), ''), + 204 + ); + } + + /** + * Remove team member. + * + * In order to remove a user from a team, the authenticated user must have ‘admin’ permissions + * to the team or be an owner of the org that the team is associated with. + * NOTE: This does not delete the user, it just remove them from the team. + * + * @param integer $id The team id. + * @param string $user The name of the user. + * + * @return object + * + * @since 1.0 + * @deprecated Use removeTeamMembership() instead + */ + public function removeMember($id, $user) + { + // Build the request path. + $path = '/teams/' . $id . '/members/' . $user; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * Get team membership + * + * In order to get a user's membership with a team, the team must be visible to the authenticated user. + * + * @param integer $id The team id. + * @param string $user The name of the user. + * + * @return string|boolean The state the user's membership is in or boolean false if the user is not a member. + * + * @since 1.4.0 + * @throws \UnexpectedValueException + */ + public function getTeamMembership($id, $user) + { + // Build the request path. + $path = "/teams/$id/memberships/$user"; + + $response = $this->client->get($this->fetchUrl($path)); + + switch ($response->code) { + case 200: + // Response if user is an active member or pending membership + $body = json_decode($response->body); + + return $body->state; + + case 404: + // Response if user is not a member + return false; + + default: + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + } + + /** + * Add team membership + * + * If the user is already a member of the team's organization, this endpoint will add the user to the team. + * In order to add a membership between an organization member and a team, the authenticated user must be + * an organization owner or a maintainer of the team. + * + * @param integer $id The team id. + * @param string $user The name of the user. + * @param string $role The role the user should have on the team. Can be either 'member' or 'maintainer'. + * + * @return object + * + * @since 1.4.0 + * @throws \UnexpectedValueException + */ + public function addTeamMembership($id, $user, $role = 'member') + { + // Build the request path. + $path = "/teams/$id/memberships/$user"; + + if (\in_array($role, ['member', 'maintainer']) == false) { + throw new \UnexpectedValueException('Roles must be either "member" or "maintainer".'); + } + + $data = [ + 'role' => $role, + ]; + + return $this->processResponse($this->client->put($this->fetchUrl($path), $data)); + } + + /** + * Remove team membership + * + * In order to remove a membership between a user and a team, the authenticated user must have 'admin' permissions to the team + * or be an owner of the organization that the team is associated with. + * NOTE: This does not delete the user, it just removes their membership from the team. + * + * @param integer $id The team id. + * @param string $user The name of the user. + * + * @return object + * + * @since 1.4.0 + * @throws \UnexpectedValueException + */ + public function removeTeamMembership($id, $user) + { + // Build the request path. + $path = "/teams/$id/memberships/$user"; + + return $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } + + /** + * List team repos. + * + * @param integer $id The team id. + * + * @return object + * + * @since 1.0 + */ + public function getListRepos($id) + { + // Build the request path. + $path = '/teams/' . $id . '/repos'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Check if a team manages a repository. + * + * @param integer $id The team id. + * @param string $owner The owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * + * @return boolean + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function checkRepo($id, $owner, $repo) + { + // Build the request path. + $path = '/teams/' . $id . '/repos/' . $owner . '/' . $repo; + + $response = $this->client->get($this->fetchUrl($path)); + + switch ($response->code) { + case 204: + // Response if repo is managed by this team. + return true; + + case 404: + // Response if repo is not managed by this team. + return false; + + default: + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + } + + /** + * Add or update team repository. + * + * In order to add a repo to a team, the authenticated user must be an owner of the + * org that the team is associated with. Also, the repo must be owned by the organization, + * or a direct form of a repo owned by the organization. + * + * If you attempt to add a repo to a team that is not owned by the organization, you get: + * Status: 422 Unprocessable Entity + * + * @param integer $id The team id. + * @param string $org The name of the organization of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * + * @return object + * + * @since 1.0 + */ + public function addRepo($id, $org, $repo) + { + // Build the request path. + $path = '/teams/' . $id . '/repos/' . $org . '/' . $repo; + + return $this->processResponse( + $this->client->put($this->fetchUrl($path), ''), + 204 + ); + } + + /** + * Remove team repository. + * + * In order to remove a repo from a team, the authenticated user must be an owner + * of the org that the team is associated with. NOTE: This does not delete the + * repo, it just removes it from the team. + * + * @param integer $id The team id. + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * + * @return object + * + * @since 1.0 + */ + public function removeRepo($id, $owner, $repo) + { + // Build the request path. + $path = '/teams/' . (int) $id . '/repos/' . $owner . '/' . $repo; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * List user teams. + * + * List all of the teams across all of the organizations to which the authenticated user belongs. + * This method requires user, repo, or read:org scope when authenticating via OAuth. + * + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.4.0 + */ + public function getUserTeams($page = 0, $limit = 0) + { + // Build the request path. + $path = '/user/teams'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path, $page, $limit)) + ); + } } diff --git a/src/Package/Pulls.php b/src/Package/Pulls.php index 7ddb61ff..69cc63ae 100644 --- a/src/Package/Pulls.php +++ b/src/Package/Pulls.php @@ -1,4 +1,5 @@ $title, - 'base' => $base, - 'head' => $head, - 'body' => $body, - ] - ); - - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } - - /** - * Method to create a pull request from an existing issue. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $issueId The issue number for which to attach the new pull request. - * @param string $base The branch (or git ref) you want your changes pulled into. This - * should be an existing branch on the current repository. You cannot - * submit a pull request to one repo that requests a merge to a base - * of another repo. - * @param string $head The branch (or git ref) where your changes are implemented. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function createFromIssue($user, $repo, $issueId, $base, $head) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls'; - - // Build the request data. - $data = json_encode( - [ - 'issue' => (int) $issueId, - 'base' => $base, - 'head' => $head, - ] - ); - - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } - - /** - * Update a pull request. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $pullId The pull request number. - * @param string $title The optional new title for the pull request. - * @param string $body The optional new body text for the pull request. - * @param string $state The optional new state for the pull request. [open, closed] - * @param string $base The optional new base branch for the pull request. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function edit($user, $repo, $pullId, $title = null, $body = null, $state = null, $base = null) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId; - - // Create the data object. - $data = new \stdClass; - - // If a title is set add it to the data object. - if (isset($title)) - { - $data->title = $title; - } - - // If a body is set add it to the data object. - if (isset($body)) - { - $data->body = $body; - } - - // If a state is set add it to the data object. - if (isset($state)) - { - $data->state = $state; - } - - // If a base branch is set add it to the data object. - if (isset($base)) - { - $data->base = $base; - } - - // Encode the request data. - $data = json_encode($data); - - // Send the request. - return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); - } - - /** - * Get a single pull request. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $pullId The pull request number. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function get($user, $repo, $pullId) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * List commits on a pull request. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $pullId The pull request number. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function getCommits($user, $repo, $pullId, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/commits'; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); - } - - /** - * List pull requests files. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $pullId The pull request number. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function getFiles($user, $repo, $pullId, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/files'; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); - } - - /** - * List pull requests. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $state The optional state to filter requests by. [open, closed] - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return array - * - * @since 1.0 - * @throws \DomainException - */ - public function getList($user, $repo, $state = 'open', $page = 0, $limit = 0) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls'; - - // If a state exists append it as an option. - if ($state != 'open') - { - $path .= '?state=' . $state; - } - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); - } - - /** - * Get if a pull request has been merged. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $pullId The pull request number. The pull request number. - * - * @return boolean True if the pull request has been merged - * - * @since 1.0 - * @throws UnexpectedResponseException - */ - public function isMerged($user, $repo, $pullId) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/merge'; - - // Send the request. - $response = $this->client->get($this->fetchUrl($path)); - - // Validate the response code. - if ($response->code == 204) - { - return true; - } - - if ($response->code == 404) - { - return false; - } - - // Decode the error response and throw an exception. - $error = json_decode($response->body); - $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; - - throw new UnexpectedResponseException($response, $message, $response->code); - } - - /** - * Merge a pull request (Merge Button). - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $pullId The pull request number. - * @param string $message The message that will be used for the merge commit. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function merge($user, $repo, $pullId, $message = '') - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/merge'; - - // Build the request data. - $data = json_encode( - [ - 'commit_message' => $message, - ] - ); - - // Send the request. - return $this->processResponse($this->client->put($this->fetchUrl($path), $data)); - } + /** + * Create a pull request. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $title The title of the new pull request. + * @param string $base The branch (or git ref) you want your changes pulled into. This + * should be an existing branch on the current repository. You cannot + * submit a pull request to one repo that requests a merge to a base + * of another repo. + * @param string $head The branch (or git ref) where your changes are implemented. + * @param string $body The body text for the new pull request. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function create($user, $repo, $title, $base, $head, $body = '') + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls'; + + // Build the request data. + $data = json_encode( + [ + 'title' => $title, + 'base' => $base, + 'head' => $head, + 'body' => $body, + ] + ); + + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Method to create a pull request from an existing issue. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $issueId The issue number for which to attach the new pull request. + * @param string $base The branch (or git ref) you want your changes pulled into. This + * should be an existing branch on the current repository. You cannot + * submit a pull request to one repo that requests a merge to a base + * of another repo. + * @param string $head The branch (or git ref) where your changes are implemented. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function createFromIssue($user, $repo, $issueId, $base, $head) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls'; + + // Build the request data. + $data = json_encode( + [ + 'issue' => (int) $issueId, + 'base' => $base, + 'head' => $head, + ] + ); + + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Update a pull request. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $pullId The pull request number. + * @param string $title The optional new title for the pull request. + * @param string $body The optional new body text for the pull request. + * @param string $state The optional new state for the pull request. [open, closed] + * @param string $base The optional new base branch for the pull request. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function edit($user, $repo, $pullId, $title = null, $body = null, $state = null, $base = null) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId; + + // Create the data object. + $data = new \stdClass(); + + // If a title is set add it to the data object. + if (isset($title)) { + $data->title = $title; + } + + // If a body is set add it to the data object. + if (isset($body)) { + $data->body = $body; + } + + // If a state is set add it to the data object. + if (isset($state)) { + $data->state = $state; + } + + // If a base branch is set add it to the data object. + if (isset($base)) { + $data->base = $base; + } + + // Encode the request data. + $data = json_encode($data); + + // Send the request. + return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); + } + + /** + * Get a single pull request. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $pullId The pull request number. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function get($user, $repo, $pullId) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * List commits on a pull request. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $pullId The pull request number. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getCommits($user, $repo, $pullId, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/commits'; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } + + /** + * List pull requests files. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $pullId The pull request number. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getFiles($user, $repo, $pullId, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/files'; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } + + /** + * List pull requests. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $state The optional state to filter requests by. [open, closed] + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array + * + * @since 1.0 + * @throws \DomainException + */ + public function getList($user, $repo, $state = 'open', $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls'; + + // If a state exists append it as an option. + if ($state != 'open') { + $path .= '?state=' . $state; + } + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } + + /** + * Get if a pull request has been merged. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $pullId The pull request number. The pull request number. + * + * @return boolean True if the pull request has been merged + * + * @since 1.0 + * @throws UnexpectedResponseException + */ + public function isMerged($user, $repo, $pullId) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/merge'; + + // Send the request. + $response = $this->client->get($this->fetchUrl($path)); + + // Validate the response code. + if ($response->code == 204) { + return true; + } + + if ($response->code == 404) { + return false; + } + + // Decode the error response and throw an exception. + $error = json_decode($response->body); + $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; + + throw new UnexpectedResponseException($response, $message, $response->code); + } + + /** + * Merge a pull request (Merge Button). + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $pullId The pull request number. + * @param string $message The message that will be used for the merge commit. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function merge($user, $repo, $pullId, $message = '') + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/merge'; + + // Build the request data. + $data = json_encode( + [ + 'commit_message' => $message, + ] + ); + + // Send the request. + return $this->processResponse($this->client->put($this->fetchUrl($path), $data)); + } } diff --git a/src/Package/Pulls/Comments.php b/src/Package/Pulls/Comments.php index 8dc02185..dae14a45 100644 --- a/src/Package/Pulls/Comments.php +++ b/src/Package/Pulls/Comments.php @@ -1,4 +1,5 @@ $body, - 'commit_id' => $commitId, - 'path' => $filePath, - 'position' => $position, - ] - ); - - // Send the request. - return $this->processResponse( - $this->client->post($this->fetchUrl($path), $data), - 201 - ); - } - - /** - * Method to create a comment in reply to another comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $pullId The pull request number. - * @param string $body The comment body text. - * @param integer $inReplyTo The id of the comment to reply to. - * - * @since 1.0 - * - * @return object - */ - public function createReply($user, $repo, $pullId, $body, $inReplyTo) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/comments'; - - // Build the request data. - $data = json_encode( - [ - 'body' => $body, - 'in_reply_to' => (int) $inReplyTo, - ] - ); - - // Send the request. - return $this->processResponse( - $this->client->post($this->fetchUrl($path), $data), - 201 - ); - } - - /** - * Delete a comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $commentId The id of the comment to delete. - * - * @since 1.0 - * - * @return void - */ - public function delete($user, $repo, $commentId) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/comments/' . (int) $commentId; - - // Send the request. - $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * Edit a comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $commentId The id of the comment to update. - * @param string $body The new body text for the comment. - * - * @since 1.0 - * - * @return object - */ - public function edit($user, $repo, $commentId, $body) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/comments/' . (int) $commentId; - - // Build the request data. - $data = json_encode( - [ - 'body' => $body, - ] - ); - - // Send the request. - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), $data) - ); - } - - /** - * Get a single comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $commentId The comment id to get. - * - * @since 1.0 - * - * @return object - */ - public function get($user, $repo, $commentId) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/comments/' . (int) $commentId; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List comments on a pull request. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $pullId The pull request number. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @since 1.0 - * - * @return array - */ - public function getList($user, $repo, $pullId, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/comments'; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path, $page, $limit)) - ); - } - - /** - * List comments in a repository. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return array - * - * @since 1.4.0 - */ - public function getListForRepo($user, $repo, $page = 0, $limit = 0) - { - // Build the request path. - $path = "/repos/$user/$repo/pulls/comments"; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path, $page, $limit)) - ); - } + /** + * Create a comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $pullId The pull request number. + * @param string $body The comment body text. + * @param string $commitId The SHA1 hash of the commit to comment on. + * @param string $filePath The Relative path of the file to comment on. + * @param string $position The line index in the diff to comment on. + * + * @since 1.0 + * + * @return object + */ + public function create($user, $repo, $pullId, $body, $commitId, $filePath, $position) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/comments'; + + // Build the request data. + $data = json_encode( + [ + 'body' => $body, + 'commit_id' => $commitId, + 'path' => $filePath, + 'position' => $position, + ] + ); + + // Send the request. + return $this->processResponse( + $this->client->post($this->fetchUrl($path), $data), + 201 + ); + } + + /** + * Method to create a comment in reply to another comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $pullId The pull request number. + * @param string $body The comment body text. + * @param integer $inReplyTo The id of the comment to reply to. + * + * @since 1.0 + * + * @return object + */ + public function createReply($user, $repo, $pullId, $body, $inReplyTo) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/comments'; + + // Build the request data. + $data = json_encode( + [ + 'body' => $body, + 'in_reply_to' => (int) $inReplyTo, + ] + ); + + // Send the request. + return $this->processResponse( + $this->client->post($this->fetchUrl($path), $data), + 201 + ); + } + + /** + * Delete a comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $commentId The id of the comment to delete. + * + * @since 1.0 + * + * @return void + */ + public function delete($user, $repo, $commentId) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/comments/' . (int) $commentId; + + // Send the request. + $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * Edit a comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $commentId The id of the comment to update. + * @param string $body The new body text for the comment. + * + * @since 1.0 + * + * @return object + */ + public function edit($user, $repo, $commentId, $body) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/comments/' . (int) $commentId; + + // Build the request data. + $data = json_encode( + [ + 'body' => $body, + ] + ); + + // Send the request. + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), $data) + ); + } + + /** + * Get a single comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $commentId The comment id to get. + * + * @since 1.0 + * + * @return object + */ + public function get($user, $repo, $commentId) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/comments/' . (int) $commentId; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List comments on a pull request. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $pullId The pull request number. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @since 1.0 + * + * @return array + */ + public function getList($user, $repo, $pullId, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/pulls/' . (int) $pullId . '/comments'; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path, $page, $limit)) + ); + } + + /** + * List comments in a repository. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array + * + * @since 1.4.0 + */ + public function getListForRepo($user, $repo, $page = 0, $limit = 0) + { + // Build the request path. + $path = "/repos/$user/$repo/pulls/comments"; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path, $page, $limit)) + ); + } } diff --git a/src/Package/Repositories.php b/src/Package/Repositories.php index cc8ab7f9..c79bd231 100644 --- a/src/Package/Repositories.php +++ b/src/Package/Repositories.php @@ -1,4 +1,5 @@ fetchUrl('/user/repos'); - $uri->setVar('type', $type); - $uri->setVar('sort', $sort); - $uri->setVar('direction', $direction); - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * List user repositories. - * - * List public repositories for the specified user. - * - * @param string $user The user name. - * @param string $type Sort type. all, owner, member. Default: all. - * @param string $sort Sort field. created, updated, pushed, full_name, default: full_name. - * @param string $direction Sort direction. asc or desc, default: when using full_name: asc, otherwise desc. - * - * @return object - * - * @since 1.0 - * @throws \RuntimeException - */ - public function getListUser($user, $type = 'all', $sort = 'full_name', $direction = '') - { - if (\in_array($type, ['all', 'owner', 'member']) == false) - { - throw new \RuntimeException('Invalid type'); - } - - if (\in_array($sort, ['created', 'updated', 'pushed', 'full_name']) == false) - { - throw new \RuntimeException('Invalid sort field'); - } - - // Sort direction default: when using full_name: asc, otherwise desc. - $direction = $direction ?: ($sort == 'full_name' ? 'asc' : 'desc'); - - if (\in_array($direction, ['asc', 'desc']) == false) - { - throw new \RuntimeException('Invalid sort order'); - } - - // Build the request path. - $uri = $this->fetchUrl('/users/' . $user . '/repos'); - $uri->setVar('type', $type); - $uri->setVar('sort', $sort); - $uri->setVar('direction', $direction); - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * List organization repositories. - * - * List repositories for the specified org. - * - * @param string $org The name of the organization. - * @param string $type Sort type. all, public, private, forks, sources, member. Default: all. - * - * @return object - * - * @since 1.0 - * @throws \RuntimeException - */ - public function getListOrg($org, $type = 'all') - { - if (\in_array($type, ['all', 'public', 'private', 'forks', 'sources', 'member']) == false) - { - throw new \RuntimeException('Invalid type'); - } - - // Build the request path. - $uri = $this->fetchUrl('/orgs/' . $org . '/repos'); - $uri->setVar('type', $type); - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * List all public repositories. - * - * This provides a dump of every repository, in the order that they were created. - * - * @param integer $id The integer ID of the last Repository that you’ve seen. - * - * @return object - * - * @since 1.0 - * @throws \RuntimeException - */ - public function getList($id = 0) - { - // Build the request path. - $uri = $this->fetchUrl('/repositories'); - - if ($id) - { - $uri->setVar('since', (int) $id); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * Create. - * - * Create a new repository for the authenticated user or an organization. OAuth users must supply repo scope. - * - * @param string $name The repository name. - * @param string $org The organization name (if needed). - * @param string $description The repository description. - * @param string $homepage The repository homepage. - * @param boolean $private Set true to create a private repository, false to create a public one. Creating private repositories - * requires a paid GitHub account. - * @param boolean $hasIssues Set true to enable issues for this repository, false to disable them. - * @param boolean $hasWiki Set true to enable the wiki for this repository, false to disable it. - * @param boolean $hasDownloads Set true to enable downloads for this repository, false to disable them. - * @param integer $teamId The id of the team that will be granted access to this repository. This is only valid when creating a - * repo in an organization. - * @param boolean $autoInit true to create an initial commit with empty README. - * @param string $gitignoreTemplate Desired language or platform .gitignore template to apply. Use the name of the template without the - * extension. For example, “Haskell” Ignored if auto_init parameter is not provided. - * - * @return object - * - * @since 1.0 - */ - public function create($name, $org = '', $description = '', $homepage = '', $private = false, $hasIssues = false, $hasWiki = false, - $hasDownloads = false, $teamId = 0, $autoInit = false, $gitignoreTemplate = '' - ) - { - $path = ($org) - // Create a repository for an organization - ? '/orgs/' . $org . '/repos' - // Create a repository for a user - : '/user/repos'; - - $data = [ - 'name' => $name, - 'description' => $description, - 'homepage' => $homepage, - 'private' => $private, - 'has_issues' => $hasIssues, - 'has_wiki' => $hasWiki, - 'has_downloads' => $hasDownloads, - 'team_id' => $teamId, - 'auto_init' => $autoInit, - 'gitignore_template' => $gitignoreTemplate, - ]; - - // Send the request. - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($data)), - 201 - ); - } - - /** - * Get. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function get($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Edit. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * @param string $name The repository name. - * @param string $description The repository description. - * @param string $homepage The repository homepage. - * @param boolean $private Set true to create a private repository, false to create a public one. Creating private repositories - * requires a paid GitHub account. - * @param boolean $hasIssues Set true to enable issues for this repository, false to disable them. - * @param boolean $hasWiki Set true to enable the wiki for this repository, false to disable it. - * @param boolean $hasDownloads Set true to enable downloads for this repository, false to disable them. - * @param string $defaultBranch Update the default branch for this repository - * - * @return object - * - * @since 1.0 - */ - public function edit($owner, $repo, $name, $description = '', $homepage = '', $private = false, $hasIssues = false, $hasWiki = false, - $hasDownloads = false, $defaultBranch = '' - ) - { - $path = '/repos/' . $owner . '/' . $repo; - - $data = [ - 'name' => $name, - 'description' => $description, - 'homepage' => $homepage, - 'private' => $private, - 'has_issues' => $hasIssues, - 'has_wiki' => $hasWiki, - 'has_downloads' => $hasDownloads, - 'default_branch' => $defaultBranch, - ]; - - // Send the request. - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), json_encode($data)) - ); - } - - /** - * List contributors. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * @param boolean $anon Set to 1 or true to include anonymous contributors in results. - * - * @return object - * - * @since 1.0 - */ - public function getListContributors($owner, $repo, $anon = false) - { - // Build the request path. - $uri = $this->fetchUrl('/repos/' . $owner . '/' . $repo . '/contributors'); - - if ($anon) - { - $uri->setVar('anon', 'true'); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * List languages. - * - * List languages for the specified repository. The value on the right of a language is the number of bytes of code - * written in that language. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function getListLanguages($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/languages'; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List Teams - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function getListTeams($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/teams'; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List Tags. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function getListTags($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/tags'; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Delete a Repository. - * - * Deleting a repository requires admin access. If OAuth is used, the delete_repo scope is required. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * - * @return object - * - * @since 1.0 - */ - public function delete($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo; - - // Send the request. - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)) - ); - } + /** + * List your repositories. + * + * List repositories for the authenticated user. + * + * @param string $type Sort type. all, owner, public, private, member. Default: all. + * @param string $sort Sort field. created, updated, pushed, full_name, default: full_name. + * @param string $direction Sort direction. asc or desc, default: when using full_name: asc, otherwise desc. + * + * @return object + * + * @since 1.0 + * @throws \RuntimeException + */ + public function getListOwn($type = 'all', $sort = 'full_name', $direction = '') + { + if (\in_array($type, ['all', 'owner', 'public', 'private', 'member']) == false) { + throw new \RuntimeException('Invalid type'); + } + + if (\in_array($sort, ['created', 'updated', 'pushed', 'full_name']) == false) { + throw new \RuntimeException('Invalid sort field'); + } + + // Sort direction default: when using full_name: asc, otherwise desc. + $direction = ($direction) ?: (($sort == 'full_name') ? 'asc' : 'desc'); + + if (\in_array($direction, ['asc', 'desc']) == false) { + throw new \RuntimeException('Invalid sort order'); + } + + // Build the request path. + $uri = $this->fetchUrl('/user/repos'); + $uri->setVar('type', $type); + $uri->setVar('sort', $sort); + $uri->setVar('direction', $direction); + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * List user repositories. + * + * List public repositories for the specified user. + * + * @param string $user The user name. + * @param string $type Sort type. all, owner, member. Default: all. + * @param string $sort Sort field. created, updated, pushed, full_name, default: full_name. + * @param string $direction Sort direction. asc or desc, default: when using full_name: asc, otherwise desc. + * + * @return object + * + * @since 1.0 + * @throws \RuntimeException + */ + public function getListUser($user, $type = 'all', $sort = 'full_name', $direction = '') + { + if (\in_array($type, ['all', 'owner', 'member']) == false) { + throw new \RuntimeException('Invalid type'); + } + + if (\in_array($sort, ['created', 'updated', 'pushed', 'full_name']) == false) { + throw new \RuntimeException('Invalid sort field'); + } + + // Sort direction default: when using full_name: asc, otherwise desc. + $direction = $direction ?: ($sort == 'full_name' ? 'asc' : 'desc'); + + if (\in_array($direction, ['asc', 'desc']) == false) { + throw new \RuntimeException('Invalid sort order'); + } + + // Build the request path. + $uri = $this->fetchUrl('/users/' . $user . '/repos'); + $uri->setVar('type', $type); + $uri->setVar('sort', $sort); + $uri->setVar('direction', $direction); + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * List organization repositories. + * + * List repositories for the specified org. + * + * @param string $org The name of the organization. + * @param string $type Sort type. all, public, private, forks, sources, member. Default: all. + * + * @return object + * + * @since 1.0 + * @throws \RuntimeException + */ + public function getListOrg($org, $type = 'all') + { + if (\in_array($type, ['all', 'public', 'private', 'forks', 'sources', 'member']) == false) { + throw new \RuntimeException('Invalid type'); + } + + // Build the request path. + $uri = $this->fetchUrl('/orgs/' . $org . '/repos'); + $uri->setVar('type', $type); + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * List all public repositories. + * + * This provides a dump of every repository, in the order that they were created. + * + * @param integer $id The integer ID of the last Repository that you’ve seen. + * + * @return object + * + * @since 1.0 + * @throws \RuntimeException + */ + public function getList($id = 0) + { + // Build the request path. + $uri = $this->fetchUrl('/repositories'); + + if ($id) { + $uri->setVar('since', (int) $id); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * Create. + * + * Create a new repository for the authenticated user or an organization. OAuth users must supply repo scope. + * + * @param string $name The repository name. + * @param string $org The organization name (if needed). + * @param string $description The repository description. + * @param string $homepage The repository homepage. + * @param boolean $private Set true to create a private repository, false to create a public one. Creating private repositories + * requires a paid GitHub account. + * @param boolean $hasIssues Set true to enable issues for this repository, false to disable them. + * @param boolean $hasWiki Set true to enable the wiki for this repository, false to disable it. + * @param boolean $hasDownloads Set true to enable downloads for this repository, false to disable them. + * @param integer $teamId The id of the team that will be granted access to this repository. This is only valid when creating a + * repo in an organization. + * @param boolean $autoInit true to create an initial commit with empty README. + * @param string $gitignoreTemplate Desired language or platform .gitignore template to apply. Use the name of the template without the + * extension. For example, “Haskell” Ignored if auto_init parameter is not provided. + * + * @return object + * + * @since 1.0 + */ + public function create( + $name, + $org = '', + $description = '', + $homepage = '', + $private = false, + $hasIssues = false, + $hasWiki = false, + $hasDownloads = false, + $teamId = 0, + $autoInit = false, + $gitignoreTemplate = '' + ) { + $path = ($org) + // Create a repository for an organization + ? '/orgs/' . $org . '/repos' + // Create a repository for a user + : '/user/repos'; + + $data = [ + 'name' => $name, + 'description' => $description, + 'homepage' => $homepage, + 'private' => $private, + 'has_issues' => $hasIssues, + 'has_wiki' => $hasWiki, + 'has_downloads' => $hasDownloads, + 'team_id' => $teamId, + 'auto_init' => $autoInit, + 'gitignore_template' => $gitignoreTemplate, + ]; + + // Send the request. + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($data)), + 201 + ); + } + + /** + * Get. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function get($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Edit. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * @param string $name The repository name. + * @param string $description The repository description. + * @param string $homepage The repository homepage. + * @param boolean $private Set true to create a private repository, false to create a public one. Creating private repositories + * requires a paid GitHub account. + * @param boolean $hasIssues Set true to enable issues for this repository, false to disable them. + * @param boolean $hasWiki Set true to enable the wiki for this repository, false to disable it. + * @param boolean $hasDownloads Set true to enable downloads for this repository, false to disable them. + * @param string $defaultBranch Update the default branch for this repository + * + * @return object + * + * @since 1.0 + */ + public function edit( + $owner, + $repo, + $name, + $description = '', + $homepage = '', + $private = false, + $hasIssues = false, + $hasWiki = false, + $hasDownloads = false, + $defaultBranch = '' + ) { + $path = '/repos/' . $owner . '/' . $repo; + + $data = [ + 'name' => $name, + 'description' => $description, + 'homepage' => $homepage, + 'private' => $private, + 'has_issues' => $hasIssues, + 'has_wiki' => $hasWiki, + 'has_downloads' => $hasDownloads, + 'default_branch' => $defaultBranch, + ]; + + // Send the request. + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), json_encode($data)) + ); + } + + /** + * List contributors. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * @param boolean $anon Set to 1 or true to include anonymous contributors in results. + * + * @return object + * + * @since 1.0 + */ + public function getListContributors($owner, $repo, $anon = false) + { + // Build the request path. + $uri = $this->fetchUrl('/repos/' . $owner . '/' . $repo . '/contributors'); + + if ($anon) { + $uri->setVar('anon', 'true'); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * List languages. + * + * List languages for the specified repository. The value on the right of a language is the number of bytes of code + * written in that language. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function getListLanguages($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/languages'; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List Teams + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function getListTeams($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/teams'; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List Tags. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function getListTags($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/tags'; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Delete a Repository. + * + * Deleting a repository requires admin access. If OAuth is used, the delete_repo scope is required. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * + * @return object + * + * @since 1.0 + */ + public function delete($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo; + + // Send the request. + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)) + ); + } } diff --git a/src/Package/Repositories/Branches.php b/src/Package/Repositories/Branches.php index 1a5c485c..ce6ab844 100644 --- a/src/Package/Repositories/Branches.php +++ b/src/Package/Repositories/Branches.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Get Branch. - * - * @param string $owner Repository owner. - * @param string $repo Repository name. - * @param string $branch Branch name. - * - * @return object - * - * @since 1.4.0 - */ - public function get($owner, $repo, $branch) - { - // Build the request path. - $path = "/repos/$owner/$repo/branches/$branch"; + /** + * Get Branch. + * + * @param string $owner Repository owner. + * @param string $repo Repository name. + * @param string $branch Branch name. + * + * @return object + * + * @since 1.4.0 + */ + public function get($owner, $repo, $branch) + { + // Build the request path. + $path = "/repos/$owner/$repo/branches/$branch"; - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } } diff --git a/src/Package/Repositories/Collaborators.php b/src/Package/Repositories/Collaborators.php index e3cd324e..e9fcdc75 100644 --- a/src/Package/Repositories/Collaborators.php +++ b/src/Package/Repositories/Collaborators.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Check if a user is a collaborator. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $user The name of the GitHub user. - * - * @throws \UnexpectedValueException - * @since 1.0 - * - * @return boolean - */ - public function get($owner, $repo, $user) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/collaborators/' . $user; + /** + * Check if a user is a collaborator. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $user The name of the GitHub user. + * + * @throws \UnexpectedValueException + * @since 1.0 + * + * @return boolean + */ + public function get($owner, $repo, $user) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/collaborators/' . $user; - $response = $this->client->get($this->fetchUrl($path)); + $response = $this->client->get($this->fetchUrl($path)); - switch ($response->code) - { - case '204' : - return true; + switch ($response->code) { + case '204': + return true; - case '404' : - return false; + case '404': + return false; - default : - throw new \UnexpectedValueException('Unexpected code: ' . $response->code); - } - } + default: + throw new \UnexpectedValueException('Unexpected code: ' . $response->code); + } + } - /** - * Add user as a collaborator. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $user The name of the GitHub user. - * - * @since 1.0 - * - * @return object - */ - public function add($owner, $repo, $user) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/collaborators/' . $user; + /** + * Add user as a collaborator. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $user The name of the GitHub user. + * + * @since 1.0 + * + * @return object + */ + public function add($owner, $repo, $user) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/collaborators/' . $user; - return $this->processResponse( - $this->client->put($this->fetchUrl($path), ''), - 204 - ); - } + return $this->processResponse( + $this->client->put($this->fetchUrl($path), ''), + 204 + ); + } - /** - * Remove user as a collaborator. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $user The name of the GitHub user. - * - * @since 1.0 - * - * @return object - */ - public function remove($owner, $repo, $user) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/collaborators/' . $user; + /** + * Remove user as a collaborator. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $user The name of the GitHub user. + * + * @since 1.0 + * + * @return object + */ + public function remove($owner, $repo, $user) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/collaborators/' . $user; - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } } diff --git a/src/Package/Repositories/Comments.php b/src/Package/Repositories/Comments.php index 986c0672..5f807cd5 100644 --- a/src/Package/Repositories/Comments.php +++ b/src/Package/Repositories/Comments.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path, $page, $limit)) - ); - } - - /** - * List comments for a single commit. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $sha The SHA of the commit to retrieve. - * @param integer $page Page to request - * @param integer $limit Number of results to return per page - * - * @return array - * - * @since 1.0 - */ - public function getList($user, $repo, $sha, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments'; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path, $page, $limit)) - ); - } - - /** - * Get a single commit comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id ID of the comment to retrieve - * - * @return array - * - * @since 1.0 - */ - public function get($user, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/comments/' . (int) $id; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Update a commit comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $id The ID of the comment to edit. - * @param string $comment The text of the comment. - * - * @return object - * - * @since 1.0 - */ - public function edit($user, $repo, $id, $comment) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id; - - $data = json_encode( - [ - 'body' => $comment, - ] - ); - - // Send the request. - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), $data) - ); - } - - /** - * Delete a commit comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $id The ID of the comment to edit. - * - * @return object - * - * @since 1.0 - */ - public function delete($user, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id; - - // Send the request. - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * Create a commit comment. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $sha The SHA of the commit to comment on. - * @param string $comment The text of the comment. - * @param integer $line The line number of the commit to comment on. - * @param string $filepath A relative path to the file to comment on within the commit. - * @param integer $position Line index in the diff to comment on. - * - * @return object - * - * @since 1.0 - */ - public function create($user, $repo, $sha, $comment, $line, $filepath, $position) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments'; - - $data = json_encode( - [ - 'body' => $comment, - 'path' => $filepath, - 'position' => (int) $position, - 'line' => (int) $line, - ] - ); - - // Send the request. - return $this->processResponse( - $this->client->post($this->fetchUrl($path), $data), - 201 - ); - } + /** + * List commit comments for a repository. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $page Page to request + * @param integer $limit Number of results to return per page + * + * @return array + * + * @since 1.0 + */ + public function getListRepository($user, $repo, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/comments'; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path, $page, $limit)) + ); + } + + /** + * List comments for a single commit. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $sha The SHA of the commit to retrieve. + * @param integer $page Page to request + * @param integer $limit Number of results to return per page + * + * @return array + * + * @since 1.0 + */ + public function getList($user, $repo, $sha, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments'; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path, $page, $limit)) + ); + } + + /** + * Get a single commit comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id ID of the comment to retrieve + * + * @return array + * + * @since 1.0 + */ + public function get($user, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/comments/' . (int) $id; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Update a commit comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $id The ID of the comment to edit. + * @param string $comment The text of the comment. + * + * @return object + * + * @since 1.0 + */ + public function edit($user, $repo, $id, $comment) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id; + + $data = json_encode( + [ + 'body' => $comment, + ] + ); + + // Send the request. + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), $data) + ); + } + + /** + * Delete a commit comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $id The ID of the comment to edit. + * + * @return object + * + * @since 1.0 + */ + public function delete($user, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id; + + // Send the request. + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * Create a commit comment. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $sha The SHA of the commit to comment on. + * @param string $comment The text of the comment. + * @param integer $line The line number of the commit to comment on. + * @param string $filepath A relative path to the file to comment on within the commit. + * @param integer $position Line index in the diff to comment on. + * + * @return object + * + * @since 1.0 + */ + public function create($user, $repo, $sha, $comment, $line, $filepath, $position) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments'; + + $data = json_encode( + [ + 'body' => $comment, + 'path' => $filepath, + 'position' => (int) $position, + 'line' => (int) $line, + ] + ); + + // Send the request. + return $this->processResponse( + $this->client->post($this->fetchUrl($path), $data), + 201 + ); + } } diff --git a/src/Package/Repositories/Commits.php b/src/Package/Repositories/Commits.php index e4ac6289..2e1dc50d 100644 --- a/src/Package/Repositories/Commits.php +++ b/src/Package/Repositories/Commits.php @@ -1,4 +1,5 @@ fetchUrl($rPath); - - if ($sha) - { - $uri->setVar('sha', $sha); - } - - if ($path) - { - $uri->setVar('path', $path); - } - - if ($author) - { - $uri->setVar('author', $author); - } - - if ($since) - { - $uri->setVar('since', $since->format(\DateTime::RFC3339)); - } - - if ($until) - { - $uri->setVar('until', $until->format(\DateTime::RFC3339)); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * Get a single commit. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $sha The SHA of the commit to retrieve. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function get($user, $repo, $sha) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Get the SHA-1 of a commit reference. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $ref The commit reference - * - * @return string - * - * @since 1.4.0 - * @throws UnexpectedResponseException - */ - public function getSha($user, $repo, $ref) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/commits/' . $ref; - - // Send the request. - $response = $this->client->get($this->fetchUrl($path)); - - // Validate the response code. - if ($response->code != 200) - { - // Decode the error response and throw an exception. - $error = json_decode($response->body); - $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; - - throw new UnexpectedResponseException($response, $message, $response->code); - } - - return $response->body; - } - - /** - * Compare two commits. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $base The base of the diff, either a commit SHA or branch. - * @param string $head The head of the diff, either a commit SHA or branch. - * - * @return object - * - * @since 1.0 - */ - public function compare($user, $repo, $base, $head) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/compare/' . $base . '...' . $head; - - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + /** + * List commits on a repository. + * + * A special note on pagination: Due to the way Git works, commits are paginated based on SHA + * instead of page number. + * Please follow the link headers as outlined in the pagination overview instead of constructing + * page links yourself. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $sha Sha or branch to start listing commits from. + * @param string $path Only commits containing this file path will be returned. + * @param string $author GitHub login, name, or email by which to filter by commit author. + * @param ?\DateTimeInterface $since ISO 8601 Date - Only commits after this date will be returned. + * @param ?\DateTimeInterface $until ISO 8601 Date - Only commits before this date will be returned. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getList($user, $repo, $sha = '', $path = '', $author = '', \DateTimeInterface $since = null, \DateTimeInterface $until = null) + { + // Build the request path. + $rPath = '/repos/' . $user . '/' . $repo . '/commits'; + + $uri = $this->fetchUrl($rPath); + + if ($sha) { + $uri->setVar('sha', $sha); + } + + if ($path) { + $uri->setVar('path', $path); + } + + if ($author) { + $uri->setVar('author', $author); + } + + if ($since) { + $uri->setVar('since', $since->format(\DateTime::RFC3339)); + } + + if ($until) { + $uri->setVar('until', $until->format(\DateTime::RFC3339)); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * Get a single commit. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $sha The SHA of the commit to retrieve. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function get($user, $repo, $sha) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Get the SHA-1 of a commit reference. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $ref The commit reference + * + * @return string + * + * @since 1.4.0 + * @throws UnexpectedResponseException + */ + public function getSha($user, $repo, $ref) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/commits/' . $ref; + + // Send the request. + $response = $this->client->get($this->fetchUrl($path)); + + // Validate the response code. + if ($response->code != 200) { + // Decode the error response and throw an exception. + $error = json_decode($response->body); + $message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; + + throw new UnexpectedResponseException($response, $message, $response->code); + } + + return $response->body; + } + + /** + * Compare two commits. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $base The base of the diff, either a commit SHA or branch. + * @param string $head The head of the diff, either a commit SHA or branch. + * + * @return object + * + * @since 1.0 + */ + public function compare($user, $repo, $base, $head) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/compare/' . $base . '...' . $head; + + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } } diff --git a/src/Package/Repositories/Contents.php b/src/Package/Repositories/Contents.php index 64cfe1ed..f863714d 100644 --- a/src/Package/Repositories/Contents.php +++ b/src/Package/Repositories/Contents.php @@ -1,4 +1,5 @@ fetchUrl($path); - - if ($ref) - { - $uri->setVar('ref', $ref); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * Get contents. - * - * This method returns the contents of any file or directory in a repository. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $path The content path. - * @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. - * - * @return object - * - * @since 1.0 - */ - public function get($owner, $repo, $path, $ref = '') - { - // Build the request path. - $rPath = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; - - $uri = $this->fetchUrl($rPath); - - if ($ref) - { - $uri->setVar('ref', $ref); - } - - // Send the request. - return $this->processResponse($this->client->get($uri)); - } - - /** - * Get archive link. - * - * This method will return a 302 to a URL to download a tarball or zipball archive for a repository. - * Please make sure your HTTP framework is configured to follow redirects or you will need to use the - * Location header to make a second GET request. - * - * Note: For private repositories, these links are temporary and expire quickly. - * - * To follow redirects with curl, use the -L switch: - * curl -L https://api.github.com/repos/pengwynn/octokit/tarball > octokit.tar.gz - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $archiveFormat Either tarball or zipball. - * @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. - * - * @return object - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function getArchiveLink($owner, $repo, $archiveFormat = 'zipball', $ref = '') - { - if (\in_array($archiveFormat, ['tarball', 'zipball']) == false) - { - throw new \UnexpectedValueException('Archive format must be either "tarball" or "zipball".'); - } - - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/' . $archiveFormat; - $uri = $this->fetchUrl($path); - - if ($ref) - { - $uri->setVar('ref', $ref); - } - - // Send the request. - return $this->processResponse($this->client->get($uri), 302); - } - - /** - * Create a file. - * - * This method creates a new file in a repository. - * - * Optional Parameters - * The author section is optional and is filled in with the committer information if omitted. - * If the committer information is omitted, the authenticated user’s information is used. - * - * You must provide values for both name and email, whether you choose to use author or committer. - * Otherwise, you’ll receive a 500 status code. - * - * @param string $owner The owner of the repository. - * @param string $repo The repository name. - * @param string $path The content path. - * @param string $message The commit message. - * @param string $content The new file content, Base64 encoded. - * @param string $branch The branch name. If not provided, uses the repository’s default branch (usually master). - * @param string $authorName The name of the author of the commit - * @param string $authorEmail The email of the author of the commit - * @param string $committerName The name of the committer of the commit - * @param string $committerEmail The email of the committer of the commit - * - * @return object - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function create($owner, $repo, $path, $message, $content, $branch = 'master', $authorName = '', $authorEmail = '', $committerName = '', - $committerEmail = '' - ) - { - // Build the request path. - $route = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; - - $data = [ - 'message' => $message, - 'content' => $content, - 'branch' => $branch, - ]; - - if ($authorName) - { - if (!$authorEmail) - { - throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); - } - - $data['author'] = [ - 'name' => $authorName, - 'email' => $authorEmail, - ]; - } - - if ($committerName) - { - if (!$committerEmail) - { - throw new \UnexpectedValueException('You must provide a committer e-mail if you supply a committer name'); - } - - $data['committer'] = [ - 'name' => $committerName, - 'email' => $committerEmail, - ]; - } - - return $this->processResponse($this->client->put($this->fetchUrl($route), json_encode($data)), 201); - } - - /** - * Update a file. - * - * This method updates a file in a repository. - * - * Optional Parameters - * The author section is optional and is filled in with the committer information if omitted. - * If the committer information is omitted, the authenticated user’s information is used. - * - * You must provide values for both name and email, whether you choose to use author or committer. - * Otherwise, you’ll receive a 500 status code. - * - * @param string $owner The owner of the repository. - * @param string $repo The repository name. - * @param string $path The content path. - * @param string $message The commit message. - * @param string $content The new file content, Base64 encoded. - * @param string $sha The blob SHA of the file being replaced. - * @param string $branch The branch name. If not provided, uses the repository’s default branch (usually master). - * @param string $authorName The name of the author of the commit - * @param string $authorEmail The email of the author of the commit - * @param string $committerName The name of the committer of the commit - * @param string $committerEmail The email of the committer of the commit - * - * @return object - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function update($owner, $repo, $path, $message, $content, $sha, $branch = 'master', $authorName = '', $authorEmail = '', - $committerName = '', $committerEmail = '' - ) - { - // Build the request path. - $route = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; - - $data = [ - 'message' => $message, - 'content' => $content, - 'sha' => $sha, - 'branch' => $branch, - ]; - - if ($authorName) - { - if (!$authorEmail) - { - throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); - } - - $data['author'] = [ - 'name' => $authorName, - 'email' => $authorEmail, - ]; - } - - if ($committerName) - { - if (!$committerEmail) - { - throw new \UnexpectedValueException('You must provide a committer e-mail if you supply a committer name'); - } - - $data['committer'] = [ - 'name' => $committerName, - 'email' => $committerEmail, - ]; - } - - return $this->processResponse($this->client->put($this->fetchUrl($route), json_encode($data))); - } - - /** - * Delete a file. - * - * This method deletes a file in a repository. - * - * @param string $owner The owner of the repository. - * @param string $repo The repository name. - * @param string $path The content path. - * @param string $message The commit message. - * @param string $sha The blob SHA of the file being replaced. - * @param string $branch The branch name. If not provided, uses the repository’s default branch (usually master). - * @param string $authorName The name of the author of the commit - * @param string $authorEmail The email of the author of the commit - * @param string $committerName The name of the committer of the commit - * @param string $committerEmail The email of the committer of the commit - * - * @return object - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function delete($owner, $repo, $path, $message, $sha, $branch = 'master', $authorName = '', $authorEmail = '', $committerName = '', - $committerEmail = '' - ) - { - // Build the request path. - $route = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; - - $data = [ - 'message' => $message, - 'sha' => $sha, - 'branch' => $branch, - ]; - - if ($authorName) - { - if (!$authorEmail) - { - throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); - } - - $data['author'] = [ - 'name' => $authorName, - 'email' => $authorEmail, - ]; - } - - if ($committerName) - { - if (!$committerEmail) - { - throw new \UnexpectedValueException('You must provide a committer e-mail if you supply a committer name'); - } - - $data['committer'] = [ - 'name' => $committerName, - 'email' => $committerEmail, - ]; - } - - return $this->processResponse( - $this->client->delete( - $this->fetchUrl($route), - [], - null, - json_encode($data) - ) - ); - } + /** + * Get the README. + * + * This method returns the preferred README for a repository. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. + * + * @return object + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function getReadme($owner, $repo, $ref = '') + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/readme'; + + $uri = $this->fetchUrl($path); + + if ($ref) { + $uri->setVar('ref', $ref); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * Get contents. + * + * This method returns the contents of any file or directory in a repository. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $path The content path. + * @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. + * + * @return object + * + * @since 1.0 + */ + public function get($owner, $repo, $path, $ref = '') + { + // Build the request path. + $rPath = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; + + $uri = $this->fetchUrl($rPath); + + if ($ref) { + $uri->setVar('ref', $ref); + } + + // Send the request. + return $this->processResponse($this->client->get($uri)); + } + + /** + * Get archive link. + * + * This method will return a 302 to a URL to download a tarball or zipball archive for a repository. + * Please make sure your HTTP framework is configured to follow redirects or you will need to use the + * Location header to make a second GET request. + * + * Note: For private repositories, these links are temporary and expire quickly. + * + * To follow redirects with curl, use the -L switch: + * curl -L https://api.github.com/repos/pengwynn/octokit/tarball > octokit.tar.gz + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $archiveFormat Either tarball or zipball. + * @param string $ref The String name of the Commit/Branch/Tag. Defaults to master. + * + * @return object + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function getArchiveLink($owner, $repo, $archiveFormat = 'zipball', $ref = '') + { + if (\in_array($archiveFormat, ['tarball', 'zipball']) == false) { + throw new \UnexpectedValueException('Archive format must be either "tarball" or "zipball".'); + } + + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/' . $archiveFormat; + $uri = $this->fetchUrl($path); + + if ($ref) { + $uri->setVar('ref', $ref); + } + + // Send the request. + return $this->processResponse($this->client->get($uri), 302); + } + + /** + * Create a file. + * + * This method creates a new file in a repository. + * + * Optional Parameters + * The author section is optional and is filled in with the committer information if omitted. + * If the committer information is omitted, the authenticated user’s information is used. + * + * You must provide values for both name and email, whether you choose to use author or committer. + * Otherwise, you’ll receive a 500 status code. + * + * @param string $owner The owner of the repository. + * @param string $repo The repository name. + * @param string $path The content path. + * @param string $message The commit message. + * @param string $content The new file content, Base64 encoded. + * @param string $branch The branch name. If not provided, uses the repository’s default branch (usually master). + * @param string $authorName The name of the author of the commit + * @param string $authorEmail The email of the author of the commit + * @param string $committerName The name of the committer of the commit + * @param string $committerEmail The email of the committer of the commit + * + * @return object + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function create( + $owner, + $repo, + $path, + $message, + $content, + $branch = 'master', + $authorName = '', + $authorEmail = '', + $committerName = '', + $committerEmail = '' + ) { + // Build the request path. + $route = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; + + $data = [ + 'message' => $message, + 'content' => $content, + 'branch' => $branch, + ]; + + if ($authorName) { + if (!$authorEmail) { + throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); + } + + $data['author'] = [ + 'name' => $authorName, + 'email' => $authorEmail, + ]; + } + + if ($committerName) { + if (!$committerEmail) { + throw new \UnexpectedValueException('You must provide a committer e-mail if you supply a committer name'); + } + + $data['committer'] = [ + 'name' => $committerName, + 'email' => $committerEmail, + ]; + } + + return $this->processResponse($this->client->put($this->fetchUrl($route), json_encode($data)), 201); + } + + /** + * Update a file. + * + * This method updates a file in a repository. + * + * Optional Parameters + * The author section is optional and is filled in with the committer information if omitted. + * If the committer information is omitted, the authenticated user’s information is used. + * + * You must provide values for both name and email, whether you choose to use author or committer. + * Otherwise, you’ll receive a 500 status code. + * + * @param string $owner The owner of the repository. + * @param string $repo The repository name. + * @param string $path The content path. + * @param string $message The commit message. + * @param string $content The new file content, Base64 encoded. + * @param string $sha The blob SHA of the file being replaced. + * @param string $branch The branch name. If not provided, uses the repository’s default branch (usually master). + * @param string $authorName The name of the author of the commit + * @param string $authorEmail The email of the author of the commit + * @param string $committerName The name of the committer of the commit + * @param string $committerEmail The email of the committer of the commit + * + * @return object + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function update( + $owner, + $repo, + $path, + $message, + $content, + $sha, + $branch = 'master', + $authorName = '', + $authorEmail = '', + $committerName = '', + $committerEmail = '' + ) { + // Build the request path. + $route = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; + + $data = [ + 'message' => $message, + 'content' => $content, + 'sha' => $sha, + 'branch' => $branch, + ]; + + if ($authorName) { + if (!$authorEmail) { + throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); + } + + $data['author'] = [ + 'name' => $authorName, + 'email' => $authorEmail, + ]; + } + + if ($committerName) { + if (!$committerEmail) { + throw new \UnexpectedValueException('You must provide a committer e-mail if you supply a committer name'); + } + + $data['committer'] = [ + 'name' => $committerName, + 'email' => $committerEmail, + ]; + } + + return $this->processResponse($this->client->put($this->fetchUrl($route), json_encode($data))); + } + + /** + * Delete a file. + * + * This method deletes a file in a repository. + * + * @param string $owner The owner of the repository. + * @param string $repo The repository name. + * @param string $path The content path. + * @param string $message The commit message. + * @param string $sha The blob SHA of the file being replaced. + * @param string $branch The branch name. If not provided, uses the repository’s default branch (usually master). + * @param string $authorName The name of the author of the commit + * @param string $authorEmail The email of the author of the commit + * @param string $committerName The name of the committer of the commit + * @param string $committerEmail The email of the committer of the commit + * + * @return object + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function delete( + $owner, + $repo, + $path, + $message, + $sha, + $branch = 'master', + $authorName = '', + $authorEmail = '', + $committerName = '', + $committerEmail = '' + ) { + // Build the request path. + $route = '/repos/' . $owner . '/' . $repo . '/contents/' . $path; + + $data = [ + 'message' => $message, + 'sha' => $sha, + 'branch' => $branch, + ]; + + if ($authorName) { + if (!$authorEmail) { + throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); + } + + $data['author'] = [ + 'name' => $authorName, + 'email' => $authorEmail, + ]; + } + + if ($committerName) { + if (!$committerEmail) { + throw new \UnexpectedValueException('You must provide a committer e-mail if you supply a committer name'); + } + + $data['committer'] = [ + 'name' => $committerName, + 'email' => $committerEmail, + ]; + } + + return $this->processResponse( + $this->client->delete( + $this->fetchUrl($route), + [], + null, + json_encode($data) + ) + ); + } } diff --git a/src/Package/Repositories/Deployments.php b/src/Package/Repositories/Deployments.php index c235677e..08411111 100644 --- a/src/Package/Repositories/Deployments.php +++ b/src/Package/Repositories/Deployments.php @@ -1,4 +1,5 @@ fetchUrl($path, $page, $limit); - - if ($sha) - { - $uri->setVar('sha', $sha); - } - - if ($ref) - { - $uri->setVar('ref', $ref); - } - - if ($task) - { - $uri->setVar('task', $task); - } - - if ($environment) - { - $uri->setVar('environment', $environment); - } - - return $this->processResponse($this->client->get($uri)); - } - - /** - * Create a Deployment. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $ref The ref to deploy. This can be a branch, tag, or SHA. - * @param string $task Optional parameter to specify a task to execute. - * @param boolean $autoMerge Optional parameter to merge the default branch into the requested ref if - * it is behind the default branch. - * @param array|null $requiredContexts Optional array of status contexts verified against commit status checks. - * If this parameter is omitted - * from the parameters then all unique contexts will be verified before a - * deployment is created. To bypass checking entirely pass an empty array. - * Defaults to all unique contexts. - * @param string $payload Optional JSON payload with extra information about the deployment. - * @param string $environment Optional name for the target deployment environment. - * @param string $description Optional short description. - * - * @return object - * - * @since 1.4.0 - * @throws \RuntimeException - */ - public function create($owner, $repo, $ref, $task = '', $autoMerge = true, $requiredContexts = null, $payload = '', $environment = '', - $description = '' - ) - { - // Build the request path. - $path = "/repos/$owner/$repo/deployments"; - - $data = [ - 'ref' => $ref, - 'auto_merge' => $autoMerge, - ]; - - if ($task) - { - $data['task'] = $task; - } - - if (\is_array($requiredContexts)) - { - $data['required_contexts'] = $requiredContexts; - } - - if ($payload) - { - $data['payload'] = $payload; - } - - if ($environment) - { - $data['environment'] = $environment; - } - - if ($description) - { - $data['description'] = $description; - } - - $response = $this->client->post($this->fetchUrl($path), json_encode($data)); - - switch ($response->code) - { - case 201 : - // The deployment was successful - return json_decode($response->body); - - case 409 : - // There was a merge conflict or a status check failed. - $body = json_decode($response->body); - $message = isset($body->message) ? $body->message : 'Invalid response received from GitHub.'; - - throw new \RuntimeException($message, $response->code); - - default : - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - } - - /** - * List Deployment Statuses. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id The Deployment ID to list the statuses from. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return object - * - * @since 1.4.0 - */ - public function getDeploymentStatuses($owner, $repo, $id, $page = 0, $limit = 0) - { - // Build the request path. - $path = "/repos/$owner/$repo/deployments/" . (int) $id . '/statuses'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path, $page, $limit)) - ); - } - - /** - * Create a Deployment Status. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id The Deployment ID to list the statuses from. - * @param string $state The state of the status. - * @param string $targetUrl The target URL to associate with this status. This URL should contain output to keep the user updated while - * the task is running or serve as historical information for what happened in the deployment. - * @param string $description A short description of the status. Maximum length of 140 characters. - * - * @return object - * - * @since 1.4.0 - * @throws \InvalidArgumentException - */ - public function createStatus($owner, $repo, $id, $state, $targetUrl = '', $description = '') - { - $allowedStates = ['pending', 'success', 'error', 'failure']; - - // Build the request path. - $path = "/repos/$owner/$repo/deployments/" . (int) $id . '/statuses'; - - if (!\in_array($state, $allowedStates)) - { - throw new \InvalidArgumentException(sprintf('The deployment state must be: %s', implode(', ', $allowedStates))); - } - - $data = [ - 'state' => $state, - ]; - - if ($targetUrl) - { - $data['target_url'] = $targetUrl; - } - - if ($description) - { - $data['description'] = $description; - } - - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($data)), - 201 - ); - } + /** + * List Deployments. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $sha The SHA that was recorded at creation time. + * @param string $ref The name of the ref. This can be a branch, tag, or SHA. + * @param string $task The name of the task for the deployment. + * @param string $environment The name of the environment that was deployed to. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.4.0 + */ + public function getList($owner, $repo, $sha = '', $ref = '', $task = '', $environment = '', $page = 0, $limit = 0) + { + // Build the request path. + $path = "/repos/$owner/$repo/deployments"; + + $uri = $this->fetchUrl($path, $page, $limit); + + if ($sha) { + $uri->setVar('sha', $sha); + } + + if ($ref) { + $uri->setVar('ref', $ref); + } + + if ($task) { + $uri->setVar('task', $task); + } + + if ($environment) { + $uri->setVar('environment', $environment); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * Create a Deployment. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $ref The ref to deploy. This can be a branch, tag, or SHA. + * @param string $task Optional parameter to specify a task to execute. + * @param boolean $autoMerge Optional parameter to merge the default branch into the requested ref if + * it is behind the default branch. + * @param array|null $requiredContexts Optional array of status contexts verified against commit status checks. + * If this parameter is omitted + * from the parameters then all unique contexts will be verified before a + * deployment is created. To bypass checking entirely pass an empty array. + * Defaults to all unique contexts. + * @param string $payload Optional JSON payload with extra information about the deployment. + * @param string $environment Optional name for the target deployment environment. + * @param string $description Optional short description. + * + * @return object + * + * @since 1.4.0 + * @throws \RuntimeException + */ + public function create( + $owner, + $repo, + $ref, + $task = '', + $autoMerge = true, + $requiredContexts = null, + $payload = '', + $environment = '', + $description = '' + ) { + // Build the request path. + $path = "/repos/$owner/$repo/deployments"; + + $data = [ + 'ref' => $ref, + 'auto_merge' => $autoMerge, + ]; + + if ($task) { + $data['task'] = $task; + } + + if (\is_array($requiredContexts)) { + $data['required_contexts'] = $requiredContexts; + } + + if ($payload) { + $data['payload'] = $payload; + } + + if ($environment) { + $data['environment'] = $environment; + } + + if ($description) { + $data['description'] = $description; + } + + $response = $this->client->post($this->fetchUrl($path), json_encode($data)); + + switch ($response->code) { + case 201: + // The deployment was successful + return json_decode($response->body); + + case 409: + // There was a merge conflict or a status check failed. + $body = json_decode($response->body); + $message = isset($body->message) ? $body->message : 'Invalid response received from GitHub.'; + + throw new \RuntimeException($message, $response->code); + + default: + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + } + + /** + * List Deployment Statuses. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id The Deployment ID to list the statuses from. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.4.0 + */ + public function getDeploymentStatuses($owner, $repo, $id, $page = 0, $limit = 0) + { + // Build the request path. + $path = "/repos/$owner/$repo/deployments/" . (int) $id . '/statuses'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path, $page, $limit)) + ); + } + + /** + * Create a Deployment Status. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id The Deployment ID to list the statuses from. + * @param string $state The state of the status. + * @param string $targetUrl The target URL to associate with this status. This URL should contain output to keep the user updated while + * the task is running or serve as historical information for what happened in the deployment. + * @param string $description A short description of the status. Maximum length of 140 characters. + * + * @return object + * + * @since 1.4.0 + * @throws \InvalidArgumentException + */ + public function createStatus($owner, $repo, $id, $state, $targetUrl = '', $description = '') + { + $allowedStates = ['pending', 'success', 'error', 'failure']; + + // Build the request path. + $path = "/repos/$owner/$repo/deployments/" . (int) $id . '/statuses'; + + if (!\in_array($state, $allowedStates)) { + throw new \InvalidArgumentException(sprintf('The deployment state must be: %s', implode(', ', $allowedStates))); + } + + $data = [ + 'state' => $state, + ]; + + if ($targetUrl) { + $data['target_url'] = $targetUrl; + } + + if ($description) { + $data['description'] = $description; + } + + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($data)), + 201 + ); + } } diff --git a/src/Package/Repositories/Downloads.php b/src/Package/Repositories/Downloads.php index ed6c62a4..4e23ea1a 100644 --- a/src/Package/Repositories/Downloads.php +++ b/src/Package/Repositories/Downloads.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Get a single download. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id The id of the download. - * - * @return object - * - * @since 1.0 - * @deprecated The Releases API should be used instead - */ - public function get($owner, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/downloads/' . $id; + /** + * Get a single download. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id The id of the download. + * + * @return object + * + * @since 1.0 + * @deprecated The Releases API should be used instead + */ + public function get($owner, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/downloads/' . $id; - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Create a new download (Part 1: Create the resource). - * - * Creating a new download is a two step process. You must first create a new download resource. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $name The name. - * @param string $size Size of file in bytes. - * @param string $description The description. - * @param string $contentType The content type. - * - * @return void - * - * @note This API endpoint no longer exists at GitHub - * @since 1.0 - * @throws \RuntimeException - * @deprecated The Releases API should be used instead - */ - public function create($owner, $repo, $name, $size, $description = '', $contentType = '') - { - throw new \RuntimeException('The GitHub API no longer supports creating downloads. The Releases API should be used instead.'); - } + /** + * Create a new download (Part 1: Create the resource). + * + * Creating a new download is a two step process. You must first create a new download resource. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $name The name. + * @param string $size Size of file in bytes. + * @param string $description The description. + * @param string $contentType The content type. + * + * @return void + * + * @note This API endpoint no longer exists at GitHub + * @since 1.0 + * @throws \RuntimeException + * @deprecated The Releases API should be used instead + */ + public function create($owner, $repo, $name, $size, $description = '', $contentType = '') + { + throw new \RuntimeException('The GitHub API no longer supports creating downloads. The Releases API should be used instead.'); + } - /** - * Create a new download (Part 2: Upload file to s3). - * - * Now that you have created the download resource, you can use the information - * in the response to upload your file to s3. This can be done with a POST to - * the s3_url you got in the create response. Here is a brief example using curl: - * - * curl \ - * -F "key=downloads/octocat/Hello-World/new_file.jpg" \ - * -F "acl=public-read" \ - * -F "success_action_status=201" \ - * -F "Filename=new_file.jpg" \ - * -F "AWSAccessKeyId=1ABCDEF..." \ - * -F "Policy=ewogIC..." \ - * -F "Signature=mwnF..." \ - * -F "Content-Type=image/jpeg" \ - * -F "file=@new_file.jpg" \ - * https://github.s3.amazonaws.com/ - * - * NOTES - * The order in which you pass these fields matters! Follow the order shown above exactly. - * All parameters shown are required and if you excluded or modify them your upload will - * fail because the values are hashed and signed by the policy. - * - * More information about using the REST API to interact with s3 can be found here: - * http://docs.amazonwebservices.com/AmazonS3/latest/API/ - * - * @param string $key Value of path field in the response. - * @param string $acl Value of acl field in the response. - * @param string $successActionStatus 201, or whatever you want to get back. - * @param string $filename Value of name field in the response. - * @param string $awsAccessKeyId Value of accesskeyid field in the response. - * @param string $policy Value of policy field in the response. - * @param string $signature Value of signature field in the response. - * @param string $contentType Value of mime_type field in the response. - * @param string $file Local file. Example assumes the file existing in the directory - * where you are running the curl command. Yes, the @ matters. - * - * @return void - * - * @note This API endpoint no longer exists at GitHub - * @since 1.0 - * @throws \RuntimeException - * @deprecated The Releases API should be used instead - */ - public function upload($key, $acl, $successActionStatus, $filename, $awsAccessKeyId, $policy, $signature, $contentType, $file) - { - throw new \RuntimeException('The GitHub API no longer supports creating downloads. The Releases API should be used instead.'); - } + /** + * Create a new download (Part 2: Upload file to s3). + * + * Now that you have created the download resource, you can use the information + * in the response to upload your file to s3. This can be done with a POST to + * the s3_url you got in the create response. Here is a brief example using curl: + * + * curl \ + * -F "key=downloads/octocat/Hello-World/new_file.jpg" \ + * -F "acl=public-read" \ + * -F "success_action_status=201" \ + * -F "Filename=new_file.jpg" \ + * -F "AWSAccessKeyId=1ABCDEF..." \ + * -F "Policy=ewogIC..." \ + * -F "Signature=mwnF..." \ + * -F "Content-Type=image/jpeg" \ + * -F "file=@new_file.jpg" \ + * https://github.s3.amazonaws.com/ + * + * NOTES + * The order in which you pass these fields matters! Follow the order shown above exactly. + * All parameters shown are required and if you excluded or modify them your upload will + * fail because the values are hashed and signed by the policy. + * + * More information about using the REST API to interact with s3 can be found here: + * http://docs.amazonwebservices.com/AmazonS3/latest/API/ + * + * @param string $key Value of path field in the response. + * @param string $acl Value of acl field in the response. + * @param string $successActionStatus 201, or whatever you want to get back. + * @param string $filename Value of name field in the response. + * @param string $awsAccessKeyId Value of accesskeyid field in the response. + * @param string $policy Value of policy field in the response. + * @param string $signature Value of signature field in the response. + * @param string $contentType Value of mime_type field in the response. + * @param string $file Local file. Example assumes the file existing in the directory + * where you are running the curl command. Yes, the @ matters. + * + * @return void + * + * @note This API endpoint no longer exists at GitHub + * @since 1.0 + * @throws \RuntimeException + * @deprecated The Releases API should be used instead + */ + public function upload($key, $acl, $successActionStatus, $filename, $awsAccessKeyId, $policy, $signature, $contentType, $file) + { + throw new \RuntimeException('The GitHub API no longer supports creating downloads. The Releases API should be used instead.'); + } - /** - * Delete a download. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id The id of the download. - * - * @return object - * - * @since 1.0 - * @deprecated The Releases API should be used instead - */ - public function delete($owner, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/downloads/' . (int) $id; + /** + * Delete a download. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id The id of the download. + * + * @return object + * + * @since 1.0 + * @deprecated The Releases API should be used instead + */ + public function delete($owner, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/downloads/' . (int) $id; - // Send the request. - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + // Send the request. + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } } diff --git a/src/Package/Repositories/Forks.php b/src/Package/Repositories/Forks.php index 6aa7d68b..e93fc257 100644 --- a/src/Package/Repositories/Forks.php +++ b/src/Package/Repositories/Forks.php @@ -1,4 +1,5 @@ 0) - { - $data = json_encode( - ['org' => $org] - ); - } - else - { - $data = json_encode([]); - } + if (\strlen($org) > 0) { + $data = json_encode( + ['org' => $org] + ); + } else { + $data = json_encode([]); + } - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 202); - } + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 202); + } - /** - * List forks. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $page Page to request - * @param integer $limit Number of results to return per page - * - * @return array - * - * @since 1.0 - * @throws \DomainException - */ - public function getList($owner, $repo, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/forks'; + /** + * List forks. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $page Page to request + * @param integer $limit Number of results to return per page + * + * @return array + * + * @since 1.0 + * @throws \DomainException + */ + public function getList($owner, $repo, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/forks'; - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit)), 200); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit)), 200); + } } diff --git a/src/Package/Repositories/Hooks.php b/src/Package/Repositories/Hooks.php index 0f7b1739..4c706291 100644 --- a/src/Package/Repositories/Hooks.php +++ b/src/Package/Repositories/Hooks.php @@ -1,4 +1,5 @@ hookEvents)) - { - throw new \RuntimeException('Your events array contains an unauthorized event.'); - } - } - - $data = json_encode( - ['name' => $name, 'config' => $config, 'events' => $events, 'active' => $active] - ); - - return $this->processResponse( - $this->client->post($this->fetchUrl($path), $data), - 201 - ); - } - - /** - * Delete a hook - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id ID of the hook to delete. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function delete($user, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * Edit a hook. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id ID of the hook to edit. - * @param string $name The name of the service being called. - * @param array $config Array containing the config for the service. - * @param array $events The events the hook will be triggered for. This resets the currently set list - * @param array $addEvents Events to add to the hook. - * @param array $removeEvents Events to remove from the hook. - * @param boolean $active Flag to determine if the hook is active - * - * @return object - * - * @since 1.0 - * @throws \DomainException - * @throws \RuntimeException - */ - public function edit($user, $repo, $id, $name, $config, array $events = ['push'], array $addEvents = [], array $removeEvents = [], $active = true) - { - // Check to ensure all events are in the allowed list - foreach ($events as $event) - { - if (!\in_array($event, $this->hookEvents)) - { - throw new \RuntimeException('Your events array contains an unauthorized event.'); - } - } - - foreach ($addEvents as $event) - { - if (!\in_array($event, $this->hookEvents)) - { - throw new \RuntimeException('Your active_events array contains an unauthorized event.'); - } - } - - foreach ($removeEvents as $event) - { - if (!\in_array($event, $this->hookEvents)) - { - throw new \RuntimeException('Your remove_events array contains an unauthorized event.'); - } - } - - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id; - - $data = json_encode( - [ - 'name' => $name, - 'config' => $config, - 'events' => $events, - 'add_events' => $addEvents, - 'remove_events' => $removeEvents, - 'active' => $active, - ] - ); - - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), $data) - ); - } - - /** - * Get single hook. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id ID of the hook to retrieve - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function get($user, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List hooks. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function getList($user, $repo) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/hooks'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Ping a hook. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id ID of the hook to ping - * - * @return object - * - * @since 1.4.0 - * @throws \DomainException - */ - public function ping($user, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id . '/pings'; - - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode('')), - 204 - ); - } - - /** - * Test a `push` hook. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id ID of the hook to test - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function test($user, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id . '/test'; - - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode('')), - 204 - ); - } + /** + * Create a hook. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $name The name of the service being called. + * @param array $config Array containing the config for the service. + * @param array $events The events the hook will be triggered for. + * @param boolean $active Flag to determine if the hook is active + * + * @return object + * + * @since 1.0 + * @throws \DomainException + * @throws \RuntimeException + */ + public function create($user, $repo, $name, $config, array $events = ['push'], $active = true) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/hooks'; + + // Check to ensure all events are in the allowed list + foreach ($events as $event) { + if (!\in_array($event, $this->hookEvents)) { + throw new \RuntimeException('Your events array contains an unauthorized event.'); + } + } + + $data = json_encode( + ['name' => $name, 'config' => $config, 'events' => $events, 'active' => $active] + ); + + return $this->processResponse( + $this->client->post($this->fetchUrl($path), $data), + 201 + ); + } + + /** + * Delete a hook + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id ID of the hook to delete. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function delete($user, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * Edit a hook. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id ID of the hook to edit. + * @param string $name The name of the service being called. + * @param array $config Array containing the config for the service. + * @param array $events The events the hook will be triggered for. This resets the currently set list + * @param array $addEvents Events to add to the hook. + * @param array $removeEvents Events to remove from the hook. + * @param boolean $active Flag to determine if the hook is active + * + * @return object + * + * @since 1.0 + * @throws \DomainException + * @throws \RuntimeException + */ + public function edit($user, $repo, $id, $name, $config, array $events = ['push'], array $addEvents = [], array $removeEvents = [], $active = true) + { + // Check to ensure all events are in the allowed list + foreach ($events as $event) { + if (!\in_array($event, $this->hookEvents)) { + throw new \RuntimeException('Your events array contains an unauthorized event.'); + } + } + + foreach ($addEvents as $event) { + if (!\in_array($event, $this->hookEvents)) { + throw new \RuntimeException('Your active_events array contains an unauthorized event.'); + } + } + + foreach ($removeEvents as $event) { + if (!\in_array($event, $this->hookEvents)) { + throw new \RuntimeException('Your remove_events array contains an unauthorized event.'); + } + } + + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id; + + $data = json_encode( + [ + 'name' => $name, + 'config' => $config, + 'events' => $events, + 'add_events' => $addEvents, + 'remove_events' => $removeEvents, + 'active' => $active, + ] + ); + + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), $data) + ); + } + + /** + * Get single hook. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id ID of the hook to retrieve + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function get($user, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List hooks. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getList($user, $repo) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/hooks'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Ping a hook. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id ID of the hook to ping + * + * @return object + * + * @since 1.4.0 + * @throws \DomainException + */ + public function ping($user, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id . '/pings'; + + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode('')), + 204 + ); + } + + /** + * Test a `push` hook. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id ID of the hook to test + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function test($user, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/hooks/' . $id . '/test'; + + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode('')), + 204 + ); + } } diff --git a/src/Package/Repositories/Keys.php b/src/Package/Repositories/Keys.php index 1e53f49c..92253384 100644 --- a/src/Package/Repositories/Keys.php +++ b/src/Package/Repositories/Keys.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Get a deploy key. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id The id of the key. - * - * @since 1.0 - * - * @return object - */ - public function get($owner, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/keys/' . (int) $id; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Add a new deploy key. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $title The key title. - * @param string $key The key. - * - * @since 1.0 - * - * @return object - */ - public function create($owner, $repo, $title, $key) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/keys'; - - $data = [ - 'title' => $title, - 'key' => $key, - ]; - - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($data)), - 201 - ); - } - - /** - * Edit a deploy key. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id The id of the key. - * @param string $title The key title. - * @param string $key The key. - * - * @since 1.0 - * - * @return object - */ - public function edit($owner, $repo, $id, $title, $key) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/keys/' . (int) $id; - - $data = [ - 'title' => $title, - 'key' => $key, - ]; - - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), json_encode($data)) - ); - } - - /** - * Remove a deploy key. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $id The id of the key. - * - * @since 1.0 - * - * @return boolean - */ - public function delete($owner, $repo, $id) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/keys/' . (int) $id; - - $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - - return true; - } + /** + * List deploy keys. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * + * @since 1.0 + * + * @return object + */ + public function getList($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/keys'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Get a deploy key. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id The id of the key. + * + * @since 1.0 + * + * @return object + */ + public function get($owner, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/keys/' . (int) $id; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Add a new deploy key. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $title The key title. + * @param string $key The key. + * + * @since 1.0 + * + * @return object + */ + public function create($owner, $repo, $title, $key) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/keys'; + + $data = [ + 'title' => $title, + 'key' => $key, + ]; + + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($data)), + 201 + ); + } + + /** + * Edit a deploy key. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id The id of the key. + * @param string $title The key title. + * @param string $key The key. + * + * @since 1.0 + * + * @return object + */ + public function edit($owner, $repo, $id, $title, $key) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/keys/' . (int) $id; + + $data = [ + 'title' => $title, + 'key' => $key, + ]; + + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), json_encode($data)) + ); + } + + /** + * Remove a deploy key. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $id The id of the key. + * + * @since 1.0 + * + * @return boolean + */ + public function delete($owner, $repo, $id) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/keys/' . (int) $id; + + $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + + return true; + } } diff --git a/src/Package/Repositories/Merging.php b/src/Package/Repositories/Merging.php index 292c8cbd..13773556 100644 --- a/src/Package/Repositories/Merging.php +++ b/src/Package/Repositories/Merging.php @@ -1,4 +1,5 @@ base = $base; - $data->head = $head; - - if ($commitMessage) - { - $data->commit_message = $commitMessage; - } - - // Send the request. - $response = $this->client->post($this->fetchUrl($path), json_encode($data)); - - switch ($response->code) - { - case '201': - // Success - return json_decode($response->body); - - case '204': - // No-op response (base already contains the head, nothing to merge) - throw new \UnexpectedValueException('Nothing to merge'); - - case '404': - // Missing base or Missing head response - $error = json_decode($response->body); - - $message = (isset($error->message)) ? $error->message : 'Missing base or head: ' . $response->code; - - throw new \UnexpectedValueException($message); - - case '409': - // Merge conflict response - $error = json_decode($response->body); - - $message = (isset($error->message)) ? $error->message : 'Merge conflict ' . $response->code; - - throw new \UnexpectedValueException($message); - - default : - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - } + /** + * Perform a merge. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $base The name of the base branch that the head will be merged into. + * @param string $head The head to merge. This can be a branch name or a commit SHA1. + * @param string $commitMessage Commit message to use for the merge commit. + * If omitted, a default message will be used. + * + * @return boolean + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function perform($owner, $repo, $base, $head, $commitMessage = '') + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/merges'; + + $data = new \stdClass(); + + $data->base = $base; + $data->head = $head; + + if ($commitMessage) { + $data->commit_message = $commitMessage; + } + + // Send the request. + $response = $this->client->post($this->fetchUrl($path), json_encode($data)); + + switch ($response->code) { + case '201': + // Success + return json_decode($response->body); + + case '204': + // No-op response (base already contains the head, nothing to merge) + throw new \UnexpectedValueException('Nothing to merge'); + + case '404': + // Missing base or Missing head response + $error = json_decode($response->body); + + $message = (isset($error->message)) ? $error->message : 'Missing base or head: ' . $response->code; + + throw new \UnexpectedValueException($message); + + case '409': + // Merge conflict response + $error = json_decode($response->body); + + $message = (isset($error->message)) ? $error->message : 'Merge conflict ' . $response->code; + + throw new \UnexpectedValueException($message); + + default: + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + } } diff --git a/src/Package/Repositories/Pages.php b/src/Package/Repositories/Pages.php index 44dd6198..62b0fa2f 100644 --- a/src/Package/Repositories/Pages.php +++ b/src/Package/Repositories/Pages.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * List Pages builds. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return object - * - * @since 1.4.0 - */ - public function getList($owner, $repo, $page = 0, $limit = 0) - { - // Build the request path. - $path = "/repos/$owner/$repo/pages/builds"; + /** + * List Pages builds. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.4.0 + */ + public function getList($owner, $repo, $page = 0, $limit = 0) + { + // Build the request path. + $path = "/repos/$owner/$repo/pages/builds"; - return $this->processResponse( - $this->client->get($this->fetchUrl($path, $page, $limit)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path, $page, $limit)) + ); + } - /** - * List latest Pages build. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * - * @return object - * - * @since 1.4.0 - */ - public function getLatest($owner, $repo) - { - // Build the request path. - $path = "/repos/$owner/$repo/pages/builds/latest"; + /** + * List latest Pages build. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * + * @return object + * + * @since 1.4.0 + */ + public function getLatest($owner, $repo) + { + // Build the request path. + $path = "/repos/$owner/$repo/pages/builds/latest"; - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } } diff --git a/src/Package/Repositories/Releases.php b/src/Package/Repositories/Releases.php index cacc90a6..a6014466 100644 --- a/src/Package/Repositories/Releases.php +++ b/src/Package/Repositories/Releases.php @@ -1,4 +1,5 @@ $tagName, - 'target_commitish' => $targetCommitish, - 'name' => $name, - 'body' => $body, - 'draft' => (boolean) $draft, - 'prerelease' => (boolean) $preRelease, - ] - ); - - // Send the request. - return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); - } - - /** - * Delete a release. - * - * @param string $owner The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $releaseId The release id. - * - * @return object - * - * @since 1.4.0 - */ - public function delete($owner, $repo, $releaseId) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/releases/' . (int) $releaseId; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } - - /** - * Edit a release. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $releaseId The release id. - * @param string $tagName The name of the tag. - * @param string $targetCommitish The commitish value that determines where the Git tag is created from. - * @param string $name The branch (or git ref) you want your changes pulled into. This should be an existing branch on the current - * repository. You cannot submit a pull request to one repo that requests a merge to a base of another repo. - * @param boolean $body The body text for the new pull request. - * @param boolean $draft The branch (or git ref) where your changes are implemented. - * @param string $preRelease The branch (or git ref) where your changes are implemented. - * - * @return object - * - * @link http://developer.github.com/v3/repos/releases/#edit-a-release - * @since 1.1.0 - * @throws \DomainException - */ - public function edit($user, $repo, $releaseId, $tagName, $targetCommitish = null, $name = null, $body = null, $draft = null, $preRelease = null) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/releases/' . (int) $releaseId; - - // Create the data object. - $data = new \stdClass; - $data->tag_name = $tagName; - - // Check if input values are set and add them to the data object. - if (isset($targetCommitish)) - { - $data->target_commitish = $targetCommitish; - } - - if (isset($name)) - { - $data->name = $name; - } - - if (isset($body)) - { - $data->body = $body; - } - - if (isset($draft)) - { - $data->draft = $draft; - } - - if (isset($preRelease)) - { - $data->prerelease = $preRelease; - } - - // Encode the request data. - $data = json_encode($data); - - // Send the request. - return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); - } - - /** - * Get a single release. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $ref Valid values are: 'latest', 'tags/2.0.24' or Release Id, for example: '1643513' - * - * @return object - * - * @since 1.1.0 - * @throws \DomainException - */ - public function get($user, $repo, $ref) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/releases/' . $ref; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Get the latest release. - * - * View the latest published full release for the repository. - * Draft releases and prereleases are not returned by this endpoint. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * - * @return object - * - * @since 1.4.0 - */ - public function getLatest($user, $repo) - { - // Build the request path. - $path = "/repos/$user/$repo/releases/latest"; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Get a release by tag name. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $tag The name of the tag. - * - * @return object - * - * @since 1.4.0 - */ - public function getByTag($user, $repo, $tag) - { - // Build the request path. - $path = "/repos/$user/$repo/releases/tags/$tag"; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * List releases for a repository. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return array An associative array of releases keyed by the tag name. - * - * @since 1.1.0 - * @throws \DomainException - */ - public function getList($user, $repo, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/releases'; - - // Send the request. - $response = $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); - - $releases = []; - - if (\is_array($response)) - { - foreach ($response as $release) - { - $releases[$release->tag_name] = $release; - } - } - - return $releases; - } - - /** - * List assets for a release. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $releaseId The release id. - * @param integer $page The page number from which to get items. - * @param integer $limit The number of items on a page. - * - * @return object - * - * @since 1.4.0 - */ - public function getListAssets($user, $repo, $releaseId, $page = 0, $limit = 0) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/releases/' . (int) $releaseId . '/assets'; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); - } - - /** - * Get a single release asset. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $assetId The asset id. - * - * @return object - * - * @since 1.4.0 - */ - public function getAsset($user, $repo, $assetId) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/releases/assets/' . (int) $assetId; - - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } - - /** - * Edit a release asset. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $assetId The asset id. - * @param string $name The file name of the asset. - * @param string $label An alternate short description of the asset. Used in place of the filename. - * - * @return object - * - * @since 1.4.0 - */ - public function editAsset($user, $repo, $assetId, $name, $label = '') - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/releases/assets/' . (int) $assetId; - - $data = [ - 'name' => $name, - ]; - - if ($label) - { - $data['label'] = $label; - } - - // Send the request. - return $this->processResponse($this->client->patch($this->fetchUrl($path), json_encode($data))); - } - - /** - * Delete a release asset. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $assetId The asset id. - * - * @return boolean - * - * @since 1.4.0 - */ - public function deleteAsset($user, $repo, $assetId) - { - // Build the request path. - $path = '/repos/' . $user . '/' . $repo . '/releases/assets/' . (int) $assetId; - - // Send the request. - $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); - - return true; - } + /** + * Create a release. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $tagName The name of the tag. + * @param string $targetCommitish The commitish value that determines where the Git tag is created from. + * @param string $name The name of the release. + * @param string $body Text describing the contents of the tag. + * @param boolean $draft True to create a draft (unpublished) release, false to create a published one. + * @param boolean $preRelease True to identify the release as a prerelease. false to identify the release as a full release. + * + * @return object + * + * @link http://developer.github.com/v3/repos/releases/#create-a-release + * @since 1.1.0 + */ + public function create($user, $repo, $tagName, $targetCommitish = '', $name = '', $body = '', $draft = false, $preRelease = false) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/releases'; + + // Build the request data. + $data = json_encode( + [ + 'tag_name' => $tagName, + 'target_commitish' => $targetCommitish, + 'name' => $name, + 'body' => $body, + 'draft' => (bool) $draft, + 'prerelease' => (bool) $preRelease, + ] + ); + + // Send the request. + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Delete a release. + * + * @param string $owner The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $releaseId The release id. + * + * @return object + * + * @since 1.4.0 + */ + public function delete($owner, $repo, $releaseId) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/releases/' . (int) $releaseId; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } + + /** + * Edit a release. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $releaseId The release id. + * @param string $tagName The name of the tag. + * @param string $targetCommitish The commitish value that determines where the Git tag is created from. + * @param string $name The branch (or git ref) you want your changes pulled into. This should be an existing branch on the current + * repository. You cannot submit a pull request to one repo that requests a merge to a base of another repo. + * @param boolean $body The body text for the new pull request. + * @param boolean $draft The branch (or git ref) where your changes are implemented. + * @param string $preRelease The branch (or git ref) where your changes are implemented. + * + * @return object + * + * @link http://developer.github.com/v3/repos/releases/#edit-a-release + * @since 1.1.0 + * @throws \DomainException + */ + public function edit($user, $repo, $releaseId, $tagName, $targetCommitish = null, $name = null, $body = null, $draft = null, $preRelease = null) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/releases/' . (int) $releaseId; + + // Create the data object. + $data = new \stdClass(); + $data->tag_name = $tagName; + + // Check if input values are set and add them to the data object. + if (isset($targetCommitish)) { + $data->target_commitish = $targetCommitish; + } + + if (isset($name)) { + $data->name = $name; + } + + if (isset($body)) { + $data->body = $body; + } + + if (isset($draft)) { + $data->draft = $draft; + } + + if (isset($preRelease)) { + $data->prerelease = $preRelease; + } + + // Encode the request data. + $data = json_encode($data); + + // Send the request. + return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); + } + + /** + * Get a single release. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $ref Valid values are: 'latest', 'tags/2.0.24' or Release Id, for example: '1643513' + * + * @return object + * + * @since 1.1.0 + * @throws \DomainException + */ + public function get($user, $repo, $ref) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/releases/' . $ref; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Get the latest release. + * + * View the latest published full release for the repository. + * Draft releases and prereleases are not returned by this endpoint. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * + * @return object + * + * @since 1.4.0 + */ + public function getLatest($user, $repo) + { + // Build the request path. + $path = "/repos/$user/$repo/releases/latest"; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Get a release by tag name. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $tag The name of the tag. + * + * @return object + * + * @since 1.4.0 + */ + public function getByTag($user, $repo, $tag) + { + // Build the request path. + $path = "/repos/$user/$repo/releases/tags/$tag"; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * List releases for a repository. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array An associative array of releases keyed by the tag name. + * + * @since 1.1.0 + * @throws \DomainException + */ + public function getList($user, $repo, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/releases'; + + // Send the request. + $response = $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + + $releases = []; + + if (\is_array($response)) { + foreach ($response as $release) { + $releases[$release->tag_name] = $release; + } + } + + return $releases; + } + + /** + * List assets for a release. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $releaseId The release id. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @since 1.4.0 + */ + public function getListAssets($user, $repo, $releaseId, $page = 0, $limit = 0) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/releases/' . (int) $releaseId . '/assets'; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } + + /** + * Get a single release asset. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $assetId The asset id. + * + * @return object + * + * @since 1.4.0 + */ + public function getAsset($user, $repo, $assetId) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/releases/assets/' . (int) $assetId; + + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Edit a release asset. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $assetId The asset id. + * @param string $name The file name of the asset. + * @param string $label An alternate short description of the asset. Used in place of the filename. + * + * @return object + * + * @since 1.4.0 + */ + public function editAsset($user, $repo, $assetId, $name, $label = '') + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/releases/assets/' . (int) $assetId; + + $data = [ + 'name' => $name, + ]; + + if ($label) { + $data['label'] = $label; + } + + // Send the request. + return $this->processResponse($this->client->patch($this->fetchUrl($path), json_encode($data))); + } + + /** + * Delete a release asset. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $assetId The asset id. + * + * @return boolean + * + * @since 1.4.0 + */ + public function deleteAsset($user, $repo, $assetId) + { + // Build the request path. + $path = '/repos/' . $user . '/' . $repo . '/releases/assets/' . (int) $assetId; + + // Send the request. + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + + return true; + } } diff --git a/src/Package/Repositories/Statistics.php b/src/Package/Repositories/Statistics.php index d8db3ee3..534482ef 100644 --- a/src/Package/Repositories/Statistics.php +++ b/src/Package/Repositories/Statistics.php @@ -1,4 +1,5 @@ processResponse($this->client->get($this->fetchUrl($path))); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } - /** - * Get the last year of commit activity data. - * - * Returns the last year of commit activity grouped by week. - * The days array is a group of commits per day, starting on Sunday. - * - * @param string $owner The owner of the repository. - * @param string $repo The repository name. - * - * @return object - * - * @since 1.0 - */ - public function getActivityData($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/stats/commit_activity'; + /** + * Get the last year of commit activity data. + * + * Returns the last year of commit activity grouped by week. + * The days array is a group of commits per day, starting on Sunday. + * + * @param string $owner The owner of the repository. + * @param string $repo The repository name. + * + * @return object + * + * @since 1.0 + */ + public function getActivityData($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/stats/commit_activity'; - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } - /** - * Get the number of additions and deletions per week. - * - * Response returns a weekly aggregate of the number of additions and deletions pushed to a repository. - * - * @param string $owner The owner of the repository. - * @param string $repo The repository name. - * - * @return object - * - * @since 1.0 - */ - public function getCodeFrequency($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/stats/code_frequency'; + /** + * Get the number of additions and deletions per week. + * + * Response returns a weekly aggregate of the number of additions and deletions pushed to a repository. + * + * @param string $owner The owner of the repository. + * @param string $repo The repository name. + * + * @return object + * + * @since 1.0 + */ + public function getCodeFrequency($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/stats/code_frequency'; - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } - /** - * Get the weekly commit count for the repository owner and everyone else. - * - * Returns the total commit counts for the "owner" and total commit counts in "all". "all" is everyone combined, - * including the owner in the last 52 weeks. - * If you’d like to get the commit counts for non-owners, you can subtract all from owner. - * - * The array order is oldest week (index 0) to most recent week. - * - * @param string $owner The owner of the repository. - * @param string $repo The repository name. - * - * @return object - * - * @since 1.0 - */ - public function getParticipation($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/stats/participation'; + /** + * Get the weekly commit count for the repository owner and everyone else. + * + * Returns the total commit counts for the "owner" and total commit counts in "all". "all" is everyone combined, + * including the owner in the last 52 weeks. + * If you’d like to get the commit counts for non-owners, you can subtract all from owner. + * + * The array order is oldest week (index 0) to most recent week. + * + * @param string $owner The owner of the repository. + * @param string $repo The repository name. + * + * @return object + * + * @since 1.0 + */ + public function getParticipation($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/stats/participation'; - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } - /** - * Get the number of commits per hour in each day. - * - * Response - * Each array contains the day number, hour number, and number of commits: - * - * 0-6: Sunday - Saturday - * 0-23: Hour of day - * Number of commits - * - * For example, [2, 14, 25] indicates that there were 25 total commits, during the 2:00pm hour on Tuesdays. - * All times are based on the time zone of individual commits. - * - * @param string $owner The owner of the repository. - * @param string $repo The repository name. - * - * @return object - * - * @since 1.0 - */ - public function getPunchCard($owner, $repo) - { - // Build the request path. - $path = '/repos/' . $owner . '/' . $repo . '/stats/punch_card'; + /** + * Get the number of commits per hour in each day. + * + * Response + * Each array contains the day number, hour number, and number of commits: + * + * 0-6: Sunday - Saturday + * 0-23: Hour of day + * Number of commits + * + * For example, [2, 14, 25] indicates that there were 25 total commits, during the 2:00pm hour on Tuesdays. + * All times are based on the time zone of individual commits. + * + * @param string $owner The owner of the repository. + * @param string $repo The repository name. + * + * @return object + * + * @since 1.0 + */ + public function getPunchCard($owner, $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/stats/punch_card'; - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } - /** - * Process the response and decode it. - * - * @param Response $response The response. - * @param integer $expectedCode The expected "good" code. - * - * @return mixed - * - * @since 1.0 - * @throws \DomainException - */ - protected function processResponse(Response $response, $expectedCode = 200) - { - if ($response->code == 202) - { - throw new \DomainException( - 'GitHub is building the statistics data. Please try again in a few moments.', - $response->code - ); - } + /** + * Process the response and decode it. + * + * @param Response $response The response. + * @param integer $expectedCode The expected "good" code. + * + * @return mixed + * + * @since 1.0 + * @throws \DomainException + */ + protected function processResponse(Response $response, $expectedCode = 200) + { + if ($response->code == 202) { + throw new \DomainException( + 'GitHub is building the statistics data. Please try again in a few moments.', + $response->code + ); + } - return parent::processResponse($response, $expectedCode); - } + return parent::processResponse($response, $expectedCode); + } } diff --git a/src/Package/Repositories/Statuses.php b/src/Package/Repositories/Statuses.php index 4136eaef..5d160c1c 100644 --- a/src/Package/Repositories/Statuses.php +++ b/src/Package/Repositories/Statuses.php @@ -1,4 +1,5 @@ $state, - ]; + // Build the request data. + $data = [ + 'state' => $state, + ]; - if ($targetUrl !== null) - { - $data['target_url'] = $targetUrl; - } + if ($targetUrl !== null) { + $data['target_url'] = $targetUrl; + } - if ($description !== null) - { - $data['description'] = $description; - } + if ($description !== null) { + $data['description'] = $description; + } - if ($context !== null) - { - $data['context'] = $context; - } + if ($context !== null) { + $data['context'] = $context; + } - // Send the request. - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($data)), - 201 - ); - } + // Send the request. + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($data)), + 201 + ); + } - /** - * List Statuses for a specific Ref. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $sha SHA1 for which to get the statuses. - * - * @return array - * - * @since 1.0 - */ - public function getList($user, $repo, $sha) - { - // Build the request path. - $path = "/repos/$user/$repo/statuses/$sha"; + /** + * List Statuses for a specific Ref. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $sha SHA1 for which to get the statuses. + * + * @return array + * + * @since 1.0 + */ + public function getList($user, $repo, $sha) + { + // Build the request path. + $path = "/repos/$user/$repo/statuses/$sha"; - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } - /** - * Get the combined Status for a specific Ref. - * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param string $sha SHA1 for which to get the combined status. - * - * @return array - * - * @since 1.4.0 - */ - public function getCombinedStatus($user, $repo, $sha) - { - // Build the request path. - $path = "/repos/$user/$repo/commits/$sha/status"; + /** + * Get the combined Status for a specific Ref. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param string $sha SHA1 for which to get the combined status. + * + * @return array + * + * @since 1.4.0 + */ + public function getCombinedStatus($user, $repo, $sha) + { + // Build the request path. + $path = "/repos/$user/$repo/commits/$sha/status"; - // Send the request. - return $this->processResponse($this->client->get($this->fetchUrl($path))); - } + // Send the request. + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } } diff --git a/src/Package/Search.php b/src/Package/Search.php index e50f4d79..f1a744c0 100644 --- a/src/Package/Search.php +++ b/src/Package/Search.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Search repositories. - * - * Find repositories by keyword. Note, this legacy method does not follow - * the v3 pagination pattern. - * This method returns up to 100 results per page and pages can be fetched - * using the start_page parameter. - * - * @param string $keyword The search term. - * @param string $language Filter results by language https://github.com/languages - * @param integer $startPage Page number to fetch - * - * @return object - * - * @since 1.0 - * @deprecated The legacy API is deprecated - */ - public function repositories($keyword, $language = '', $startPage = 0) - { - // Build the request path. - $uri = $this->fetchUrl('/legacy/repos/search/' . $keyword); + /** + * Search repositories. + * + * Find repositories by keyword. Note, this legacy method does not follow + * the v3 pagination pattern. + * This method returns up to 100 results per page and pages can be fetched + * using the start_page parameter. + * + * @param string $keyword The search term. + * @param string $language Filter results by language https://github.com/languages + * @param integer $startPage Page number to fetch + * + * @return object + * + * @since 1.0 + * @deprecated The legacy API is deprecated + */ + public function repositories($keyword, $language = '', $startPage = 0) + { + // Build the request path. + $uri = $this->fetchUrl('/legacy/repos/search/' . $keyword); - if ($language) - { - $uri->setVar('language', $language); - } + if ($language) { + $uri->setVar('language', $language); + } - if ($startPage) - { - $uri->setVar('start_page', $startPage); - } + if ($startPage) { + $uri->setVar('start_page', $startPage); + } - // Send the request. - return $this->processResponse($this->client->get($uri)); - } + // Send the request. + return $this->processResponse($this->client->get($uri)); + } - /** - * Search users. - * - * Find users by keyword. - * - * @param string $keyword The search term. - * @param integer $startPage Page number to fetch - * - * @return object - * - * @since 1.0 - * @deprecated The legacy API is deprecated - */ - public function users($keyword, $startPage = 0) - { - // Build the request path. - $uri = $this->fetchUrl('/legacy/user/search/' . $keyword); + /** + * Search users. + * + * Find users by keyword. + * + * @param string $keyword The search term. + * @param integer $startPage Page number to fetch + * + * @return object + * + * @since 1.0 + * @deprecated The legacy API is deprecated + */ + public function users($keyword, $startPage = 0) + { + // Build the request path. + $uri = $this->fetchUrl('/legacy/user/search/' . $keyword); - if ($startPage) - { - $uri->setVar('start_page', $startPage); - } + if ($startPage) { + $uri->setVar('start_page', $startPage); + } - // Send the request. - return $this->processResponse($this->client->get($uri)); - } + // Send the request. + return $this->processResponse($this->client->get($uri)); + } - /** - * Email search. - * - * This API call is added for compatibility reasons only. There’s no guarantee - * that full email searches will always be available. The @ character in the - * address must be left unencoded. Searches only against public email addresses - * (as configured on the user’s GitHub profile). - * - * @param string $email The email address(es). - * - * @return object - * - * @since 1.0 - * @deprecated The legacy API is deprecated - */ - public function email($email) - { - // Build the request path. - $path = '/legacy/user/email/' . $email; + /** + * Email search. + * + * This API call is added for compatibility reasons only. There’s no guarantee + * that full email searches will always be available. The @ character in the + * address must be left unencoded. Searches only against public email addresses + * (as configured on the user’s GitHub profile). + * + * @param string $email The email address(es). + * + * @return object + * + * @since 1.0 + * @deprecated The legacy API is deprecated + */ + public function email($email) + { + // Build the request path. + $path = '/legacy/user/email/' . $email; - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } } diff --git a/src/Package/Users.php b/src/Package/Users.php index 445df649..153d8bf6 100644 --- a/src/Package/Users.php +++ b/src/Package/Users.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Get the authenticated user. - * - * @return mixed - * - * @since 1.0 - * @throws \DomainException - */ - public function getAuthenticatedUser() - { - // Build the request path. - $path = '/user'; + /** + * Get the authenticated user. + * + * @return mixed + * + * @since 1.0 + * @throws \DomainException + */ + public function getAuthenticatedUser() + { + // Build the request path. + $path = '/user'; - // Send the request. - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Update the authenticated user. - * - * @param string $name The full name - * @param string $email The email - * @param string $blog The blog - * @param string $company The company - * @param string $location The location - * @param string $hireable If he is unemployed :P - * @param string $bio The biometrical DNA fingerprint (or smthng...) - * - * @return mixed - * - * @since 1.0 - * @throws \DomainException - */ - public function edit($name = '', $email = '', $blog = '', $company = '', $location = '', $hireable = '', $bio = '') - { - $data = [ - 'name' => $name, - 'email' => $email, - 'blog' => $blog, - 'company' => $company, - 'location' => $location, - 'hireable' => $hireable, - 'bio' => $bio, - ]; + /** + * Update the authenticated user. + * + * @param string $name The full name + * @param string $email The email + * @param string $blog The blog + * @param string $company The company + * @param string $location The location + * @param string $hireable If he is unemployed :P + * @param string $bio The biometrical DNA fingerprint (or smthng...) + * + * @return mixed + * + * @since 1.0 + * @throws \DomainException + */ + public function edit($name = '', $email = '', $blog = '', $company = '', $location = '', $hireable = '', $bio = '') + { + $data = [ + 'name' => $name, + 'email' => $email, + 'blog' => $blog, + 'company' => $company, + 'location' => $location, + 'hireable' => $hireable, + 'bio' => $bio, + ]; - // Build the request path. - $path = '/user'; + // Build the request path. + $path = '/user'; - // Send the request. - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), json_encode($data)) - ); - } + // Send the request. + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), json_encode($data)) + ); + } - /** - * Get all users. - * - * This provides a dump of every user, in the order that they signed up for GitHub. - * - * @param integer $since The integer ID of the last User that you’ve seen. - * - * @return object - * - * @since 1.0 - * @throws \DomainException - */ - public function getList($since = 0) - { - // Build the request path. - $uri = $this->fetchUrl('/users'); + /** + * Get all users. + * + * This provides a dump of every user, in the order that they signed up for GitHub. + * + * @param integer $since The integer ID of the last User that you’ve seen. + * + * @return object + * + * @since 1.0 + * @throws \DomainException + */ + public function getList($since = 0) + { + // Build the request path. + $uri = $this->fetchUrl('/users'); - if ($since) - { - $uri->setVar('since', $since); - } + if ($since) { + $uri->setVar('since', $since); + } - // Send the request. - return $this->processResponse($this->client->get($uri)); - } + // Send the request. + return $this->processResponse($this->client->get($uri)); + } } diff --git a/src/Package/Users/Emails.php b/src/Package/Users/Emails.php index ca7592a8..15f90033 100644 --- a/src/Package/Users/Emails.php +++ b/src/Package/Users/Emails.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } - /** - * Add email address(es). - * - * @param string|array $email The email address(es). - * - * @return object - * - * @since 1.0 - */ - public function add($email) - { - // Build the request path. - $path = '/user/emails'; + /** + * Add email address(es). + * + * @param string|array $email The email address(es). + * + * @return object + * + * @since 1.0 + */ + public function add($email) + { + // Build the request path. + $path = '/user/emails'; - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($email)), - 201 - ); - } + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($email)), + 201 + ); + } - /** - * Delete email address(es). - * - * @param string|array $email The email address(es). - * - * @return object - * - * @since 1.0 - */ - public function delete($email) - { - // Build the request path. - $path = '/user/emails'; + /** + * Delete email address(es). + * + * @param string|array $email The email address(es). + * + * @return object + * + * @since 1.0 + */ + public function delete($email) + { + // Build the request path. + $path = '/user/emails'; - $this->client->setOption('body', json_encode($email)); + $this->client->setOption('body', json_encode($email)); - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } } diff --git a/src/Package/Users/Followers.php b/src/Package/Users/Followers.php index 8df9bc39..b45afec6 100644 --- a/src/Package/Users/Followers.php +++ b/src/Package/Users/Followers.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List users followed by another user. - * - * @param string $user The name of the user. If not set the current authenticated user will be used. - * - * @return object - * - * @since 1.0 - */ - public function getListFollowedBy($user = '') - { - // Build the request path. - $path = ($user) - ? '/users/' . $user . '/following' - : '/user/following'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Check if you are following a user. - * - * @param string $user The name of the user. - * - * @return boolean - * - * @since 1.0 - * @throws \UnexpectedValueException - */ - public function check($user) - { - // Build the request path. - $path = '/user/following/' . $user; - - $response = $this->client->get($this->fetchUrl($path)); - - switch ($response->code) - { - case '204' : - // You are following this user - return true; - - case '404' : - // You are not following this user - return false; - - default : - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - } - - /** - * Check if one user follows another. - * - * @param string $user The name of the user. - * @param string $target The name of the user to check is being followed. - * - * @return boolean - * - * @since 1.4.0 - * @throws \UnexpectedValueException - */ - public function checkUserFollowing($user, $target) - { - // Build the request path. - $path = "/user/$user/following/$target"; - - $response = $this->client->get($this->fetchUrl($path)); - - switch ($response->code) - { - case '204' : - // User is following the target - return true; - - case '404' : - // User is not following the target - return false; - - default : - throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); - } - } - - /** - * Follow a user. - * - * Following a user requires the user to be logged in and authenticated with - * basic auth or OAuth with the user:follow scope. - * - * @param string $user The name of the user. - * - * @return object - * - * @since 1.0 - */ - public function follow($user) - { - // Build the request path. - $path = '/user/following/' . $user; - - return $this->processResponse( - $this->client->put($this->fetchUrl($path), ''), - 204 - ); - } - - /** - * Unfollow a user. - * - * Unfollowing a user requires the user to be logged in and authenticated with - * basic auth or OAuth with the user:follow scope. - * - * @param string $user The name of the user. - * - * @return object - * - * @since 1.0 - */ - public function unfollow($user) - { - // Build the request path. - $path = '/user/following/' . $user; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + /** + * List followers of a user. + * + * @param string $user The name of the user. If not set the current authenticated user will be used. + * + * @return object + * + * @since 1.0 + */ + public function getList($user = '') + { + // Build the request path. + $path = ($user) + ? '/users/' . $user . '/followers' + : '/user/followers'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List users followed by another user. + * + * @param string $user The name of the user. If not set the current authenticated user will be used. + * + * @return object + * + * @since 1.0 + */ + public function getListFollowedBy($user = '') + { + // Build the request path. + $path = ($user) + ? '/users/' . $user . '/following' + : '/user/following'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Check if you are following a user. + * + * @param string $user The name of the user. + * + * @return boolean + * + * @since 1.0 + * @throws \UnexpectedValueException + */ + public function check($user) + { + // Build the request path. + $path = '/user/following/' . $user; + + $response = $this->client->get($this->fetchUrl($path)); + + switch ($response->code) { + case '204': + // You are following this user + return true; + + case '404': + // You are not following this user + return false; + + default: + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + } + + /** + * Check if one user follows another. + * + * @param string $user The name of the user. + * @param string $target The name of the user to check is being followed. + * + * @return boolean + * + * @since 1.4.0 + * @throws \UnexpectedValueException + */ + public function checkUserFollowing($user, $target) + { + // Build the request path. + $path = "/user/$user/following/$target"; + + $response = $this->client->get($this->fetchUrl($path)); + + switch ($response->code) { + case '204': + // User is following the target + return true; + + case '404': + // User is not following the target + return false; + + default: + throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); + } + } + + /** + * Follow a user. + * + * Following a user requires the user to be logged in and authenticated with + * basic auth or OAuth with the user:follow scope. + * + * @param string $user The name of the user. + * + * @return object + * + * @since 1.0 + */ + public function follow($user) + { + // Build the request path. + $path = '/user/following/' . $user; + + return $this->processResponse( + $this->client->put($this->fetchUrl($path), ''), + 204 + ); + } + + /** + * Unfollow a user. + * + * Unfollowing a user requires the user to be logged in and authenticated with + * basic auth or OAuth with the user:follow scope. + * + * @param string $user The name of the user. + * + * @return object + * + * @since 1.0 + */ + public function unfollow($user) + { + // Build the request path. + $path = '/user/following/' . $user; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } } diff --git a/src/Package/Users/Keys.php b/src/Package/Users/Keys.php index 52aa501e..d8c5091e 100644 --- a/src/Package/Users/Keys.php +++ b/src/Package/Users/Keys.php @@ -1,4 +1,5 @@ processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * List your public keys. - * - * Lists the current user’s keys. - * Management of public keys via the API requires that you are authenticated - * through basic auth, or OAuth with the ‘user’ scope. - * - * @return object - * - * @since 1.0 - */ - public function getList() - { - // Build the request path. - $path = '/users/keys'; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Get a single public key. - * - * @param integer $id The id of the key. - * - * @return object - * - * @since 1.0 - */ - public function get($id) - { - // Build the request path. - $path = '/users/keys/' . $id; - - return $this->processResponse( - $this->client->get($this->fetchUrl($path)) - ); - } - - /** - * Create a public key - * - * @param string $title The title of the key. - * @param string $key The key. - * - * @return object - * - * @since 1.0 - */ - public function create($title, $key) - { - // Build the request path. - $path = '/users/keys'; - - $data = [ - 'title' => $title, - 'key' => $key, - ]; - - return $this->processResponse( - $this->client->post($this->fetchUrl($path), json_encode($data)), - 201 - ); - } - - /** - * Update a public key. - * - * @param integer $id The id of the key. - * @param string $title The title of the key. - * @param string $key The key. - * - * @return object - * - * @since 1.0 - */ - public function edit($id, $title, $key) - { - // Build the request path. - $path = '/users/keys/' . $id; - - $data = [ - 'title' => $title, - 'key' => $key, - ]; - - return $this->processResponse( - $this->client->patch($this->fetchUrl($path), json_encode($data)) - ); - } - - /** - * Delete a public key. - * - * @param integer $id The id of the key. - * - * @return object - * - * @since 1.0 - */ - public function delete($id) - { - // Build the request path. - $path = '/users/keys/' . (int) $id; - - return $this->processResponse( - $this->client->delete($this->fetchUrl($path)), - 204 - ); - } + /** + * List public keys for a user. + * + * Lists the verified public keys for a user. This is accessible by anyone. + * + * @param string $user The name of the user. + * + * @return object + * + * @since 1.0 + */ + public function getListUser($user) + { + // Build the request path. + $path = '/users/' . $user . '/keys'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * List your public keys. + * + * Lists the current user’s keys. + * Management of public keys via the API requires that you are authenticated + * through basic auth, or OAuth with the ‘user’ scope. + * + * @return object + * + * @since 1.0 + */ + public function getList() + { + // Build the request path. + $path = '/users/keys'; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Get a single public key. + * + * @param integer $id The id of the key. + * + * @return object + * + * @since 1.0 + */ + public function get($id) + { + // Build the request path. + $path = '/users/keys/' . $id; + + return $this->processResponse( + $this->client->get($this->fetchUrl($path)) + ); + } + + /** + * Create a public key + * + * @param string $title The title of the key. + * @param string $key The key. + * + * @return object + * + * @since 1.0 + */ + public function create($title, $key) + { + // Build the request path. + $path = '/users/keys'; + + $data = [ + 'title' => $title, + 'key' => $key, + ]; + + return $this->processResponse( + $this->client->post($this->fetchUrl($path), json_encode($data)), + 201 + ); + } + + /** + * Update a public key. + * + * @param integer $id The id of the key. + * @param string $title The title of the key. + * @param string $key The key. + * + * @return object + * + * @since 1.0 + */ + public function edit($id, $title, $key) + { + // Build the request path. + $path = '/users/keys/' . $id; + + $data = [ + 'title' => $title, + 'key' => $key, + ]; + + return $this->processResponse( + $this->client->patch($this->fetchUrl($path), json_encode($data)) + ); + } + + /** + * Delete a public key. + * + * @param integer $id The id of the key. + * + * @return object + * + * @since 1.0 + */ + public function delete($id) + { + // Build the request path. + $path = '/users/keys/' . (int) $id; + + return $this->processResponse( + $this->client->delete($this->fetchUrl($path)), + 204 + ); + } } diff --git a/src/Package/Zen.php b/src/Package/Zen.php index bc012718..029bcf8d 100644 --- a/src/Package/Zen.php +++ b/src/Package/Zen.php @@ -1,4 +1,5 @@ client->get($this->fetchUrl('/zen')); + /** + * Get a random response about one of our design philosophies. + * + * @throws \RuntimeException + * + * @return string + */ + public function get() + { + $response = $this->client->get($this->fetchUrl('/zen')); - if ($response->code != 200) - { - throw new \RuntimeException('Can\'t get a Zen'); - } + if ($response->code != 200) { + throw new \RuntimeException('Can\'t get a Zen'); + } - return $response->body; - } + return $response->body; + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy