From d318bd0d42a6e273b1845d76b9f5f531d5606ed4 Mon Sep 17 00:00:00 2001 From: weryques Date: Mon, 27 Nov 2017 12:32:03 -0200 Subject: [PATCH] Test endpoint 'collections' --- composer.json | 3 +- composer.lock | 237 +++++++++++++++++- .../class-tainacan-rest-items-controller.php | 11 + src/api/tainacan-rest-creator.php | 2 +- .../entities/class-tainacan-collection.php | 30 +-- tests/test-api-collections.php | 40 +++ 6 files changed, 305 insertions(+), 18 deletions(-) create mode 100644 src/api/endpoints/class-tainacan-rest-items-controller.php create mode 100644 tests/test-api-collections.php diff --git a/composer.json b/composer.json index e1a34004c..700c8ea35 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,8 @@ "description": "Protótipo", "type": "wordpress-plugin", "require": { - "respect/validation": "^1.1" + "respect/validation": "^1.1", + "guzzlehttp/guzzle": "^6.2@dev" }, "minimum-stability": "dev", "config": { diff --git a/composer.lock b/composer.lock index 21706ee0d..a95c05091 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,239 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "e0eb272f68b3132a64ef299d34cf9700", + "content-hash": "06908e8219e421f4b636b0159525e648", "packages": [ + { + "name": "guzzlehttp/guzzle", + "version": "6.3.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "f4db5a78a5ea468d4831de7f0bf9d9415e348699" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/f4db5a78a5ea468d4831de7f0bf9d9415e348699", + "reference": "f4db5a78a5ea468d4831de7f0bf9d9415e348699", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.4", + "php": ">=5.5" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "^4.0 || ^5.0", + "psr/log": "^1.0" + }, + "suggest": { + "psr/log": "Required for using the Log middleware" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.2-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2017-06-22T18:50:49+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "e9cdab6ff93ff789b5b599326c727f51d10893a6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/e9cdab6ff93ff789b5b599326c727f51d10893a6", + "reference": "e9cdab6ff93ff789b5b599326c727f51d10893a6", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.36" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2017-10-06T12:25:00+00:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "d2537c86fa8b004c29e9b9f5e10028f0a29df101" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/d2537c86fa8b004c29e9b9f5e10028f0a29df101", + "reference": "d2537c86fa8b004c29e9b9f5e10028f0a29df101", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Schultze", + "homepage": "https://github.com/Tobion" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "request", + "response", + "stream", + "uri", + "url" + ], + "time": "2017-10-07T03:19:56+00:00" + }, + { + "name": "psr/http-message", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" + }, { "name": "respect/validation", "version": "1.1.x-dev", @@ -134,7 +365,9 @@ "packages-dev": [], "aliases": [], "minimum-stability": "dev", - "stability-flags": [], + "stability-flags": { + "guzzlehttp/guzzle": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": [], diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php new file mode 100644 index 000000000..4433150d0 --- /dev/null +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/src/api/tainacan-rest-creator.php b/src/api/tainacan-rest-creator.php index add4add25..2e1e6f2b9 100644 --- a/src/api/tainacan-rest-creator.php +++ b/src/api/tainacan-rest-creator.php @@ -1,6 +1,6 @@ 0) { $post = get_post($which); - + if ($post instanceof \WP_Post) { $this->WP_Post = get_post($which); } - + } elseif ($which instanceof \WP_Post) { $this->WP_Post = $which; } else { @@ -41,7 +41,9 @@ class Collection extends Entity { 'name' => $this->get_name(), 'description' => $this->get_description(), 'items_per_page' => $this->get_itens_per_page(), - ] + ], + JSON_NUMERIC_CHECK, + JSON_UNESCAPED_UNICODE ); } /** @@ -68,9 +70,9 @@ class Collection extends Entity { 'parent_item_colon' => 'Item acima:', 'menu_name' => $this->get_name() ); - + $cpt_slug = $this->get_db_identifier(); - + $args = array( 'labels' => $cpt_labels, 'hierarchical' => true, @@ -91,10 +93,10 @@ class Collection extends Entity { ], 'capability_type' => 'post', ); - - if (post_type_exists($this->get_db_identifier())) + + if (post_type_exists($this->get_db_identifier())) unregister_post_type($this->get_db_identifier()); - + register_post_type($cpt_slug, $args); }*/ @@ -159,7 +161,7 @@ class Collection extends Entity { function get_itens_per_page() { return $this->get_mapped_property('itens_per_page'); } - + /** * Get collection DB identifier * @@ -170,7 +172,7 @@ class Collection extends Entity { function get_db_identifier() { return $this->get_id() ? 'tnc_col_' . $this->get_id() : false; } - + /** * Get collection metadata. * @@ -184,7 +186,7 @@ class Collection extends Entity { $Tainacan_Metadatas = new \Tainacan\Repositories\Metadatas(); return $Tainacan_Metadatas->fetch($this); } - + /** * Set the collection name * @@ -198,7 +200,7 @@ class Collection extends Entity { /** * Set the collection slug * - * If you dont set the collection slug, it will be set automatically based on the name and + * If you dont set the collection slug, it will be set automatically based on the name and * following WordPress default behavior of creating slugs for posts. * * If you set the slug for an existing one, WordPress will append a number at the end of in order @@ -250,4 +252,4 @@ class Collection extends Entity { function set_itens_per_page($value) { $this->set_mapped_property('itens_per_page', $value); } -} \ No newline at end of file +} diff --git a/tests/test-api-collections.php b/tests/test-api-collections.php new file mode 100644 index 000000000..ea3ccb36d --- /dev/null +++ b/tests/test-api-collections.php @@ -0,0 +1,40 @@ +client = new Client([ + 'base_uri' => self::URL, + ]); + } + + public function test_fetch_collection_by_id(){ + $id = 1; + $response = $this->client->request('GET', 'wp-json/tainacan/v2/collections/'. $id); + + $this->assertEquals(200, $response->getStatusCode()); + + $data = json_decode(json_decode($response->getBody(), true)); + var_dump($data); + $this->assertEquals('teste', $data->name); + } + + public function test_fetch_collections(){ + $response = $this->client->request('GET', 'wp-json/tainacan/v2/collections/'); + + $this->assertEquals(200, $response->getStatusCode()); + + $data = json_decode($response->getBody(), true); + var_dump($data); + } + +} + +?>