Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/mongodb-extension' into develop
Browse files Browse the repository at this point in the history
Close #149
Fixes #145
Fixes #138
  • Loading branch information
weierophinney committed Jul 10, 2016
2 parents 0b2832a + 12ea561 commit 20f802c
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 34 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ matrix:
- php: 7
env:
- DEPS=lowest
- EXT_MONGODB=true
- php: 7
env:
- DEPS=locked
- EXT_MONGODB=true
- CS_CHECK=true
- php: 7
env:
- DEPS=latest
- EXT_MONGODB=true
- php: hhvm
env:
- DEPS=lowest
Expand All @@ -44,6 +47,7 @@ matrix:
- DEPS=locked
- php: hhvm
env:
- EXT_MONGODB=true
- DEPS=latest
allow_failures:
- php: hhvm
Expand All @@ -54,13 +58,19 @@ notifications:

before_install:
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
- if [[ $EXT_MONGODB == 'true' && $TRAVIS_PHP_VERSION == '7' ]]; then echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; fi
- if [[ $EXT_MONGODB == 'true' && $TRAVIS_PHP_VERSION == 'hhvm' ]]; then echo "extension = mongodb.so" >> /etc/hhvm/php.ini ; fi
- if [[ $EXT_MONGODB != 'true' && $TRAVIS_PHP_VERSION != 'hhvm' ]]; then echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; fi
- if [[ $EXT_MONGODB != 'true' && $TRAVIS_PHP_VERSION == 'hhvm' ]]; then echo "extension = mongo.so" >> /etc/hhvm/php.ini ; fi
- travis_retry composer self-update
- chmod -R +rwX test/TestAsset

install:
- if [[ $EXT_MONGODB == 'true' ]]; then composer require --dev $COMPOSER_ARGS alcaeus/mongo-php-adapter ; fi
- if [[ $DEPS == 'latest' ]]; then travis_retry composer update $COMPOSER_ARGS ; fi
- if [[ $DEPS == 'lowest' ]]; then travis_retry composer update --prefer-lowest --prefer-stable $COMPOSER_ARGS ; fi
- travis_retry composer install $COMPOSER_ARGS
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' ]]; then php -m ; fi
- composer show

script:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- [#149](https://github.com/zfcampus/zf-oauth2/pull/149) adds support for usage
of ext/mongodb with `ZF\OAuth2\Adapter\MongoAdapter`; users will need to also
install a compatibility package to do so:
`composer require alcaeus/mongo-php-adapter`
- [#141](https://github.com/zfcampus/zf-oauth2/pull/141) and
[#148](https://github.com/zfcampus/zf-oauth2/pull/148) update the component to
allow usage with v3 releases of Zend Framework components on which it depends,
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ You can install using:
$ composer require zfcampus/zf-oauth2
```

You should also add the following modules to your application's configuration:
If you are using ext/mongodb, you will also need to install a compatibility
package:

```bash
$ composer require alcaeus/mongo-php-adapter
```

Finally, you will need to add the following modules to your application's
configuration:

```php
'modules' => [
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"zendframework/zend-serializer": "^2.8",
"zendframework/zend-test": "^2.6.1 || ^3.0.1"
},
"suggest": {
"alcaeus/mongo-php-adapter": "^1.0.5, if you are using ext/mongodb and wish to use the MongoAdapter for OAuth2 credential storage."
},
"autoload": {
"psr-4": {
"ZF\\OAuth2\\": "src/"
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/Adapter/MongoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ protected function verifyHash($check, $hash)
public function __construct($connection, $config = [])
{
// @codeCoverageIgnoreStart
if (!extension_loaded('mongo')
|| ! class_exists('MongoClient')
if (! (extension_loaded('mongodb') || extension_loaded('mongo'))
|| ! class_exists(MongoClient::class)
|| version_compare(MongoClient::VERSION, '1.4.1', '<')
) {
throw new Exception\RuntimeException(
'The Mongo Driver v1.4.1 required for this adapter to work'
'The MongoAdapter requires either the Mongo Driver v1.4.1 or '
. 'ext/mongodb + the alcaeus/mongo-php-adapter package (which provides '
. 'backwards compatibility for ext/mongo classes)'
);
}
// @codeCoverageIgnoreEnd
Expand Down
42 changes: 30 additions & 12 deletions test/Controller/AuthControllerWithMongoAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,62 @@

namespace ZFTest\OAuth2\Controller;

use MongoClient;
use MongoConnectionException;
use MongoDB;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;

class AuthControllerWithMongoAdapterTest extends AbstractHttpControllerTestCase
{
/**
* @var \MongoDB
* @var MongoDB
*/
protected $db;

/**
* Default data to insert in database.
*
* Defined here to prevent "cannot pass by reference" issues when using
* alcaeus/mongo-php-adapter (as that library accepts a reference for the
* first argument, and unassigned arrays fail that).
*
* @var array
*/
protected $defaultData = [
'client_id' => 'testclient',
'client_secret' => '$2y$14$f3qml4G2hG6sxM26VMq.geDYbsS089IBtVJ7DlD05BoViS9PFykE2',
'redirect_uri' => '/oauth/receivecode',
'grant_types' => null,
];

public function setUp()
{
if (!extension_loaded('mongo')) {
$this->markTestSkipped('The Mongo extension is not available.');
if (! (extension_loaded('mongodb') || extension_loaded('mongo'))
|| ! class_exists(MongoClient::class)
|| version_compare(MongoClient::VERSION, '1.4.1', '<')
) {
$this->markTestSkipped('ext/mongo ^1.4.1 or ext/mongodb + alcaeus/mongo-php-adapter is not available.');
}

$this->setApplicationConfig(include __DIR__ . '/../TestAsset/mongo.application.config.php');

parent::setUp();

try {
$client = new \MongoClient("mongodb://127.0.0.1:27017");
} catch (\MongoConnectionException $e) {
$client = new MongoClient("mongodb://127.0.0.1:27017");
} catch (MongoConnectionException $e) {
$this->markTestSkipped($e->getMessage());
}

$this->db = $client->selectDB('zf_oauth2_test');
$this->db->oauth_clients->insert([
'client_id' => 'testclient',
'client_secret' => '$2y$14$f3qml4G2hG6sxM26VMq.geDYbsS089IBtVJ7DlD05BoViS9PFykE2',
'redirect_uri' => '/oauth/receivecode',
'grant_types' => null
]);
$this->db->oauth_clients->insert($this->defaultData);

$this->getApplicationServiceLocator()->setService('MongoDB', $this->db);
}

public function tearDown()
{
if ($this->db instanceof \MongoDB) {
if ($this->db instanceof MongoDB) {
$this->db->drop();
}

Expand Down
17 changes: 13 additions & 4 deletions test/Factory/MongoAdapterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace ZFTest\OAuth2\Factory;

use MongoClient;
use MongoDB;
use ReflectionObject;
use Zend\ServiceManager\ServiceManager;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
Expand All @@ -25,8 +27,11 @@ class MongoAdapterFactoryTest extends AbstractHttpControllerTestCase

protected function setUp()
{
if (!extension_loaded('mongo')) {
$this->markTestSkipped('The Mongo extension is not available.');
if (! (extension_loaded('mongodb') || extension_loaded('mongo'))
|| ! class_exists(MongoClient::class)
|| version_compare(MongoClient::VERSION, '1.4.1', '<')
) {
$this->markTestSkipped('ext/mongo or ext/mongodb + alcaeus/mongo-php-adapter is not available.');
}

$this->factory = new MongoAdapterFactory();
Expand Down Expand Up @@ -81,7 +86,9 @@ public function testInstanceCreatedWithMongoDbInServiceLocator()
],
],
]);
$mock = $this->getMock('\MongoDB', [], [], '', false);
$mock = $this->getMockBuilder(MongoDB::class, [], [], '', false)
->disableOriginalConstructor()
->getMock();
$this->services->setService('testdb', $mock);

$adapter = $this->factory->createService($this->services);
Expand All @@ -100,7 +107,9 @@ public function testCanPassAdapterConfigurationWhenCreatingInstance()
],
],
]);
$mock = $this->getMock('\MongoDB', [], [], '', false);
$mock = $this->getMockBuilder(MongoDB::class, [], [], '', false)
->disableOriginalConstructor()
->getMock();
$this->services->setService('testdb', $mock);

$adapter = $this->factory->createService($this->services);
Expand Down
31 changes: 18 additions & 13 deletions test/TestAsset/mongo.application.config.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
<?php
return array(
$modules = [
'ZF\ContentNegotiation',
'ZF\OAuth2',
];

if (class_exists('Zend\Router\Module')) {
$modules[] = 'Zend\Router';
}

return [
// This should be an array of module namespaces used in the application.
'modules' => array(
'ZF\ContentNegotiation',
'ZF\OAuth2'
),
'modules' => $modules,

// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => array(
'module_listener_options' => [
// This should be an array of paths in which modules reside.
// If a string key is provided, the listener will consider that a module
// namespace, the value of that key the specific path to that module's
// Module class.
'module_paths' => array(
'module_paths' => [
__DIR__ . '/../../../../..',
__DIR__ . '/../../../../../vendor',
),
],

// An array of paths from which to glob configuration files after
// modules are loaded. These effectively override configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => array(
'config_glob_paths' => [
__DIR__ . '/autoload_mongo/{,*.}{global,local}.php',
),

),
);
],
],
];

0 comments on commit 20f802c

Please sign in to comment.