From bd55939d2ed0c35b85086ebe53cb9fd6991b6726 Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Thu, 15 Oct 2015 02:06:21 +0200 Subject: [PATCH 1/3] moved runtime exception to native TypeError --- src/Facebook/Facebook.php | 23 +++++++++++------------ tests/FacebookTest.php | 9 ++++++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Facebook/Facebook.php b/src/Facebook/Facebook.php index 227a16667..be3f4fe54 100644 --- a/src/Facebook/Facebook.php +++ b/src/Facebook/Facebook.php @@ -158,14 +158,6 @@ public function __construct(array $config = []) $enableBeta = isset($config['enable_beta_mode']) && $config['enable_beta_mode'] === true; $this->client = new FacebookClient($httpClientHandler, $enableBeta); - if (isset($config['url_detection_handler'])) { - if ($config['url_detection_handler'] instanceof UrlDetectionInterface) { - $this->urlDetectionHandler = $config['url_detection_handler']; - } else { - throw new \InvalidArgumentException('The url_detection_handler must be an instance of Facebook\Url\UrlDetectionInterface'); - } - } - if (isset($config['pseudo_random_string_generator'])) { if ($config['pseudo_random_string_generator'] instanceof PseudoRandomStringGeneratorInterface) { $this->pseudoRandomStringGenerator = $config['pseudo_random_string_generator']; @@ -191,6 +183,7 @@ public function __construct(array $config = []) throw new \InvalidArgumentException('The persistent_data_handler must be set to "session", "memory", or be an instance of Facebook\PersistentData\PersistentDataInterface'); } } + $this->setUrlDetectionHandler($config['url_detection_handler'] ?: new FacebookUrlDetectionHandler()); if (isset($config['default_access_token'])) { $this->setDefaultAccessToken($config['default_access_token']); @@ -257,13 +250,19 @@ public function getLastResponse() */ public function getUrlDetectionHandler() { - if (!$this->urlDetectionHandler instanceof UrlDetectionInterface) { - $this->urlDetectionHandler = new FacebookUrlDetectionHandler(); - } - return $this->urlDetectionHandler; } + /** + * Changes the URL detection handler. + * + * @param UrlDetectionInterface $urlDetectionHandler + */ + private function setUrlDetectionHandler(UrlDetectionInterface $urlDetectionHandler) + { + $this->urlDetectionHandler = $urlDetectionHandler; + } + /** * Returns the default AccessToken entity. * diff --git a/tests/FacebookTest.php b/tests/FacebookTest.php index 36486655b..98f81a8ab 100644 --- a/tests/FacebookTest.php +++ b/tests/FacebookTest.php @@ -176,11 +176,14 @@ public function testPersistentDataHandlerCanBeForced() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testSettingAnInvalidUrlHandlerThrows() { + $expectedException = (PHP_MAJOR_VERSION > 5 && class_exists('TypeError')) + ? 'TypeError' + : 'PHPUnit_Framework_Error'; + + $this->setExpectedException($expectedException); + $config = array_merge($this->config, [ 'url_detection_handler' => 'foo_handler', ]); From fb4de4f35c6995babd672ee3e4b3cf00ce14c9b2 Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Thu, 15 Oct 2015 02:06:49 +0200 Subject: [PATCH 2/3] splitted complex constructor into different factories --- src/Facebook/Facebook.php | 96 ++++++------------ .../HttpClients/HttpClientsFactory.php | 98 +++++++++++++++++++ .../PersistentData/PersistentDataFactory.php | 65 ++++++++++++ .../PseudoRandomStringGeneratorFactory.php | 93 ++++++++++++++++++ 4 files changed, 286 insertions(+), 66 deletions(-) create mode 100644 src/Facebook/HttpClients/HttpClientsFactory.php create mode 100644 src/Facebook/PersistentData/PersistentDataFactory.php create mode 100644 src/Facebook/PseudoRandomString/PseudoRandomStringGeneratorFactory.php diff --git a/src/Facebook/Facebook.php b/src/Facebook/Facebook.php index be3f4fe54..e91648dba 100644 --- a/src/Facebook/Facebook.php +++ b/src/Facebook/Facebook.php @@ -30,17 +30,11 @@ use Facebook\GraphNodes\GraphEdge; use Facebook\Url\UrlDetectionInterface; use Facebook\Url\FacebookUrlDetectionHandler; +use Facebook\PseudoRandomString\PseudoRandomStringGeneratorFactory; use Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface; -use Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator; -use Facebook\PseudoRandomString\OpenSslPseudoRandomStringGenerator; -use Facebook\PseudoRandomString\UrandomPseudoRandomStringGenerator; -use Facebook\HttpClients\FacebookHttpClientInterface; -use Facebook\HttpClients\FacebookCurlHttpClient; -use Facebook\HttpClients\FacebookStreamHttpClient; -use Facebook\HttpClients\FacebookGuzzleHttpClient; +use Facebook\HttpClients\HttpClientsFactory; +use Facebook\PersistentData\PersistentDataFactory; use Facebook\PersistentData\PersistentDataInterface; -use Facebook\PersistentData\FacebookSessionPersistentDataHandler; -use Facebook\PersistentData\FacebookMemoryPersistentDataHandler; use Facebook\Helpers\FacebookCanvasHelper; use Facebook\Helpers\FacebookJavaScriptHelper; use Facebook\Helpers\FacebookPageTabHelper; @@ -128,73 +122,43 @@ class Facebook */ public function __construct(array $config = []) { - $appId = isset($config['app_id']) ? $config['app_id'] : getenv(static::APP_ID_ENV_NAME); - if (!$appId) { + $config = array_merge([ + 'app_id' => getenv(static::APP_ID_ENV_NAME), + 'app_secret' => getenv(static::APP_SECRET_ENV_NAME), + 'default_graph_version' => static::DEFAULT_GRAPH_VERSION, + 'enable_beta_mode' => false, + 'http_client_handler' => null, + 'persistent_data_handler' => null, + 'pseudo_random_string_generator' => null, + 'url_detection_handler' => null, + ], $config); + + if (!$config['app_id']) { throw new FacebookSDKException('Required "app_id" key not supplied in config and could not find fallback environment variable "' . static::APP_ID_ENV_NAME . '"'); } - - $appSecret = isset($config['app_secret']) ? $config['app_secret'] : getenv(static::APP_SECRET_ENV_NAME); - if (!$appSecret) { + if (!$config['app_secret']) { throw new FacebookSDKException('Required "app_secret" key not supplied in config and could not find fallback environment variable "' . static::APP_SECRET_ENV_NAME . '"'); } - $this->app = new FacebookApp($appId, $appSecret); - - $httpClientHandler = null; - if (isset($config['http_client_handler'])) { - if ($config['http_client_handler'] instanceof FacebookHttpClientInterface) { - $httpClientHandler = $config['http_client_handler']; - } elseif ($config['http_client_handler'] === 'curl') { - $httpClientHandler = new FacebookCurlHttpClient(); - } elseif ($config['http_client_handler'] === 'stream') { - $httpClientHandler = new FacebookStreamHttpClient(); - } elseif ($config['http_client_handler'] === 'guzzle') { - $httpClientHandler = new FacebookGuzzleHttpClient(); - } else { - throw new \InvalidArgumentException('The http_client_handler must be set to "curl", "stream", "guzzle", or be an instance of Facebook\HttpClients\FacebookHttpClientInterface'); - } - } - - $enableBeta = isset($config['enable_beta_mode']) && $config['enable_beta_mode'] === true; - $this->client = new FacebookClient($httpClientHandler, $enableBeta); - - if (isset($config['pseudo_random_string_generator'])) { - if ($config['pseudo_random_string_generator'] instanceof PseudoRandomStringGeneratorInterface) { - $this->pseudoRandomStringGenerator = $config['pseudo_random_string_generator']; - } elseif ($config['pseudo_random_string_generator'] === 'mcrypt') { - $this->pseudoRandomStringGenerator = new McryptPseudoRandomStringGenerator(); - } elseif ($config['pseudo_random_string_generator'] === 'openssl') { - $this->pseudoRandomStringGenerator = new OpenSslPseudoRandomStringGenerator(); - } elseif ($config['pseudo_random_string_generator'] === 'urandom') { - $this->pseudoRandomStringGenerator = new UrandomPseudoRandomStringGenerator(); - } else { - throw new \InvalidArgumentException('The pseudo_random_string_generator must be set to "mcrypt", "openssl", or "urandom", or be an instance of Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface'); - } - } - - if (isset($config['persistent_data_handler'])) { - if ($config['persistent_data_handler'] instanceof PersistentDataInterface) { - $this->persistentDataHandler = $config['persistent_data_handler']; - } elseif ($config['persistent_data_handler'] === 'session') { - $this->persistentDataHandler = new FacebookSessionPersistentDataHandler(); - } elseif ($config['persistent_data_handler'] === 'memory') { - $this->persistentDataHandler = new FacebookMemoryPersistentDataHandler(); - } else { - throw new \InvalidArgumentException('The persistent_data_handler must be set to "session", "memory", or be an instance of Facebook\PersistentData\PersistentDataInterface'); - } - } + $this->app = new FacebookApp($config['app_id'], $config['app_secret']); + $this->client = new FacebookClient( + HttpClientsFactory::createHttpClient($config['http_client_handler']), + $config['enable_beta_mode'] + ); + $this->pseudoRandomStringGenerator = PseudoRandomStringGeneratorFactory::createPseudoRandomStringGenerator( + $config['pseudo_random_string_generator'] + ); $this->setUrlDetectionHandler($config['url_detection_handler'] ?: new FacebookUrlDetectionHandler()); + $this->persistentDataHandler = PersistentDataFactory::createPersistentDataHandler( + $config['persistent_data_handler'] + ); if (isset($config['default_access_token'])) { $this->setDefaultAccessToken($config['default_access_token']); } - if (isset($config['default_graph_version'])) { - $this->defaultGraphVersion = $config['default_graph_version']; - } else { - // @todo v6: Throw an InvalidArgumentException if "default_graph_version" is not set - $this->defaultGraphVersion = static::DEFAULT_GRAPH_VERSION; - } + // @todo v6: Throw an InvalidArgumentException if "default_graph_version" is not set + $this->defaultGraphVersion = $config['default_graph_version']; } /** @@ -255,7 +219,7 @@ public function getUrlDetectionHandler() /** * Changes the URL detection handler. - * + * * @param UrlDetectionInterface $urlDetectionHandler */ private function setUrlDetectionHandler(UrlDetectionInterface $urlDetectionHandler) diff --git a/src/Facebook/HttpClients/HttpClientsFactory.php b/src/Facebook/HttpClients/HttpClientsFactory.php new file mode 100644 index 000000000..977a5cbbc --- /dev/null +++ b/src/Facebook/HttpClients/HttpClientsFactory.php @@ -0,0 +1,98 @@ + Date: Thu, 15 Oct 2015 23:07:55 +0200 Subject: [PATCH 3/3] added changelog and tests --- CHANGELOG.md | 1 + tests/HttpClients/HttpClientsFactoryTest.php | 68 +++++++++++++++++++ .../PersistentDataFactoryTest.php | 68 +++++++++++++++++++ .../PseudoRandomStringFactoryTest.php | 63 +++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 tests/HttpClients/HttpClientsFactoryTest.php create mode 100644 tests/PersistentData/PersistentDataFactoryTest.php create mode 100644 tests/PseudoRandomString/PseudoRandomStringFactoryTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index aeb629e10..55dc0ef68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ Version 5 of the Facebook PHP SDK is a complete refactor of version 4. It comes - Renamed `FacebookHttpable` to `FacebookHttpClientInterface` - Added `FacebookApp` entity that contains info about the Facebook app - Updated the API for the helpers + - Added `HttpClients`, `PersistentData` and `PseudoRandomString` factories to reduce main class' complexity - Tests - Added namespaces to the tests - Grouped functional tests under `functional` group diff --git a/tests/HttpClients/HttpClientsFactoryTest.php b/tests/HttpClients/HttpClientsFactoryTest.php new file mode 100644 index 000000000..78876b13d --- /dev/null +++ b/tests/HttpClients/HttpClientsFactoryTest.php @@ -0,0 +1,68 @@ +assertInstanceOf(self::COMMON_INTERFACE, $httpClient); + $this->assertInstanceOf($expected, $httpClient); + } + + /** + * @return array + */ + public function httpClientsProvider() + { + return [ + ['curl', self::COMMON_NAMESPACE . 'FacebookCurlHttpClient'], + ['guzzle', self::COMMON_NAMESPACE . 'FacebookGuzzleHttpClient'], + ['stream', self::COMMON_NAMESPACE . 'FacebookStreamHttpClient'], + [new Client(), self::COMMON_NAMESPACE . 'FacebookGuzzleHttpClient'], + [new FacebookCurlHttpClient(), self::COMMON_NAMESPACE . 'FacebookCurlHttpClient'], + [new FacebookGuzzleHttpClient(), self::COMMON_NAMESPACE . 'FacebookGuzzleHttpClient'], + [new FacebookStreamHttpClient(), self::COMMON_NAMESPACE . 'FacebookStreamHttpClient'], + [null, self::COMMON_INTERFACE], + ]; + } +} diff --git a/tests/PersistentData/PersistentDataFactoryTest.php b/tests/PersistentData/PersistentDataFactoryTest.php new file mode 100644 index 000000000..dfb871ce4 --- /dev/null +++ b/tests/PersistentData/PersistentDataFactoryTest.php @@ -0,0 +1,68 @@ +assertInstanceOf(self::COMMON_INTERFACE, $persistentDataHandler); + $this->assertInstanceOf($expected, $persistentDataHandler); + } + + /** + * @return array + */ + public function persistentDataHandlerProviders() + { + $handlers = [ + ['memory', self::COMMON_NAMESPACE . 'FacebookMemoryPersistentDataHandler'], + [new FacebookMemoryPersistentDataHandler(), self::COMMON_NAMESPACE . 'FacebookMemoryPersistentDataHandler'], + [new FacebookSessionPersistentDataHandler(false), self::COMMON_NAMESPACE . 'FacebookSessionPersistentDataHandler'], + [null, self::COMMON_INTERFACE], + ]; + + if (session_status() === PHP_SESSION_ACTIVE) { + $handlers[] = ['session', self::COMMON_NAMESPACE . 'FacebookSessionPersistentDataHandler']; + } + + return $handlers; + } +} diff --git a/tests/PseudoRandomString/PseudoRandomStringFactoryTest.php b/tests/PseudoRandomString/PseudoRandomStringFactoryTest.php new file mode 100644 index 000000000..e6f2094f3 --- /dev/null +++ b/tests/PseudoRandomString/PseudoRandomStringFactoryTest.php @@ -0,0 +1,63 @@ +assertInstanceOf(self::COMMON_INTERFACE, $pseudoRandomStringGenerator); + $this->assertInstanceOf($expected, $pseudoRandomStringGenerator); + } + + /** + * @return array + */ + public function httpClientsProvider() + { + return [ + ['mcrypt', self::COMMON_NAMESPACE . 'McryptPseudoRandomStringGenerator'], + ['openssl', self::COMMON_NAMESPACE . 'OpenSslPseudoRandomStringGenerator'], + ['urandom', self::COMMON_NAMESPACE . 'UrandomPseudoRandomStringGenerator'], + [null, self::COMMON_INTERFACE], + ]; + } +}