Commit aa2ef6f6 authored by Will's avatar Will

Updating to work with php7, laravel 5.2 and the new php mongodb driver.

parent 1b32d02d
...@@ -3,7 +3,7 @@ language: php ...@@ -3,7 +3,7 @@ language: php
php: php:
- 5.5 - 5.5
- 5.6 - 5.6
- 7.0 - 7
- hhvm - hhvm
matrix: matrix:
...@@ -13,7 +13,16 @@ matrix: ...@@ -13,7 +13,16 @@ matrix:
sudo: false sudo: false
services: mongodb services:
- mongodb
- mysql
addons:
apt:
sources:
- mongodb-3.0-precise
packages:
- mongodb-org-server
before_script: before_script:
- pecl install mongodb - pecl install mongodb
...@@ -23,7 +32,7 @@ before_script: ...@@ -23,7 +32,7 @@ before_script:
script: script:
- mkdir -p build/logs - mkdir -p build/logs
- phpunit --coverage-clover build/logs/clover.xml - vendor/bin/phpunit --coverage-clover build/logs/clover.xml
after_success: after_success:
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;' - sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;'
...@@ -15,12 +15,12 @@ ...@@ -15,12 +15,12 @@
"illuminate/container": "^5.1", "illuminate/container": "^5.1",
"illuminate/database": "^5.1", "illuminate/database": "^5.1",
"illuminate/events": "^5.1", "illuminate/events": "^5.1",
"mongodb/mongodb": "^1.0.0@beta" "mongodb/mongodb": "^1.0.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^4.0|^5.0", "phpunit/phpunit": "^4.0|^5.0",
"orchestra/testbench": "^3.2", "orchestra/testbench": "^3.1",
"mockery/mockery": "^0.9", "mockery/mockery": "^0.9",
"satooshi/php-coveralls": "^0.6" "satooshi/php-coveralls": "^0.6"
}, },
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
processIsolation="false" processIsolation="false"
stopOnFailure="false" stopOnFailure="false"
syntaxCheck="false" syntaxCheck="false"
verbose="true"
> >
<testsuites> <testsuites>
<testsuite name="all"> <testsuite name="all">
......
...@@ -39,7 +39,7 @@ class Collection { ...@@ -39,7 +39,7 @@ class Collection {
{ {
$start = microtime(true); $start = microtime(true);
$result = call_user_func_array([$this->collection, $method], $parameters); $result = call_user_func_array([$this->collection, $method], $parameters);
if ($this->connection->logging()) if ($this->connection->logging())
{ {
// Once we have run the query we will calculate the time that it took to run and // Once we have run the query we will calculate the time that it took to run and
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\Relations\Relation;
use MongoDB\Driver\Cursor; use MongoDB\Driver\Cursor;
use MongoDB\Model\BSONDocument;
class Builder extends EloquentBuilder { class Builder extends EloquentBuilder {
...@@ -221,18 +222,20 @@ class Builder extends EloquentBuilder { ...@@ -221,18 +222,20 @@ class Builder extends EloquentBuilder {
if ($results instanceof Cursor) if ($results instanceof Cursor)
{ {
$results = iterator_to_array($results, false); $results = iterator_to_array($results, false);
return $this->model->hydrate($results); return $this->model->hydrate($results);
} }
// Convert Mongo BSONDocument to a single object.
elseif ($results instanceof BSONDocument)
{
$results = $results->getArrayCopy();
return $this->model->newFromBuilder((array) $results);
}
// The result is a single object. // The result is a single object.
elseif (is_array($results) and array_key_exists('_id', $results)) elseif (is_array($results) and array_key_exists('_id', $results))
{ {
$model = $this->model->newFromBuilder((array) $results); return $this->model->newFromBuilder((array) $results);
$model->setConnection($this->model->getConnection());
return $model;
} }
return $results; return $results;
......
...@@ -360,7 +360,7 @@ abstract class Model extends BaseModel { ...@@ -360,7 +360,7 @@ abstract class Model extends BaseModel {
* *
* @return array * @return array
*/ */
protected function getCasts() public function getCasts()
{ {
return $this->casts; return $this->casts;
} }
......
...@@ -85,6 +85,7 @@ class Builder extends BaseBuilder { ...@@ -85,6 +85,7 @@ class Builder extends BaseBuilder {
*/ */
public function __construct(Connection $connection, Processor $processor) public function __construct(Connection $connection, Processor $processor)
{ {
$this->grammar = new Grammar;
$this->connection = $connection; $this->connection = $connection;
$this->processor = $processor; $this->processor = $processor;
} }
......
<?php namespace Jenssegers\Mongodb\Query;
use Illuminate\Database\Query\Grammars\Grammar as BaseGrammar;
class Grammar extends BaseGrammar {
}
...@@ -19,6 +19,21 @@ class HasMany extends EloquentHasMany { ...@@ -19,6 +19,21 @@ class HasMany extends EloquentHasMany {
return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true); return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true);
} }
/**
* Add the constraints for a relationship query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parent
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
*/
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
{
$query->select($columns);
$key = $this->wrap($this->getQualifiedParentKeyName());
return $query->where($this->getHasCompareKey(), 'exists', true);
}
/** /**
* Get the plain foreign key. * Get the plain foreign key.
* *
......
...@@ -19,6 +19,21 @@ class HasOne extends EloquentHasOne { ...@@ -19,6 +19,21 @@ class HasOne extends EloquentHasOne {
return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true); return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true);
} }
/**
* Add the constraints for a relationship query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parent
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
*/
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
{
$query->select($columns);
$key = $this->wrap($this->getQualifiedParentKeyName());
return $query->where($this->getHasCompareKey(), 'exists', true);
}
/** /**
* Get the plain foreign key. * Get the plain foreign key.
* *
......
...@@ -99,13 +99,16 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint { ...@@ -99,13 +99,16 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
foreach ($columns as $column) foreach ($columns as $column)
{ {
$transform[$column] = 1; $transform[$column] = $column . '_1';
} }
$columns = $transform; $columns = $transform;
} }
$this->collection->deleteIndex($columns); foreach ($columns as $column)
{
$this->collection->dropIndex($column);
}
return $this; return $this;
} }
......
...@@ -38,6 +38,7 @@ class Builder extends \Illuminate\Database\Schema\Builder { ...@@ -38,6 +38,7 @@ class Builder extends \Illuminate\Database\Schema\Builder {
{ {
return true; return true;
} }
/** /**
* Determine if the given collection exists. * Determine if the given collection exists.
* *
...@@ -48,9 +49,15 @@ class Builder extends \Illuminate\Database\Schema\Builder { ...@@ -48,9 +49,15 @@ class Builder extends \Illuminate\Database\Schema\Builder {
{ {
$db = $this->connection->getMongoDB(); $db = $this->connection->getMongoDB();
return in_array($collection, $db->getCollectionNames()); foreach($db->listCollections() as $collectionFromMongo) {
} if($collectionFromMongo->getName() == $collection) {
return true;
}
}
return false;
}
/** /**
* Determine if the given collection exists. * Determine if the given collection exists.
* *
......
...@@ -460,6 +460,7 @@ class ModelTest extends TestCase { ...@@ -460,6 +460,7 @@ class ModelTest extends TestCase {
{ {
return $collection->findOne(['age' => 35]); return $collection->findOne(['age' => 35]);
}); });
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user); $this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
$count = User::raw(function ($collection) $count = User::raw(function ($collection)
......
...@@ -187,8 +187,8 @@ class QueryBuilderTest extends TestCase { ...@@ -187,8 +187,8 @@ class QueryBuilderTest extends TestCase {
return $collection->find(['age' => 20]); return $collection->find(['age' => 20]);
}); });
$this->assertInstanceOf('MongoCursor', $cursor); $this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
$this->assertEquals(1, $cursor->count()); $this->assertEquals(1, count($cursor->toArray()));
$collection = DB::collection('users')->raw(); $collection = DB::collection('users')->raw();
$this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection); $this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection);
...@@ -540,11 +540,11 @@ class QueryBuilderTest extends TestCase { ...@@ -540,11 +540,11 @@ class QueryBuilderTest extends TestCase {
$results = DB::collection('items')->where('tags', 'size', 4)->get(); $results = DB::collection('items')->where('tags', 'size', 4)->get();
$this->assertEquals(1, count($results)); $this->assertEquals(1, count($results));
$regex = new Regex("/.*doe/i"); $regex = new Regex(".*doe", "i");
$results = DB::collection('users')->where('name', 'regex', $regex)->get(); $results = DB::collection('users')->where('name', 'regex', $regex)->get();
$this->assertEquals(2, count($results)); $this->assertEquals(2, count($results));
$regex = new Regex("/.*doe/i"); $regex = new Regex(".*doe", "i");
$results = DB::collection('users')->where('name', 'regexp', $regex)->get(); $results = DB::collection('users')->where('name', 'regexp', $regex)->get();
$this->assertEquals(2, count($results)); $this->assertEquals(2, count($results));
......
...@@ -196,7 +196,7 @@ class SchemaTest extends TestCase { ...@@ -196,7 +196,7 @@ class SchemaTest extends TestCase {
{ {
$collection = DB::getCollection($collection); $collection = DB::getCollection($collection);
foreach ($collection->getIndexInfo() as $index) foreach ($collection->listIndexes() as $index)
{ {
if (isset($index['key'][$name])) return $index; if (isset($index['key'][$name])) return $index;
} }
......
...@@ -12,7 +12,7 @@ class TestCase extends Orchestra\Testbench\TestCase { ...@@ -12,7 +12,7 @@ class TestCase extends Orchestra\Testbench\TestCase {
{ {
return [ return [
'Jenssegers\Mongodb\MongodbServiceProvider', 'Jenssegers\Mongodb\MongodbServiceProvider',
'Jenssegers\Mongodb\Auth\PasswordResetServiceProvider', 'Jenssegers\Mongodb\Auth\PasswordResetServiceProvider'
]; ];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment