diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..f2eb370 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,15 @@ +build: + environment: + php: + version: 5.4 # Common versions: 5.4, 5.5, 5.6, 7.0 or hhvm + tests: + override: + - + command: 'phpunit -c tests/phpunit.xml --coverage-clover=clover.xml' + coverage: + file: 'clover.xml' + format: 'php-clover' +checks: + php: + custom_coding_standard: + ruleset_path: 'ruleset.xml' diff --git a/.travis.yml b/.travis.yml index b511d66..14276e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,22 +1,23 @@ language: php php: - - 5.5 - - 5.4 - 5.3 + - 5.4 + - 5.5 + - 5.6 + - 7 + - hhvm + - nightly matrix: allow_failures: - - php: 5.5 + - php: 7 + - php: hhvm + - php: nightly before_script: - curl -s http://getcomposer.org/installer | php - php composer.phar update --no-interaction --dev script: - - mkdir -p build/logs - php vendor/bin/phpunit -c tests/phpunit.xml - -after_script: - - php vendor/bin/coveralls - # or enable logging - # - php vendor/bin/coveralls -v + - vendor/bin/phpcs --standard=ruleset.xml --runtime-set ignore_warnings_on_exit 1 src diff --git a/README.md b/README.md index f6e728a..9b0193c 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A PHP interface to the YOTPO API First clone the repository: - git clone git@github.com:YotpoLtd/yotpo-php.git + git clone https://github.com/YotpoLtd/yotpo-php.git Then download and install the composer dependencies: @@ -24,38 +24,42 @@ Then download and install the composer dependencies: First [register your application with Yotpo][register] Then copy and past the app_key and secret -```php -include "yotpo-php/bootstrap.php"; +```php5 +// Require composer autoloader +require 'vendor/autoload.php'; + +use Yotpo\Yotpo; $ak = "APP_KEY"; $st = "SECRET"; -$yotpo = new \Yotpo\Yotpo($ak, $st); +$yotpo = new Yotpo($ak, $st); ``` -That's it, you are ready. -Now lets make some public calls to our api. Public calls only require you to use a valid app_key. +That's it, you are ready. + +Now lets make some public calls to our api. Public calls only require you to use a valid app_key. Creating your first review using the API -```php +```php5 $yotpo->create_review(array( - 'app_key' => $ak, - 'product_id' => "BLABLA", - 'shop_domain' => "omri.co", - 'product_title' => "pt", - 'product_description' => "pd", - 'product_url' => "http://google.com/?q=myproducturl", - 'product_image_url' => "https://www.google.com/images/srpr/logo4w.png", - 'user_display_name' => "MOSHE5656", - 'user_email' => 'haim@yotpo.com', - 'review_body' => "this is my review body", - 'review_title' => "my review title" , - 'review_score' => 5 + 'app_key' => $ak, + 'product_id' => "BLABLA", + 'shop_domain' => "omri.co", + 'product_title' => "pt", + 'product_description' => "pd", + 'product_url' => "http://google.com/?q=myproducturl", + 'product_image_url' => "https://www.google.com/images/srpr/logo4w.png", + 'user_display_name' => "MOSHE5656", + 'user_email' => 'haim@yotpo.com', + 'review_body' => "this is my review body", + 'review_title' => "my review title", + 'review_score' => 5 )); ``` and now lets retrieve all the reviews of our product BLABLA -```php +```php5 $response = $yotpo->get_product_reviews(array('app_key' => $ak, 'product_id' => "BLABLA")); @@ -65,7 +69,7 @@ echo $response->response->reviews[0]->score; getting the bottom line of product BLABLA -```php +```php5 $response = $yotpo->get_product_bottom_line(array('app_key' => $ak, 'product_id' => "BLABLA")); echo $response->response->bottomline->average_score; @@ -76,8 +80,7 @@ Now lets try something a little bit more complicated. Lets try to create a purch For that we will need to go through Yotpo authenticaton process, provide an app_key and secret, and return to get the utoken. The utoken will allow us to make authenticated API calls. -```php - +```php5 // retrieving the utoken - will be valid for 24 hours $credentials = $yotpo->get_oauth_token(); $utoken = $credentials->access_token; @@ -85,33 +88,32 @@ $utoken = $credentials->access_token; // first creating the products that are in the order, notice that the key of the product hash is the product_sku $products = array( "BLABLA1" => array( - 'url' => "http://shop.yotpo.com/products/amazing-yotpo-poster", - 'name' => "Yotpo Amazing Poster", - 'image_url' => "http://cdn.shopify.com/s/files/1/0098/1912/products/qa2_medium.png?41", - 'description' => "this is the most awesome poster in the world!", + 'url' => "http://shop.yotpo.com/products/amazing-yotpo-poster", + 'name' => "Yotpo Amazing Poster", + 'image_url' => "http://cdn.shopify.com/s/files/1/0098/1912/products/qa2_medium.png?41", + 'description' => "this is the most awesome poster in the world!", 'price' => "100" ) ); //now we will create a purchase using this the token we have received in the previous step -$response = $yotpo->create_purchase(array( 'app_key' => $ak, - 'utoken' => $utoken, - 'email' => "trial@yotpo.com", - 'customer_name' => "bob", - 'order_id' => "13444", - 'platform' => "Shopify", - 'order_date' => "2013-05-28", - 'products' => $products, +$response = $yotpo->create_purchase(array( 'app_key' => $ak, + 'utoken' => $utoken, + 'email' => "trial@yotpo.com", + 'customer_name' => "bob", + 'order_id' => "13444", + 'platform' => "Shopify", + 'order_date' => "2013-05-28", + 'products' => $products, 'currency_iso' => "USD" )); - ``` We can pull all the purchases of a certain account to make sure that the previous calls has worked -```php +```php5 $response = $yotpo->get_purchases(array('app_key' => $ak, 'utoken' => $utoken, 'since_date' => "2013-05-26")); echo $response->response->total_purchases; ``` diff --git a/bootstrap.php b/bootstrap.php deleted file mode 100644 index 78d15d6..0000000 --- a/bootstrap.php +++ /dev/null @@ -1,4 +0,0 @@ -=5.3.2", "nategood/httpful": "*" - }, "require-dev": { - "satooshi/php-coveralls": "dev-master", - "phpunit/phpunit": "3.7.*@dev" + "squizlabs/php_codesniffer": "2.*", + "phpunit/phpunit": "4.8.*" }, "autoload": { "psr-0": { "Yotpo": "src/" } } -} \ No newline at end of file +} diff --git a/ruleset.xml b/ruleset.xml new file mode 100644 index 0000000..5d23b30 --- /dev/null +++ b/ruleset.xml @@ -0,0 +1,9 @@ + + + + The QuickPay ruleset based on the PSR-2 coding standard. + + + 0 + + diff --git a/src/Yotpo/Bootstrap.php b/src/Yotpo/Bootstrap.php deleted file mode 100644 index 3408ac0..0000000 --- a/src/Yotpo/Bootstrap.php +++ /dev/null @@ -1,67 +0,0 @@ - - */ -class Bootstrap { - - const DIR_GLUE = DIRECTORY_SEPARATOR; - const NS_GLUE = '\\'; - - public static $registered = false; - - /** - * Register the autoloader and any other setup needed - */ - public static function init() - { - spl_autoload_register(array('\Yotpo\Bootstrap', 'autoload')); - } - - /** - * The autoload magic (PSR-0 style) - * - * @param string $classname - */ - public static function autoload($classname) - { - self::_autoload(dirname(dirname(__FILE__)), $classname); - } - - /** - * Register the autoloader and any other setup needed - */ - public static function pharInit() - { - spl_autoload_register(array('\Yotpo\Bootstrap', 'pharAutoload')); - } - - /** - * Phar specific autoloader - * - * @param string $classname - */ - public static function pharAutoload($classname) - { - self::_autoload('phar://yotpo.phar', $classname); - } - - /** - * @param string base - * @param string classname - */ - private static function _autoload($base, $classname) - { - $parts = explode(self::NS_GLUE, $classname); - $path = $base . self::DIR_GLUE . implode(self::DIR_GLUE, $parts) . '.php'; - - if (file_exists($path)) { - require_once($path); - } - } -} diff --git a/src/Yotpo/Yotpo.php b/src/Yotpo/Yotpo.php index c6ef904..3346475 100644 --- a/src/Yotpo/Yotpo.php +++ b/src/Yotpo/Yotpo.php @@ -11,16 +11,20 @@ * * @author vlad */ -class Yotpo { +class Yotpo +{ const VERSION = '0.0.3'; - protected static $app_key, $secret, $base_uri = 'https://api.yotpo.com'; + protected static $app_key; + protected static $secret; + protected static $base_uri = 'https://api.yotpo.com'; - public function __construct($app_key = null, $secret = null, $base_uri = null) { + public function __construct($app_key = null, $secret = null, $base_uri = null) + { $this->set_app_key($app_key); - - $this->set_secret($secret); + + $this->set_secret($secret); if ($base_uri != null) { self::$base_uri = $base_uri; @@ -36,33 +40,38 @@ public function __construct($app_key = null, $secret = null, $base_uri = null) { Request::ini($template); } - protected function get($uri, array $params = null) { - if(!is_null($params)){ + protected function get($uri, array $params = null) + { + if (!is_null($params)) { $params = self::clean_array($params); return self::process_response(Request::get(self::$base_uri . $uri .'?'. http_build_query($params))->send()); } return self::process_response(Request::get(self::$base_uri . $uri)->send()); } - protected function post($uri, array $params = null) { + protected function post($uri, array $params = null) + { $params = self::clean_array($params); return self::process_response(Request::post(self::$base_uri . $uri)->body($params)->send()); } - protected function put($uri, array $params = null) { + protected function put($uri, array $params = null) + { $params = self::clean_array($params); return self::process_response(Request::put(self::$base_uri . $uri)->body($params)->send()); } - protected function delete($uri, array $params = null) { - if(!is_null($params)){ + protected function delete($uri, array $params = null) + { + if (!is_null($params)) { $params = self::clean_array($params); return self::process_response(Request::delete(self::$base_uri . $uri .'?'. http_build_query($params))->send()); } return self::process_response(Request::delete(self::$base_uri . $uri)->send()); } - protected static function process_response(Response $response) { + protected static function process_response(Response $response) + { if ($response->hasBody()) { return $response->body; } else { @@ -70,7 +79,8 @@ protected static function process_response(Response $response) { } } - public function create_user(array $user_hash) { + public function create_user(array $user_hash) + { $user = array( 'email' => $user_hash['email'], 'display_name' => $user_hash['display_name'], @@ -85,7 +95,8 @@ public function create_user(array $user_hash) { return $this->post('/users', array('user' => $user)); } - public function get_oauth_token(array $credentials_hash = array()) { + public function get_oauth_token(array $credentials_hash = array()) + { $request = array( 'grant_type' => 'client_credentials' ); @@ -101,7 +112,8 @@ public function get_oauth_token(array $credentials_hash = array()) { return $this->post('/oauth/token', $request); } - public function create_account_platform(array $account_platform_hash) { + public function create_account_platform(array $account_platform_hash) + { $account_platform = self::build_request(array('shop_token' => 'shop_token', 'shop_domain' => 'shop_domain', 'plan_name' => 'plan_name', 'platform_type_id' => 'platform_type_id'), $account_platform_hash); $account_platform['deleted'] = false; $request = array('utoken' => $account_platform_hash['utoken'], 'account_platform' => $account_platform); @@ -109,7 +121,8 @@ public function create_account_platform(array $account_platform_hash) { return $this->post("/apps/$app_key/account_platform", $request); } - public function get_login_url(array $credentials_hash = null) { + public function get_login_url(array $credentials_hash = null) + { $request = array(); $request['app_key'] = $app_key = $this->get_app_key($credentials_hash); if (!is_null($credentials_hash) && array_key_exists('secret', $credentials_hash)) { @@ -121,36 +134,41 @@ public function get_login_url(array $credentials_hash = null) { return $this->get('/users/b2blogin.json', $request); } - public function check_subdomain(array $subdomain_hash) { + public function check_subdomain(array $subdomain_hash) + { $app_key = $this->get_app_key($subdomain_hash); $subdomain = $subdomain_hash['subdomain']; - if(is_null($subdomain)){ + if (is_null($subdomain)) { throw new \Exception('subdomain Can not be blank'); } $utoken = $subdomain_hash['utoken']; return $this->get("/apps/$app_key/subomain_check/$subdomain?utoken=$utoken"); } - public function update_account(array $account_hash) { + public function update_account(array $account_hash) + { $request = array( 'account' => self::build_request( - array( + array( 'minisite_website_name' => 'minisite_website_name', 'minisite_website' => 'minisite_website', 'minisite_subdomain' => 'minisite_subdomain', 'minisite_cname' => 'minisite_cname', 'minisite_subdomain_active' => 'minisite_subdomain_active' - ), $account_hash), - 'utoken' => $account_hash['utoken'] + ), + $account_hash + ), + 'utoken' => $account_hash['utoken'] ); $app_key = $this->get_app_key($account_hash); return $this->put("/apps/$app_key", $request); } - public function create_purchase(array $purchase_hash) { + public function create_purchase(array $purchase_hash) + { $request = self::build_request( - array( + array( 'utoken' => 'utoken', 'email' => 'email', 'customer_name' => 'customer_name', @@ -159,18 +177,22 @@ public function create_purchase(array $purchase_hash) { 'order_id' => 'order_id', 'platform' => 'platform', 'products' => 'products' - ), $purchase_hash); - $app_key = $this->get_app_key($purchase_hash); - return $this->post("/apps/$app_key/purchases", $request); + ), + $purchase_hash + ); + $app_key = $this->get_app_key($purchase_hash); + return $this->post("/apps/$app_key/purchases", $request); } - public function create_purchases(array $purchases_hash) { + public function create_purchases(array $purchases_hash) + { $request = self::build_request(array('utoken' => 'utoken', 'platform' => 'platform', 'orders' => 'orders'), $purchases_hash); $app_key = $this->get_app_key($purchases_hash); return $this->post("/apps/$app_key/purchases/mass_create", $request); } - public function get_purchases(array $request_hash) { + public function get_purchases(array $request_hash) + { $request = self::build_request(array('utoken' => 'utoken', 'since_id' => 'since_id', 'since_date' => 'since_date', 'page' => 'page', 'count' => 'count'), $request_hash); if (!array_key_exists('page', $request)) { $request['page'] = 1; @@ -182,19 +204,22 @@ public function get_purchases(array $request_hash) { return $this->get("/apps/$app_key/purchases", $request); } - public function send_test_reminder(array $reminder_hash) { + public function send_test_reminder(array $reminder_hash) + { $request = self::build_request(array('utoken' => 'utoken', 'email' => 'email'), $reminder_hash); $app_key = $this->get_app_key($reminder_hash); return $this->post("/apps/$app_key/reminders/send_test_email", $request); } - public function get_all_bottom_lines(array $request_hash) { + public function get_all_bottom_lines(array $request_hash) + { $request = self::build_request(array('utoken' => 'utoken', 'since_date' => 'since_date', 'since_id' => 'since_id'), $request_hash); $app_key = $this->get_app_key($request_hash); return $this->get("/apps/$app_key/bottom_lines", $request); } - public function create_review(array $review_hash) { + public function create_review(array $review_hash) + { $params = array( 'app_key' => 'appkey', 'product_id' => 'sku', @@ -215,7 +240,8 @@ public function create_review(array $review_hash) { return $this->get('/reviews/dynamic_create', $request); } - public function get_product_reviews(array $request_hash) { + public function get_product_reviews(array $request_hash) + { $app_key = $this->get_app_key($request_hash); $product_id = $request_hash['product_id']; @@ -233,7 +259,8 @@ public function get_product_reviews(array $request_hash) { return $this->get("/products/$app_key/$product_id/reviews", $request_params); } - public function get_product_bottom_line(array $request_hash) { + public function get_product_bottom_line(array $request_hash) + { $app_key = $this->get_app_key($request_hash); $product_id = $request_hash['product_id']; @@ -242,30 +269,34 @@ public function get_product_bottom_line(array $request_hash) { } return $this->get("/products/$app_key/$product_id/bottomline"); } - - public function set_app_key($app_key) { + + public function set_app_key($app_key) + { if ($app_key != null) { self::$app_key = $app_key; } } - - public function set_secret($secret) { + + public function set_secret($secret) + { if ($secret != null) { self::$secret = $secret; } } - protected function get_app_key($hash){ - if(!is_null($hash) && !empty($hash) && array_key_exists('app_key', $hash)){ + protected function get_app_key($hash) + { + if (!is_null($hash) && !empty($hash) && array_key_exists('app_key', $hash)) { return $hash['app_key']; } elseif (self::$app_key != null) { - return self::$app_key; - }else { + return self::$app_key; + } else { throw new \Exception('app_key is mandatory for this request'); } } - protected static function build_request(array $params, array $request_params) { + protected static function build_request(array $params, array $request_params) + { $request = array(); foreach ($params as $key => $value) { if (array_key_exists($key, $request_params)) { @@ -274,21 +305,22 @@ protected static function build_request(array $params, array $request_params) { } return $request; } - - protected static function clean_array(array $array){ - - foreach( $array as $key => $value ) { - if( is_array( $value ) ) { - foreach( $value as $key2 => $value2 ) { - if( empty( $value2 ) ) - unset( $array[ $key ][ $key2 ] ); + + protected static function clean_array(array $array) + { + + foreach ($array as $key => $value) { + if (is_array($value)) { + foreach ($value as $key2 => $value2) { + if (empty($value2)) { + unset($array[ $key ][ $key2 ]); + } } } - if( empty( $array[ $key ] ) ) - unset( $array[ $key ] ); + if (empty($array[ $key ])) { + unset($array[ $key ]); + } } return $array; } - } - diff --git a/tests/Yotpo/YotpoTest.php b/tests/Yotpo/YotpoTest.php index 9c0f9d1..09d903a 100644 --- a/tests/Yotpo/YotpoTest.php +++ b/tests/Yotpo/YotpoTest.php @@ -6,33 +6,42 @@ namespace Yotpo\Test; -require(dirname(dirname(dirname(__FILE__))) . '/bootstrap.php'); -\Yotpo\Bootstrap::init(); +use Yotpo\Yotpo; -class YotpoTest extends \PHPUnit_Framework_TestCase { +class YotpoTest extends \PHPUnit_Framework_TestCase +{ const TEST_APP_KEY = 'c2cThXB8foo9O63Xj4hx2L4SJFiioCJPsIOP83dr'; const TEST_SECRET = 'DgGS4b7dBEAUbx5hYE29S0TKIpypDGpShVMjYFlz'; private $utoken = null; private $yotpo = null; - public function setUp(){ + + public function setUp() + { $this->yotpo = new \Yotpo\Yotpo(self::TEST_APP_KEY, self::TEST_SECRET); - $this->utoken = $this->yotpo->get_oauth_token()->access_token; + if (isset($this->yotpo->get_oauth_token()->access_token)) { + $this->utoken = $this->yotpo->get_oauth_token()->access_token; + } else { + $this->markTestSkipped('No access token available.'); + } } - - public function testInit() { + + public function testConstructor() + { $this->assertEquals('Yotpo\Yotpo', get_class($this->yotpo)); } - public function test_get_oauth_token() { + public function testGetOauthToken() + { $yotpo = new \Yotpo\Yotpo(self::TEST_APP_KEY, self::TEST_SECRET); $credentials = $yotpo->get_oauth_token(); $this->assertObjectHasAttribute('access_token', $credentials); $this->assertObjectHasAttribute('token_type', $credentials); } - public function test_create_user() { + public function testCreateUser() + { $user = $this->yotpo->create_user(array('email' => 'moshe1@ynet.co.il', 'display_name' => 'Moshe The PHP Tester', 'first_name' => 'Moshe', @@ -46,7 +55,8 @@ public function test_create_user() { $this->assertEquals(200, $user->status->code, 'Code was not 200'); } - public function test_create_account_platform() { + public function testCreateAccountPlatform() + { $account_platform_hash = array( 'utoken' => $this->utoken, 'shop_token' => $this->utoken, @@ -59,14 +69,16 @@ public function test_create_account_platform() { $this->assertEquals(200, $account_platform->status->code, 'Code was not 200'); } - public function test_get_login_url() { + public function testGetLoginUrl() + { $login_url = $this->yotpo->get_login_url(); $this->assertObjectHasAttribute('code', $login_url->status); $this->assertEquals(200, $login_url->status->code, 'Code was not 200'); $this->assertNotEmpty($login_url->response->signin_url); } - public function test_check_subdomain() { + public function testCheckSubdomain() + { $subdomain_hash = array( 'subdomain' => 'wwwgooglecom', 'utoken' => $this->utoken @@ -76,7 +88,8 @@ public function test_check_subdomain() { $this->assertEquals(200, $response->status->code, 'Code was not 200'); } - public function test_update_account() { + public function testUpdateAccount() + { $account_hash = array( 'utoken' => $this->utoken, 'minisite_website_name' => 'Moshe!', @@ -90,7 +103,8 @@ public function test_update_account() { $this->assertEquals(200, $response->status->code, 'Code was not 200'); } - public function test_create_purchase() { + public function testCreatePurchase() + { $purchase_hash = array( 'utoken' => $this->utoken, 'email' => 'customer@the.com', @@ -105,7 +119,7 @@ public function test_create_purchase() { 'name' => 'product1', 'image' => 'http://example_product_image_url1.com', 'description' => 'this is the description of a product' - ) + ) ) ); $response = $this->yotpo->create_purchase($purchase_hash); @@ -113,7 +127,8 @@ public function test_create_purchase() { $this->assertEquals(200, $response->status->code, 'Code was not 200'); } - public function test_create_purchases() { + public function testCreatePurchases() + { $purchases_hash = array( 'utoken' => $this->utoken, 'platform' => 'general', @@ -129,7 +144,7 @@ public function test_create_purchases() { 'name' => 'product1', 'image' => 'http://example_product_image_url1.com', 'description' => 'this is the description of a product' - ) + ) ) ) ); @@ -138,34 +153,52 @@ public function test_create_purchases() { $this->assertEquals(200, $response->status->code, 'Code was not 200'); } - public function test_get_purchases() { - $this->fail('Not Yet Implemented Test'); + public function testGetPurchases() + { + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); } - public function test_send_test_reminder() { - $this->fail('Not Yet Implemented Test'); + public function testSendTestReminder() + { + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); } - public function test_get_all_bottom_lines() { - $this->fail('Not Yet Implemented Test'); + public function testGetAllBottomLines() + { + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); } - public function test_create_review() { - $this->fail('Not Yet Implemented Test'); + public function testCreateReview() + { + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); } - public function test_get_product_reviews() { - $this->fail('Not Yet Implemented Test'); + public function testGetProductReviews() + { + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); } - public function test_get_product_bottom_line() { - $this->fail('Not Yet Implemented Test'); + public function testGetProductBottomLine() + { + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); } - public function test_build_request() { - $this->fail('Not Yet Implemented Test'); + public function testBuildRequest() + { + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); } - } - -?> diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 43c4df5..0fd25c5 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,11 +1,27 @@ - - - . - + + + + + ./ + + + + + + ../src/ + + + - -