From 1d2807b49742d6529c81592fbc969a220cb7a366 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Mon, 18 Jun 2018 09:45:55 +0200 Subject: [PATCH 01/47] :construction_worker: Travis --- .travis.yml | 18 +++--------------- docker/Dockerfile => Dockerfile | 4 ++-- docker-compose.yml | 4 ++-- docker/entrypoint.sh | 3 --- 4 files changed, 7 insertions(+), 22 deletions(-) rename docker/Dockerfile => Dockerfile (86%) delete mode 100755 docker/entrypoint.sh diff --git a/.travis.yml b/.travis.yml index 30972bfb5..a6358266b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,24 +9,12 @@ services: - docker install: - # Update docker-engine using Ubuntu 'trusty' apt repo - - > - curl -sSL "https://get.docker.com/gpg" | - sudo -E apt-key add - - - > - echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" | - sudo tee -a /etc/apt/sources.list - - sudo apt-get update - - > - sudo apt-get -o Dpkg::Options::="--force-confdef" \ - -o Dpkg::Options::="--force-confold" --assume-yes install docker-engine --allow-unauthenticated - docker version - - # Update docker-compose via pip - sudo pip install docker-compose - docker-compose version - - docker-compose up --build -d - - docker ps -a + - sed -i -e "s/php:cli/php:${TRAVIS_PHP_VERSION}-cli/g" Dockerfile + - cat Dockerfile + - docker-compose build script: - docker-compose up --exit-code-from php diff --git a/docker/Dockerfile b/Dockerfile similarity index 86% rename from docker/Dockerfile rename to Dockerfile index 62c68e45d..03c2ba7e2 100644 --- a/docker/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:7.1-cli +FROM php:cli RUN pecl install xdebug @@ -11,4 +11,4 @@ RUN curl -sS https://getcomposer.org/installer | php \ && mv composer.phar /usr/local/bin/ \ && ln -s /usr/local/bin/composer.phar /usr/local/bin/composer -ENV PATH="~/.composer/vendor/bin:./vendor/bin:${PATH}" \ No newline at end of file +ENV PATH="~/.composer/vendor/bin:./vendor/bin:${PATH}" diff --git a/docker-compose.yml b/docker-compose.yml index ce5c95652..c1d83dd97 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,11 +6,11 @@ services: container_name: php build: context: . - dockerfile: docker/Dockerfile + dockerfile: Dockerfile volumes: - .:/code working_dir: /code - command: docker/entrypoint.sh + command: bash -c "composer install --prefer-source --no-interaction && php ./vendor/bin/phpunit" depends_on: - mysql - mongodb diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh deleted file mode 100755 index c646f3917..000000000 --- a/docker/entrypoint.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -sleep 3 && composer install --prefer-source --no-interaction && php ./vendor/bin/phpunit From 903ebb044b43c7d5818168232eddaaaa5b3f29c0 Mon Sep 17 00:00:00 2001 From: Stanislav Shupilkin Date: Tue, 26 Jun 2018 14:30:13 +0300 Subject: [PATCH 02/47] Add arg PHP-VERSION in Dockerfile --- Dockerfile | 4 +++- docker-compose.yml | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 03c2ba7e2..4c07c7f5a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ -FROM php:cli +ARG PHP_VERSION + +FROM php:${PHP_VERSION}-cli RUN pecl install xdebug diff --git a/docker-compose.yml b/docker-compose.yml index c1d83dd97..440db55e4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,8 @@ services: volumes: - .:/code working_dir: /code + environment: + PHP_VERSION: ${TRAVIS_PHP_VERSION} command: bash -c "composer install --prefer-source --no-interaction && php ./vendor/bin/phpunit" depends_on: - mysql From e51fe15ab0f0831a2c0ecd44c3e0473f986bcda9 Mon Sep 17 00:00:00 2001 From: Stanislav Shupilkin Date: Tue, 26 Jun 2018 14:33:41 +0300 Subject: [PATCH 03/47] Delete empty row --- docker-compose.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 440db55e4..5f42d8469 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,6 @@ version: '3' services: - php: container_name: php build: From 43c380043bdec3b0c60ba5783e1e255bd4a10db4 Mon Sep 17 00:00:00 2001 From: Stanislav Shupilkin Date: Tue, 26 Jun 2018 14:43:12 +0300 Subject: [PATCH 04/47] Add build arg in docker-compose.yml --- .travis.yml | 3 +-- docker-compose.yml | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index a6358266b..1047a1734 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,9 +12,8 @@ install: - docker version - sudo pip install docker-compose - docker-compose version - - sed -i -e "s/php:cli/php:${TRAVIS_PHP_VERSION}-cli/g" Dockerfile - cat Dockerfile - - docker-compose build + - docker-compose build --build-arg PHP_VERSION="${TRAVIS_PHP_VERSION}" script: - docker-compose up --exit-code-from php diff --git a/docker-compose.yml b/docker-compose.yml index 5f42d8469..4bcc740da 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +10,7 @@ services: - .:/code working_dir: /code environment: - PHP_VERSION: ${TRAVIS_PHP_VERSION} + PHP_VERSION: ${PHP_VERSION} command: bash -c "composer install --prefer-source --no-interaction && php ./vendor/bin/phpunit" depends_on: - mysql From e2a91bb71e5f6562df8a88e7edf460e1be71ebbc Mon Sep 17 00:00:00 2001 From: Stanislav Shupilkin Date: Tue, 26 Jun 2018 15:03:43 +0300 Subject: [PATCH 05/47] Add changes to Dcokerfile --- .travis.yml | 2 +- Dockerfile | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1047a1734..fe11b61da 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ install: - sudo pip install docker-compose - docker-compose version - cat Dockerfile - - docker-compose build --build-arg PHP_VERSION="${TRAVIS_PHP_VERSION}" + - docker-compose build --build-arg PHP_VERSION=${TRAVIS_PHP_VERSION} script: - docker-compose up --exit-code-from php diff --git a/Dockerfile b/Dockerfile index 4c07c7f5a..a01fc5dc8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,8 +5,9 @@ FROM php:${PHP_VERSION}-cli RUN pecl install xdebug RUN apt-get update && \ - apt-get install -y autoconf pkg-config libssl-dev git && \ - pecl install mongodb git zlib1g-dev && docker-php-ext-enable mongodb && \ + apt-get install -y autoconf pkg-config libssl-dev git zlib1g-dev + +RUN pecl install mongodb && docker-php-ext-enable mongodb && \ docker-php-ext-install -j$(nproc) pdo pdo_mysql zip && docker-php-ext-enable xdebug RUN curl -sS https://getcomposer.org/installer | php \ From 49ec04ddda7bb5dcf696f4ac6b2bf40bd02a3bfd Mon Sep 17 00:00:00 2001 From: Stanislav Shupilkin Date: Tue, 26 Jun 2018 15:27:41 +0300 Subject: [PATCH 06/47] Separate installing vendors by composer use official image of composer --- .travis.yml | 3 +++ docker-compose.yml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index fe11b61da..d4184bad0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,9 @@ php: services: - docker +before_script: + - docker pull composer + - docker run --rm -v $(pwd):/app composer install --prefer-source --no-interaction install: - docker version - sudo pip install docker-compose diff --git a/docker-compose.yml b/docker-compose.yml index 4bcc740da..23f7775ce 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,7 +11,7 @@ services: working_dir: /code environment: PHP_VERSION: ${PHP_VERSION} - command: bash -c "composer install --prefer-source --no-interaction && php ./vendor/bin/phpunit" + command: bash -c "php ./vendor/bin/phpunit" depends_on: - mysql - mongodb From 3bba0ac4d6cd6fb6d233f679a61ef5ad0552dd98 Mon Sep 17 00:00:00 2001 From: Stanislav Shupilkin Date: Tue, 26 Jun 2018 15:47:25 +0300 Subject: [PATCH 07/47] Delete use composer image --- .travis.yml | 3 --- docker-compose.yml | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index d4184bad0..fe11b61da 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,9 +8,6 @@ php: services: - docker -before_script: - - docker pull composer - - docker run --rm -v $(pwd):/app composer install --prefer-source --no-interaction install: - docker version - sudo pip install docker-compose diff --git a/docker-compose.yml b/docker-compose.yml index 23f7775ce..4bcc740da 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,7 +11,7 @@ services: working_dir: /code environment: PHP_VERSION: ${PHP_VERSION} - command: bash -c "php ./vendor/bin/phpunit" + command: bash -c "composer install --prefer-source --no-interaction && php ./vendor/bin/phpunit" depends_on: - mysql - mongodb From d1d06753a6a4eb54c15e8382eb80aa115df7c801 Mon Sep 17 00:00:00 2001 From: Stanislav Shupilkin Date: Wed, 27 Jun 2018 13:18:05 +0300 Subject: [PATCH 08/47] Add default value PHP_VERSION --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 4bcc740da..decc0d653 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +10,7 @@ services: - .:/code working_dir: /code environment: - PHP_VERSION: ${PHP_VERSION} + PHP_VERSION: 7.1 command: bash -c "composer install --prefer-source --no-interaction && php ./vendor/bin/phpunit" depends_on: - mysql From 96e153146a532085655fc54ce8baca7b1ce2b480 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Sun, 22 Jul 2018 13:16:11 +0200 Subject: [PATCH 09/47] :whale: Provide default docker arg --- Dockerfile | 2 +- docker-compose.yml | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index a01fc5dc8..68eac169d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -ARG PHP_VERSION +ARG PHP_VERSION=7.2 FROM php:${PHP_VERSION}-cli diff --git a/docker-compose.yml b/docker-compose.yml index decc0d653..42546660b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,8 +9,6 @@ services: volumes: - .:/code working_dir: /code - environment: - PHP_VERSION: 7.1 command: bash -c "composer install --prefer-source --no-interaction && php ./vendor/bin/phpunit" depends_on: - mysql From 8f9f4c651ada8e66d546d128387f5c411d69bb7c Mon Sep 17 00:00:00 2001 From: Tokman Date: Thu, 16 Aug 2018 11:59:50 +0300 Subject: [PATCH 10/47] Fix issue using query builder first method --- src/Jenssegers/Mongodb/Query/Builder.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index c425f5165..a340976e3 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -152,6 +152,8 @@ protected function shouldUseCollections() $version = filter_var(explode(')', $version)[0], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); // lumen return version_compare($version, '5.3', '>='); } + + return true; } /** From d795dac3c87bedbac414b3de2ea0d5b194ded0b6 Mon Sep 17 00:00:00 2001 From: jim5359 Date: Mon, 5 Nov 2018 18:08:28 -0800 Subject: [PATCH 11/47] EmbedsMany respect primaryKey on association --- src/Jenssegers/Mongodb/Relations/EmbedsMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php index b0e40893d..d16a8ca15 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php @@ -236,7 +236,7 @@ public function attach(Model $model) protected function associateNew($model) { // Create a new key if needed. - if (!$model->getAttribute('_id')) { + if ($model->getKeyName() == '_id' && !$model->getAttribute('_id')) { $model->setAttribute('_id', new ObjectID); } From 5614b7805cf4318f0b7c1ce142b94e6ddae3d95c Mon Sep 17 00:00:00 2001 From: Rowayda-Khayri Date: Tue, 6 Nov 2018 22:51:01 +0200 Subject: [PATCH 12/47] fix typos --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0aa40e5c7..6f9ef0405 100644 --- a/README.md +++ b/README.md @@ -298,7 +298,7 @@ This service provider will slightly modify the internal DatabaseReminderReposito ### Queues -If you want to use MongoDB as your database backend, change the the driver in `config/queue.php`: +If you want to use MongoDB as your database backend, change the driver in `config/queue.php`: ```php 'connections' => [ @@ -689,7 +689,7 @@ For more information about model manipulation, check http://laravel.com/docs/elo ### Dates -Eloquent allows you to work with Carbon/DateTime objects instead of MongoDate objects. Internally, these dates will be converted to MongoDate objects when saved to the database. If you wish to use this functionality on non-default date fields you will need to manually specify them as described here: http://laravel.com/docs/eloquent#date-mutators +Eloquent allows you to work with Carbon/DateTime objects instead of MongoDate objects. Internally, these dates will be converted to MongoDate objects when saved to the database. If you wish to use this functionality on non-default date fields, you will need to manually specify them as described here: http://laravel.com/docs/eloquent#date-mutators Example: @@ -787,7 +787,7 @@ class User extends Eloquent { } ``` -You access the embedded models through the dynamic property: +You can access the embedded models through the dynamic property: ```php $books = User::first()->books; @@ -849,7 +849,7 @@ Embedded relations will return a Collection of embedded items instead of a query ### EmbedsOne Relations -The embedsOne relation is similar to the EmbedsMany relation, but only embeds a single model. +The embedsOne relation is similar to the embedsMany relation, but only embeds a single model. ```php use Jenssegers\Mongodb\Eloquent\Model as Eloquent; @@ -864,7 +864,7 @@ class Book extends Eloquent { } ``` -You access the embedded models through the dynamic property: +You can access the embedded models through the dynamic property: ```php $author = Book::first()->author; @@ -1014,7 +1014,7 @@ DB::collection('items')->paginate($limit, $projections); **Push** -Add an items to an array. +Add items to an array. ```php DB::collection('users')->where('name', 'John')->push('items', 'boots'); From 25a4a8740418459921adf413acd8e0e9c47a12fe Mon Sep 17 00:00:00 2001 From: Zuken Date: Fri, 23 Nov 2018 15:48:30 +0200 Subject: [PATCH 13/47] laravel/lumen 5.7.14 compatibility Updated model to be compatible with laravel/lumen 5.7.14 --- src/Jenssegers/Mongodb/Eloquent/Model.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Jenssegers/Mongodb/Eloquent/Model.php b/src/Jenssegers/Mongodb/Eloquent/Model.php index 894ebe41a..6b931e468 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Model.php +++ b/src/Jenssegers/Mongodb/Eloquent/Model.php @@ -31,6 +31,13 @@ abstract class Model extends BaseModel * @var string */ protected $primaryKey = '_id'; + + /** + * The primary key type. + * + * @var string + */ + protected $keyType = 'string'; /** * The parent relation instance. From 4c1ce51f21c074e9978e8f39112270006fec08e3 Mon Sep 17 00:00:00 2001 From: roman Date: Wed, 28 Nov 2018 17:29:41 +0200 Subject: [PATCH 14/47] Overrided whereIn method retrieval for relations, to force use standart whereIn nor whereInRawInteger --- src/Jenssegers/Mongodb/Relations/BelongsTo.php | 13 +++++++++++++ src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 13 +++++++++++++ src/Jenssegers/Mongodb/Relations/EmbedsMany.php | 13 +++++++++++++ src/Jenssegers/Mongodb/Relations/EmbedsOne.php | 13 +++++++++++++ .../Mongodb/Relations/EmbedsOneOrMany.php | 13 +++++++++++++ src/Jenssegers/Mongodb/Relations/HasMany.php | 13 +++++++++++++ src/Jenssegers/Mongodb/Relations/HasOne.php | 13 +++++++++++++ src/Jenssegers/Mongodb/Relations/MorphTo.php | 13 +++++++++++++ 8 files changed, 104 insertions(+) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsTo.php b/src/Jenssegers/Mongodb/Relations/BelongsTo.php index 78b5d63be..457e6bc1f 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsTo.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsTo.php @@ -3,6 +3,7 @@ namespace Jenssegers\Mongodb\Relations; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model as EloquentModel; class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo { @@ -59,4 +60,16 @@ public function getOwnerKey() { return property_exists($this, 'ownerKey') ? $this->ownerKey : $this->otherKey; } + + /** + * Get the name of the "where in" method for eager loading. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @return string + */ + protected function whereInMethod(EloquentModel $model, $key) + { + return 'whereIn'; + } } diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 73816b049..0e1b5280b 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany; use Illuminate\Support\Arr; +use Illuminate\Database\Eloquent\Model as EloquentModel; class BelongsToMany extends EloquentBelongsToMany { @@ -337,4 +338,16 @@ public function getRelatedKey() { return property_exists($this, 'relatedPivotKey') ? $this->relatedPivotKey : $this->relatedKey; } + + /** + * Get the name of the "where in" method for eager loading. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @return string + */ + protected function whereInMethod(EloquentModel $model, $key) + { + return 'whereIn'; + } } diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php index b0e40893d..825b0d594 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php @@ -7,6 +7,7 @@ use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; use MongoDB\BSON\ObjectID; +use Illuminate\Database\Eloquent\Model as EloquentModel; class EmbedsMany extends EmbedsOneOrMany { @@ -328,4 +329,16 @@ public function __call($method, $parameters) return parent::__call($method, $parameters); } + + /** + * Get the name of the "where in" method for eager loading. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @return string + */ + protected function whereInMethod(EloquentModel $model, $key) + { + return 'whereIn'; + } } diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsOne.php b/src/Jenssegers/Mongodb/Relations/EmbedsOne.php index 2efbfe1df..4640bdeb1 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsOne.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsOne.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use MongoDB\BSON\ObjectID; +use Illuminate\Database\Eloquent\Model as EloquentModel; class EmbedsOne extends EmbedsOneOrMany { @@ -136,4 +137,16 @@ public function delete() { return $this->performDelete(); } + + /** + * Get the name of the "where in" method for eager loading. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @return string + */ + protected function whereInMethod(EloquentModel $model, $key) + { + return 'whereIn'; + } } diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php index a8f90544d..177160bf1 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Relations\Relation; use Jenssegers\Mongodb\Eloquent\Model; +use Illuminate\Database\Eloquent\Model as EloquentModel; abstract class EmbedsOneOrMany extends Relation { @@ -403,4 +404,16 @@ public function getQualifiedForeignKeyName() { return $this->foreignKey; } + + /** + * Get the name of the "where in" method for eager loading. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @return string + */ + protected function whereInMethod(EloquentModel $model, $key) + { + return 'whereIn'; + } } diff --git a/src/Jenssegers/Mongodb/Relations/HasMany.php b/src/Jenssegers/Mongodb/Relations/HasMany.php index 47e60e533..93a25425a 100644 --- a/src/Jenssegers/Mongodb/Relations/HasMany.php +++ b/src/Jenssegers/Mongodb/Relations/HasMany.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\HasMany as EloquentHasMany; +use Illuminate\Database\Eloquent\Model as EloquentModel; class HasMany extends EloquentHasMany { @@ -77,4 +78,16 @@ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['* return $query->where($this->getHasCompareKey(), 'exists', true); } + + /** + * Get the name of the "where in" method for eager loading. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @return string + */ + protected function whereInMethod(EloquentModel $model, $key) + { + return 'whereIn'; + } } diff --git a/src/Jenssegers/Mongodb/Relations/HasOne.php b/src/Jenssegers/Mongodb/Relations/HasOne.php index c91a65787..a8d65dba9 100644 --- a/src/Jenssegers/Mongodb/Relations/HasOne.php +++ b/src/Jenssegers/Mongodb/Relations/HasOne.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\HasOne as EloquentHasOne; +use Illuminate\Database\Eloquent\Model as EloquentModel; class HasOne extends EloquentHasOne { @@ -77,4 +78,16 @@ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['* return $query->where($this->getForeignKeyName(), 'exists', true); } + + /** + * Get the name of the "where in" method for eager loading. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @return string + */ + protected function whereInMethod(EloquentModel $model, $key) + { + return 'whereIn'; + } } diff --git a/src/Jenssegers/Mongodb/Relations/MorphTo.php b/src/Jenssegers/Mongodb/Relations/MorphTo.php index 08344f526..32f3d3ab6 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphTo.php +++ b/src/Jenssegers/Mongodb/Relations/MorphTo.php @@ -3,6 +3,7 @@ namespace Jenssegers\Mongodb\Relations; use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo; +use Illuminate\Database\Eloquent\Model as EloquentModel; class MorphTo extends EloquentMorphTo { @@ -42,4 +43,16 @@ public function getOwnerKey() { return property_exists($this, 'ownerKey') ? $this->ownerKey : $this->otherKey; } + + /** + * Get the name of the "where in" method for eager loading. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @return string + */ + protected function whereInMethod(EloquentModel $model, $key) + { + return 'whereIn'; + } } From 317369cfcb05f10ab5b6ba79d2aa78d9f02b941a Mon Sep 17 00:00:00 2001 From: reatang Date: Thu, 20 Dec 2018 21:56:53 +0800 Subject: [PATCH 15/47] fix bug The main solution is to solve the bugs encountered in using database alone --- README.md | 4 +++- src/Jenssegers/Mongodb/Query/Builder.php | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d1bc60f09..816d7b385 100644 --- a/README.md +++ b/README.md @@ -61,8 +61,10 @@ The service provider will register a mongodb database extension with the origina For usage outside Laravel, check out the [Capsule manager](https://github.com/illuminate/database/blob/master/README.md) and add: ```php -$capsule->getDatabaseManager()->extend('mongodb', function($config) +$capsule->getDatabaseManager()->extend('mongodb', function($config, $name) { + $config['name'] = $name; + return new Jenssegers\Mongodb\Connection($config); }); ``` diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 62f203f7c..2effd897f 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -152,6 +152,8 @@ protected function shouldUseCollections() $version = filter_var(explode(')', $version)[0], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); // lumen return version_compare($version, '5.3', '>='); } + + return true; } /** From 625033e5c539db68221e3d0c8a691bd1f64d6524 Mon Sep 17 00:00:00 2001 From: "i.prokopenko" Date: Thu, 24 Jan 2019 11:25:09 +0300 Subject: [PATCH 16/47] reassigned getDatabaseName method --- src/Jenssegers/Mongodb/Connection.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 7276155a6..b95d922a1 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -113,6 +113,14 @@ public function getMongoClient() return $this->connection; } + /** + * {@inheritdoc} + */ + public function getDatabaseName() + { + return $this->getMongoDB()->getDatabaseName(); + } + /** * Create a new MongoDB connection. * From fdced669be0c61ce7d0f8b0427962aeedd8a59ae Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Sat, 9 Feb 2019 01:09:52 +0100 Subject: [PATCH 17/47] Remove dead code --- src/Jenssegers/Mongodb/Auth/PasswordBrokerManager.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Jenssegers/Mongodb/Auth/PasswordBrokerManager.php b/src/Jenssegers/Mongodb/Auth/PasswordBrokerManager.php index 5320ae890..281f8af75 100644 --- a/src/Jenssegers/Mongodb/Auth/PasswordBrokerManager.php +++ b/src/Jenssegers/Mongodb/Auth/PasswordBrokerManager.php @@ -11,14 +11,6 @@ class PasswordBrokerManager extends BasePasswordBrokerManager */ protected function createTokenRepository(array $config) { - $key = $this->app['config']['app.key']; - - if (\Illuminate\Support\Str::startsWith($key, 'base64:')) { - $key = base64_decode(substr($key, 7)); - } - - $connection = isset($config['connection']) ? $config['connection'] : null; - return new DatabaseTokenRepository( $this->app['db']->connection(), $this->app['hash'], From 08868a2a716711dcd992913118440d8fd63f0547 Mon Sep 17 00:00:00 2001 From: Filip Iulian Pacurar Date: Tue, 26 Feb 2019 19:40:34 +0200 Subject: [PATCH 18/47] Update Model.php --- src/Jenssegers/Mongodb/Eloquent/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Eloquent/Model.php b/src/Jenssegers/Mongodb/Eloquent/Model.php index 6b931e468..ef5e87184 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Model.php +++ b/src/Jenssegers/Mongodb/Eloquent/Model.php @@ -228,7 +228,7 @@ public function getCasts() /** * @inheritdoc */ - protected function originalIsEquivalent($key, $current) + public function originalIsEquivalent($key, $current) { if (!array_key_exists($key, $this->original)) { return false; From 4cdcdd2528355977ee171d9d6c682f1275c323ee Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Wed, 27 Feb 2019 17:25:45 +0100 Subject: [PATCH 19/47] Update supported versions --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4bf1c4384..e444dd9d2 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ composer require jenssegers/mongodb 5.4.x | 3.2.x 5.5.x | 3.3.x 5.6.x | 3.4.x + 5.7.x | 3.4.x + 5.8.x | 3.5.x And add the service provider in `config/app.php`: From 55c1aa15305a2711c0c34af16602ca2b36275fa6 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Wed, 27 Feb 2019 17:26:11 +0100 Subject: [PATCH 20/47] Require Laravel 5.8 --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index ce32f6407..e0da76b94 100644 --- a/composer.json +++ b/composer.json @@ -11,10 +11,10 @@ ], "license" : "MIT", "require": { - "illuminate/support": "^5.6", - "illuminate/container": "^5.6", - "illuminate/database": "^5.6", - "illuminate/events": "^5.6", + "illuminate/support": "^5.8", + "illuminate/container": "^5.8", + "illuminate/database": "^5.8", + "illuminate/events": "^5.8", "mongodb/mongodb": "^1.0.0" }, "require-dev": { From bdb15c948cb1f3d703df0996753d7e5336950bee Mon Sep 17 00:00:00 2001 From: Stas Date: Tue, 12 Mar 2019 09:49:10 +0300 Subject: [PATCH 21/47] Fix tests (#1724) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit *   Update Dockerfile, add for methods setUp and tearDown return type hint in all tests, replace fire on dispatch * Resolve error test with touch * Use COMPOSER_VERSION in Dockerfile * Add alias for composer * Change parent method's names Broken commit in laravel: https://github.com/laravel/framework/commit/2ee18923eaff142a89439f0adf0d3f15c30db85b * Remove changes in .travis.yml * Remove changes from Dockerfile * Revert changes in docker-compose.yml * Update image with mysql --- docker-compose.yml | 2 +- src/Jenssegers/Mongodb/Eloquent/Builder.php | 25 ++++++++ .../Mongodb/Helpers/QueriesRelationships.php | 4 +- tests/AuthTest.php | 3 +- tests/EmbeddedRelationsTest.php | 58 +++++++++---------- tests/GeospatialTest.php | 4 +- tests/HybridRelationsTest.php | 4 +- tests/ModelTest.php | 3 +- tests/QueryBuilderTest.php | 2 +- tests/QueryTest.php | 4 +- tests/QueueTest.php | 2 +- tests/RelationsTest.php | 2 +- tests/SchemaTest.php | 2 +- tests/SeederTest.php | 2 +- tests/ValidationTest.php | 2 +- 15 files changed, 72 insertions(+), 47 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 42546660b..a6e521d3b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,7 +16,7 @@ services: mysql: container_name: mysql - image: mysql + image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: MYSQL_DATABASE: unittest diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index 1ee1c41fe..b14053229 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -177,6 +177,31 @@ public function raw($expression = null) return $results; } + /** + * Add the "updated at" column to an array of values. + * TODO Remove if https://github.com/laravel/framework/commit/6484744326531829341e1ff886cc9b628b20d73e + * wiil be reverted + * Issue in laravel frawework https://github.com/laravel/framework/issues/27791 + * + * @param array $values + * @return array + */ + protected function addUpdatedAtColumn(array $values) + { + if (! $this->model->usesTimestamps() || + is_null($this->model->getUpdatedAtColumn())) { + return $values; + } + + $column = $this->model->getUpdatedAtColumn(); + $values = array_merge( + [$column => $this->model->freshTimestampString()], + $values + ); + + return $values; + } + /** * @return \Illuminate\Database\ConnectionInterface */ diff --git a/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php b/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php index 5fe908208..766567627 100644 --- a/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php +++ b/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php @@ -110,7 +110,7 @@ protected function getRelatedConstraintKey($relation) } if ($relation instanceof BelongsTo) { - return $relation->getForeignKey(); + return $relation->getForeignKeyName(); } if ($relation instanceof BelongsToMany && ! $this->isAcrossConnections($relation)) { @@ -130,7 +130,7 @@ protected function getHasCompareKey($relation) return $relation->getHasCompareKey(); } - return $relation instanceof HasOneOrMany ? $relation->getForeignKeyName() : $relation->getOwnerKey(); + return $relation instanceof HasOneOrMany ? $relation->getForeignKeyName() : $relation->getOwnerKeyName(); } /** diff --git a/tests/AuthTest.php b/tests/AuthTest.php index 2b671fdd5..e81efed90 100644 --- a/tests/AuthTest.php +++ b/tests/AuthTest.php @@ -5,8 +5,9 @@ class AuthTest extends TestCase { - public function tearDown() + public function tearDown(): void { + parent::setUp(); User::truncate(); DB::collection('password_reminders')->truncate(); } diff --git a/tests/EmbeddedRelationsTest.php b/tests/EmbeddedRelationsTest.php index 5caae5b28..440761e78 100644 --- a/tests/EmbeddedRelationsTest.php +++ b/tests/EmbeddedRelationsTest.php @@ -2,7 +2,7 @@ class EmbeddedRelationsTest extends TestCase { - public function tearDown() + public function tearDown(): void { Mockery::close(); @@ -21,11 +21,11 @@ public function testEmbedsManySave() $address = new Address(['city' => 'London']); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true); - $events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($address), $address); - $events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($address), $address); + $events->shouldReceive('dispatch')->once()->with('eloquent.created: ' . get_class($address), $address); + $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($address), $address); $address = $user->addresses()->save($address); $address->unsetEventDispatcher(); @@ -47,11 +47,11 @@ public function testEmbedsManySave() $this->assertEquals(['London', 'Paris'], $user->addresses->pluck('city')->all()); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true); - $events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($address), $address); - $events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($address), $address); + $events->shouldReceive('dispatch')->once()->with('eloquent.updated: ' . get_class($address), $address); + $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($address), $address); $address->city = 'New York'; $user->addresses()->save($address); @@ -94,16 +94,16 @@ public function testEmbedsManySave() // $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); // $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); // $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true); - // $events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($address), $address); - // $events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($address), $address); + // $events->shouldReceive('dispatch')->once()->with('eloquent.created: ' . get_class($address), $address); + // $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($address), $address); // $address->save(); // $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); // $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); // $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true); - // $events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($address), $address); - // $events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($address), $address); + // $events->shouldReceive('dispatch')->once()->with('eloquent.updated: ' . get_class($address), $address); + // $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($address), $address); // $address->city = 'Paris'; // $address->save(); @@ -213,9 +213,9 @@ public function testEmbedsManyDestroy() $address = $user->addresses->first(); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true); - $events->shouldReceive('fire')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address')); + $events->shouldReceive('dispatch')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address')); $user->addresses()->destroy($address->_id); $this->assertEquals(['Bristol', 'Bruxelles'], $user->addresses->pluck('city')->all()); @@ -252,9 +252,9 @@ public function testEmbedsManyDelete() $address = $user->addresses->first(); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true); - $events->shouldReceive('fire')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address')); + $events->shouldReceive('dispatch')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address')); $address->delete(); @@ -301,7 +301,7 @@ public function testEmbedsManyCreatingEventReturnsFalse() $address = new Address(['city' => 'London']); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(false); @@ -316,7 +316,7 @@ public function testEmbedsManySavingEventReturnsFalse() $address->exists = true; $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(false); $this->assertFalse($user->addresses()->save($address)); @@ -330,7 +330,7 @@ public function testEmbedsManyUpdatingEventReturnsFalse() $user->addresses()->save($address); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(false); @@ -348,7 +348,7 @@ public function testEmbedsManyDeletingEventReturnsFalse() $address = $user->addresses->first(); $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::mustBe($address))->andReturn(false); $this->assertEquals(0, $user->addresses()->destroy($address)); @@ -452,11 +452,11 @@ public function testEmbedsOne() $father = new User(['name' => 'Mark Doe']); $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true); - $events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($father), $father); - $events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($father), $father); + $events->shouldReceive('dispatch')->once()->with('eloquent.created: ' . get_class($father), $father); + $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($father), $father); $father = $user->father()->save($father); $father->unsetEventDispatcher(); @@ -472,11 +472,11 @@ public function testEmbedsOne() $this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']); $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($father), $father)->andReturn(true); - $events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($father), $father); - $events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($father), $father); + $events->shouldReceive('dispatch')->once()->with('eloquent.updated: ' . get_class($father), $father); + $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($father), $father); $father->name = 'Tom Doe'; $user->father()->save($father); @@ -488,11 +488,11 @@ public function testEmbedsOne() $father = new User(['name' => 'Jim Doe']); $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true); - $events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($father), $father); - $events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($father), $father); + $events->shouldReceive('dispatch')->once()->with('eloquent.created: ' . get_class($father), $father); + $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($father), $father); $father = $user->father()->save($father); $father->unsetEventDispatcher(); @@ -507,7 +507,7 @@ public function testEmbedsOneAssociate() $father = new User(['name' => 'Mark Doe']); $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); - $events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); + $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); $events->shouldReceive('until')->times(0)->with('eloquent.saving: ' . get_class($father), $father); $father = $user->father()->associate($father); diff --git a/tests/GeospatialTest.php b/tests/GeospatialTest.php index 2d646b337..b13ed46af 100644 --- a/tests/GeospatialTest.php +++ b/tests/GeospatialTest.php @@ -2,7 +2,7 @@ class GeospatialTest extends TestCase { - public function setUp() + public function setUp(): void { parent::setUp(); @@ -43,7 +43,7 @@ public function setUp() ]); } - public function tearDown() + public function tearDown(): void { Schema::drop('locations'); } diff --git a/tests/HybridRelationsTest.php b/tests/HybridRelationsTest.php index 2223950ec..ade509067 100644 --- a/tests/HybridRelationsTest.php +++ b/tests/HybridRelationsTest.php @@ -2,7 +2,7 @@ class HybridRelationsTest extends TestCase { - public function setUp() + public function setUp(): void { parent::setUp(); @@ -11,7 +11,7 @@ public function setUp() MysqlRole::executeSchema(); } - public function tearDown() + public function tearDown(): void { MysqlUser::truncate(); MysqlBook::truncate(); diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 4387d1231..f6e4f4eb6 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -8,7 +8,7 @@ class ModelTest extends TestCase { - public function tearDown() + public function tearDown(): void { User::truncate(); Soft::truncate(); @@ -262,7 +262,6 @@ public function testTouch() $user->save(); $old = $user->updated_at; - sleep(1); $user->touch(); $check = User::find($user->_id); diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index ac1479d26..0807b4ece 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -5,7 +5,7 @@ class QueryBuilderTest extends TestCase { - public function tearDown() + public function tearDown(): void { DB::collection('users')->truncate(); DB::collection('items')->truncate(); diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 2011305c2..de8589ebd 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -4,7 +4,7 @@ class QueryTest extends TestCase { protected static $started = false; - public function setUp() + public function setUp(): void { parent::setUp(); User::create(['name' => 'John Doe', 'age' => 35, 'title' => 'admin']); @@ -18,7 +18,7 @@ public function setUp() User::create(['name' => 'Error', 'age' => null, 'title' => null]); } - public function tearDown() + public function tearDown(): void { User::truncate(); parent::tearDown(); diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 35be33f9a..f3ebc94f6 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -2,7 +2,7 @@ class QueueTest extends TestCase { - public function setUp() + public function setUp(): void { parent::setUp(); diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 1e2aaa491..de3e0f222 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -2,7 +2,7 @@ class RelationsTest extends TestCase { - public function tearDown() + public function tearDown(): void { Mockery::close(); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index a04715d9d..5d63e28eb 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -2,7 +2,7 @@ class SchemaTest extends TestCase { - public function tearDown() + public function tearDown(): void { Schema::drop('newcollection'); } diff --git a/tests/SeederTest.php b/tests/SeederTest.php index 9581df3d3..61143e330 100644 --- a/tests/SeederTest.php +++ b/tests/SeederTest.php @@ -2,7 +2,7 @@ class SeederTest extends TestCase { - public function tearDown() + public function tearDown(): void { User::truncate(); } diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index 16e31f4cf..267996420 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -2,7 +2,7 @@ class ValidationTest extends TestCase { - public function tearDown() + public function tearDown(): void { User::truncate(); } From ae7b1a9ec4508570261dc16c208cdc6bf27e36ba Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Sun, 17 Mar 2019 15:52:15 +0100 Subject: [PATCH 22/47] :whale: New travis docker setup --- .travis.yml | 26 +++++++++++++++++--------- Dockerfile | 18 ++++++++---------- composer.json | 2 +- docker-compose.yml | 9 +++++---- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index fe11b61da..736c4c86e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,27 @@ -sudo: required -dist: trusty -language: php -php: - - "7.2" - - "7.1" +language: minimal + +matrix: + include: + - name: "7.1" + env: PHP_VERSION=7.1 + - name: "7.2" + env: PHP_VERSION=7.2 + - name: "7.3" + env: PHP_VERSION=7.3 services: - docker +cache: + directories: + - $HOME/.composer/cache + install: - docker version - sudo pip install docker-compose - docker-compose version - - cat Dockerfile - - docker-compose build --build-arg PHP_VERSION=${TRAVIS_PHP_VERSION} + - docker-compose build --build-arg PHP_VERSION=${PHP_VERSION} + - docker-compose run --rm tests composer install --no-interaction script: - - docker-compose up --exit-code-from php + - docker-compose run --rm tests ./vendor/bin/phpunit --coverage-clover ./clover.xml diff --git a/Dockerfile b/Dockerfile index 68eac169d..360f67e66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,15 @@ ARG PHP_VERSION=7.2 +ARG COMPOSER_VERSION=1.8 +FROM composer:${COMPOSER_VERSION} FROM php:${PHP_VERSION}-cli -RUN pecl install xdebug - RUN apt-get update && \ - apt-get install -y autoconf pkg-config libssl-dev git zlib1g-dev - -RUN pecl install mongodb && docker-php-ext-enable mongodb && \ - docker-php-ext-install -j$(nproc) pdo pdo_mysql zip && docker-php-ext-enable xdebug + apt-get install -y autoconf pkg-config libssl-dev git libzip-dev zlib1g-dev && \ + pecl install mongodb && docker-php-ext-enable mongodb && \ + pecl install xdebug && docker-php-ext-enable xdebug && \ + docker-php-ext-install -j$(nproc) pdo_mysql zip -RUN curl -sS https://getcomposer.org/installer | php \ - && mv composer.phar /usr/local/bin/ \ - && ln -s /usr/local/bin/composer.phar /usr/local/bin/composer +COPY --from=composer /usr/bin/composer /usr/local/bin/composer -ENV PATH="~/.composer/vendor/bin:./vendor/bin:${PATH}" +WORKDIR /code diff --git a/composer.json b/composer.json index e0da76b94..cdd4bff2b 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "illuminate/container": "^5.8", "illuminate/database": "^5.8", "illuminate/events": "^5.8", - "mongodb/mongodb": "^1.0.0" + "mongodb/mongodb": "^1.0" }, "require-dev": { "phpunit/phpunit": "^6.0|^7.0", diff --git a/docker-compose.yml b/docker-compose.yml index a6e521d3b..c6f20163e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,18 +1,17 @@ version: '3' services: - php: - container_name: php + tests: + container_name: tests build: context: . dockerfile: Dockerfile volumes: - .:/code working_dir: /code - command: bash -c "composer install --prefer-source --no-interaction && php ./vendor/bin/phpunit" depends_on: - - mysql - mongodb + - mysql mysql: container_name: mysql @@ -27,5 +26,7 @@ services: mongodb: container_name: mongodb image: mongo + ports: + - 27017:27017 logging: driver: none From a612c5f076a90af2219eb10d0f1190fc1676c8d6 Mon Sep 17 00:00:00 2001 From: Stas Date: Mon, 18 Mar 2019 10:04:43 +0300 Subject: [PATCH 23/47] Update phpunit.xml (#1733) --- phpunit.xml.dist | 3 --- 1 file changed, 3 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index bd9d7f5c4..2669537cb 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,13 +8,11 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" verbose="true" > tests/ - tests/MysqlRelationsTest.php tests/SchemaTest.php @@ -39,7 +37,6 @@ tests/RelationsTest.php - tests/MysqlRelationsTest.php tests/ValidationTest.php From e998cd0a2a859fbff80c8b77e030276df28f0dcc Mon Sep 17 00:00:00 2001 From: Stas Date: Wed, 20 Mar 2019 21:27:14 +0300 Subject: [PATCH 24/47] Add arg options for creating collections with options (#1734) --- composer.json | 2 +- src/Jenssegers/Mongodb/Schema/Builder.php | 4 ++-- tests/SchemaTest.php | 8 ++++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index cdd4bff2b..36d14bf48 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "illuminate/container": "^5.8", "illuminate/database": "^5.8", "illuminate/events": "^5.8", - "mongodb/mongodb": "^1.0" + "mongodb/mongodb": "^1.4" }, "require-dev": { "phpunit/phpunit": "^6.0|^7.0", diff --git a/src/Jenssegers/Mongodb/Schema/Builder.php b/src/Jenssegers/Mongodb/Schema/Builder.php index 799fe8e80..0e352846d 100644 --- a/src/Jenssegers/Mongodb/Schema/Builder.php +++ b/src/Jenssegers/Mongodb/Schema/Builder.php @@ -85,11 +85,11 @@ public function table($collection, Closure $callback) /** * @inheritdoc */ - public function create($collection, Closure $callback = null) + public function create($collection, Closure $callback = null, array $options = []) { $blueprint = $this->createBlueprint($collection); - $blueprint->create(); + $blueprint->create($options); if ($callback) { $callback($blueprint); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 5d63e28eb..b56cc639c 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -5,6 +5,7 @@ class SchemaTest extends TestCase public function tearDown(): void { Schema::drop('newcollection'); + Schema::drop('newcollection_two'); } public function testCreate() @@ -25,6 +26,13 @@ public function testCreateWithCallback() $this->assertTrue(Schema::hasCollection('newcollection')); } + public function testCreateWithOptions() + { + Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]); + $this->assertTrue(Schema::hasCollection('newcollection_two')); + $this->assertTrue(Schema::hasTable('newcollection_two')); + } + public function testDrop() { Schema::create('newcollection'); From fdf7f67b31e4911e8dc19e471701836a03360a6b Mon Sep 17 00:00:00 2001 From: Hamid Alaei Varnosfaderani Date: Sun, 7 Apr 2019 17:49:49 +0430 Subject: [PATCH 25/47] fix chunkById for types other than ObjectId and laravel >= 5.6.25 (#1543) --- src/Jenssegers/Mongodb/Query/Builder.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index a340976e3..ad63d462e 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -633,12 +633,6 @@ public function chunkById($count, callable $callback, $column = '_id', $alias = */ public function forPageAfterId($perPage = 15, $lastId = 0, $column = '_id') { - // When using ObjectIDs to paginate, we need to use a hex string as the - // "minimum" ID rather than the integer zero so the '$lt' query works. - if ($column === '_id' && $lastId === 0) { - $lastId = '000000000000000000000000'; - } - return parent::forPageAfterId($perPage, $lastId, $column); } From 83d0b8b4a5159d22836c11d7ee52fea641679f83 Mon Sep 17 00:00:00 2001 From: Matthieu Fauveau Date: Wed, 24 Apr 2019 17:21:49 -0400 Subject: [PATCH 26/47] Adds support for _id of binary type (#1611) --- src/Jenssegers/Mongodb/Eloquent/Model.php | 7 ++++++- src/Jenssegers/Mongodb/Query/Builder.php | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Jenssegers/Mongodb/Eloquent/Model.php b/src/Jenssegers/Mongodb/Eloquent/Model.php index ef5e87184..f84c4d66a 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Model.php +++ b/src/Jenssegers/Mongodb/Eloquent/Model.php @@ -9,6 +9,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; use Jenssegers\Mongodb\Query\Builder as QueryBuilder; +use MongoDB\BSON\Binary; use MongoDB\BSON\ObjectID; use MongoDB\BSON\UTCDateTime; use Illuminate\Contracts\Queue\QueueableEntity; @@ -63,6 +64,8 @@ public function getIdAttribute($value = null) // Convert ObjectID to string. if ($value instanceof ObjectID) { return (string) $value; + } elseif ($value instanceof Binary) { + return (string) $value->getData(); } return $value; @@ -172,7 +175,7 @@ protected function getAttributeFromArray($key) public function setAttribute($key, $value) { // Convert _id to ObjectID. - if ($key == '_id' && is_string($value)) { + if (($key == '_id' || Str::endsWith($key, '_id')) && is_string($value)) { $builder = $this->newBaseQueryBuilder(); $value = $builder->convertKey($value); @@ -204,6 +207,8 @@ public function attributesToArray() foreach ($attributes as $key => &$value) { if ($value instanceof ObjectID) { $value = (string) $value; + } elseif ($value instanceof Binary) { + $value = (string) $value->getData(); } } diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index ad63d462e..84874ed96 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -11,6 +11,7 @@ use Illuminate\Support\Str; use Jenssegers\Mongodb\Connection; use MongoCollection; +use MongoDB\BSON\Binary; use MongoDB\BSON\ObjectID; use MongoDB\BSON\Regex; use MongoDB\BSON\UTCDateTime; @@ -843,6 +844,8 @@ public function convertKey($id) { if (is_string($id) && strlen($id) === 24 && ctype_xdigit($id)) { return new ObjectID($id); + } elseif (strlen($id) === 16 && preg_match('~[^\x20-\x7E\t\r\n]~', $id) > 0) { + return new Binary($id, Binary::TYPE_UUID); } return $id; @@ -903,7 +906,7 @@ protected function compileWheres() } // Convert id's. - if (isset($where['column']) && ($where['column'] == '_id' || Str::endsWith($where['column'], '._id'))) { + if (isset($where['column']) && ($where['column'] == '_id' || Str::endsWith($where['column'], '_id'))) { // Multiple values. if (isset($where['values'])) { foreach ($where['values'] as &$value) { From eb51687ec4e304f1a52f4da86bbd90888b47bbc5 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Fri, 24 May 2019 13:25:41 +0200 Subject: [PATCH 27/47] Create FUNDING.yml --- .github/FUNDING.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 000000000..ba538f577 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: jenssegers From ea779ee2d190d34a8292ad86ba4111a8e8589648 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Fri, 24 May 2019 13:32:58 +0200 Subject: [PATCH 28/47] Update FUNDING.yml --- .github/FUNDING.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index ba538f577..cfc3b018b 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1 +1,2 @@ github: jenssegers +open_collective: jenssegers From 4ef3483e7a6e15c04c3d81e105dd7b398d6c2122 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Fri, 24 May 2019 13:36:47 +0200 Subject: [PATCH 29/47] Update FUNDING.yml --- .github/FUNDING.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index cfc3b018b..bd031bc1e 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,2 +1,2 @@ github: jenssegers -open_collective: jenssegers +open_collective: laravel-mongodb From 3f588096544e5de26fbadfb920f0713057bf8026 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Mon, 29 Jul 2019 14:54:22 +0200 Subject: [PATCH 30/47] Add hasIndex and dropIndexIfExists methods --- src/Jenssegers/Mongodb/Schema/Blueprint.php | 60 +++++++++++++++++---- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 0c01c96aa..551aac1ee 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -78,21 +78,24 @@ public function primary($columns = null, $name = null, $algorithm = null, $optio */ public function dropIndex($indexOrColumns = null) { - if (is_array($indexOrColumns)) { - $indexOrColumns = $this->fluent($indexOrColumns); + $indexOrColumns = $this->transformColumns($indexOrColumns); - // Transform the columns to the index name. - $transform = []; + $this->collection->dropIndex($indexOrColumns); - foreach ($indexOrColumns as $column) { - $transform[$column] = $column . '_1'; - } + return $this; + } - $indexOrColumns = join('_', $transform); + /** + * Indicate that the given index should be dropped, but do not fail if it didn't exist. + * + * @param string|array $indexOrColumns + * @return Blueprint + */ + public function dropIndexIfExists($indexOrColumns = null) + { + if ($this->hasIndex($indexOrColumns)) { + $this->dropIndex($indexOrColumns); } - - $this->collection->dropIndex($indexOrColumns); - return $this; } @@ -235,6 +238,41 @@ public function sparse_and_unique($columns = null, $options = []) return $this; } + /** + * Check whether the given index exists. + * + * @param string|array $indexOrColumns + * @return bool + */ + public function hasIndex($indexOrColumns = null) + { + $indexOrColumns = $this->transformColumns($indexOrColumns); + foreach ($this->collection->listIndexes() as $index) { + if ($index->getName() == $indexOrColumns) { + return true; + } + } + return false; + } + + /** + * @param string|array $indexOrColumns + * @return string|array + */ + private function transformColumns($indexOrColumns) + { + if (is_array($indexOrColumns)) { + $indexOrColumns = $this->fluent($indexOrColumns); + // Transform the columns to the index name. + $transform = []; + foreach ($indexOrColumns as $column) { + $transform[$column] = $column . '_1'; + } + $indexOrColumns = join('_', $transform); + } + return $indexOrColumns; + } + /** * Allow fluent columns. * From b9cc872a804e35547eb123b5f22d4f145d215da4 Mon Sep 17 00:00:00 2001 From: Stas Date: Thu, 1 Aug 2019 18:21:40 +0300 Subject: [PATCH 31/47] Revert changes in Builder and Model for id keys, fix test for Queue (#1795) * Revert changes in Builder and Model for id keys, fix test for Queue * Fix format in Model --- src/Jenssegers/Mongodb/Eloquent/Model.php | 4 ++-- src/Jenssegers/Mongodb/Query/Builder.php | 2 +- tests/QueueTest.php | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Jenssegers/Mongodb/Eloquent/Model.php b/src/Jenssegers/Mongodb/Eloquent/Model.php index f84c4d66a..de77b07bc 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Model.php +++ b/src/Jenssegers/Mongodb/Eloquent/Model.php @@ -32,7 +32,7 @@ abstract class Model extends BaseModel * @var string */ protected $primaryKey = '_id'; - + /** * The primary key type. * @@ -175,7 +175,7 @@ protected function getAttributeFromArray($key) public function setAttribute($key, $value) { // Convert _id to ObjectID. - if (($key == '_id' || Str::endsWith($key, '_id')) && is_string($value)) { + if ($key == '_id' && is_string($value)) { $builder = $this->newBaseQueryBuilder(); $value = $builder->convertKey($value); diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 84874ed96..79b034e27 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -906,7 +906,7 @@ protected function compileWheres() } // Convert id's. - if (isset($where['column']) && ($where['column'] == '_id' || Str::endsWith($where['column'], '_id'))) { + if (isset($where['column']) && ($where['column'] == '_id' || Str::endsWith($where['column'], '._id'))) { // Multiple values. if (isset($where['values'])) { foreach ($where['values'] as &$value) { diff --git a/tests/QueueTest.php b/tests/QueueTest.php index f3ebc94f6..7502ce6f7 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -24,6 +24,7 @@ public function testQueueJobLifeCycle() 'displayName' => 'test', 'job' => 'test', 'maxTries' => null, + 'delay' => null, 'timeout' => null, 'data' => ['action' => 'QueueJobLifeCycle'], ]), $job->getRawBody()); From cc66b3a85ac154f7db4c71238448396e71d2b333 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Thu, 1 Aug 2019 14:33:26 -0500 Subject: [PATCH 32/47] Get base query before update so that scopes are applied (#1799) Fixes #1798 --- src/Jenssegers/Mongodb/Eloquent/Builder.php | 2 +- tests/QueryTest.php | 18 ++++++++++++++++++ tests/models/Scoped.php | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/models/Scoped.php diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index b14053229..fff1cbdb8 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -44,7 +44,7 @@ public function update(array $values, array $options = []) return 1; } - return $this->query->update($this->addUpdatedAtColumn($values), $options); + return $this->toBase()->update($this->addUpdatedAtColumn($values), $options); } /** diff --git a/tests/QueryTest.php b/tests/QueryTest.php index de8589ebd..0175fd2ad 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -21,6 +21,7 @@ public function setUp(): void public function tearDown(): void { User::truncate(); + Scoped::truncate(); parent::tearDown(); } @@ -309,4 +310,21 @@ public function testPaginate() $this->assertEquals(9, $results->total()); $this->assertEquals(1, $results->currentPage()); } + + public function testUpdate() + { + $this->assertEquals(1, User::where(['name' => 'John Doe'])->update(['name' => 'Jim Morrison'])); + $this->assertEquals(1, User::where(['name' => 'Jim Morrison'])->count()); + + Scoped::create(['favorite' => true]); + Scoped::create(['favorite' => false]); + + $this->assertCount(1, Scoped::get()); + $this->assertEquals(1, Scoped::query()->update(['name' => 'Johnny'])); + $this->assertCount(1, Scoped::withoutGlobalScopes()->where(['name' => 'Johnny'])->get()); + + $this->assertCount(2, Scoped::withoutGlobalScopes()->get()); + $this->assertEquals(2, Scoped::withoutGlobalScopes()->update(['name' => 'Jimmy'])); + $this->assertCount(2, Scoped::withoutGlobalScopes()->where(['name' => 'Jimmy'])->get()); + } } diff --git a/tests/models/Scoped.php b/tests/models/Scoped.php new file mode 100644 index 000000000..77a55bd55 --- /dev/null +++ b/tests/models/Scoped.php @@ -0,0 +1,20 @@ +where('favorite', true); + }); + } +} From e2a8fae1504d5126ca3aecf467e629e6d7307ec6 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Thu, 1 Aug 2019 14:34:47 -0500 Subject: [PATCH 33/47] Return proper value instead of _id on QueryBuilder (#1747) Fixes #1741 --- src/Jenssegers/Mongodb/Query/Builder.php | 10 ++++++++++ tests/QueryBuilderTest.php | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 79b034e27..51b1527c9 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -204,6 +204,16 @@ public function find($id, $columns = []) return $this->where('_id', '=', $this->convertKey($id))->first($columns); } + /** + * @inheritdoc + */ + public function value($column) + { + $result = (array) $this->first([$column]); + + return Arr::get($result, $column); + } + /** * @inheritdoc */ diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 0807b4ece..097ded65b 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -701,4 +701,16 @@ public function testProjections() $this->assertEquals(1, count($result['tags'])); } } + + public function testValue() + { + DB::collection('books')->insert([ + ['title' => 'Moby-Dick', 'author' => ['first_name' => 'Herman', 'last_name' => 'Melville']] + ]); + + $this->assertEquals('Moby-Dick', DB::collection('books')->value('title')); + $this->assertEquals(['first_name' => 'Herman', 'last_name' => 'Melville'], DB::collection('books')->value('author')); + $this->assertEquals('Herman', DB::collection('books')->value('author.first_name')); + $this->assertEquals('Melville', DB::collection('books')->value('author.last_name')); + } } From c416f52408b6907a5f86313139ed76b9d89c2f6e Mon Sep 17 00:00:00 2001 From: Hamid Alaei V Date: Tue, 13 Aug 2019 10:52:47 +0430 Subject: [PATCH 34/47] fix for non string id --- src/Jenssegers/Mongodb/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 51b1527c9..d5c5b8278 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -854,7 +854,7 @@ public function convertKey($id) { if (is_string($id) && strlen($id) === 24 && ctype_xdigit($id)) { return new ObjectID($id); - } elseif (strlen($id) === 16 && preg_match('~[^\x20-\x7E\t\r\n]~', $id) > 0) { + } elseif (is_string($id) && strlen($id) === 16 && preg_match('~[^\x20-\x7E\t\r\n]~', $id) > 0) { return new Binary($id, Binary::TYPE_UUID); } From 14305686fa32a7943b56aa10bd301fcaf8ff528d Mon Sep 17 00:00:00 2001 From: Stas Date: Thu, 15 Aug 2019 10:48:57 +0300 Subject: [PATCH 35/47] use env values from phpunit.xml, update database.php and queue.php (#1738) * use env values from phpunit.xml, update database.php and queue.php * Add .editorconfig * Update QueueTest.php * Update phpunit.xml.dist * Use in casting instead intval --- .editorconfig | 9 +++++++++ phpunit.xml.dist | 31 ++++++++++++++++++++----------- tests/config/database.php | 17 ++++++++++------- tests/config/queue.php | 4 ++-- 4 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..fcdf61edc --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2669537cb..e2d82d39d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -15,31 +15,40 @@ tests/ - tests/SchemaTest.php + tests/SchemaTest.php - tests/SeederTest.php + tests/SeederTest.php - tests/CacheTest.php + tests/CacheTest.php - tests/QueryBuilderTest.php - tests/QueryTest.php + tests/QueryBuilderTest.php + tests/QueryTest.php - tests/ModelTest.php - tests/RelationsTest.php + tests/ModelTest.php + tests/RelationsTest.php - tests/RelationsTest.php - tests/EmbeddedRelationsTest.php + tests/RelationsTest.php + tests/EmbeddedRelationsTest.php - tests/RelationsTest.php + tests/RelationsTest.php - tests/ValidationTest.php + tests/ValidationTest.php + + + + + + + + + diff --git a/tests/config/database.php b/tests/config/database.php index f24d20d2f..9c22bb05a 100644 --- a/tests/config/database.php +++ b/tests/config/database.php @@ -1,5 +1,8 @@ [ @@ -7,21 +10,21 @@ 'mongodb' => [ 'name' => 'mongodb', 'driver' => 'mongodb', - 'host' => 'mongodb', - 'database' => 'unittest', + 'host' => $mongoHost, + 'database' => env('MONGO_DATABASE', 'unittest'), ], 'dsn_mongodb' => [ 'driver' => 'mongodb', - 'dsn' => 'mongodb://mongodb:27017', - 'database' => 'unittest', + 'dsn' => "mongodb://$mongoHost:$mongoPort", + 'database' => env('MONGO_DATABASE', 'unittest'), ], 'mysql' => [ 'driver' => 'mysql', - 'host' => 'mysql', - 'database' => 'unittest', - 'username' => 'root', + 'host' => env('MYSQL_HOST', 'mysql'), + 'database' => env('MYSQL_DATABASE', 'unittest'), + 'username' => env('MYSQL_USERNAME', 'root'), 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', diff --git a/tests/config/queue.php b/tests/config/queue.php index 03bc12044..e8203d90b 100644 --- a/tests/config/queue.php +++ b/tests/config/queue.php @@ -2,7 +2,7 @@ return [ - 'default' => 'database', + 'default' => env('QUEUE_CONNECTION'), 'connections' => [ @@ -16,7 +16,7 @@ ], 'failed' => [ - 'database' => 'mongodb', + 'database' => env('MONGO_DATABASE'), 'table' => 'failed_jobs', ], From 044290f8fc37efa14fcc56fb4d1127a89a4b812f Mon Sep 17 00:00:00 2001 From: Keven Lefebvre Date: Tue, 20 Aug 2019 16:32:35 -0400 Subject: [PATCH 36/47] Change operator (-> to ::) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e444dd9d2..a31c1330e 100644 --- a/README.md +++ b/README.md @@ -484,7 +484,7 @@ User::where('name', 'Jaques')->decrement('weight', 50); The number of updated objects is returned: ```php -$count = User->increment('age'); +$count = User::increment('age'); ``` You may also specify additional columns to update: From e821aec1a258294d6d7da631bf7ad7ed90af6a84 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Fri, 23 Aug 2019 19:09:37 +0200 Subject: [PATCH 37/47] Code cleanup --- .../Mongodb/Auth/DatabaseTokenRepository.php | 2 +- src/Jenssegers/Mongodb/Eloquent/Builder.php | 3 +- .../Mongodb/Eloquent/EmbedsRelations.php | 12 +-- .../Mongodb/Eloquent/HybridRelations.php | 28 +++--- src/Jenssegers/Mongodb/Query/Builder.php | 36 ++++---- src/Jenssegers/Mongodb/Queue/MongoQueue.php | 2 +- .../Mongodb/Relations/EmbedsMany.php | 4 +- .../Mongodb/Relations/EmbedsOneOrMany.php | 2 +- src/Jenssegers/Mongodb/Schema/Blueprint.php | 2 +- .../Validation/DatabasePresenceVerifier.php | 2 +- tests/CollectionTest.php | 1 + tests/ConnectionTest.php | 17 ++-- tests/DsnTest.php | 1 + tests/EmbeddedRelationsTest.php | 40 +++++---- tests/GeospatialTest.php | 1 + tests/HybridRelationsTest.php | 1 + tests/ModelTest.php | 89 +++++++++++-------- tests/QueryBuilderTest.php | 2 + tests/QueryTest.php | 37 ++++---- tests/QueueTest.php | 5 +- tests/RelationsTest.php | 41 ++++----- tests/SchemaTest.php | 37 ++++---- tests/SeederTest.php | 5 +- tests/TestCase.php | 5 +- tests/ValidationTest.php | 5 +- tests/config/database.php | 2 +- tests/models/Address.php | 4 +- tests/models/Book.php | 13 ++- tests/models/Client.php | 10 ++- tests/models/Group.php | 4 +- tests/models/Item.php | 12 ++- tests/models/Location.php | 1 + tests/models/MysqlBook.php | 10 ++- tests/models/MysqlRole.php | 10 ++- tests/models/MysqlUser.php | 15 ++-- tests/models/Photo.php | 4 +- tests/models/Role.php | 6 +- tests/models/Scoped.php | 1 + tests/models/Soft.php | 6 ++ tests/models/User.php | 15 +++- 40 files changed, 297 insertions(+), 196 deletions(-) diff --git a/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php b/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php index da6159f9e..515fb60af 100644 --- a/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php +++ b/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php @@ -27,7 +27,7 @@ protected function tokenExpired($createdAt) $date = $createdAt->toDateTime(); $date->setTimezone(new DateTimeZone(date_default_timezone_get())); $createdAt = $date->format('Y-m-d H:i:s'); - } elseif (is_array($createdAt) and isset($createdAt['date'])) { + } elseif (is_array($createdAt) && isset($createdAt['date'])) { $date = new DateTime($createdAt['date'], new DateTimeZone(isset($createdAt['timezone']) ? $createdAt['timezone'] : 'UTC')); $date->setTimezone(new DateTimeZone(date_default_timezone_get())); $createdAt = $date->format('Y-m-d H:i:s'); diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index fff1cbdb8..358be6b50 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -188,8 +188,7 @@ public function raw($expression = null) */ protected function addUpdatedAtColumn(array $values) { - if (! $this->model->usesTimestamps() || - is_null($this->model->getUpdatedAtColumn())) { + if (! $this->model->usesTimestamps() || $this->model->getUpdatedAtColumn() === null) { return $values; } diff --git a/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php b/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php index 307f5e330..c073e58a1 100644 --- a/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php +++ b/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php @@ -22,17 +22,17 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r // If no relation name was given, we will use this debug backtrace to extract // the calling method's name and use that as the relationship name as most // of the time this will be what we desire to use for the relationships. - if (is_null($relation)) { + if ($relation === null) { list(, $caller) = debug_backtrace(false); $relation = $caller['function']; } - if (is_null($localKey)) { + if ($localKey === null) { $localKey = $relation; } - if (is_null($foreignKey)) { + if ($foreignKey === null) { $foreignKey = Str::snake(class_basename($this)); } @@ -57,17 +57,17 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re // If no relation name was given, we will use this debug backtrace to extract // the calling method's name and use that as the relationship name as most // of the time this will be what we desire to use for the relationships. - if (is_null($relation)) { + if ($relation === null) { list(, $caller) = debug_backtrace(false); $relation = $caller['function']; } - if (is_null($localKey)) { + if ($localKey === null) { $localKey = $relation; } - if (is_null($foreignKey)) { + if ($foreignKey === null) { $foreignKey = Str::snake(class_basename($this)); } diff --git a/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php b/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php index 34b8b5788..b4af4dfc8 100644 --- a/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php +++ b/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php @@ -133,7 +133,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat // If no relation name was given, we will use this debug backtrace to extract // the calling method's name and use that as the relationship name as most // of the time this will be what we desire to use for the relationships. - if (is_null($relation)) { + if ($relation === null) { list($current, $caller) = debug_backtrace(false, 2); $relation = $caller['function']; @@ -147,7 +147,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat // If no foreign key was supplied, we can use a backtrace to guess the proper // foreign key name by using the name of the relationship function, which // when combined with an "_id" should conventionally match the columns. - if (is_null($foreignKey)) { + if ($foreignKey === null) { $foreignKey = Str::snake($relation) . '_id'; } @@ -177,7 +177,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null // If no name is provided, we will use the backtrace to get the function name // since that is most likely the name of the polymorphic interface. We can // use that to get both the class and foreign key that will be utilized. - if (is_null($name)) { + if ($name === null) { list($current, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); $name = Str::snake($caller['function']); @@ -188,7 +188,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null // If the type value is null it is probably safe to assume we're eager loading // the relationship. When that is the case we will pass in a dummy query as // there are multiple types in the morph and we can't use single queries. - if (is_null($class = $this->$type)) { + if (($class = $this->$type) === null) { return new MorphTo( $this->newQuery(), $this, $id, null, $type, $name ); @@ -197,15 +197,13 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null // If we are not eager loading the relationship we will essentially treat this // as a belongs-to style relationship since morph-to extends that class and // we will pass in the appropriate values so that it behaves as expected. - else { - $class = $this->getActualClassNameForMorph($class); + $class = $this->getActualClassNameForMorph($class); - $instance = new $class; + $instance = new $class; - return new MorphTo( - $instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name - ); - } + return new MorphTo( + $instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name + ); } /** @@ -232,7 +230,7 @@ public function belongsToMany( // If no relationship name was passed, we will pull backtraces to get the // name of the calling function. We will use that function name as the // title of this relation since that is a great convention to apply. - if (is_null($relation)) { + if ($relation === null) { $relation = $this->guessBelongsToManyRelation(); } @@ -261,7 +259,7 @@ public function belongsToMany( // If no table name was provided, we can guess it by concatenating the two // models using underscores in alphabetical order. The two model names // are transformed to snake case from their default CamelCase also. - if (is_null($collection)) { + if ($collection === null) { $collection = $instance->getTable(); } @@ -303,8 +301,8 @@ public function newEloquentBuilder($query) { if (is_subclass_of($this, \Jenssegers\Mongodb\Eloquent\Model::class)) { return new Builder($query); - } else { - return new EloquentBuilder($query); } + + return new EloquentBuilder($query); } } diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index d5c5b8278..88dfef68a 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -233,7 +233,7 @@ public function getFresh($columns = []) // If no columns have been specified for the select statement, we will set them // here to either the passed columns, or the standard default of retrieving // all of the columns on the table using the "wildcard" column character. - if (is_null($this->columns)) { + if ($this->columns === null) { $this->columns = $columns; } @@ -469,7 +469,7 @@ public function aggregate($function, $columns = []) */ public function exists() { - return !is_null($this->first()); + return $this->first() !== null; } /** @@ -580,7 +580,7 @@ public function insertGetId(array $values, $sequence = null) $result = $this->collection->insertOne($values); if (1 == (int) $result->isAcknowledged()) { - if (is_null($sequence)) { + if ($sequence === null) { $sequence = '_id'; } @@ -652,7 +652,7 @@ public function forPageAfterId($perPage = 15, $lastId = 0, $column = '_id') */ public function pluck($column, $key = null) { - $results = $this->get(is_null($key) ? [$column] : [$column, $key]); + $results = $this->get($key === null ? [$column] : [$column, $key]); // Convert ObjectID's to strings if ($key == '_id') { @@ -674,7 +674,7 @@ public function delete($id = null) // If an ID is passed to the method, we will set the where clause to check // the ID to allow developers to simply and quickly remove a single row // from their database without manually specifying the where clauses. - if (!is_null($id)) { + if ($id !== null) { $this->where('_id', '=', $id); } @@ -730,8 +730,10 @@ public function raw($expression = null) // Execute the closure on the mongodb collection if ($expression instanceof Closure) { return call_user_func($expression, $this->collection); - } // Create an expression for the given value - elseif (!is_null($expression)) { + } + + // Create an expression for the given value + if ($expression !== null) { return new Expression($expression); } @@ -854,7 +856,9 @@ public function convertKey($id) { if (is_string($id) && strlen($id) === 24 && ctype_xdigit($id)) { return new ObjectID($id); - } elseif (is_string($id) && strlen($id) === 16 && preg_match('~[^\x20-\x7E\t\r\n]~', $id) > 0) { + } + + if (is_string($id) && strlen($id) === 16 && preg_match('~[^\x20-\x7E\t\r\n]~', $id) > 0) { return new Binary($id, Binary::TYPE_UUID); } @@ -1009,7 +1013,7 @@ protected function compileWhereBasic(array $where) $regex = '^' . $regex; } if (!Str::endsWith($value, '%')) { - $regex = $regex . '$'; + $regex .= '$'; } $value = new Regex($regex, 'i'); @@ -1121,14 +1125,14 @@ protected function compileWhereBetween(array $where) ], ], ]; - } else { - return [ - $column => [ - '$gte' => $values[0], - '$lte' => $values[1], - ], - ]; } + + return [ + $column => [ + '$gte' => $values[0], + '$lte' => $values[1], + ], + ]; } /** diff --git a/src/Jenssegers/Mongodb/Queue/MongoQueue.php b/src/Jenssegers/Mongodb/Queue/MongoQueue.php index e9cd8da9c..5f08f7071 100644 --- a/src/Jenssegers/Mongodb/Queue/MongoQueue.php +++ b/src/Jenssegers/Mongodb/Queue/MongoQueue.php @@ -39,7 +39,7 @@ public function pop($queue = null) { $queue = $this->getQueue($queue); - if (!is_null($this->retryAfter)) { + if ($this->retryAfter !== null) { $this->releaseJobsThatHaveBeenReservedTooLong($queue); } diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php index 825b0d594..e26658e69 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php @@ -130,9 +130,9 @@ public function associate(Model $model) { if (!$this->contains($model)) { return $this->associateNew($model); - } else { - return $this->associateExisting($model); } + + return $this->associateExisting($model); } /** diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php index 177160bf1..f3bc58b8e 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php @@ -279,7 +279,7 @@ protected function toCollection(array $records = []) */ protected function toModel($attributes = []) { - if (is_null($attributes)) { + if ($attributes === null) { return; } diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 0c01c96aa..4b30c87fc 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -243,7 +243,7 @@ public function sparse_and_unique($columns = null, $options = []) */ protected function fluent($columns = null) { - if (is_null($columns)) { + if ($columns === null) { return $this->columns; } elseif (is_string($columns)) { return $this->columns = [$columns]; diff --git a/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php b/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php index 75722a8cc..fa3d68854 100644 --- a/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php +++ b/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php @@ -19,7 +19,7 @@ public function getCount($collection, $column, $value, $excludeId = null, $idCol { $query = $this->table($collection)->where($column, 'regex', "/$value/i"); - if (!is_null($excludeId) && $excludeId != 'NULL') { + if ($excludeId !== null && $excludeId != 'NULL') { $query->where($idColumn ?: 'id', '<>', $excludeId); } diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index f7b37bbac..d38687a54 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -1,4 +1,5 @@ assertInstanceOf('Jenssegers\Mongodb\Connection', $connection); + $this->assertInstanceOf(\Jenssegers\Mongodb\Connection::class, $connection); } public function testReconnect() @@ -23,22 +26,22 @@ public function testReconnect() public function testDb() { $connection = DB::connection('mongodb'); - $this->assertInstanceOf('MongoDB\Database', $connection->getMongoDB()); + $this->assertInstanceOf(\MongoDB\Database::class, $connection->getMongoDB()); $connection = DB::connection('mongodb'); - $this->assertInstanceOf('MongoDB\Client', $connection->getMongoClient()); + $this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient()); } public function testCollection() { $collection = DB::connection('mongodb')->getCollection('unittest'); - $this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection); + $this->assertInstanceOf(Jenssegers\Mongodb\Collection::class, $collection); $collection = DB::connection('mongodb')->collection('unittests'); - $this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', $collection); + $this->assertInstanceOf(Jenssegers\Mongodb\Query\Builder::class, $collection); $collection = DB::connection('mongodb')->table('unittests'); - $this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', $collection); + $this->assertInstanceOf(Jenssegers\Mongodb\Query\Builder::class, $collection); } // public function testDynamic() @@ -87,7 +90,7 @@ public function testQueryLog() public function testSchemaBuilder() { $schema = DB::connection('mongodb')->getSchemaBuilder(); - $this->assertInstanceOf('Jenssegers\Mongodb\Schema\Builder', $schema); + $this->assertInstanceOf(\Jenssegers\Mongodb\Schema\Builder::class, $schema); } public function testDriverName() diff --git a/tests/DsnTest.php b/tests/DsnTest.php index 08fa0a8aa..2eed354f4 100644 --- a/tests/DsnTest.php +++ b/tests/DsnTest.php @@ -1,4 +1,5 @@ 'John Doe']); $address = new Address(['city' => 'London']); - $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true); @@ -31,7 +32,7 @@ public function testEmbedsManySave() $address->unsetEventDispatcher(); $this->assertNotNull($user->addresses); - $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->addresses); + $this->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, $user->addresses); $this->assertEquals(['London'], $user->addresses->pluck('city')->all()); $this->assertInstanceOf('DateTime', $address->created_at); $this->assertInstanceOf('DateTime', $address->updated_at); @@ -39,14 +40,14 @@ public function testEmbedsManySave() $this->assertInternalType('string', $address->_id); $raw = $address->getAttributes(); - $this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']); + $this->assertInstanceOf(\MongoDB\BSON\ObjectID::class, $raw['_id']); $address = $user->addresses()->save(new Address(['city' => 'Paris'])); $user = User::find($user->_id); $this->assertEquals(['London', 'Paris'], $user->addresses->pluck('city')->all()); - $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true); @@ -91,7 +92,7 @@ public function testEmbedsManySave() // $user = User::create(['name' => 'John Doe']); // $address = new Address(['city' => 'London']); - // $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + // $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); // $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); // $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true); // $events->shouldReceive('dispatch')->once()->with('eloquent.created: ' . get_class($address), $address); @@ -99,7 +100,7 @@ public function testEmbedsManySave() // $address->save(); - // $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + // $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); // $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); // $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true); // $events->shouldReceive('dispatch')->once()->with('eloquent.updated: ' . get_class($address), $address); @@ -180,7 +181,7 @@ public function testEmbedsManyCreate() $this->assertEquals(['Bruxelles'], $user->addresses->pluck('city')->all()); $raw = $address->getAttributes(); - $this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']); + $this->assertInstanceOf(\MongoDB\BSON\ObjectID::class, $raw['_id']); $freshUser = User::find($user->id); $this->assertEquals(['Bruxelles'], $freshUser->addresses->pluck('city')->all()); @@ -190,7 +191,7 @@ public function testEmbedsManyCreate() $this->assertInternalType('string', $address->_id); $raw = $address->getAttributes(); - $this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']); + $this->assertInstanceOf(\MongoDB\BSON\ObjectID::class, $raw['_id']); } public function testEmbedsManyCreateMany() @@ -212,7 +213,7 @@ public function testEmbedsManyDestroy() $address = $user->addresses->first(); - $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true); $events->shouldReceive('dispatch')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address')); @@ -251,7 +252,7 @@ public function testEmbedsManyDelete() $address = $user->addresses->first(); - $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true); $events->shouldReceive('dispatch')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address')); @@ -300,7 +301,7 @@ public function testEmbedsManyCreatingEventReturnsFalse() $user = User::create(['name' => 'John Doe']); $address = new Address(['city' => 'London']); - $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(false); @@ -315,7 +316,7 @@ public function testEmbedsManySavingEventReturnsFalse() $address = new Address(['city' => 'Paris']); $address->exists = true; - $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(false); @@ -329,7 +330,7 @@ public function testEmbedsManyUpdatingEventReturnsFalse() $address = new Address(['city' => 'New York']); $user->addresses()->save($address); - $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(false); @@ -347,7 +348,7 @@ public function testEmbedsManyDeletingEventReturnsFalse() $address = $user->addresses->first(); - $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::mustBe($address))->andReturn(false); @@ -451,7 +452,7 @@ public function testEmbedsOne() $user = User::create(['name' => 'John Doe']); $father = new User(['name' => 'Mark Doe']); - $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $father->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true); @@ -471,7 +472,7 @@ public function testEmbedsOne() $raw = $father->getAttributes(); $this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']); - $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $father->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($father), $father)->andReturn(true); @@ -487,7 +488,7 @@ public function testEmbedsOne() $father = new User(['name' => 'Jim Doe']); - $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $father->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true); @@ -506,7 +507,7 @@ public function testEmbedsOneAssociate() $user = User::create(['name' => 'John Doe']); $father = new User(['name' => 'Mark Doe']); - $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher')); + $father->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); $events->shouldReceive('until')->times(0)->with('eloquent.saving: ' . get_class($father), $father); @@ -534,6 +535,7 @@ public function testEmbedsOneDelete() public function testEmbedsManyToArray() { + /** @var User $user */ $user = User::create(['name' => 'John Doe']); $user->addresses()->save(new Address(['city' => 'New York'])); $user->addresses()->save(new Address(['city' => 'Paris'])); @@ -546,7 +548,9 @@ public function testEmbedsManyToArray() public function testEmbeddedSave() { + /** @var User $user */ $user = User::create(['name' => 'John Doe']); + /** @var \Address $address */ $address = $user->addresses()->create(['city' => 'New York']); $father = $user->father()->create(['name' => 'Mark Doe']); diff --git a/tests/GeospatialTest.php b/tests/GeospatialTest.php index b13ed46af..f1237e582 100644 --- a/tests/GeospatialTest.php +++ b/tests/GeospatialTest.php @@ -1,4 +1,5 @@ assertInstanceOf(Model::class, $user); - $this->assertInstanceOf('Jenssegers\Mongodb\Connection', $user->getConnection()); + $this->assertInstanceOf(\Jenssegers\Mongodb\Connection::class, $user->getConnection()); $this->assertFalse($user->exists); $this->assertEquals('users', $user->getTable()); $this->assertEquals('_id', $user->getKeyName()); } - public function testInsert() + public function testInsert(): void { $user = new User; $user->name = 'John Doe'; @@ -51,7 +53,7 @@ public function testInsert() $this->assertEquals(35, $user->age); } - public function testUpdate() + public function testUpdate(): void { $user = new User; $user->name = 'John Doe'; @@ -62,8 +64,8 @@ public function testUpdate() $raw = $user->getAttributes(); $this->assertInstanceOf(ObjectID::class, $raw['_id']); + /** @var User $check */ $check = User::find($user->_id); - $check->age = 36; $check->save(); @@ -84,7 +86,7 @@ public function testUpdate() $this->assertEquals(20, $check->age); } - public function testManualStringId() + public function testManualStringId(): void { $user = new User; $user->_id = '4af9f23d8ead0e1d32000000'; @@ -113,7 +115,7 @@ public function testManualStringId() $this->assertInternalType('string', $raw['_id']); } - public function testManualIntId() + public function testManualIntId(): void { $user = new User; $user->_id = 1; @@ -129,7 +131,7 @@ public function testManualIntId() $this->assertInternalType('integer', $raw['_id']); } - public function testDelete() + public function testDelete(): void { $user = new User; $user->name = 'John Doe'; @@ -145,7 +147,7 @@ public function testDelete() $this->assertEquals(0, User::count()); } - public function testAll() + public function testAll(): void { $user = new User; $user->name = 'John Doe'; @@ -166,7 +168,7 @@ public function testAll() $this->assertContains('Jane Doe', $all->pluck('name')); } - public function testFind() + public function testFind(): void { $user = new User; $user->name = 'John Doe'; @@ -174,6 +176,7 @@ public function testFind() $user->age = 35; $user->save(); + /** @var User $check */ $check = User::find($user->_id); $this->assertInstanceOf(Model::class, $check); @@ -184,7 +187,7 @@ public function testFind() $this->assertEquals(35, $check->age); } - public function testGet() + public function testGet(): void { User::insert([ ['name' => 'John Doe'], @@ -197,19 +200,20 @@ public function testGet() $this->assertInstanceOf(Model::class, $users[0]); } - public function testFirst() + public function testFirst(): void { User::insert([ ['name' => 'John Doe'], ['name' => 'Jane Doe'], ]); + /** @var User $user */ $user = User::first(); $this->assertInstanceOf(Model::class, $user); $this->assertEquals('John Doe', $user->name); } - public function testNoDocument() + public function testNoDocument(): void { $items = Item::where('name', 'nothing')->get(); $this->assertInstanceOf(Collection::class, $items); @@ -222,25 +226,27 @@ public function testNoDocument() $this->assertNull($item); } - public function testFindOrfail() + public function testFindOrFail(): void { - $this->expectException(Illuminate\Database\Eloquent\ModelNotFoundException::class); - User::findOrfail('51c33d8981fec6813e00000a'); + $this->expectException(ModelNotFoundException::class); + User::findOrFail('51c33d8981fec6813e00000a'); } - public function testCreate() + public function testCreate(): void { + /** @var User $user */ $user = User::create(['name' => 'Jane Poe']); $this->assertInstanceOf(Model::class, $user); $this->assertTrue($user->exists); $this->assertEquals('Jane Poe', $user->name); + /** @var User $check */ $check = User::where('name', 'Jane Poe')->first(); $this->assertEquals($user->_id, $check->_id); } - public function testDestroy() + public function testDestroy(): void { $user = new User; $user->name = 'John Doe'; @@ -253,7 +259,7 @@ public function testDestroy() $this->assertEquals(0, User::count()); } - public function testTouch() + public function testTouch(): void { $user = new User; $user->name = 'John Doe'; @@ -264,18 +270,21 @@ public function testTouch() $old = $user->updated_at; sleep(1); $user->touch(); + + /** @var User $check */ $check = User::find($user->_id); $this->assertNotEquals($old, $check->updated_at); } - public function testSoftDelete() + public function testSoftDelete(): void { Soft::create(['name' => 'John Doe']); Soft::create(['name' => 'Jane Doe']); $this->assertEquals(2, Soft::count()); + /** @var Soft $user */ $user = Soft::where('name', 'John Doe')->first(); $this->assertTrue($user->exists); $this->assertFalse($user->trashed()); @@ -300,7 +309,7 @@ public function testSoftDelete() $this->assertEquals(2, Soft::count()); } - public function testPrimaryKey() + public function testPrimaryKey(): void { $user = new User; $this->assertEquals('_id', $user->getKeyName()); @@ -314,13 +323,14 @@ public function testPrimaryKey() $this->assertEquals('A Game of Thrones', $book->getKey()); + /** @var Book $check */ $check = Book::find('A Game of Thrones'); $this->assertEquals('title', $check->getKeyName()); $this->assertEquals('A Game of Thrones', $check->getKey()); $this->assertEquals('A Game of Thrones', $check->title); } - public function testScope() + public function testScope(): void { Item::insert([ ['name' => 'knife', 'type' => 'sharp'], @@ -331,7 +341,7 @@ public function testScope() $this->assertEquals(1, $sharp->count()); } - public function testToArray() + public function testToArray(): void { $item = Item::create(['name' => 'fork', 'type' => 'sharp']); @@ -344,7 +354,7 @@ public function testToArray() $this->assertInternalType('string', $array['_id']); } - public function testUnset() + public function testUnset(): void { $user1 = User::create(['name' => 'John Doe', 'note1' => 'ABC', 'note2' => 'DEF']); $user2 = User::create(['name' => 'Jane Doe', 'note1' => 'ABC', 'note2' => 'DEF']); @@ -371,7 +381,7 @@ public function testUnset() $this->assertObjectNotHasAttribute('note2', $user2); } - public function testDates() + public function testDates(): void { $birthday = new DateTime('1980/1/1'); $user = User::create(['name' => 'John Doe', 'birthday' => $birthday]); @@ -398,10 +408,12 @@ public function testDates() $this->assertLessThan(2, abs(time() - $item->created_at->getTimestamp())); // test default date format for json output + /** @var Item $item */ $item = Item::create(['name' => 'sword']); $json = $item->toArray(); $this->assertEquals($item->created_at->format('Y-m-d H:i:s'), $json['created_at']); + /** @var User $user */ $user = User::create(['name' => 'Jane Doe', 'birthday' => time()]); $this->assertInstanceOf(Carbon::class, $user->birthday); @@ -422,8 +434,9 @@ public function testDates() $this->assertEquals((string) $user->getAttribute('entry.date')->format('Y-m-d H:i:s'), $data['entry']['date']); } - public function testIdAttribute() + public function testIdAttribute(): void { + /** @var User $user */ $user = User::create(['name' => 'John Doe']); $this->assertEquals($user->id, $user->_id); @@ -431,8 +444,9 @@ public function testIdAttribute() $this->assertNotEquals($user->id, $user->_id); } - public function testPushPull() + public function testPushPull(): void { + /** @var User $user */ $user = User::create(['name' => 'John Doe']); $user->push('tags', 'tag1'); @@ -457,36 +471,36 @@ public function testPushPull() $this->assertEquals([], $user->tags); } - public function testRaw() + public function testRaw(): void { User::create(['name' => 'John Doe', 'age' => 35]); User::create(['name' => 'Jane Doe', 'age' => 35]); User::create(['name' => 'Harry Hoe', 'age' => 15]); - $users = User::raw(function ($collection) { + $users = User::raw(function (\Jenssegers\Mongodb\Collection $collection) { return $collection->find(['age' => 35]); }); $this->assertInstanceOf(Collection::class, $users); $this->assertInstanceOf(Model::class, $users[0]); - $user = User::raw(function ($collection) { + $user = User::raw(function (\Jenssegers\Mongodb\Collection $collection) { return $collection->findOne(['age' => 35]); }); $this->assertInstanceOf(Model::class, $user); - $count = User::raw(function ($collection) { + $count = User::raw(function (\Jenssegers\Mongodb\Collection $collection) { return $collection->count(); }); $this->assertEquals(3, $count); - $result = User::raw(function ($collection) { + $result = User::raw(function (\Jenssegers\Mongodb\Collection $collection) { return $collection->insertOne(['name' => 'Yvonne Yoe', 'age' => 35]); }); $this->assertNotNull($result); } - public function testDotNotation() + public function testDotNotation(): void { $user = User::create([ 'name' => 'John Doe', @@ -508,8 +522,9 @@ public function testDotNotation() $this->assertEquals('Strasbourg', $user['address.city']); } - public function testMultipleLevelDotNotation() + public function testMultipleLevelDotNotation(): void { + /** @var Book $book */ $book = Book::create([ 'title' => 'A Game of Thrones', 'chapters' => [ @@ -524,7 +539,7 @@ public function testMultipleLevelDotNotation() $this->assertEquals('The first chapter', $book['chapters.one.title']); } - public function testGetDirtyDates() + public function testGetDirtyDates(): void { $user = new User(); $user->setRawAttributes(['name' => 'John Doe', 'birthday' => new DateTime('19 august 1989')], true); @@ -534,14 +549,14 @@ public function testGetDirtyDates() $this->assertEmpty($user->getDirty()); } - public function testChunkById() + public function testChunkById(): void { User::create(['name' => 'fork', 'tags' => ['sharp', 'pointy']]); User::create(['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']]); User::create(['name' => 'spoon', 'tags' => ['round', 'bowl']]); $count = 0; - User::chunkById(2, function ($items) use (&$count) { + User::chunkById(2, function (\Illuminate\Database\Eloquent\Collection $items) use (&$count) { $count += count($items); }); diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 097ded65b..4b1321684 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -1,5 +1,7 @@ get(); $this->assertCount(3, $users); @@ -46,7 +47,7 @@ public function testWhere() $this->assertCount(6, $users); } - public function testAndWhere() + public function testAndWhere(): void { $users = User::where('age', 35)->where('title', 'admin')->get(); $this->assertCount(2, $users); @@ -55,7 +56,7 @@ public function testAndWhere() $this->assertCount(2, $users); } - public function testLike() + public function testLike(): void { $users = User::where('name', 'like', '%doe')->get(); $this->assertCount(2, $users); @@ -70,7 +71,7 @@ public function testLike() $this->assertCount(1, $users); } - public function testSelect() + public function testSelect(): void { $user = User::where('name', 'John Doe')->select('name')->first(); @@ -96,7 +97,7 @@ public function testSelect() $this->assertNull($user->age); } - public function testOrWhere() + public function testOrWhere(): void { $users = User::where('age', 13)->orWhere('title', 'admin')->get(); $this->assertCount(4, $users); @@ -105,7 +106,7 @@ public function testOrWhere() $this->assertCount(2, $users); } - public function testBetween() + public function testBetween(): void { $users = User::whereBetween('age', [0, 25])->get(); $this->assertCount(2, $users); @@ -118,7 +119,7 @@ public function testBetween() $this->assertCount(6, $users); } - public function testIn() + public function testIn(): void { $users = User::whereIn('age', [13, 23])->get(); $this->assertCount(2, $users); @@ -134,19 +135,19 @@ public function testIn() $this->assertCount(3, $users); } - public function testWhereNull() + public function testWhereNull(): void { $users = User::whereNull('age')->get(); $this->assertCount(1, $users); } - public function testWhereNotNull() + public function testWhereNotNull(): void { $users = User::whereNotNull('age')->get(); $this->assertCount(8, $users); } - public function testOrder() + public function testOrder(): void { $user = User::whereNotNull('age')->orderBy('age', 'asc')->first(); $this->assertEquals(13, $user->age); @@ -167,7 +168,7 @@ public function testOrder() $this->assertEquals(35, $user->age); } - public function testGroupBy() + public function testGroupBy(): void { $users = User::groupBy('title')->get(); $this->assertCount(3, $users); @@ -197,7 +198,7 @@ public function testGroupBy() $this->assertNotNull($users[0]->name); } - public function testCount() + public function testCount(): void { $count = User::where('age', '<>', 35)->count(); $this->assertEquals(6, $count); @@ -207,13 +208,13 @@ public function testCount() $this->assertEquals(6, $count); } - public function testExists() + public function testExists(): void { $this->assertFalse(User::where('age', '>', 37)->exists()); $this->assertTrue(User::where('age', '<', 37)->exists()); } - public function testSubquery() + public function testSubQuery(): void { $users = User::where('title', 'admin')->orWhere(function ($query) { $query->where('name', 'Tommy Toe') @@ -262,7 +263,7 @@ public function testSubquery() $this->assertEquals(5, $users->count()); } - public function testWhereRaw() + public function testWhereRaw(): void { $where = ['age' => ['$gt' => 30, '$lt' => 40]]; $users = User::whereRaw($where)->get(); @@ -276,7 +277,7 @@ public function testWhereRaw() $this->assertCount(6, $users); } - public function testMultipleOr() + public function testMultipleOr(): void { $users = User::where(function ($query) { $query->where('age', 35)->orWhere('age', 33); @@ -297,7 +298,7 @@ public function testMultipleOr() $this->assertCount(2, $users); } - public function testPaginate() + public function testPaginate(): void { $results = User::paginate(2); $this->assertEquals(2, $results->count()); @@ -311,7 +312,7 @@ public function testPaginate() $this->assertEquals(1, $results->currentPage()); } - public function testUpdate() + public function testUpdate(): void { $this->assertEquals(1, User::where(['name' => 'John Doe'])->update(['name' => 'Jim Morrison'])); $this->assertEquals(1, User::where(['name' => 'Jim Morrison'])->count()); diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 7502ce6f7..6ff26d35c 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -1,4 +1,5 @@ table(Config::get('queue.failed.table'))->truncate(); } - public function testQueueJobLifeCycle() + public function testQueueJobLifeCycle(): void { $id = Queue::push('test', ['action' => 'QueueJobLifeCycle'], 'test'); $this->assertNotNull($id); @@ -34,7 +35,7 @@ public function testQueueJobLifeCycle() $this->assertEquals(0, Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->count()); } - public function testQueueJobExpired() + public function testQueueJobExpired(): void { $id = Queue::push('test', ['action' => 'QueueJobExpired'], 'test'); $this->assertNotNull($id); diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index de3e0f222..decc4f14b 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -1,4 +1,5 @@ 'George R. R. Martin']); Book::create(['title' => 'A Game of Thrones', 'author_id' => $author->_id]); @@ -36,7 +37,7 @@ public function testHasMany() $this->assertCount(3, $items); } - public function testBelongsTo() + public function testBelongsTo(): void { $user = User::create(['name' => 'George R. R. Martin']); Book::create(['title' => 'A Game of Thrones', 'author_id' => $user->_id]); @@ -55,7 +56,7 @@ public function testBelongsTo() $this->assertNull($book->author); } - public function testHasOne() + public function testHasOne(): void { $user = User::create(['name' => 'John Doe']); Role::create(['type' => 'admin', 'user_id' => $user->_id]); @@ -78,7 +79,7 @@ public function testHasOne() $this->assertEquals($user->_id, $role->user_id); } - public function testWithBelongsTo() + public function testWithBelongsTo(): void { $user = User::create(['name' => 'John Doe']); Item::create(['type' => 'knife', 'user_id' => $user->_id]); @@ -95,7 +96,7 @@ public function testWithBelongsTo() $this->assertNull($items[3]->getRelation('user')); } - public function testWithHashMany() + public function testWithHashMany(): void { $user = User::create(['name' => 'John Doe']); Item::create(['type' => 'knife', 'user_id' => $user->_id]); @@ -110,7 +111,7 @@ public function testWithHashMany() $this->assertInstanceOf('Item', $items[0]); } - public function testWithHasOne() + public function testWithHasOne(): void { $user = User::create(['name' => 'John Doe']); Role::create(['type' => 'admin', 'user_id' => $user->_id]); @@ -123,7 +124,7 @@ public function testWithHasOne() $this->assertEquals('admin', $role->type); } - public function testEasyRelation() + public function testEasyRelation(): void { // Has Many $user = User::create(['name' => 'John Doe']); @@ -148,7 +149,7 @@ public function testEasyRelation() $this->assertEquals($user->_id, $role->user_id); } - public function testBelongsToMany() + public function testBelongsToMany(): void { $user = User::create(['name' => 'John Doe']); @@ -222,7 +223,7 @@ public function testBelongsToMany() $this->assertCount(1, $client->users); } - public function testBelongsToManyAttachesExistingModels() + public function testBelongsToManyAttachesExistingModels(): void { $user = User::create(['name' => 'John Doe', 'client_ids' => ['1234523']]); @@ -261,7 +262,7 @@ public function testBelongsToManyAttachesExistingModels() $this->assertStringStartsWith('synced', $user->clients[1]->name); } - public function testBelongsToManySync() + public function testBelongsToManySync(): void { // create test instances $user = User::create(['name' => 'John Doe']); @@ -280,7 +281,7 @@ public function testBelongsToManySync() $this->assertCount(1, $user->clients); } - public function testBelongsToManyAttachArray() + public function testBelongsToManyAttachArray(): void { $user = User::create(['name' => 'John Doe']); $client1 = Client::create(['name' => 'Test 1'])->_id; @@ -291,7 +292,7 @@ public function testBelongsToManyAttachArray() $this->assertCount(2, $user->clients); } - public function testBelongsToManyAttachEloquentCollection() + public function testBelongsToManyAttachEloquentCollection(): void { $user = User::create(['name' => 'John Doe']); $client1 = Client::create(['name' => 'Test 1']); @@ -303,7 +304,7 @@ public function testBelongsToManyAttachEloquentCollection() $this->assertCount(2, $user->clients); } - public function testBelongsToManySyncAlreadyPresent() + public function testBelongsToManySyncAlreadyPresent(): void { $user = User::create(['name' => 'John Doe']); $client1 = Client::create(['name' => 'Test 1'])->_id; @@ -320,7 +321,7 @@ public function testBelongsToManySyncAlreadyPresent() $this->assertCount(1, $user['client_ids']); } - public function testBelongsToManyCustom() + public function testBelongsToManyCustom(): void { $user = User::create(['name' => 'John Doe']); $group = $user->groups()->create(['name' => 'Admins']); @@ -340,7 +341,7 @@ public function testBelongsToManyCustom() $this->assertEquals($user->_id, $group->users()->first()->_id); } - public function testMorph() + public function testMorph(): void { $user = User::create(['name' => 'John Doe']); $client = Client::create(['name' => 'Jane Doe']); @@ -383,7 +384,7 @@ public function testMorph() $this->assertInstanceOf('Client', $photos[1]->imageable); } - public function testHasManyHas() + public function testHasManyHas(): void { $author1 = User::create(['name' => 'George R. R. Martin']); $author1->books()->create(['title' => 'A Game of Thrones', 'rating' => 5]); @@ -433,7 +434,7 @@ public function testHasManyHas() $this->assertCount(1, $authors); } - public function testHasOneHas() + public function testHasOneHas(): void { $user1 = User::create(['name' => 'John Doe']); $user1->role()->create(['title' => 'admin']); @@ -455,7 +456,7 @@ public function testHasOneHas() $this->assertCount(2, $users); } - public function testNestedKeys() + public function testNestedKeys(): void { $client = Client::create([ 'data' => [ @@ -481,7 +482,7 @@ public function testNestedKeys() $this->assertEquals('Paris', $client->addresses->first()->data['city']); } - public function testDoubleSaveOneToMany() + public function testDoubleSaveOneToMany(): void { $author = User::create(['name' => 'George R. R. Martin']); $book = Book::create(['title' => 'A Game of Thrones']); @@ -504,7 +505,7 @@ public function testDoubleSaveOneToMany() $this->assertEquals($author->_id, $book->author_id); } - public function testDoubleSaveManyToMany() + public function testDoubleSaveManyToMany(): void { $user = User::create(['name' => 'John Doe']); $client = Client::create(['name' => 'Admins']); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index b56cc639c..a8fac2f1f 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -1,4 +1,5 @@ assertTrue(Schema::hasCollection('newcollection')); $this->assertTrue(Schema::hasTable('newcollection')); } - public function testCreateWithCallback() + public function testCreateWithCallback(): void { $instance = $this; @@ -26,21 +27,21 @@ public function testCreateWithCallback() $this->assertTrue(Schema::hasCollection('newcollection')); } - public function testCreateWithOptions() + public function testCreateWithOptions(): void { Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]); $this->assertTrue(Schema::hasCollection('newcollection_two')); $this->assertTrue(Schema::hasTable('newcollection_two')); } - public function testDrop() + public function testDrop(): void { Schema::create('newcollection'); Schema::drop('newcollection'); $this->assertFalse(Schema::hasCollection('newcollection')); } - public function testBluePrint() + public function testBluePrint(): void { $instance = $this; @@ -53,7 +54,7 @@ public function testBluePrint() }); } - public function testIndex() + public function testIndex(): void { Schema::collection('newcollection', function ($collection) { $collection->index('mykey1'); @@ -77,7 +78,7 @@ public function testIndex() $this->assertEquals(1, $index['key']['mykey3']); } - public function testPrimary() + public function testPrimary(): void { Schema::collection('newcollection', function ($collection) { $collection->string('mykey', 100)->primary(); @@ -87,7 +88,7 @@ public function testPrimary() $this->assertEquals(1, $index['unique']); } - public function testUnique() + public function testUnique(): void { Schema::collection('newcollection', function ($collection) { $collection->unique('uniquekey'); @@ -97,7 +98,7 @@ public function testUnique() $this->assertEquals(1, $index['unique']); } - public function testDropIndex() + public function testDropIndex(): void { Schema::collection('newcollection', function ($collection) { $collection->unique('uniquekey'); @@ -144,7 +145,7 @@ public function testDropIndex() $this->assertFalse($index); } - public function testBackground() + public function testBackground(): void { Schema::collection('newcollection', function ($collection) { $collection->background('backgroundkey'); @@ -154,7 +155,7 @@ public function testBackground() $this->assertEquals(1, $index['background']); } - public function testSparse() + public function testSparse(): void { Schema::collection('newcollection', function ($collection) { $collection->sparse('sparsekey'); @@ -164,7 +165,7 @@ public function testSparse() $this->assertEquals(1, $index['sparse']); } - public function testExpire() + public function testExpire(): void { Schema::collection('newcollection', function ($collection) { $collection->expire('expirekey', 60); @@ -174,7 +175,7 @@ public function testExpire() $this->assertEquals(60, $index['expireAfterSeconds']); } - public function testSoftDeletes() + public function testSoftDeletes(): void { Schema::collection('newcollection', function ($collection) { $collection->softDeletes(); @@ -188,7 +189,7 @@ public function testSoftDeletes() $this->assertEquals(1, $index['key']['email']); } - public function testFluent() + public function testFluent(): void { Schema::collection('newcollection', function ($collection) { $collection->string('email')->index(); @@ -203,7 +204,7 @@ public function testFluent() $this->assertEquals(1, $index['key']['token']); } - public function testGeospatial() + public function testGeospatial(): void { Schema::collection('newcollection', function ($collection) { $collection->geospatial('point'); @@ -221,7 +222,7 @@ public function testGeospatial() $this->assertEquals('2dsphere', $index['key']['continent']); } - public function testDummies() + public function testDummies(): void { Schema::collection('newcollection', function ($collection) { $collection->boolean('activated')->default(0); @@ -229,7 +230,7 @@ public function testDummies() }); } - public function testSparseUnique() + public function testSparseUnique(): void { Schema::collection('newcollection', function ($collection) { $collection->sparse_and_unique('sparseuniquekey'); @@ -240,7 +241,7 @@ public function testSparseUnique() $this->assertEquals(1, $index['unique']); } - protected function getIndex($collection, $name) + protected function getIndex(string $collection, string $name) { $collection = DB::getCollection($collection); diff --git a/tests/SeederTest.php b/tests/SeederTest.php index 61143e330..d78117799 100644 --- a/tests/SeederTest.php +++ b/tests/SeederTest.php @@ -1,4 +1,5 @@ run(); @@ -16,7 +17,7 @@ public function testSeed() $this->assertTrue($user->seed); } - public function testArtisan() + public function testArtisan(): void { Artisan::call('db:seed'); diff --git a/tests/TestCase.php b/tests/TestCase.php index f4b26be2d..5f9ec7a89 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,4 +1,7 @@ 'John Doe'], @@ -42,7 +43,7 @@ public function testUnique() $this->assertFalse($validator->fails()); } - public function testExists() + public function testExists(): void { $validator = Validator::make( ['name' => 'John Doe'], diff --git a/tests/config/database.php b/tests/config/database.php index 9c22bb05a..a210595fa 100644 --- a/tests/config/database.php +++ b/tests/config/database.php @@ -25,7 +25,7 @@ 'host' => env('MYSQL_HOST', 'mysql'), 'database' => env('MYSQL_DATABASE', 'unittest'), 'username' => env('MYSQL_USERNAME', 'root'), - 'password' => '', + 'password' => env('MYSQL_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', diff --git a/tests/models/Address.php b/tests/models/Address.php index f2f1278e1..9d094cfcd 100644 --- a/tests/models/Address.php +++ b/tests/models/Address.php @@ -1,13 +1,15 @@ embedsMany('Address'); } diff --git a/tests/models/Book.php b/tests/models/Book.php index 1cf8d22cb..27243ebcb 100644 --- a/tests/models/Book.php +++ b/tests/models/Book.php @@ -1,7 +1,16 @@ belongsTo('User', 'author_id'); } - public function mysqlAuthor() + public function mysqlAuthor(): BelongsTo { return $this->belongsTo('MysqlUser', 'author_id'); } diff --git a/tests/models/Client.php b/tests/models/Client.php index b8309deef..dc023e00a 100644 --- a/tests/models/Client.php +++ b/tests/models/Client.php @@ -1,5 +1,9 @@ belongsToMany('User'); } - public function photo() + public function photo(): MorphOne { return $this->morphOne('Photo', 'imageable'); } - public function addresses() + public function addresses(): HasMany { return $this->hasMany('Address', 'data.client_id', 'data.client_id'); } diff --git a/tests/models/Group.php b/tests/models/Group.php index 494836ad9..369f673e8 100644 --- a/tests/models/Group.php +++ b/tests/models/Group.php @@ -1,5 +1,7 @@ belongsToMany('User', 'users', 'groups', 'users', '_id', '_id', 'users'); } diff --git a/tests/models/Item.php b/tests/models/Item.php index ac52226db..1bdc4189e 100644 --- a/tests/models/Item.php +++ b/tests/models/Item.php @@ -1,19 +1,27 @@ belongsTo('User'); } - public function scopeSharp($query) + public function scopeSharp(Builder $query) { return $query->where('type', 'sharp'); } diff --git a/tests/models/Location.php b/tests/models/Location.php index aa5f36a57..3d44d5ea5 100644 --- a/tests/models/Location.php +++ b/tests/models/Location.php @@ -1,4 +1,5 @@ belongsTo('User', 'author_id'); } @@ -20,12 +23,13 @@ public function author() /** * Check if we need to run the schema. */ - public static function executeSchema() + public static function executeSchema(): void { + /** @var \Illuminate\Database\Schema\MySqlBuilder $schema */ $schema = Schema::connection('mysql'); if (!$schema->hasTable('books')) { - Schema::connection('mysql')->create('books', function ($table) { + Schema::connection('mysql')->create('books', function (Blueprint $table) { $table->string('title'); $table->string('author_id')->nullable(); $table->integer('mysql_user_id')->unsigned()->nullable(); diff --git a/tests/models/MysqlRole.php b/tests/models/MysqlRole.php index e7db21d60..c721ad8c0 100644 --- a/tests/models/MysqlRole.php +++ b/tests/models/MysqlRole.php @@ -1,5 +1,8 @@ belongsTo('User'); } - public function mysqlUser() + public function mysqlUser(): BelongsTo { return $this->belongsTo('MysqlUser'); } @@ -26,10 +29,11 @@ public function mysqlUser() */ public static function executeSchema() { + /** @var \Illuminate\Database\Schema\MySqlBuilder $schema */ $schema = Schema::connection('mysql'); if (!$schema->hasTable('roles')) { - Schema::connection('mysql')->create('roles', function ($table) { + Schema::connection('mysql')->create('roles', function (Blueprint $table) { $table->string('type'); $table->string('user_id'); $table->timestamps(); diff --git a/tests/models/MysqlUser.php b/tests/models/MysqlUser.php index ca15c53ff..67b1052ee 100644 --- a/tests/models/MysqlUser.php +++ b/tests/models/MysqlUser.php @@ -1,5 +1,9 @@ hasMany('Book', 'author_id'); } - public function role() + public function role(): HasOne { return $this->hasOne('Role'); } - public function mysqlBooks() + public function mysqlBooks(): HasMany { return $this->hasMany(MysqlBook::class); } @@ -29,12 +33,13 @@ public function mysqlBooks() /** * Check if we need to run the schema. */ - public static function executeSchema() + public static function executeSchema(): void { + /** @var \Illuminate\Database\Schema\MySqlBuilder $schema */ $schema = Schema::connection('mysql'); if (!$schema->hasTable('users')) { - Schema::connection('mysql')->create('users', function ($table) { + Schema::connection('mysql')->create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); diff --git a/tests/models/Photo.php b/tests/models/Photo.php index 81a5cc2de..beff63825 100644 --- a/tests/models/Photo.php +++ b/tests/models/Photo.php @@ -1,5 +1,7 @@ morphTo(); } diff --git a/tests/models/Role.php b/tests/models/Role.php index a59ce7e02..1e1dc9eb6 100644 --- a/tests/models/Role.php +++ b/tests/models/Role.php @@ -1,5 +1,7 @@ belongsTo('User'); } - public function mysqlUser() + public function mysqlUser(): BelongsTo { return $this->belongsTo('MysqlUser'); } diff --git a/tests/models/Scoped.php b/tests/models/Scoped.php index 77a55bd55..444549916 100644 --- a/tests/models/Scoped.php +++ b/tests/models/Scoped.php @@ -1,4 +1,5 @@ Date: Fri, 6 Sep 2019 17:37:04 +0200 Subject: [PATCH 38/47] Add hasIndex and dropIndexIfExists methods --- src/Jenssegers/Mongodb/Schema/Blueprint.php | 77 +++++++++++---------- tests/SchemaTest.php | 72 +++++++++++++++++++ 2 files changed, 114 insertions(+), 35 deletions(-) diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 551aac1ee..682b7e761 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -99,6 +99,48 @@ public function dropIndexIfExists($indexOrColumns = null) return $this; } + /** + * Check whether the given index exists. + * + * @param string|array $indexOrColumns + * @return bool + */ + public function hasIndex($indexOrColumns = null) + { + $indexOrColumns = $this->transformColumns($indexOrColumns); + foreach ($this->collection->listIndexes() as $index) { + if (is_array($indexOrColumns) && in_array($index->getName(), $indexOrColumns)) { + return true; + } + + if (is_string($indexOrColumns) && $index->getName() == $indexOrColumns) { + return true; + } + } + return false; + } + + /** + * @param string|array $indexOrColumns + * @return string + */ + protected function transformColumns($indexOrColumns) + { + if (is_array($indexOrColumns)) { + $indexOrColumns = $this->fluent($indexOrColumns); + + // Transform the columns to the index name. + $transform = []; + + foreach ($indexOrColumns as $column) { + $transform[$column] = $column . '_1'; + } + + $indexOrColumns = implode('_', $transform); + } + return $indexOrColumns; + } + /** * @inheritdoc */ @@ -238,41 +280,6 @@ public function sparse_and_unique($columns = null, $options = []) return $this; } - /** - * Check whether the given index exists. - * - * @param string|array $indexOrColumns - * @return bool - */ - public function hasIndex($indexOrColumns = null) - { - $indexOrColumns = $this->transformColumns($indexOrColumns); - foreach ($this->collection->listIndexes() as $index) { - if ($index->getName() == $indexOrColumns) { - return true; - } - } - return false; - } - - /** - * @param string|array $indexOrColumns - * @return string|array - */ - private function transformColumns($indexOrColumns) - { - if (is_array($indexOrColumns)) { - $indexOrColumns = $this->fluent($indexOrColumns); - // Transform the columns to the index name. - $transform = []; - foreach ($indexOrColumns as $column) { - $transform[$column] = $column . '_1'; - } - $indexOrColumns = join('_', $transform); - } - return $indexOrColumns; - } - /** * Allow fluent columns. * diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index b56cc639c..006654bf6 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -1,5 +1,7 @@ assertFalse($index); } + public function testDropIndexIfExists() + { + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->unique('uniquekey'); + $collection->dropIndexIfExists('uniquekey_1'); + }); + + $index = $this->getIndex('newcollection', 'uniquekey'); + $this->assertEquals(null, $index); + + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->unique('uniquekey'); + $collection->dropIndexIfExists(['uniquekey']); + }); + + $index = $this->getIndex('newcollection', 'uniquekey'); + $this->assertEquals(null, $index); + + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->index(['field_a', 'field_b']); + }); + + $index = $this->getIndex('newcollection', 'field_a_1_field_b_1'); + $this->assertNotNull($index); + + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->dropIndexIfExists(['field_a', 'field_b']); + }); + + $index = $this->getIndex('newcollection', 'field_a_1_field_b_1'); + $this->assertFalse($index); + + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->index(['field_a', 'field_b'], 'custom_index_name'); + }); + + $index = $this->getIndex('newcollection', 'custom_index_name'); + $this->assertNotNull($index); + + Schema::collection('newcollection', function (Blueprint $collection) { + $collection->dropIndexIfExists('custom_index_name'); + }); + + $index = $this->getIndex('newcollection', 'custom_index_name'); + $this->assertFalse($index); + } + + public function testHasIndex() + { + $instance = $this; + + Schema::collection('newcollection', function (Blueprint $collection) use ($instance) { + $collection->index('myhaskey1'); + $instance->assertTrue($collection->hasIndex('myhaskey1_1')); + $instance->assertFalse($collection->hasIndex('myhaskey1')); + }); + + Schema::collection('newcollection', function (Blueprint $collection) use ($instance) { + $collection->index('myhaskey2'); + $instance->assertTrue($collection->hasIndex(['myhaskey2'])); + $instance->assertFalse($collection->hasIndex(['myhaskey2_1'])); + }); + + Schema::collection('newcollection', function (Blueprint $collection) use ($instance) { + $collection->index(['field_a', 'field_b']); + $instance->assertTrue($collection->hasIndex(['field_a_1_field_b'])); + $instance->assertFalse($collection->hasIndex(['field_a_1_field_b_1'])); + }); + } + public function testBackground() { Schema::collection('newcollection', function ($collection) { From 7592967c936d75d4e9d246a6b3936e5cfdb87998 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Sun, 8 Sep 2019 10:06:02 +0200 Subject: [PATCH 39/47] :arrow_up: Support for laravel 6 --- composer.json | 110 ++++++++++++----------- src/Jenssegers/Mongodb/Connection.php | 3 +- src/Jenssegers/Mongodb/Query/Builder.php | 2 +- 3 files changed, 62 insertions(+), 53 deletions(-) diff --git a/composer.json b/composer.json index 36d14bf48..b683abfc8 100644 --- a/composer.json +++ b/composer.json @@ -1,51 +1,59 @@ -{ - "name": "jenssegers/mongodb", - "description": "A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)", - "keywords": ["laravel","eloquent","mongodb","mongo","database","model","moloquent"], - "homepage": "https://github.com/jenssegers/laravel-mongodb", - "authors": [ - { - "name": "Jens Segers", - "homepage": "https://jenssegers.com" - } - ], - "license" : "MIT", - "require": { - "illuminate/support": "^5.8", - "illuminate/container": "^5.8", - "illuminate/database": "^5.8", - "illuminate/events": "^5.8", - "mongodb/mongodb": "^1.4" - }, - "require-dev": { - "phpunit/phpunit": "^6.0|^7.0", - "orchestra/testbench": "^3.1", - "mockery/mockery": "^1.0", - "satooshi/php-coveralls": "^2.0", - "doctrine/dbal": "^2.5" - }, - "autoload": { - "psr-0": { - "Jenssegers\\Mongodb": "src/" - } - }, - "autoload-dev": { - "classmap": [ - "tests/TestCase.php", - "tests/models", - "tests/seeds" - ] - }, - "suggest": { - "jenssegers/mongodb-session": "Add MongoDB session support to Laravel-MongoDB", - "jenssegers/mongodb-sentry": "Add Sentry support to Laravel-MongoDB" - }, - "extra": { - "laravel": { - "providers": [ - "Jenssegers\\Mongodb\\MongodbServiceProvider", - "Jenssegers\\Mongodb\\MongodbQueueServiceProvider" - ] - } - } -} +{ + "name": "jenssegers/mongodb", + "description": "A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)", + "keywords": [ + "laravel", + "eloquent", + "mongodb", + "mongo", + "database", + "model", + "moloquent" + ], + "homepage": "https://github.com/jenssegers/laravel-mongodb", + "authors": [ + { + "name": "Jens Segers", + "homepage": "https://jenssegers.com" + } + ], + "license": "MIT", + "require": { + "illuminate/support": "^5.8|^6.0", + "illuminate/container": "^5.8|^6.0", + "illuminate/database": "^5.8|^6.0", + "illuminate/events": "^5.8|^6.0", + "mongodb/mongodb": "^1.4" + }, + "require-dev": { + "phpunit/phpunit": "^6.0|^7.0|^8.0", + "orchestra/testbench": "^3.1|^4.0", + "mockery/mockery": "^1.0", + "satooshi/php-coveralls": "^2.0", + "doctrine/dbal": "^2.5" + }, + "autoload": { + "psr-0": { + "Jenssegers\\Mongodb": "src/" + } + }, + "autoload-dev": { + "classmap": [ + "tests/TestCase.php", + "tests/models", + "tests/seeds" + ] + }, + "suggest": { + "jenssegers/mongodb-session": "Add MongoDB session support to Laravel-MongoDB", + "jenssegers/mongodb-sentry": "Add Sentry support to Laravel-MongoDB" + }, + "extra": { + "laravel": { + "providers": [ + "Jenssegers\\Mongodb\\MongodbServiceProvider", + "Jenssegers\\Mongodb\\MongodbQueueServiceProvider" + ] + } + } +} diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index b95d922a1..1d46c5f00 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -67,9 +67,10 @@ public function collection($collection) * Begin a fluent query against a database collection. * * @param string $table + * @param string|null $as * @return Query\Builder */ - public function table($table) + public function table($table, $as = null) { return $this->collection($table); } diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 88dfef68a..4ef855fb2 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -690,7 +690,7 @@ public function delete($id = null) /** * @inheritdoc */ - public function from($collection) + public function from($collection, $as = null) { if ($collection) { $this->collection = $this->connection->getCollection($collection); From cf45ec6330884007793a015b6906643f63a06d1c Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Sun, 8 Sep 2019 10:07:12 +0200 Subject: [PATCH 40/47] :recycle: Make tests compatible with latest phpunit version --- .gitignore | 1 + phpunit.xml.dist | 4 +--- tests/ConnectionTest.php | 2 +- tests/EmbeddedRelationsTest.php | 14 +++++++------- tests/HybridRelationsTest.php | 10 +++++----- tests/ModelTest.php | 12 ++++++------ tests/QueryBuilderTest.php | 12 ++++++------ 7 files changed, 27 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 1fe4e30f6..c7e087c4c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ composer.lock *.sublime-workspace *.project .idea/ +.phpunit.result.cache diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e2d82d39d..0a10014c2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,9 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" - verbose="true" -> + stopOnFailure="false"> tests/ diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 777169a06..b636ef2fd 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -47,7 +47,7 @@ public function testCollection() // public function testDynamic() // { // $dbs = DB::connection('mongodb')->listCollections(); - // $this->assertInternalType('array', $dbs); + // $this->assertIsArray($dbs); // } // public function testMultipleConnections() diff --git a/tests/EmbeddedRelationsTest.php b/tests/EmbeddedRelationsTest.php index 93af2180f..81e9e1bd8 100644 --- a/tests/EmbeddedRelationsTest.php +++ b/tests/EmbeddedRelationsTest.php @@ -37,7 +37,7 @@ public function testEmbedsManySave() $this->assertInstanceOf('DateTime', $address->created_at); $this->assertInstanceOf('DateTime', $address->updated_at); $this->assertNotNull($address->_id); - $this->assertInternalType('string', $address->_id); + $this->assertIsString($address->_id); $raw = $address->getAttributes(); $this->assertInstanceOf(\MongoDB\BSON\ObjectID::class, $raw['_id']); @@ -177,7 +177,7 @@ public function testEmbedsManyCreate() $user = User::create([]); $address = $user->addresses()->create(['city' => 'Bruxelles']); $this->assertInstanceOf('Address', $address); - $this->assertInternalType('string', $address->_id); + $this->assertIsString($address->_id); $this->assertEquals(['Bruxelles'], $user->addresses->pluck('city')->all()); $raw = $address->getAttributes(); @@ -188,7 +188,7 @@ public function testEmbedsManyCreate() $user = User::create([]); $address = $user->addresses()->create(['_id' => '', 'city' => 'Bruxelles']); - $this->assertInternalType('string', $address->_id); + $this->assertIsString($address->_id); $raw = $address->getAttributes(); $this->assertInstanceOf(\MongoDB\BSON\ObjectID::class, $raw['_id']); @@ -388,14 +388,14 @@ public function testEmbedsManyEagerLoading() $relations = $user->getRelations(); $this->assertArrayNotHasKey('addresses', $relations); $this->assertArrayHasKey('addresses', $user->toArray()); - $this->assertInternalType('array', $user->toArray()['addresses']); + $this->assertIsArray($user->toArray()['addresses']); $user = User::with('addresses')->get()->first(); $relations = $user->getRelations(); $this->assertArrayHasKey('addresses', $relations); $this->assertEquals(2, $relations['addresses']->count()); $this->assertArrayHasKey('addresses', $user->toArray()); - $this->assertInternalType('array', $user->toArray()['addresses']); + $this->assertIsArray($user->toArray()['addresses']); } public function testEmbedsManyDeleteAll() @@ -467,7 +467,7 @@ public function testEmbedsOne() $this->assertInstanceOf('DateTime', $father->created_at); $this->assertInstanceOf('DateTime', $father->updated_at); $this->assertNotNull($father->_id); - $this->assertInternalType('string', $father->_id); + $this->assertIsString($father->_id); $raw = $father->getAttributes(); $this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']); @@ -543,7 +543,7 @@ public function testEmbedsManyToArray() $array = $user->toArray(); $this->assertArrayHasKey('addresses', $array); - $this->assertInternalType('array', $array['addresses']); + $this->assertIsArray($array['addresses']); } public function testEmbeddedSave() diff --git a/tests/HybridRelationsTest.php b/tests/HybridRelationsTest.php index 832eb6f86..303762ca2 100644 --- a/tests/HybridRelationsTest.php +++ b/tests/HybridRelationsTest.php @@ -28,7 +28,7 @@ public function testMysqlRelations() // Mysql User $user->name = "John Doe"; $user->save(); - $this->assertInternalType('int', $user->id); + $this->assertIsInt($user->id); // SQL has many $book = new Book(['title' => 'Game of Thrones']); @@ -94,8 +94,8 @@ public function testHybridWhereHas() $otherUser->id = 3; $otherUser->save(); // Make sure they are created - $this->assertInternalType('int', $user->id); - $this->assertInternalType('int', $otherUser->id); + $this->assertIsInt($user->id); + $this->assertIsInt($otherUser->id); // Clear to start $user->books()->truncate(); $otherUser->books()->truncate(); @@ -148,8 +148,8 @@ public function testHybridWith() $otherUser->id = 3; $otherUser->save(); // Make sure they are created - $this->assertInternalType('int', $user->id); - $this->assertInternalType('int', $otherUser->id); + $this->assertIsInt($user->id); + $this->assertIsInt($otherUser->id); // Clear to start Book::truncate(); MysqlBook::truncate(); diff --git a/tests/ModelTest.php b/tests/ModelTest.php index e68b00cb0..bbc747942 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -41,7 +41,7 @@ public function testInsert(): void $this->assertEquals(1, User::count()); $this->assertTrue(isset($user->_id)); - $this->assertInternalType('string', $user->_id); + $this->assertIsString($user->_id); $this->assertNotEquals('', (string) $user->_id); $this->assertNotEquals(0, strlen((string) $user->_id)); $this->assertInstanceOf(Carbon::class, $user->created_at); @@ -112,7 +112,7 @@ public function testManualStringId(): void $this->assertEquals('customId', $user->_id); $raw = $user->getAttributes(); - $this->assertInternalType('string', $raw['_id']); + $this->assertIsString($raw['_id']); } public function testManualIntId(): void @@ -128,7 +128,7 @@ public function testManualIntId(): void $this->assertEquals(1, $user->_id); $raw = $user->getAttributes(); - $this->assertInternalType('integer', $raw['_id']); + $this->assertIsInt($raw['_id']); } public function testDelete(): void @@ -349,9 +349,9 @@ public function testToArray(): void $keys = array_keys($array); sort($keys); $this->assertEquals(['_id', 'created_at', 'name', 'type', 'updated_at'], $keys); - $this->assertInternalType('string', $array['created_at']); - $this->assertInternalType('string', $array['updated_at']); - $this->assertInternalType('string', $array['_id']); + $this->assertIsString($array['created_at']); + $this->assertIsString($array['updated_at']); + $this->assertIsString($array['_id']); } public function testUnset(): void diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 4b1321684..1a7a8ffb9 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -87,7 +87,7 @@ public function testInsert() $user = $users[0]; $this->assertEquals('John Doe', $user['name']); - $this->assertInternalType('array', $user['tags']); + $this->assertIsArray($user['tags']); } public function testInsertGetId() @@ -111,7 +111,7 @@ public function testBatchInsert() $users = DB::collection('users')->get(); $this->assertCount(2, $users); - $this->assertInternalType('array', $users[0]['tags']); + $this->assertIsArray($users[0]['tags']); } public function testFind() @@ -247,7 +247,7 @@ public function testPush() DB::collection('users')->where('_id', $id)->push('tags', 'tag1'); $user = DB::collection('users')->find($id); - $this->assertInternalType('array', $user['tags']); + $this->assertIsArray($user['tags']); $this->assertCount(1, $user['tags']); $this->assertEquals('tag1', $user['tags'][0]); @@ -269,7 +269,7 @@ public function testPush() $message = ['from' => 'Jane', 'body' => 'Hi John']; DB::collection('users')->where('_id', $id)->push('messages', $message); $user = DB::collection('users')->find($id); - $this->assertInternalType('array', $user['messages']); + $this->assertIsArray($user['messages']); $this->assertCount(1, $user['messages']); $this->assertEquals($message, $user['messages'][0]); @@ -298,14 +298,14 @@ public function testPull() DB::collection('users')->where('_id', $id)->pull('tags', 'tag3'); $user = DB::collection('users')->find($id); - $this->assertInternalType('array', $user['tags']); + $this->assertIsArray($user['tags']); $this->assertCount(3, $user['tags']); $this->assertEquals('tag4', $user['tags'][2]); DB::collection('users')->where('_id', $id)->pull('messages', $message1); $user = DB::collection('users')->find($id); - $this->assertInternalType('array', $user['messages']); + $this->assertIsArray($user['messages']); $this->assertCount(1, $user['messages']); // Raw From cc00aff8df5cbf52741a93661c5e64777812b7d2 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Sun, 8 Sep 2019 10:18:34 +0200 Subject: [PATCH 41/47] :rotating_light: Linting --- .../Mongodb/Auth/DatabaseTokenRepository.php | 6 +- .../Auth/PasswordResetServiceProvider.php | 1 - src/Jenssegers/Mongodb/Collection.php | 7 +- src/Jenssegers/Mongodb/Connection.php | 44 ++-- src/Jenssegers/Mongodb/Eloquent/Builder.php | 6 +- .../Mongodb/Eloquent/EmbedsRelations.php | 18 +- .../Mongodb/Eloquent/HybridRelations.php | 70 +++--- src/Jenssegers/Mongodb/Eloquent/Model.php | 41 ++-- .../Mongodb/Helpers/QueriesRelationships.php | 63 ++--- src/Jenssegers/Mongodb/Query/Builder.php | 60 ++--- .../Queue/Failed/MongoFailedJobProvider.php | 15 +- .../Mongodb/Queue/MongoConnector.php | 7 +- src/Jenssegers/Mongodb/Queue/MongoJob.php | 1 - src/Jenssegers/Mongodb/Queue/MongoQueue.php | 16 +- .../Mongodb/Relations/BelongsTo.php | 7 +- .../Mongodb/Relations/BelongsToMany.php | 20 +- .../Mongodb/Relations/EmbedsMany.php | 41 ++-- .../Mongodb/Relations/EmbedsOne.php | 19 +- .../Mongodb/Relations/EmbedsOneOrMany.php | 62 ++--- src/Jenssegers/Mongodb/Relations/HasMany.php | 22 +- src/Jenssegers/Mongodb/Relations/HasOne.php | 24 +- src/Jenssegers/Mongodb/Relations/MorphTo.php | 8 +- src/Jenssegers/Mongodb/Schema/Blueprint.php | 32 +-- src/Jenssegers/Mongodb/Schema/Builder.php | 9 +- .../Validation/DatabasePresenceVerifier.php | 22 +- tests/AuthTest.php | 17 +- tests/CollectionTest.php | 4 +- tests/EmbeddedRelationsTest.php | 218 +++++++++++++----- tests/GeospatialTest.php | 44 ++-- tests/HybridRelationsTest.php | 22 +- tests/ModelTest.php | 2 +- tests/QueryBuilderTest.php | 90 +++++--- tests/QueryTest.php | 50 ++-- tests/RelationsTest.php | 36 +-- tests/SchemaTest.php | 8 +- tests/TestCase.php | 16 +- tests/config/database.php | 28 +-- tests/config/queue.php | 6 +- tests/models/Book.php | 1 - tests/models/Item.php | 1 - tests/models/Scoped.php | 2 +- tests/models/Soft.php | 1 - tests/models/User.php | 7 +- 43 files changed, 570 insertions(+), 604 deletions(-) diff --git a/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php b/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php index 515fb60af..a825bbc44 100644 --- a/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php +++ b/src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php @@ -14,7 +14,11 @@ class DatabaseTokenRepository extends BaseDatabaseTokenRepository */ protected function getPayload($email, $token) { - return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => new UTCDateTime(time() * 1000)]; + return [ + 'email' => $email, + 'token' => $this->hasher->make($token), + 'created_at' => new UTCDateTime(time() * 1000), + ]; } /** diff --git a/src/Jenssegers/Mongodb/Auth/PasswordResetServiceProvider.php b/src/Jenssegers/Mongodb/Auth/PasswordResetServiceProvider.php index ba4e32e62..6e678d2ec 100644 --- a/src/Jenssegers/Mongodb/Auth/PasswordResetServiceProvider.php +++ b/src/Jenssegers/Mongodb/Auth/PasswordResetServiceProvider.php @@ -8,7 +8,6 @@ class PasswordResetServiceProvider extends BasePasswordResetServiceProvider { /** * Register the token repository implementation. - * * @return void */ protected function registerTokenRepository() diff --git a/src/Jenssegers/Mongodb/Collection.php b/src/Jenssegers/Mongodb/Collection.php index 7f5c42b9b..4192edd43 100644 --- a/src/Jenssegers/Mongodb/Collection.php +++ b/src/Jenssegers/Mongodb/Collection.php @@ -10,14 +10,12 @@ class Collection { /** * The connection instance. - * * @var Connection */ protected $connection; /** * The MongoCollection instance.. - * * @var MongoCollection */ protected $collection; @@ -34,9 +32,8 @@ public function __construct(Connection $connection, MongoCollection $collection) /** * Handle dynamic method calls. - * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public function __call($method, $parameters) diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 1d46c5f00..7920ec9f6 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -10,22 +10,19 @@ class Connection extends BaseConnection { /** * The MongoDB database handler. - * * @var \MongoDB\Database */ protected $db; /** * The MongoDB connection handler. - * * @var \MongoDB\Client */ protected $connection; /** * Create a new database connection instance. - * - * @param array $config + * @param array $config */ public function __construct(array $config) { @@ -52,8 +49,7 @@ public function __construct(array $config) /** * Begin a fluent query against a database collection. - * - * @param string $collection + * @param string $collection * @return Query\Builder */ public function collection($collection) @@ -65,9 +61,8 @@ public function collection($collection) /** * Begin a fluent query against a database collection. - * - * @param string $table - * @param string|null $as + * @param string $table + * @param string|null $as * @return Query\Builder */ public function table($table, $as = null) @@ -77,8 +72,7 @@ public function table($table, $as = null) /** * Get a MongoDB collection. - * - * @param string $name + * @param string $name * @return Collection */ public function getCollection($name) @@ -96,7 +90,6 @@ public function getSchemaBuilder() /** * Get the MongoDB database object. - * * @return \MongoDB\Database */ public function getMongoDB() @@ -106,7 +99,6 @@ public function getMongoDB() /** * return MongoDB object. - * * @return \MongoDB\Client */ public function getMongoClient() @@ -124,10 +116,9 @@ public function getDatabaseName() /** * Create a new MongoDB connection. - * - * @param string $dsn - * @param array $config - * @param array $options + * @param string $dsn + * @param array $config + * @param array $options * @return \MongoDB\Client */ protected function createConnection($dsn, array $config, array $options) @@ -160,19 +151,17 @@ public function disconnect() /** * Determine if the given configuration array has a dsn string. - * - * @param array $config + * @param array $config * @return bool */ protected function hasDsnString(array $config) { - return isset($config['dsn']) && ! empty($config['dsn']); + return isset($config['dsn']) && !empty($config['dsn']); } /** * Get the DSN string form configuration. - * - * @param array $config + * @param array $config * @return string */ protected function getDsnString(array $config) @@ -182,8 +171,7 @@ protected function getDsnString(array $config) /** * Get the DSN string for a host / port configuration. - * - * @param array $config + * @param array $config * @return string */ protected function getHostDsn(array $config) @@ -206,8 +194,7 @@ protected function getHostDsn(array $config) /** * Create a DSN string from a configuration. - * - * @param array $config + * @param array $config * @return string */ protected function getDsn(array $config) @@ -259,9 +246,8 @@ protected function getDefaultSchemaGrammar() /** * Dynamically pass methods to the connection. - * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public function __call($method, $parameters) diff --git a/src/Jenssegers/Mongodb/Eloquent/Builder.php b/src/Jenssegers/Mongodb/Eloquent/Builder.php index 358be6b50..90b58484f 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Builder.php +++ b/src/Jenssegers/Mongodb/Eloquent/Builder.php @@ -13,7 +13,6 @@ class Builder extends EloquentBuilder /** * The methods that should be returned from query builder. - * * @var array */ protected $passthru = [ @@ -182,13 +181,12 @@ public function raw($expression = null) * TODO Remove if https://github.com/laravel/framework/commit/6484744326531829341e1ff886cc9b628b20d73e * wiil be reverted * Issue in laravel frawework https://github.com/laravel/framework/issues/27791 - * - * @param array $values + * @param array $values * @return array */ protected function addUpdatedAtColumn(array $values) { - if (! $this->model->usesTimestamps() || $this->model->getUpdatedAtColumn() === null) { + if (!$this->model->usesTimestamps() || $this->model->getUpdatedAtColumn() === null) { return $values; } diff --git a/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php b/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php index c073e58a1..caef0e693 100644 --- a/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php +++ b/src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php @@ -10,11 +10,10 @@ trait EmbedsRelations { /** * Define an embedded one-to-many relationship. - * - * @param string $related - * @param string $localKey - * @param string $foreignKey - * @param string $relation + * @param string $related + * @param string $localKey + * @param string $foreignKey + * @param string $relation * @return \Jenssegers\Mongodb\Relations\EmbedsMany */ protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null) @@ -45,11 +44,10 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r /** * Define an embedded one-to-many relationship. - * - * @param string $related - * @param string $localKey - * @param string $foreignKey - * @param string $relation + * @param string $related + * @param string $localKey + * @param string $foreignKey + * @param string $relation * @return \Jenssegers\Mongodb\Relations\EmbedsOne */ protected function embedsOne($related, $localKey = null, $foreignKey = null, $relation = null) diff --git a/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php b/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php index b4af4dfc8..bfe9a2b2c 100644 --- a/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php +++ b/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php @@ -16,10 +16,9 @@ trait HybridRelations { /** * Define a one-to-one relationship. - * - * @param string $related - * @param string $foreignKey - * @param string $localKey + * @param string $related + * @param string $foreignKey + * @param string $localKey * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function hasOne($related, $foreignKey = null, $localKey = null) @@ -40,12 +39,11 @@ public function hasOne($related, $foreignKey = null, $localKey = null) /** * Define a polymorphic one-to-one relationship. - * - * @param string $related - * @param string $name - * @param string $type - * @param string $id - * @param string $localKey + * @param string $related + * @param string $name + * @param string $type + * @param string $id + * @param string $localKey * @return \Illuminate\Database\Eloquent\Relations\MorphOne */ public function morphOne($related, $name, $type = null, $id = null, $localKey = null) @@ -66,10 +64,9 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey = /** * Define a one-to-many relationship. - * - * @param string $related - * @param string $foreignKey - * @param string $localKey + * @param string $related + * @param string $foreignKey + * @param string $localKey * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function hasMany($related, $foreignKey = null, $localKey = null) @@ -90,12 +87,11 @@ public function hasMany($related, $foreignKey = null, $localKey = null) /** * Define a polymorphic one-to-many relationship. - * - * @param string $related - * @param string $name - * @param string $type - * @param string $id - * @param string $localKey + * @param string $related + * @param string $name + * @param string $type + * @param string $id + * @param string $localKey * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ public function morphMany($related, $name, $type = null, $id = null, $localKey = null) @@ -121,11 +117,10 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey = /** * Define an inverse one-to-one or many relationship. - * - * @param string $related - * @param string $foreignKey - * @param string $otherKey - * @param string $relation + * @param string $related + * @param string $foreignKey + * @param string $otherKey + * @param string $relation * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null) @@ -165,11 +160,10 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat /** * Define a polymorphic, inverse one-to-one or many relationship. - * - * @param string $name - * @param string $type - * @param string $id - * @param string $ownerKey + * @param string $name + * @param string $type + * @param string $id + * @param string $ownerKey * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null) @@ -208,14 +202,13 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null /** * Define a many-to-many relationship. - * - * @param string $related - * @param string $collection - * @param string $foreignKey - * @param string $otherKey - * @param string $parentKey - * @param string $relatedKey - * @param string $relation + * @param string $related + * @param string $collection + * @param string $foreignKey + * @param string $otherKey + * @param string $parentKey + * @param string $relatedKey + * @param string $relation * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function belongsToMany( @@ -282,7 +275,6 @@ public function belongsToMany( /** * Get the relationship name of the belongs to many. - * * @return string */ protected function guessBelongsToManyRelation() diff --git a/src/Jenssegers/Mongodb/Eloquent/Model.php b/src/Jenssegers/Mongodb/Eloquent/Model.php index de77b07bc..78e9b1efa 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Model.php +++ b/src/Jenssegers/Mongodb/Eloquent/Model.php @@ -4,6 +4,8 @@ use Carbon\Carbon; use DateTime; +use Illuminate\Contracts\Queue\QueueableCollection; +use Illuminate\Contracts\Queue\QueueableEntity; use Illuminate\Database\Eloquent\Model as BaseModel; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Arr; @@ -12,8 +14,6 @@ use MongoDB\BSON\Binary; use MongoDB\BSON\ObjectID; use MongoDB\BSON\UTCDateTime; -use Illuminate\Contracts\Queue\QueueableEntity; -use Illuminate\Contracts\Queue\QueueableCollection; abstract class Model extends BaseModel { @@ -21,36 +21,31 @@ abstract class Model extends BaseModel /** * The collection associated with the model. - * * @var string */ protected $collection; /** * The primary key for the model. - * * @var string */ protected $primaryKey = '_id'; /** * The primary key type. - * * @var string */ protected $keyType = 'string'; /** * The parent relation instance. - * * @var Relation */ protected $parentRelation; /** * Custom accessor for the model's id. - * - * @param mixed $value + * @param mixed $value * @return mixed */ public function getIdAttribute($value = null) @@ -267,8 +262,7 @@ public function originalIsEquivalent($key, $current) /** * Remove one or more fields. - * - * @param mixed $columns + * @param mixed $columns * @return int */ public function drop($columns) @@ -313,9 +307,8 @@ public function push() /** * Remove one or more values from an array. - * - * @param string $column - * @param mixed $values + * @param string $column + * @param mixed $values * @return mixed */ public function pull($column, $values) @@ -332,10 +325,9 @@ public function pull($column, $values) /** * Append one or more values to the underlying attribute value and sync with original. - * - * @param string $column - * @param array $values - * @param bool $unique + * @param string $column + * @param array $values + * @param bool $unique */ protected function pushAttributeValues($column, array $values, $unique = false) { @@ -357,9 +349,8 @@ protected function pushAttributeValues($column, array $values, $unique = false) /** * Remove one or more values to the underlying attribute value and sync with original. - * - * @param string $column - * @param array $values + * @param string $column + * @param array $values */ protected function pullAttributeValues($column, array $values) { @@ -390,8 +381,7 @@ public function getForeignKey() /** * Set the parent relation. - * - * @param \Illuminate\Database\Eloquent\Relations\Relation $relation + * @param \Illuminate\Database\Eloquent\Relations\Relation $relation */ public function setParentRelation(Relation $relation) { @@ -400,7 +390,6 @@ public function setParentRelation(Relation $relation) /** * Get the parent relation. - * * @return \Illuminate\Database\Eloquent\Relations\Relation */ public function getParentRelation() @@ -436,7 +425,6 @@ protected function removeTableFromKey($key) /** * Get the queueable relationships for the entity. - * * @return array */ public function getQueueableRelations() @@ -450,13 +438,13 @@ public function getQueueableRelations() if ($relation instanceof QueueableCollection) { foreach ($relation->getQueueableRelations() as $collectionValue) { - $relations[] = $key.'.'.$collectionValue; + $relations[] = $key . '.' . $collectionValue; } } if ($relation instanceof QueueableEntity) { foreach ($relation->getQueueableRelations() as $entityKey => $entityValue) { - $relations[] = $key.'.'.$entityValue; + $relations[] = $key . '.' . $entityValue; } } } @@ -466,7 +454,6 @@ public function getQueueableRelations() /** * Get loaded relations for the instance without parent. - * * @return array */ protected function getRelationsWithoutParent() diff --git a/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php b/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php index 766567627..99798ea4f 100644 --- a/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php +++ b/src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php @@ -3,6 +3,8 @@ namespace Jenssegers\Mongodb\Helpers; use Closure; +use Exception; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasOneOrMany; @@ -12,13 +14,12 @@ trait QueriesRelationships { /** * Add a relationship count / exists condition to the query. - * - * @param string $relation - * @param string $operator - * @param int $count - * @param string $boolean - * @param \Closure|null $callback - * @return \Illuminate\Database\Eloquent\Builder|static + * @param string $relation + * @param string $operator + * @param int $count + * @param string $boolean + * @param Closure|null $callback + * @return Builder|static */ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null) { @@ -74,7 +75,7 @@ protected function isAcrossConnections($relation) * @param string $boolean * @param Closure|null $callback * @return mixed - * @throws \Exception + * @throws Exception */ public function addHybridHas($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null) { @@ -97,29 +98,6 @@ public function addHybridHas($relation, $operator = '>=', $count = 1, $boolean = return $this->whereIn($this->getRelatedConstraintKey($relation), $relatedIds, $boolean, $not); } - /** - * Returns key we are constraining this parent model's query with - * @param $relation - * @return string - * @throws \Exception - */ - protected function getRelatedConstraintKey($relation) - { - if ($relation instanceof HasOneOrMany) { - return $this->model->getKeyName(); - } - - if ($relation instanceof BelongsTo) { - return $relation->getForeignKeyName(); - } - - if ($relation instanceof BelongsToMany && ! $this->isAcrossConnections($relation)) { - return $this->model->getKeyName(); - } - - throw new \Exception(class_basename($relation) . ' is not supported for hybrid query constraints.'); - } - /** * @param $relation * @return string @@ -166,4 +144,27 @@ protected function getConstrainedRelatedIds($relations, $operator, $count) // All related ids. return array_keys($relationCount); } + + /** + * Returns key we are constraining this parent model's query with + * @param $relation + * @return string + * @throws Exception + */ + protected function getRelatedConstraintKey($relation) + { + if ($relation instanceof HasOneOrMany) { + return $this->model->getKeyName(); + } + + if ($relation instanceof BelongsTo) { + return $relation->getForeignKeyName(); + } + + if ($relation instanceof BelongsToMany && !$this->isAcrossConnections($relation)) { + return $this->model->getKeyName(); + } + + throw new Exception(class_basename($relation) . ' is not supported for hybrid query constraints.'); + } } diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 4ef855fb2..c706b24a4 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -20,49 +20,42 @@ class Builder extends BaseBuilder { /** * The database collection. - * * @var MongoCollection */ protected $collection; /** * The column projections. - * * @var array */ public $projections; /** * The cursor timeout value. - * * @var int */ public $timeout; /** * The cursor hint value. - * * @var int */ public $hint; /** * Custom options to add to the query. - * * @var array */ public $options = []; /** * Indicate if we are executing a pagination query. - * * @var bool */ public $paginating = false; /** * All of the available clause operators. - * * @var array */ public $operators = [ @@ -110,7 +103,6 @@ class Builder extends BaseBuilder /** * Operator conversion. - * * @var array */ protected $conversion = [ @@ -125,7 +117,6 @@ class Builder extends BaseBuilder /** * Check if we need to return Collections instead of plain arrays (laravel >= 5.3 ) - * * @var boolean */ protected $useCollections; @@ -143,7 +134,6 @@ public function __construct(Connection $connection, Processor $processor) /** * Returns true if Laravel or Lumen >= 5.3 - * * @return bool */ protected function shouldUseCollections() @@ -159,8 +149,7 @@ protected function shouldUseCollections() /** * Set the projections. - * - * @param array $columns + * @param array $columns * @return $this */ public function project($columns) @@ -172,8 +161,7 @@ public function project($columns) /** * Set the cursor timeout in seconds. - * - * @param int $seconds + * @param int $seconds * @return $this */ public function timeout($seconds) @@ -185,8 +173,7 @@ public function timeout($seconds) /** * Set the cursor hint. - * - * @param mixed $index + * @param mixed $index * @return $this */ public function hint($index) @@ -224,8 +211,7 @@ public function get($columns = []) /** * Execute the query as a fresh "select" statement. - * - * @param array $columns + * @param array $columns * @return array|static[]|Collection */ public function getFresh($columns = []) @@ -412,7 +398,6 @@ public function getFresh($columns = []) /** * Generate the unique cache key for the current query. - * * @return string */ public function generateCacheKey() @@ -506,11 +491,10 @@ public function orderBy($column, $direction = 'asc') /** * Add a "where all" clause to the query. - * - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not + * @param string $column + * @param array $values + * @param string $boolean + * @param bool $not * @return $this */ public function whereAll($column, array $values, $boolean = 'and', $not = false) @@ -711,11 +695,10 @@ public function truncate() /** * Get an array with the values of a given column. - * - * @deprecated - * @param string $column - * @param string $key + * @param string $column + * @param string $key * @return array + * @deprecated */ public function lists($column, $key = null) { @@ -743,7 +726,6 @@ public function raw($expression = null) /** * Append one or more values to an array. - * * @param mixed $column * @param mixed $value * @param bool $unique @@ -770,9 +752,8 @@ public function push($column, $value = null, $unique = false) /** * Remove one or more values from an array. - * - * @param mixed $column - * @param mixed $value + * @param mixed $column + * @param mixed $value * @return int */ public function pull($column, $value = null) @@ -794,8 +775,7 @@ public function pull($column, $value = null) /** * Remove one or more fields. - * - * @param mixed $columns + * @param mixed $columns * @return int */ public function drop($columns) @@ -825,9 +805,8 @@ public function newQuery() /** * Perform an update query. - * - * @param array $query - * @param array $options + * @param array $query + * @param array $options * @return int */ protected function performUpdate($query, array $options = []) @@ -848,8 +827,7 @@ protected function performUpdate($query, array $options = []) /** * Convert a key to ObjectID if needed. - * - * @param mixed $id + * @param mixed $id * @return mixed */ public function convertKey($id) @@ -886,7 +864,6 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' /** * Compile the where array. - * * @return array */ protected function compileWheres() @@ -1146,8 +1123,7 @@ protected function compileWhereRaw(array $where) /** * Set custom options for the query. - * - * @param array $options + * @param array $options * @return $this */ public function options(array $options) diff --git a/src/Jenssegers/Mongodb/Queue/Failed/MongoFailedJobProvider.php b/src/Jenssegers/Mongodb/Queue/Failed/MongoFailedJobProvider.php index 5548dd86f..a02639f88 100644 --- a/src/Jenssegers/Mongodb/Queue/Failed/MongoFailedJobProvider.php +++ b/src/Jenssegers/Mongodb/Queue/Failed/MongoFailedJobProvider.php @@ -9,11 +9,9 @@ class MongoFailedJobProvider extends DatabaseFailedJobProvider { /** * Log a failed job into storage. - * - * @param string $connection - * @param string $queue - * @param string $payload - * + * @param string $connection + * @param string $queue + * @param string $payload * @return void */ public function log($connection, $queue, $payload, $exception) @@ -25,7 +23,6 @@ public function log($connection, $queue, $payload, $exception) /** * Get a list of all of the failed jobs. - * * @return object[] */ public function all() @@ -42,8 +39,7 @@ public function all() /** * Get a single failed job. - * - * @param mixed $id + * @param mixed $id * @return object */ public function find($id) @@ -57,8 +53,7 @@ public function find($id) /** * Delete a single failed job from storage. - * - * @param mixed $id + * @param mixed $id * @return bool */ public function forget($id) diff --git a/src/Jenssegers/Mongodb/Queue/MongoConnector.php b/src/Jenssegers/Mongodb/Queue/MongoConnector.php index 839ac027e..91cea8c35 100644 --- a/src/Jenssegers/Mongodb/Queue/MongoConnector.php +++ b/src/Jenssegers/Mongodb/Queue/MongoConnector.php @@ -10,15 +10,13 @@ class MongoConnector implements ConnectorInterface { /** * Database connections. - * * @var \Illuminate\Database\ConnectionResolverInterface */ protected $connections; /** * Create a new connector instance. - * - * @param \Illuminate\Database\ConnectionResolverInterface $connections + * @param \Illuminate\Database\ConnectionResolverInterface $connections */ public function __construct(ConnectionResolverInterface $connections) { @@ -27,8 +25,7 @@ public function __construct(ConnectionResolverInterface $connections) /** * Establish a queue connection. - * - * @param array $config + * @param array $config * @return \Illuminate\Contracts\Queue\Queue */ public function connect(array $config) diff --git a/src/Jenssegers/Mongodb/Queue/MongoJob.php b/src/Jenssegers/Mongodb/Queue/MongoJob.php index f1a61cf46..336515f09 100644 --- a/src/Jenssegers/Mongodb/Queue/MongoJob.php +++ b/src/Jenssegers/Mongodb/Queue/MongoJob.php @@ -8,7 +8,6 @@ class MongoJob extends DatabaseJob { /** * Indicates if the job has been reserved. - * * @return bool */ public function isReserved() diff --git a/src/Jenssegers/Mongodb/Queue/MongoQueue.php b/src/Jenssegers/Mongodb/Queue/MongoQueue.php index 5f08f7071..44249456a 100644 --- a/src/Jenssegers/Mongodb/Queue/MongoQueue.php +++ b/src/Jenssegers/Mongodb/Queue/MongoQueue.php @@ -11,14 +11,12 @@ class MongoQueue extends DatabaseQueue { /** * The expiration time of a job. - * * @var int|null */ protected $retryAfter = 60; /** * The connection name for the queue. - * * @var string */ protected $connectionName; @@ -52,17 +50,13 @@ public function pop($queue = null) /** * Get the next available job for the queue and mark it as reserved. - * * When using multiple daemon queue listeners to process jobs there * is a possibility that multiple processes can end up reading the * same record before one has flagged it as reserved. - * * This race condition can result in random jobs being run more then * once. To solve this we use findOneAndUpdate to lock the next jobs * record while flagging it as reserved at the same time. - * - * @param string|null $queue - * + * @param string|null $queue * @return \StdClass|null */ protected function getNextAvailableJobAndReserve($queue) @@ -94,8 +88,7 @@ protected function getNextAvailableJobAndReserve($queue) /** * Release the jobs that have been reserved for too long. - * - * @param string $queue + * @param string $queue * @return void */ protected function releaseJobsThatHaveBeenReservedTooLong($queue) @@ -124,9 +117,8 @@ protected function releaseJobsThatHaveBeenReservedTooLong($queue) /** * Release the given job ID from reservation. - * - * @param string $id - * @param int $attempts + * @param string $id + * @param int $attempts * @return void */ protected function releaseJob($id, $attempts) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsTo.php b/src/Jenssegers/Mongodb/Relations/BelongsTo.php index 457e6bc1f..b47e856fa 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsTo.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsTo.php @@ -9,7 +9,6 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo { /** * Get the key for comparing against the parent key in "has" query. - * * @return string */ public function getHasCompareKey() @@ -53,7 +52,6 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, /** * Get the owner key with backwards compatible support. - * * @return string */ public function getOwnerKey() @@ -63,9 +61,8 @@ public function getOwnerKey() /** * Get the name of the "where in" method for eager loading. - * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key * @return string */ protected function whereInMethod(EloquentModel $model, $key) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 0e1b5280b..c57857638 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -5,15 +5,14 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany; use Illuminate\Support\Arr; -use Illuminate\Database\Eloquent\Model as EloquentModel; class BelongsToMany extends EloquentBelongsToMany { /** * Get the key for comparing against the parent key in "has" query. - * * @return string */ public function getHasCompareKey() @@ -39,8 +38,7 @@ protected function hydratePivotRelation(array $models) /** * Set the select clause for the relation query. - * - * @param array $columns + * @param array $columns * @return array */ protected function getSelectColumns(array $columns = ['*']) @@ -68,7 +66,6 @@ public function addConstraints() /** * Set the where clause for the relation query. - * * @return $this */ protected function setWhere() @@ -275,7 +272,6 @@ protected function newPivotQuery() /** * Create a new query builder for the related model. - * * @return \Illuminate\Database\Query\Builder */ public function newRelatedQuery() @@ -285,7 +281,6 @@ public function newRelatedQuery() /** * Get the fully qualified foreign key for the relation. - * * @return string */ public function getForeignKey() @@ -312,10 +307,9 @@ public function getQualifiedRelatedPivotKeyName() /** * Format the sync list so that it is keyed by ID. (Legacy Support) * The original function has been renamed to formatRecordsList since Laravel 5.3 - * - * @deprecated - * @param array $records + * @param array $records * @return array + * @deprecated */ protected function formatSyncList(array $records) { @@ -331,7 +325,6 @@ protected function formatSyncList(array $records) /** * Get the related key with backwards compatible support. - * * @return string */ public function getRelatedKey() @@ -341,9 +334,8 @@ public function getRelatedKey() /** * Get the name of the "where in" method for eager loading. - * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key * @return string */ protected function whereInMethod(EloquentModel $model, $key) diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php index e26658e69..9078b59c7 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php @@ -4,10 +4,10 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; use MongoDB\BSON\ObjectID; -use Illuminate\Database\Eloquent\Model as EloquentModel; class EmbedsMany extends EmbedsOneOrMany { @@ -33,8 +33,7 @@ public function getResults() /** * Save a new model and attach it to the parent model. - * - * @param Model $model + * @param Model $model * @return Model|bool */ public function performInsert(Model $model) @@ -63,8 +62,7 @@ public function performInsert(Model $model) /** * Save an existing model and attach it to the parent model. - * - * @param Model $model + * @param Model $model * @return Model|bool */ public function performUpdate(Model $model) @@ -95,8 +93,7 @@ public function performUpdate(Model $model) /** * Delete an existing model and detach it from the parent model. - * - * @param Model $model + * @param Model $model * @return int */ public function performDelete(Model $model) @@ -122,8 +119,7 @@ public function performDelete(Model $model) /** * Associate the model instance to the given parent, without saving it to the database. - * - * @param Model $model + * @param Model $model * @return Model */ public function associate(Model $model) @@ -137,8 +133,7 @@ public function associate(Model $model) /** * Dissociate the model instance from the given parent, without saving it to the database. - * - * @param mixed $ids + * @param mixed $ids * @return int */ public function dissociate($ids = []) @@ -166,8 +161,7 @@ public function dissociate($ids = []) /** * Destroy the embedded models for the given IDs. - * - * @param mixed $ids + * @param mixed $ids * @return int */ public function destroy($ids = []) @@ -191,7 +185,6 @@ public function destroy($ids = []) /** * Delete all embedded models. - * * @return int */ public function delete() @@ -208,8 +201,7 @@ public function delete() /** * Destroy alias. - * - * @param mixed $ids + * @param mixed $ids * @return int */ public function detach($ids = []) @@ -219,8 +211,7 @@ public function detach($ids = []) /** * Save alias. - * - * @param Model $model + * @param Model $model * @return Model */ public function attach(Model $model) @@ -230,8 +221,7 @@ public function attach(Model $model) /** * Associate a new model instance to the given parent, without saving it to the database. - * - * @param Model $model + * @param Model $model * @return Model */ protected function associateNew($model) @@ -251,8 +241,7 @@ protected function associateNew($model) /** * Associate an existing model instance to the given parent, without saving it to the database. - * - * @param Model $model + * @param Model $model * @return Model */ protected function associateExisting($model) @@ -277,8 +266,7 @@ protected function associateExisting($model) /** * Get a paginator for the "select" statement. - * - * @param int $perPage + * @param int $perPage * @return \Illuminate\Pagination\AbstractPaginator */ public function paginate($perPage = null) @@ -332,9 +320,8 @@ public function __call($method, $parameters) /** * Get the name of the "where in" method for eager loading. - * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key * @return string */ protected function whereInMethod(EloquentModel $model, $key) diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsOne.php b/src/Jenssegers/Mongodb/Relations/EmbedsOne.php index 4640bdeb1..a0e713bc1 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsOne.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsOne.php @@ -3,8 +3,8 @@ namespace Jenssegers\Mongodb\Relations; use Illuminate\Database\Eloquent\Model; -use MongoDB\BSON\ObjectID; use Illuminate\Database\Eloquent\Model as EloquentModel; +use MongoDB\BSON\ObjectID; class EmbedsOne extends EmbedsOneOrMany { @@ -30,8 +30,7 @@ public function getResults() /** * Save a new model and attach it to the parent model. - * - * @param Model $model + * @param Model $model * @return Model|bool */ public function performInsert(Model $model) @@ -59,8 +58,7 @@ public function performInsert(Model $model) /** * Save an existing model and attach it to the parent model. - * - * @param Model $model + * @param Model $model * @return Model|bool */ public function performUpdate(Model $model) @@ -85,7 +83,6 @@ public function performUpdate(Model $model) /** * Delete an existing model and detach it from the parent model. - * * @return int */ public function performDelete() @@ -109,8 +106,7 @@ public function performDelete() /** * Attach the model to its parent. - * - * @param Model $model + * @param Model $model * @return Model */ public function associate(Model $model) @@ -120,7 +116,6 @@ public function associate(Model $model) /** * Detach the model from its parent. - * * @return Model */ public function dissociate() @@ -130,7 +125,6 @@ public function dissociate() /** * Delete all embedded models. - * * @return int */ public function delete() @@ -140,9 +134,8 @@ public function delete() /** * Get the name of the "where in" method for eager loading. - * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key * @return string */ protected function whereInMethod(EloquentModel $model, $key) diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php index f3bc58b8e..5a7f636e7 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php @@ -4,42 +4,38 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Database\Eloquent\Relations\Relation; use Jenssegers\Mongodb\Eloquent\Model; -use Illuminate\Database\Eloquent\Model as EloquentModel; abstract class EmbedsOneOrMany extends Relation { /** * The local key of the parent model. - * * @var string */ protected $localKey; /** * The foreign key of the parent model. - * * @var string */ protected $foreignKey; /** * The "name" of the relationship. - * * @var string */ protected $relation; /** * Create a new embeds many relationship instance. - * - * @param Builder $query - * @param Model $parent - * @param Model $related - * @param string $localKey - * @param string $foreignKey - * @param string $relation + * @param Builder $query + * @param Model $parent + * @param Model $related + * @param string $localKey + * @param string $foreignKey + * @param string $relation */ public function __construct(Builder $query, Model $parent, Model $related, $localKey, $foreignKey, $relation) { @@ -94,9 +90,7 @@ public function match(array $models, Collection $results, $relation) /** * Shorthand to get the results of the relationship. - * - * @param array $columns - * + * @param array $columns * @return Collection */ public function get($columns = ['*']) @@ -106,7 +100,6 @@ public function get($columns = ['*']) /** * Get the number of embedded models. - * * @return int */ public function count() @@ -116,8 +109,7 @@ public function count() /** * Attach a model instance to the parent model. - * - * @param Model $model + * @param Model $model * @return Model|bool */ public function save(Model $model) @@ -129,8 +121,7 @@ public function save(Model $model) /** * Attach a collection of models to the parent instance. - * - * @param Collection|array $models + * @param Collection|array $models * @return Collection|array */ public function saveMany($models) @@ -144,8 +135,7 @@ public function saveMany($models) /** * Create a new instance of the related model. - * - * @param array $attributes + * @param array $attributes * @return Model */ public function create(array $attributes = []) @@ -164,8 +154,7 @@ public function create(array $attributes = []) /** * Create an array of new instances of the related model. - * - * @param array $records + * @param array $records * @return array */ public function createMany(array $records) @@ -181,8 +170,7 @@ public function createMany(array $records) /** * Transform single ID, single Model or array of Models into an array of IDs. - * - * @param mixed $ids + * @param mixed $ids * @return array */ protected function getIdsArrayFrom($ids) @@ -236,8 +224,7 @@ protected function setEmbedded($records) /** * Get the foreign key value for the relation. - * - * @param mixed $id + * @param mixed $id * @return mixed */ protected function getForeignKeyValue($id) @@ -252,8 +239,7 @@ protected function getForeignKeyValue($id) /** * Convert an array of records to a Collection. - * - * @param array $records + * @param array $records * @return Collection */ protected function toCollection(array $records = []) @@ -273,8 +259,7 @@ protected function toCollection(array $records = []) /** * Create a related model instanced. - * - * @param array $attributes + * @param array $attributes * @return Model */ protected function toModel($attributes = []) @@ -302,7 +287,6 @@ protected function toModel($attributes = []) /** * Get the relation instance of the parent. - * * @return Relation */ protected function getParentRelation() @@ -332,7 +316,6 @@ public function getBaseQuery() /** * Check if this relation is nested in another relation. - * * @return bool */ protected function isNested() @@ -342,8 +325,7 @@ protected function isNested() /** * Get the fully qualified local key name. - * - * @param string $glue + * @param string $glue * @return string */ protected function getPathHierarchy($glue = '.') @@ -369,7 +351,6 @@ public function getQualifiedParentKeyName() /** * Get the primary key value of the parent. - * * @return string */ protected function getParentKey() @@ -379,7 +360,6 @@ protected function getParentKey() /** * Return update values - * * @param $array * @param string $prepend * @return array @@ -389,7 +369,7 @@ public static function getUpdateValues($array, $prepend = '') $results = []; foreach ($array as $key => $value) { - $results[$prepend.$key] = $value; + $results[$prepend . $key] = $value; } return $results; @@ -397,7 +377,6 @@ public static function getUpdateValues($array, $prepend = '') /** * Get the foreign key for the relationship. - * * @return string */ public function getQualifiedForeignKeyName() @@ -407,9 +386,8 @@ public function getQualifiedForeignKeyName() /** * Get the name of the "where in" method for eager loading. - * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key * @return string */ protected function whereInMethod(EloquentModel $model, $key) diff --git a/src/Jenssegers/Mongodb/Relations/HasMany.php b/src/Jenssegers/Mongodb/Relations/HasMany.php index 93a25425a..b10426635 100644 --- a/src/Jenssegers/Mongodb/Relations/HasMany.php +++ b/src/Jenssegers/Mongodb/Relations/HasMany.php @@ -3,14 +3,13 @@ namespace Jenssegers\Mongodb\Relations; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Relations\HasMany as EloquentHasMany; use Illuminate\Database\Eloquent\Model as EloquentModel; +use Illuminate\Database\Eloquent\Relations\HasMany as EloquentHasMany; class HasMany extends EloquentHasMany { /** * Get the plain foreign key. - * * @return string */ public function getForeignKeyName() @@ -20,7 +19,6 @@ public function getForeignKeyName() /** * Get the plain foreign key. - * * @return string */ public function getPlainForeignKey() @@ -30,7 +28,6 @@ public function getPlainForeignKey() /** * Get the key for comparing against the parent key in "has" query. - * * @return string */ public function getHasCompareKey() @@ -50,9 +47,8 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, /** * Add the constraints for a relationship count query. - * - * @param Builder $query - * @param Builder $parent + * @param Builder $query + * @param Builder $parent * @return Builder */ public function getRelationCountQuery(Builder $query, Builder $parent) @@ -64,10 +60,9 @@ public function getRelationCountQuery(Builder $query, Builder $parent) /** * Add the constraints for a relationship query. - * - * @param Builder $query - * @param Builder $parent - * @param array|mixed $columns + * @param Builder $query + * @param Builder $parent + * @param array|mixed $columns * @return Builder */ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*']) @@ -81,9 +76,8 @@ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['* /** * Get the name of the "where in" method for eager loading. - * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key * @return string */ protected function whereInMethod(EloquentModel $model, $key) diff --git a/src/Jenssegers/Mongodb/Relations/HasOne.php b/src/Jenssegers/Mongodb/Relations/HasOne.php index a8d65dba9..91741c297 100644 --- a/src/Jenssegers/Mongodb/Relations/HasOne.php +++ b/src/Jenssegers/Mongodb/Relations/HasOne.php @@ -3,14 +3,13 @@ namespace Jenssegers\Mongodb\Relations; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Relations\HasOne as EloquentHasOne; use Illuminate\Database\Eloquent\Model as EloquentModel; +use Illuminate\Database\Eloquent\Relations\HasOne as EloquentHasOne; class HasOne extends EloquentHasOne { /** * Get the key for comparing against the parent key in "has" query. - * * @return string */ public function getForeignKeyName() @@ -20,7 +19,6 @@ public function getForeignKeyName() /** * Get the key for comparing against the parent key in "has" query. - * * @return string */ public function getHasCompareKey() @@ -30,7 +28,6 @@ public function getHasCompareKey() /** * Get the plain foreign key. - * * @return string */ public function getPlainForeignKey() @@ -50,9 +47,8 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, /** * Add the constraints for a relationship count query. - * - * @param Builder $query - * @param Builder $parent + * @param Builder $query + * @param Builder $parent * @return Builder */ public function getRelationCountQuery(Builder $query, Builder $parent) @@ -64,10 +60,9 @@ public function getRelationCountQuery(Builder $query, Builder $parent) /** * Add the constraints for a relationship query. - * - * @param Builder $query - * @param Builder $parent - * @param array|mixed $columns + * @param Builder $query + * @param Builder $parent + * @param array|mixed $columns * @return Builder */ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*']) @@ -78,12 +73,11 @@ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['* return $query->where($this->getForeignKeyName(), 'exists', true); } - + /** * Get the name of the "where in" method for eager loading. - * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key * @return string */ protected function whereInMethod(EloquentModel $model, $key) diff --git a/src/Jenssegers/Mongodb/Relations/MorphTo.php b/src/Jenssegers/Mongodb/Relations/MorphTo.php index 32f3d3ab6..704c4722c 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphTo.php +++ b/src/Jenssegers/Mongodb/Relations/MorphTo.php @@ -2,8 +2,8 @@ namespace Jenssegers\Mongodb\Relations; -use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo; use Illuminate\Database\Eloquent\Model as EloquentModel; +use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo; class MorphTo extends EloquentMorphTo { @@ -36,7 +36,6 @@ protected function getResultsByType($type) /** * Get the owner key with backwards compatible support. - * * @return string */ public function getOwnerKey() @@ -46,9 +45,8 @@ public function getOwnerKey() /** * Get the name of the "where in" method for eager loading. - * - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $key + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key * @return string */ protected function whereInMethod(EloquentModel $model, $key) diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 4b30c87fc..407e333de 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -8,21 +8,18 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint { /** * The MongoConnection object for this blueprint. - * * @var \Jenssegers\Mongodb\Connection */ protected $connection; /** * The MongoCollection object for this blueprint. - * * @var \Jenssegers\Mongodb\Collection|\MongoDB\Collection */ protected $collection; /** * Fluent columns. - * * @var array */ protected $columns = []; @@ -112,8 +109,7 @@ public function unique($columns = null, $name = null, $algorithm = null, $option /** * Specify a non blocking index for the collection. - * - * @param string|array $columns + * @param string|array $columns * @return Blueprint */ public function background($columns = null) @@ -127,9 +123,8 @@ public function background($columns = null) /** * Specify a sparse index for the collection. - * - * @param string|array $columns - * @param array $options + * @param string|array $columns + * @param array $options * @return Blueprint */ public function sparse($columns = null, $options = []) @@ -145,10 +140,9 @@ public function sparse($columns = null, $options = []) /** * Specify a geospatial index for the collection. - * - * @param string|array $columns - * @param string $index - * @param array $options + * @param string|array $columns + * @param string $index + * @param array $options * @return Blueprint */ public function geospatial($columns = null, $index = '2d', $options = []) @@ -171,9 +165,8 @@ public function geospatial($columns = null, $index = '2d', $options = []) /** * Specify the number of seconds after wich a document should be considered expired based, * on the given single-field index containing a date. - * - * @param string|array $columns - * @param int $seconds + * @param string|array $columns + * @param int $seconds * @return Blueprint */ public function expire($columns, $seconds) @@ -218,9 +211,8 @@ public function addColumn($type, $name, array $parameters = []) /** * Specify a sparse and unique index for the collection. - * - * @param string|array $columns - * @param array $options + * @param string|array $columns + * @param array $options * @return Blueprint */ public function sparse_and_unique($columns = null, $options = []) @@ -237,8 +229,7 @@ public function sparse_and_unique($columns = null, $options = []) /** * Allow fluent columns. - * - * @param string|array $columns + * @param string|array $columns * @return string|array */ protected function fluent($columns = null) @@ -254,7 +245,6 @@ protected function fluent($columns = null) /** * Allows the use of unsupported schema methods. - * * @param $method * @param $args * @return Blueprint diff --git a/src/Jenssegers/Mongodb/Schema/Builder.php b/src/Jenssegers/Mongodb/Schema/Builder.php index 0e352846d..1bca6c02e 100644 --- a/src/Jenssegers/Mongodb/Schema/Builder.php +++ b/src/Jenssegers/Mongodb/Schema/Builder.php @@ -33,8 +33,7 @@ public function hasColumns($table, array $columns) /** * Determine if the given collection exists. - * - * @param string $collection + * @param string $collection * @return bool */ public function hasCollection($collection) @@ -60,9 +59,8 @@ public function hasTable($collection) /** * Modify a collection on the schema. - * - * @param string $collection - * @param Closure $callback + * @param string $collection + * @param Closure $callback * @return bool */ public function collection($collection, Closure $callback) @@ -138,7 +136,6 @@ protected function createBlueprint($collection, Closure $callback = null) /** * Get all of the collections names for the database. - * * @return array */ protected function getAllCollections() diff --git a/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php b/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php index fa3d68854..a7d57a89b 100644 --- a/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php +++ b/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php @@ -6,13 +6,12 @@ class DatabasePresenceVerifier extends \Illuminate\Validation\DatabasePresenceVe { /** * Count the number of objects in a collection having the given value. - * - * @param string $collection - * @param string $column - * @param string $value - * @param int $excludeId - * @param string $idColumn - * @param array $extra + * @param string $collection + * @param string $column + * @param string $value + * @param int $excludeId + * @param string $idColumn + * @param array $extra * @return int */ public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = []) @@ -32,11 +31,10 @@ public function getCount($collection, $column, $value, $excludeId = null, $idCol /** * Count the number of objects in a collection with the given values. - * - * @param string $collection - * @param string $column - * @param array $values - * @param array $extra + * @param string $collection + * @param string $column + * @param array $values + * @param array $extra * @return int */ public function getMultiCount($collection, $column, array $values, array $extra = []) diff --git a/tests/AuthTest.php b/tests/AuthTest.php index e81efed90..2628fa626 100644 --- a/tests/AuthTest.php +++ b/tests/AuthTest.php @@ -2,6 +2,7 @@ use Illuminate\Auth\Passwords\PasswordBroker; use Illuminate\Foundation\Application; +use MongoDB\BSON\UTCDateTime; class AuthTest extends TestCase { @@ -15,8 +16,8 @@ public function tearDown(): void public function testAuthAttempt() { $user = User::create([ - 'name' => 'John Doe', - 'email' => 'john@doe.com', + 'name' => 'John Doe', + 'email' => 'john@doe.com', 'password' => Hash::make('foobar'), ]); @@ -37,8 +38,8 @@ public function testRemindOld() $broker = new PasswordBroker($tokens, $users, $mailer, ''); $user = User::create([ - 'name' => 'John Doe', - 'email' => 'john@doe.com', + 'name' => 'John Doe', + 'email' => 'john@doe.com', 'password' => Hash::make('foobar'), ]); @@ -49,13 +50,13 @@ public function testRemindOld() $reminder = DB::collection('password_resets')->first(); $this->assertEquals('john@doe.com', $reminder['email']); $this->assertNotNull($reminder['token']); - $this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $reminder['created_at']); + $this->assertInstanceOf(UTCDateTime::class, $reminder['created_at']); $credentials = [ - 'email' => 'john@doe.com', - 'password' => 'foobar', + 'email' => 'john@doe.com', + 'password' => 'foobar', 'password_confirmation' => 'foobar', - 'token' => $reminder['token'], + 'token' => $reminder['token'], ]; $response = $broker->reset($credentials, function ($user, $password) { diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index d38687a54..ab0b29b21 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -1,10 +1,10 @@ 'John Doe']); $address = new Address(['city' => 'London']); - $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $address->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); - $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.saving: ' . get_class($address), $address) + ->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.creating: ' . get_class($address), $address) + ->andReturn(true); $events->shouldReceive('dispatch')->once()->with('eloquent.created: ' . get_class($address), $address); $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($address), $address); @@ -34,23 +44,29 @@ public function testEmbedsManySave() $this->assertNotNull($user->addresses); $this->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, $user->addresses); $this->assertEquals(['London'], $user->addresses->pluck('city')->all()); - $this->assertInstanceOf('DateTime', $address->created_at); - $this->assertInstanceOf('DateTime', $address->updated_at); + $this->assertInstanceOf(DateTime::class, $address->created_at); + $this->assertInstanceOf(DateTime::class, $address->updated_at); $this->assertNotNull($address->_id); $this->assertIsString($address->_id); $raw = $address->getAttributes(); - $this->assertInstanceOf(\MongoDB\BSON\ObjectID::class, $raw['_id']); + $this->assertInstanceOf(ObjectId::class, $raw['_id']); $address = $user->addresses()->save(new Address(['city' => 'Paris'])); $user = User::find($user->_id); $this->assertEquals(['London', 'Paris'], $user->addresses->pluck('city')->all()); - $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $address->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); - $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.saving: ' . get_class($address), $address) + ->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.updating: ' . get_class($address), $address) + ->andReturn(true); $events->shouldReceive('dispatch')->once()->with('eloquent.updated: ' . get_class($address), $address); $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($address), $address); @@ -69,9 +85,9 @@ public function testEmbedsManySave() $address = $user->addresses->first(); $this->assertEquals('London', $address->city); - $this->assertInstanceOf('DateTime', $address->created_at); - $this->assertInstanceOf('DateTime', $address->updated_at); - $this->assertInstanceOf('User', $address->user); + $this->assertInstanceOf(DateTime::class, $address->created_at); + $this->assertInstanceOf(DateTime::class, $address->updated_at); + $this->assertInstanceOf(User::class, $address->user); $this->assertEmpty($address->relationsToArray()); // prevent infinite loop $user = User::find($user->_id); @@ -176,12 +192,12 @@ public function testEmbedsManyCreate() { $user = User::create([]); $address = $user->addresses()->create(['city' => 'Bruxelles']); - $this->assertInstanceOf('Address', $address); + $this->assertInstanceOf(Address::class, $address); $this->assertIsString($address->_id); $this->assertEquals(['Bruxelles'], $user->addresses->pluck('city')->all()); $raw = $address->getAttributes(); - $this->assertInstanceOf(\MongoDB\BSON\ObjectID::class, $raw['_id']); + $this->assertInstanceOf(ObjectId::class, $raw['_id']); $freshUser = User::find($user->id); $this->assertEquals(['Bruxelles'], $freshUser->addresses->pluck('city')->all()); @@ -191,14 +207,14 @@ public function testEmbedsManyCreate() $this->assertIsString($address->_id); $raw = $address->getAttributes(); - $this->assertInstanceOf(\MongoDB\BSON\ObjectID::class, $raw['_id']); + $this->assertInstanceOf(ObjectId::class, $raw['_id']); } public function testEmbedsManyCreateMany() { $user = User::create([]); list($bruxelles, $paris) = $user->addresses()->createMany([['city' => 'Bruxelles'], ['city' => 'Paris']]); - $this->assertInstanceOf('Address', $bruxelles); + $this->assertInstanceOf(Address::class, $bruxelles); $this->assertEquals('Bruxelles', $bruxelles->city); $this->assertEquals(['Bruxelles', 'Paris'], $user->addresses->pluck('city')->all()); @@ -209,14 +225,23 @@ public function testEmbedsManyCreateMany() public function testEmbedsManyDestroy() { $user = User::create(['name' => 'John Doe']); - $user->addresses()->saveMany([new Address(['city' => 'London']), new Address(['city' => 'Bristol']), new Address(['city' => 'Bruxelles'])]); + $user->addresses()->saveMany([ + new Address(['city' => 'London']), + new Address(['city' => 'Bristol']), + new Address(['city' => 'Bruxelles']), + ]); $address = $user->addresses->first(); - $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $address->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true); - $events->shouldReceive('dispatch')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address')); + $events->shouldReceive('until') + ->once() + ->with('eloquent.deleting: ' . get_class($address), Mockery::type(Address::class)) + ->andReturn(true); + $events->shouldReceive('dispatch') + ->once() + ->with('eloquent.deleted: ' . get_class($address), Mockery::type(Address::class)); $user->addresses()->destroy($address->_id); $this->assertEquals(['Bristol', 'Bruxelles'], $user->addresses->pluck('city')->all()); @@ -240,7 +265,11 @@ public function testEmbedsManyDestroy() $freshUser = User::find($user->id); $this->assertEquals([], $freshUser->addresses->pluck('city')->all()); - list($london, $bristol, $bruxelles) = $user->addresses()->saveMany([new Address(['city' => 'London']), new Address(['city' => 'Bristol']), new Address(['city' => 'Bruxelles'])]); + list($london, $bristol, $bruxelles) = $user->addresses()->saveMany([ + new Address(['city' => 'London']), + new Address(['city' => 'Bristol']), + new Address(['city' => 'Bruxelles']), + ]); $user->addresses()->destroy([$london, $bruxelles]); $this->assertEquals(['Bristol'], $user->addresses->pluck('city')->all()); } @@ -248,14 +277,23 @@ public function testEmbedsManyDestroy() public function testEmbedsManyDelete() { $user = User::create(['name' => 'John Doe']); - $user->addresses()->saveMany([new Address(['city' => 'London']), new Address(['city' => 'Bristol']), new Address(['city' => 'Bruxelles'])]); + $user->addresses()->saveMany([ + new Address(['city' => 'London']), + new Address(['city' => 'Bristol']), + new Address(['city' => 'Bruxelles']), + ]); $address = $user->addresses->first(); - $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $address->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true); - $events->shouldReceive('dispatch')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address')); + $events->shouldReceive('until') + ->once() + ->with('eloquent.deleting: ' . get_class($address), Mockery::type(Address::class)) + ->andReturn(true); + $events->shouldReceive('dispatch') + ->once() + ->with('eloquent.deleted: ' . get_class($address), Mockery::type(Address::class)); $address->delete(); @@ -301,10 +339,16 @@ public function testEmbedsManyCreatingEventReturnsFalse() $user = User::create(['name' => 'John Doe']); $address = new Address(['city' => 'London']); - $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $address->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); - $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(false); + $events->shouldReceive('until') + ->once() + ->with('eloquent.saving: ' . get_class($address), $address) + ->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.creating: ' . get_class($address), $address) + ->andReturn(false); $this->assertFalse($user->addresses()->save($address)); $address->unsetEventDispatcher(); @@ -316,9 +360,12 @@ public function testEmbedsManySavingEventReturnsFalse() $address = new Address(['city' => 'Paris']); $address->exists = true; - $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $address->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(false); + $events->shouldReceive('until') + ->once() + ->with('eloquent.saving: ' . get_class($address), $address) + ->andReturn(false); $this->assertFalse($user->addresses()->save($address)); $address->unsetEventDispatcher(); @@ -330,10 +377,16 @@ public function testEmbedsManyUpdatingEventReturnsFalse() $address = new Address(['city' => 'New York']); $user->addresses()->save($address); - $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $address->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); - $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(false); + $events->shouldReceive('until') + ->once() + ->with('eloquent.saving: ' . get_class($address), $address) + ->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.updating: ' . get_class($address), $address) + ->andReturn(false); $address->city = 'Warsaw'; @@ -348,9 +401,12 @@ public function testEmbedsManyDeletingEventReturnsFalse() $address = $user->addresses->first(); - $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $address->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($address), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::mustBe($address))->andReturn(false); + $events->shouldReceive('until') + ->once() + ->with('eloquent.deleting: ' . get_class($address), Mockery::mustBe($address)) + ->andReturn(false); $this->assertEquals(0, $user->addresses()->destroy($address)); $this->assertEquals(['New York'], $user->addresses->pluck('city')->all()); @@ -425,22 +481,52 @@ public function testEmbedsManyDeleteAll() public function testEmbedsManyCollectionMethods() { $user = User::create(['name' => 'John Doe']); - $user->addresses()->save(new Address(['city' => 'Paris', 'country' => 'France', 'visited' => 4, 'created_at' => new DateTime('3 days ago')])); - $user->addresses()->save(new Address(['city' => 'Bruges', 'country' => 'Belgium', 'visited' => 7, 'created_at' => new DateTime('5 days ago')])); - $user->addresses()->save(new Address(['city' => 'Brussels', 'country' => 'Belgium', 'visited' => 2, 'created_at' => new DateTime('4 days ago')])); - $user->addresses()->save(new Address(['city' => 'Ghent', 'country' => 'Belgium', 'visited' => 13, 'created_at' => new DateTime('2 days ago')])); + $user->addresses()->save(new Address([ + 'city' => 'Paris', + 'country' => 'France', + 'visited' => 4, + 'created_at' => new DateTime('3 days ago'), + ])); + $user->addresses()->save(new Address([ + 'city' => 'Bruges', + 'country' => 'Belgium', + 'visited' => 7, + 'created_at' => new DateTime('5 days ago'), + ])); + $user->addresses()->save(new Address([ + 'city' => 'Brussels', + 'country' => 'Belgium', + 'visited' => 2, + 'created_at' => new DateTime('4 days ago'), + ])); + $user->addresses()->save(new Address([ + 'city' => 'Ghent', + 'country' => 'Belgium', + 'visited' => 13, + 'created_at' => new DateTime('2 days ago'), + ])); $this->assertEquals(['Paris', 'Bruges', 'Brussels', 'Ghent'], $user->addresses()->pluck('city')->all()); - $this->assertEquals(['Bruges', 'Brussels', 'Ghent', 'Paris'], $user->addresses()->sortBy('city')->pluck('city')->all()); + $this->assertEquals(['Bruges', 'Brussels', 'Ghent', 'Paris'], $user->addresses() + ->sortBy('city') + ->pluck('city') + ->all()); $this->assertEquals([], $user->addresses()->where('city', 'New York')->pluck('city')->all()); - $this->assertEquals(['Bruges', 'Brussels', 'Ghent'], $user->addresses()->where('country', 'Belgium')->pluck('city')->all()); - $this->assertEquals(['Bruges', 'Brussels', 'Ghent'], $user->addresses()->where('country', 'Belgium')->sortBy('city')->pluck('city')->all()); + $this->assertEquals(['Bruges', 'Brussels', 'Ghent'], $user->addresses() + ->where('country', 'Belgium') + ->pluck('city') + ->all()); + $this->assertEquals(['Bruges', 'Brussels', 'Ghent'], $user->addresses() + ->where('country', 'Belgium') + ->sortBy('city') + ->pluck('city') + ->all()); $results = $user->addresses->first(); - $this->assertInstanceOf('Address', $results); + $this->assertInstanceOf(Address::class, $results); $results = $user->addresses()->where('country', 'Belgium'); - $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $results); + $this->assertInstanceOf(Collection::class, $results); $this->assertEquals(3, $results->count()); $results = $user->addresses()->whereIn('visited', [7, 13]); @@ -452,10 +538,16 @@ public function testEmbedsOne() $user = User::create(['name' => 'John Doe']); $father = new User(['name' => 'Mark Doe']); - $father->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $father->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); - $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.saving: ' . get_class($father), $father) + ->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.creating: ' . get_class($father), $father) + ->andReturn(true); $events->shouldReceive('dispatch')->once()->with('eloquent.created: ' . get_class($father), $father); $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($father), $father); @@ -464,18 +556,24 @@ public function testEmbedsOne() $this->assertNotNull($user->father); $this->assertEquals('Mark Doe', $user->father->name); - $this->assertInstanceOf('DateTime', $father->created_at); - $this->assertInstanceOf('DateTime', $father->updated_at); + $this->assertInstanceOf(DateTime::class, $father->created_at); + $this->assertInstanceOf(DateTime::class, $father->updated_at); $this->assertNotNull($father->_id); $this->assertIsString($father->_id); $raw = $father->getAttributes(); - $this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']); + $this->assertInstanceOf(ObjectId::class, $raw['_id']); - $father->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $father->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); - $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($father), $father)->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.saving: ' . get_class($father), $father) + ->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.updating: ' . get_class($father), $father) + ->andReturn(true); $events->shouldReceive('dispatch')->once()->with('eloquent.updated: ' . get_class($father), $father); $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($father), $father); @@ -488,10 +586,16 @@ public function testEmbedsOne() $father = new User(['name' => 'Jim Doe']); - $father->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $father->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); - $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($father), $father)->andReturn(true); - $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.saving: ' . get_class($father), $father) + ->andReturn(true); + $events->shouldReceive('until') + ->once() + ->with('eloquent.creating: ' . get_class($father), $father) + ->andReturn(true); $events->shouldReceive('dispatch')->once()->with('eloquent.created: ' . get_class($father), $father); $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($father), $father); @@ -507,7 +611,7 @@ public function testEmbedsOneAssociate() $user = User::create(['name' => 'John Doe']); $father = new User(['name' => 'Mark Doe']); - $father->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); + $father->setEventDispatcher($events = Mockery::mock(Dispatcher::class)); $events->shouldReceive('dispatch')->with('eloquent.retrieved: ' . get_class($father), Mockery::any()); $events->shouldReceive('until')->times(0)->with('eloquent.saving: ' . get_class($father), $father); diff --git a/tests/GeospatialTest.php b/tests/GeospatialTest.php index f1237e582..51d44abc7 100644 --- a/tests/GeospatialTest.php +++ b/tests/GeospatialTest.php @@ -54,28 +54,30 @@ public function testGeoWithin() $locations = Location::where('location', 'geoWithin', [ '$geometry' => [ 'type' => 'Polygon', - 'coordinates' => [[ - [ - -0.1450383, - 51.5069158, - ], - [ - -0.1367563, - 51.5100913, - ], - [ - -0.1270247, - 51.5013233, - ], - [ - -0.1460866, - 51.4952136, - ], + 'coordinates' => [ [ - -0.1450383, - 51.5069158, + [ + -0.1450383, + 51.5069158, + ], + [ + -0.1367563, + 51.5100913, + ], + [ + -0.1270247, + 51.5013233, + ], + [ + -0.1460866, + 51.4952136, + ], + [ + -0.1450383, + 51.5069158, + ], ], - ]], + ], ], ]); @@ -105,7 +107,7 @@ public function testGeoIntersects() 51.5078646, ], ], - ] + ], ]); $this->assertEquals(1, $locations->count()); diff --git a/tests/HybridRelationsTest.php b/tests/HybridRelationsTest.php index 303762ca2..025a1aa4d 100644 --- a/tests/HybridRelationsTest.php +++ b/tests/HybridRelationsTest.php @@ -1,6 +1,8 @@ assertInstanceOf('MysqlUser', $user); - $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection()); + $this->assertInstanceOf(MysqlUser::class, $user); + $this->assertInstanceOf(MySqlConnection::class, $user->getConnection()); // Mysql User $user->name = "John Doe"; @@ -80,10 +82,10 @@ public function testHybridWhereHas() { $user = new MysqlUser; $otherUser = new MysqlUser; - $this->assertInstanceOf('MysqlUser', $user); - $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection()); - $this->assertInstanceOf('MysqlUser', $otherUser); - $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $otherUser->getConnection()); + $this->assertInstanceOf(MysqlUser::class, $user); + $this->assertInstanceOf(MySqlConnection::class, $user->getConnection()); + $this->assertInstanceOf(MysqlUser::class, $otherUser); + $this->assertInstanceOf(MySqlConnection::class, $otherUser->getConnection()); //MySql User $user->name = "John Doe"; @@ -134,10 +136,10 @@ public function testHybridWith() { $user = new MysqlUser; $otherUser = new MysqlUser; - $this->assertInstanceOf('MysqlUser', $user); - $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection()); - $this->assertInstanceOf('MysqlUser', $otherUser); - $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $otherUser->getConnection()); + $this->assertInstanceOf(MysqlUser::class, $user); + $this->assertInstanceOf(MySqlConnection::class, $user->getConnection()); + $this->assertInstanceOf(MysqlUser::class, $otherUser); + $this->assertInstanceOf(MySqlConnection::class, $otherUser->getConnection()); //MySql User $user->name = "John Doe"; diff --git a/tests/ModelTest.php b/tests/ModelTest.php index bbc747942..d774e5306 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -551,7 +551,7 @@ public function testGetDirtyDates(): void public function testChunkById(): void { - User::create(['name' => 'fork', 'tags' => ['sharp', 'pointy']]); + User::create(['name' => 'fork', 'tags' => ['sharp', 'pointy']]); User::create(['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']]); User::create(['name' => 'spoon', 'tags' => ['round', 'bowl']]); diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 1a7a8ffb9..093436699 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -2,8 +2,12 @@ declare(strict_types=1); use Illuminate\Support\Facades\DB; -use MongoDB\BSON\UTCDateTime; +use Jenssegers\Mongodb\Collection; +use Jenssegers\Mongodb\Query\Builder; +use MongoDB\BSON\ObjectId; use MongoDB\BSON\Regex; +use MongoDB\BSON\UTCDateTime; +use MongoDB\Driver\Cursor; class QueryBuilderTest extends TestCase { @@ -49,7 +53,7 @@ public function testDeleteWithId() public function testCollection() { - $this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', DB::collection('users')); + $this->assertInstanceOf(Builder::class, DB::collection('users')); } public function testGet() @@ -93,7 +97,7 @@ public function testInsert() public function testInsertGetId() { $id = DB::collection('users')->insertGetId(['name' => 'John Doe']); - $this->assertInstanceOf('MongoDB\BSON\ObjectID', $id); + $this->assertInstanceOf(ObjectId::class, $id); } public function testBatchInsert() @@ -179,11 +183,11 @@ public function testSubKey() { DB::collection('users')->insert([ [ - 'name' => 'John Doe', + 'name' => 'John Doe', 'address' => ['country' => 'Belgium', 'city' => 'Ghent'], ], [ - 'name' => 'Jane Doe', + 'name' => 'Jane Doe', 'address' => ['country' => 'France', 'city' => 'Paris'], ], ]); @@ -222,14 +226,14 @@ public function testRaw() return $collection->find(['age' => 20]); }); - $this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor); + $this->assertInstanceOf(Cursor::class, $cursor); $this->assertCount(1, $cursor->toArray()); $collection = DB::collection('users')->raw(); - $this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection); + $this->assertInstanceOf(Collection::class, $collection); $collection = User::raw(); - $this->assertInstanceOf('Jenssegers\Mongodb\Collection', $collection); + $this->assertInstanceOf(Collection::class, $collection); $results = DB::collection('users')->whereRaw(['age' => 20])->get(); $this->assertCount(1, $results); @@ -239,8 +243,8 @@ public function testRaw() public function testPush() { $id = DB::collection('users')->insertGetId([ - 'name' => 'John Doe', - 'tags' => [], + 'name' => 'John Doe', + 'tags' => [], 'messages' => [], ]); @@ -274,12 +278,20 @@ public function testPush() $this->assertEquals($message, $user['messages'][0]); // Raw - DB::collection('users')->where('_id', $id)->push(['tags' => 'tag3', 'messages' => ['from' => 'Mark', 'body' => 'Hi John']]); + DB::collection('users')->where('_id', $id)->push([ + 'tags' => 'tag3', + 'messages' => ['from' => 'Mark', 'body' => 'Hi John'], + ]); $user = DB::collection('users')->find($id); $this->assertCount(4, $user['tags']); $this->assertCount(2, $user['messages']); - DB::collection('users')->where('_id', $id)->push(['messages' => ['date' => new DateTime(), 'body' => 'Hi John']]); + DB::collection('users')->where('_id', $id)->push([ + 'messages' => [ + 'date' => new DateTime(), + 'body' => 'Hi John', + ], + ]); $user = DB::collection('users')->find($id); $this->assertCount(3, $user['messages']); } @@ -290,8 +302,8 @@ public function testPull() $message2 = ['from' => 'Mark', 'body' => 'Hi John']; $id = DB::collection('users')->insertGetId([ - 'name' => 'John Doe', - 'tags' => ['tag1', 'tag2', 'tag3', 'tag4'], + 'name' => 'John Doe', + 'tags' => ['tag1', 'tag2', 'tag3', 'tag4'], 'messages' => [$message1, $message2], ]); @@ -319,7 +331,7 @@ public function testDistinct() { DB::collection('items')->insert([ ['name' => 'knife', 'type' => 'sharp'], - ['name' => 'fork', 'type' => 'sharp'], + ['name' => 'fork', 'type' => 'sharp'], ['name' => 'spoon', 'type' => 'round'], ['name' => 'spoon', 'type' => 'round'], ]); @@ -339,7 +351,7 @@ public function testCustomId() { DB::collection('items')->insert([ ['_id' => 'knife', 'type' => 'sharp', 'amount' => 34], - ['_id' => 'fork', 'type' => 'sharp', 'amount' => 20], + ['_id' => 'fork', 'type' => 'sharp', 'amount' => 20], ['_id' => 'spoon', 'type' => 'round', 'amount' => 3], ]); @@ -362,7 +374,7 @@ public function testTake() { DB::collection('items')->insert([ ['name' => 'knife', 'type' => 'sharp', 'amount' => 34], - ['name' => 'fork', 'type' => 'sharp', 'amount' => 20], + ['name' => 'fork', 'type' => 'sharp', 'amount' => 20], ['name' => 'spoon', 'type' => 'round', 'amount' => 3], ['name' => 'spoon', 'type' => 'round', 'amount' => 14], ]); @@ -376,7 +388,7 @@ public function testSkip() { DB::collection('items')->insert([ ['name' => 'knife', 'type' => 'sharp', 'amount' => 34], - ['name' => 'fork', 'type' => 'sharp', 'amount' => 20], + ['name' => 'fork', 'type' => 'sharp', 'amount' => 20], ['name' => 'spoon', 'type' => 'round', 'amount' => 3], ['name' => 'spoon', 'type' => 'round', 'amount' => 14], ]); @@ -401,7 +413,7 @@ public function testList() { DB::collection('items')->insert([ ['name' => 'knife', 'type' => 'sharp', 'amount' => 34], - ['name' => 'fork', 'type' => 'sharp', 'amount' => 20], + ['name' => 'fork', 'type' => 'sharp', 'amount' => 20], ['name' => 'spoon', 'type' => 'round', 'amount' => 3], ['name' => 'spoon', 'type' => 'round', 'amount' => 14], ]); @@ -424,7 +436,7 @@ public function testAggregate() { DB::collection('items')->insert([ ['name' => 'knife', 'type' => 'sharp', 'amount' => 34], - ['name' => 'fork', 'type' => 'sharp', 'amount' => 20], + ['name' => 'fork', 'type' => 'sharp', 'amount' => 20], ['name' => 'spoon', 'type' => 'round', 'amount' => 3], ['name' => 'spoon', 'type' => 'round', 'amount' => 14], ]); @@ -443,7 +455,7 @@ public function testSubdocumentAggregate() { DB::collection('items')->insert([ ['name' => 'knife', 'amount' => ['hidden' => 10, 'found' => 3]], - ['name' => 'fork', 'amount' => ['hidden' => 35, 'found' => 12]], + ['name' => 'fork', 'amount' => ['hidden' => 35, 'found' => 12]], ['name' => 'spoon', 'amount' => ['hidden' => 14, 'found' => 21]], ['name' => 'spoon', 'amount' => ['hidden' => 6, 'found' => 4]], ]); @@ -459,7 +471,14 @@ public function testSubdocumentArrayAggregate() { DB::collection('items')->insert([ ['name' => 'knife', 'amount' => [['hidden' => 10, 'found' => 3], ['hidden' => 5, 'found' => 2]]], - ['name' => 'fork', 'amount' => [['hidden' => 35, 'found' => 12], ['hidden' => 7, 'found' => 17], ['hidden' => 1, 'found' => 19]]], + [ + 'name' => 'fork', + 'amount' => [ + ['hidden' => 35, 'found' => 12], + ['hidden' => 7, 'found' => 17], + ['hidden' => 1, 'found' => 19], + ], + ], ['name' => 'spoon', 'amount' => [['hidden' => 14, 'found' => 21]]], ['name' => 'teaspoon', 'amount' => []], ]); @@ -474,15 +493,15 @@ public function testSubdocumentArrayAggregate() public function testUpsert() { DB::collection('items')->where('name', 'knife') - ->update( - ['amount' => 1], - ['upsert' => true] - ); + ->update( + ['amount' => 1], + ['upsert' => true] + ); $this->assertEquals(1, DB::collection('items')->count()); Item::where('name', 'spoon') - ->update( + ->update( ['amount' => 1], ['upsert' => true] ); @@ -531,7 +550,9 @@ public function testDates() ['name' => 'Mark Moe', 'birthday' => new UTCDateTime(1000 * strtotime("1983-01-01 00:00:00"))], ]); - $user = DB::collection('users')->where('birthday', new UTCDateTime(1000 * strtotime("1980-01-01 00:00:00")))->first(); + $user = DB::collection('users') + ->where('birthday', new UTCDateTime(1000 * strtotime("1980-01-01 00:00:00"))) + ->first(); $this->assertEquals('John Doe', $user['name']); $user = DB::collection('users')->where('birthday', '=', new DateTime("1980-01-01 00:00:00"))->first(); @@ -578,7 +599,7 @@ public function testOperators() $this->assertCount(0, $results); DB::collection('items')->insert([ - ['name' => 'fork', 'tags' => ['sharp', 'pointy']], + ['name' => 'fork', 'tags' => ['sharp', 'pointy']], ['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']], ['name' => 'spoon', 'tags' => ['round', 'bowl']], ]); @@ -620,14 +641,14 @@ public function testOperators() DB::collection('users')->insert([ [ - 'name' => 'John Doe', + 'name' => 'John Doe', 'addresses' => [ ['city' => 'Ghent'], ['city' => 'Paris'], ], ], [ - 'name' => 'Jane Doe', + 'name' => 'Jane Doe', 'addresses' => [ ['city' => 'Brussels'], ['city' => 'Paris'], @@ -692,7 +713,7 @@ public function testIncrement() public function testProjections() { DB::collection('items')->insert([ - ['name' => 'fork', 'tags' => ['sharp', 'pointy']], + ['name' => 'fork', 'tags' => ['sharp', 'pointy']], ['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']], ['name' => 'spoon', 'tags' => ['round', 'bowl']], ]); @@ -707,11 +728,12 @@ public function testProjections() public function testValue() { DB::collection('books')->insert([ - ['title' => 'Moby-Dick', 'author' => ['first_name' => 'Herman', 'last_name' => 'Melville']] + ['title' => 'Moby-Dick', 'author' => ['first_name' => 'Herman', 'last_name' => 'Melville']], ]); $this->assertEquals('Moby-Dick', DB::collection('books')->value('title')); - $this->assertEquals(['first_name' => 'Herman', 'last_name' => 'Melville'], DB::collection('books')->value('author')); + $this->assertEquals(['first_name' => 'Herman', 'last_name' => 'Melville'], DB::collection('books') + ->value('author')); $this->assertEquals('Herman', DB::collection('books')->value('author.first_name')); $this->assertEquals('Melville', DB::collection('books')->value('author.last_name')); } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 646340ad2..b697555cc 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -131,7 +131,7 @@ public function testIn(): void $this->assertCount(4, $users); $users = User::whereNotNull('age') - ->whereNotIn('age', [33, 35])->get(); + ->whereNotIn('age', [33, 35])->get(); $this->assertCount(3, $users); } @@ -218,7 +218,7 @@ public function testSubQuery(): void { $users = User::where('title', 'admin')->orWhere(function ($query) { $query->where('name', 'Tommy Toe') - ->orWhere('name', 'Error'); + ->orWhere('name', 'Error'); }) ->get(); @@ -226,7 +226,7 @@ public function testSubQuery(): void $users = User::where('title', 'user')->where(function ($query) { $query->where('age', 35) - ->orWhere('name', 'like', '%harry%'); + ->orWhere('name', 'like', '%harry%'); }) ->get(); @@ -234,31 +234,31 @@ public function testSubQuery(): void $users = User::where('age', 35)->orWhere(function ($query) { $query->where('title', 'admin') - ->orWhere('name', 'Error'); + ->orWhere('name', 'Error'); }) ->get(); $this->assertCount(5, $users); $users = User::whereNull('deleted_at') - ->where('title', 'admin') - ->where(function ($query) { - $query->where('age', '>', 15) - ->orWhere('name', 'Harry Hoe'); - }) - ->get(); + ->where('title', 'admin') + ->where(function ($query) { + $query->where('age', '>', 15) + ->orWhere('name', 'Harry Hoe'); + }) + ->get(); $this->assertEquals(3, $users->count()); $users = User::whereNull('deleted_at') - ->where(function ($query) { - $query->where('name', 'Harry Hoe') - ->orWhere(function ($query) { - $query->where('age', '>', 15) - ->where('title', '<>', 'admin'); - }); - }) - ->get(); + ->where(function ($query) { + $query->where('name', 'Harry Hoe') + ->orWhere(function ($query) { + $query->where('age', '>', 15) + ->where('title', '<>', 'admin'); + }); + }) + ->get(); $this->assertEquals(5, $users->count()); } @@ -271,7 +271,7 @@ public function testWhereRaw(): void $this->assertCount(6, $users); $where1 = ['age' => ['$gt' => 30, '$lte' => 35]]; - $where2 = ['age' => ['$gt' => 35, '$lt' => 40]]; + $where2 = ['age' => ['$gt' => 35, '$lt' => 40]]; $users = User::whereRaw($where1)->orWhereRaw($where2)->get(); $this->assertCount(6, $users); @@ -282,18 +282,18 @@ public function testMultipleOr(): void $users = User::where(function ($query) { $query->where('age', 35)->orWhere('age', 33); }) - ->where(function ($query) { - $query->where('name', 'John Doe')->orWhere('name', 'Jane Doe'); - })->get(); + ->where(function ($query) { + $query->where('name', 'John Doe')->orWhere('name', 'Jane Doe'); + })->get(); $this->assertCount(2, $users); $users = User::where(function ($query) { $query->orWhere('age', 35)->orWhere('age', 33); }) - ->where(function ($query) { - $query->orWhere('name', 'John Doe')->orWhere('name', 'Jane Doe'); - })->get(); + ->where(function ($query) { + $query->orWhere('name', 'John Doe')->orWhere('name', 'Jane Doe'); + })->get(); $this->assertCount(2, $users); } diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index decc4f14b..a50329682 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -1,6 +1,8 @@ orderBy('user_id', 'desc')->get(); $user = $items[0]->getRelation('user'); - $this->assertInstanceOf('User', $user); + $this->assertInstanceOf(User::class, $user); $this->assertEquals('John Doe', $user->name); $this->assertCount(1, $items[0]->getRelations()); $this->assertNull($items[3]->getRelation('user')); @@ -108,7 +110,7 @@ public function testWithHashMany(): void $items = $user->getRelation('items'); $this->assertCount(3, $items); - $this->assertInstanceOf('Item', $items[0]); + $this->assertInstanceOf(Item::class, $items[0]); } public function testWithHasOne(): void @@ -120,7 +122,7 @@ public function testWithHasOne(): void $user = User::with('role')->find($user->_id); $role = $user->getRelation('role'); - $this->assertInstanceOf('Role', $role); + $this->assertInstanceOf(Role::class, $role); $this->assertEquals('admin', $role->type); } @@ -134,7 +136,7 @@ public function testEasyRelation(): void $user = User::find($user->_id); $items = $user->items; $this->assertCount(1, $items); - $this->assertInstanceOf('Item', $items[0]); + $this->assertInstanceOf(Item::class, $items[0]); $this->assertEquals($user->_id, $items[0]->user_id); // Has one @@ -144,7 +146,7 @@ public function testEasyRelation(): void $user = User::find($user->_id); $role = $user->role; - $this->assertInstanceOf('Role', $role); + $this->assertInstanceOf(Role::class, $role); $this->assertEquals('admin', $role->type); $this->assertEquals($user->_id, $role->user_id); } @@ -168,18 +170,18 @@ public function testBelongsToMany(): void $clients = $user->getRelation('clients'); $users = $client->getRelation('users'); - $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users); - $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients); - $this->assertInstanceOf('Client', $clients[0]); - $this->assertInstanceOf('User', $users[0]); + $this->assertInstanceOf(Collection::class, $users); + $this->assertInstanceOf(Collection::class, $clients); + $this->assertInstanceOf(Client::class, $clients[0]); + $this->assertInstanceOf(User::class, $users[0]); $this->assertCount(2, $user->clients); $this->assertCount(1, $client->users); // Now create a new user to an existing client $user = $client->users()->create(['name' => 'Jane Doe']); - $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->clients); - $this->assertInstanceOf('Client', $user->clients->first()); + $this->assertInstanceOf(Collection::class, $user->clients); + $this->assertInstanceOf(Client::class, $user->clients->first()); $this->assertCount(1, $user->clients); // Get user and unattached client @@ -187,8 +189,8 @@ public function testBelongsToMany(): void $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first(); // Check the models are what they should be - $this->assertInstanceOf('Client', $client); - $this->assertInstanceOf('User', $user); + $this->assertInstanceOf(Client::class, $client); + $this->assertInstanceOf(User::class, $user); // Assert they are not attached $this->assertNotContains($client->_id, $user->client_ids); @@ -377,11 +379,11 @@ public function testMorph(): void $photos = Photo::with('imageable')->get(); $relations = $photos[0]->getRelations(); $this->assertArrayHasKey('imageable', $relations); - $this->assertInstanceOf('User', $photos[0]->imageable); + $this->assertInstanceOf(User::class, $photos[0]->imageable); $relations = $photos[1]->getRelations(); $this->assertArrayHasKey('imageable', $relations); - $this->assertInstanceOf('Client', $photos[1]->imageable); + $this->assertInstanceOf(Client::class, $photos[1]->imageable); } public function testHasManyHas(): void @@ -461,14 +463,14 @@ public function testNestedKeys(): void $client = Client::create([ 'data' => [ 'client_id' => 35298, - 'name' => 'John Doe', + 'name' => 'John Doe', ], ]); $address = $client->addresses()->create([ 'data' => [ 'address_id' => 1432, - 'city' => 'Paris', + 'city' => 'Paris', ], ]); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index a8fac2f1f..e3ca81976 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -1,6 +1,8 @@ assertInstanceOf('Jenssegers\Mongodb\Schema\Blueprint', $collection); + $instance->assertInstanceOf(Blueprint::class, $collection); }); $this->assertTrue(Schema::hasCollection('newcollection')); @@ -46,11 +48,11 @@ public function testBluePrint(): void $instance = $this; Schema::collection('newcollection', function ($collection) use ($instance) { - $instance->assertInstanceOf('Jenssegers\Mongodb\Schema\Blueprint', $collection); + $instance->assertInstanceOf(Blueprint::class, $collection); }); Schema::table('newcollection', function ($collection) use ($instance) { - $instance->assertInstanceOf('Jenssegers\Mongodb\Schema\Blueprint', $collection); + $instance->assertInstanceOf(Blueprint::class, $collection); }); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 5f9ec7a89..c27fec178 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,9 +7,7 @@ class TestCase extends Orchestra\Testbench\TestCase { /** * Get application providers. - * - * @param \Illuminate\Foundation\Application $app - * + * @param \Illuminate\Foundation\Application $app * @return array */ protected function getApplicationProviders($app) @@ -23,8 +21,7 @@ protected function getApplicationProviders($app) /** * Get package providers. - * - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app * @return array */ protected function getPackageProviders($app) @@ -32,14 +29,13 @@ protected function getPackageProviders($app) return [ Jenssegers\Mongodb\MongodbServiceProvider::class, Jenssegers\Mongodb\Auth\PasswordResetServiceProvider::class, - Jenssegers\Mongodb\Validation\ValidationServiceProvider::class + Jenssegers\Mongodb\Validation\ValidationServiceProvider::class, ]; } /** * Define environment setup. - * - * @param Illuminate\Foundation\Application $app + * @param Illuminate\Foundation\Application $app * @return void */ protected function getEnvironmentSetUp($app) @@ -63,8 +59,8 @@ protected function getEnvironmentSetUp($app) $app['config']->set('queue.default', 'database'); $app['config']->set('queue.connections.database', [ 'driver' => 'mongodb', - 'table' => 'jobs', - 'queue' => 'default', + 'table' => 'jobs', + 'queue' => 'default', 'expire' => 60, ]); } diff --git a/tests/config/database.php b/tests/config/database.php index a210595fa..23f8ca990 100644 --- a/tests/config/database.php +++ b/tests/config/database.php @@ -8,27 +8,27 @@ 'connections' => [ 'mongodb' => [ - 'name' => 'mongodb', - 'driver' => 'mongodb', - 'host' => $mongoHost, - 'database' => env('MONGO_DATABASE', 'unittest'), + 'name' => 'mongodb', + 'driver' => 'mongodb', + 'host' => $mongoHost, + 'database' => env('MONGO_DATABASE', 'unittest'), ], 'dsn_mongodb' => [ - 'driver' => 'mongodb', - 'dsn' => "mongodb://$mongoHost:$mongoPort", - 'database' => env('MONGO_DATABASE', 'unittest'), + 'driver' => 'mongodb', + 'dsn' => "mongodb://$mongoHost:$mongoPort", + 'database' => env('MONGO_DATABASE', 'unittest'), ], 'mysql' => [ - 'driver' => 'mysql', - 'host' => env('MYSQL_HOST', 'mysql'), - 'database' => env('MYSQL_DATABASE', 'unittest'), - 'username' => env('MYSQL_USERNAME', 'root'), - 'password' => env('MYSQL_PASSWORD', ''), - 'charset' => 'utf8', + 'driver' => 'mysql', + 'host' => env('MYSQL_HOST', 'mysql'), + 'database' => env('MYSQL_DATABASE', 'unittest'), + 'username' => env('MYSQL_USERNAME', 'root'), + 'password' => env('MYSQL_PASSWORD', ''), + 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', - 'prefix' => '', + 'prefix' => '', ], ], diff --git a/tests/config/queue.php b/tests/config/queue.php index e8203d90b..20ef36703 100644 --- a/tests/config/queue.php +++ b/tests/config/queue.php @@ -8,8 +8,8 @@ 'database' => [ 'driver' => 'mongodb', - 'table' => 'jobs', - 'queue' => 'default', + 'table' => 'jobs', + 'queue' => 'default', 'expire' => 60, ], @@ -17,7 +17,7 @@ 'failed' => [ 'database' => env('MONGO_DATABASE'), - 'table' => 'failed_jobs', + 'table' => 'failed_jobs', ], ]; diff --git a/tests/models/Book.php b/tests/models/Book.php index 27243ebcb..e37cb7eaf 100644 --- a/tests/models/Book.php +++ b/tests/models/Book.php @@ -6,7 +6,6 @@ /** * Class Book - * * @property string $title * @property string $author * @property array $chapters diff --git a/tests/models/Item.php b/tests/models/Item.php index 1bdc4189e..b06484d25 100644 --- a/tests/models/Item.php +++ b/tests/models/Item.php @@ -7,7 +7,6 @@ /** * Class Item - * * @property \Carbon\Carbon $created_at */ class Item extends Eloquent diff --git a/tests/models/Scoped.php b/tests/models/Scoped.php index 444549916..9f312f943 100644 --- a/tests/models/Scoped.php +++ b/tests/models/Scoped.php @@ -1,8 +1,8 @@ Date: Sun, 8 Sep 2019 10:20:21 +0200 Subject: [PATCH 42/47] :white_check_mark: Replace str_random usage --- tests/QueryBuilderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 093436699..bc30ad66b 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -46,7 +46,7 @@ public function testDeleteWithId() DB::collection('items')->where('user_id', $user_id)->delete($pid); - DB::collection('items')->where('user_id', $user_id)->delete(str_random(32)); + DB::collection('items')->where('user_id', $user_id)->delete(md5('random-id')); $this->assertEquals(2, DB::collection('items')->count()); } From 4ae4a7c6cb9b496d835ffe2b333f6065546fd768 Mon Sep 17 00:00:00 2001 From: jim5359 Date: Wed, 11 Sep 2019 11:30:35 -0700 Subject: [PATCH 43/47] Update src/Jenssegers/Mongodb/Relations/EmbedsMany.php Co-Authored-By: Jens Segers --- src/Jenssegers/Mongodb/Relations/EmbedsMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php index 3c720b73d..85574eeb0 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php @@ -237,7 +237,7 @@ public function attach(Model $model) protected function associateNew($model) { // Create a new key if needed. - if ($model->getKeyName() == '_id' && !$model->getAttribute('_id')) { + if ($model->getKeyName() === '_id' && !$model->getAttribute('_id')) { $model->setAttribute('_id', new ObjectID); } From 9d7d0c71795a2f6833b334e8cc515fb803ef2aac Mon Sep 17 00:00:00 2001 From: Ahmed Sayed Abdelsalam Date: Fri, 13 Sep 2019 03:20:45 +0200 Subject: [PATCH 44/47] fix filtering with operator not like issue --- src/Jenssegers/Mongodb/Query/Builder.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index c706b24a4..9752368e6 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -978,9 +978,13 @@ protected function compileWhereBasic(array $where) { extract($where); - // Replace like with a Regex instance. - if ($operator == 'like') { - $operator = '='; + // Replace like or not like with a Regex instance. + if (in_array($operator, ['like', 'not like'])) { + if (Str::startsWith($operator, 'not')) { + $operator = 'not'; + } else { + $operator = '='; + } // Convert to regular expression. $regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value)); From 74c420272031d15b92479b617c0439ffa2764e0d Mon Sep 17 00:00:00 2001 From: Ahmed Sayed Abdelsalam Date: Fri, 13 Sep 2019 11:23:29 +0200 Subject: [PATCH 45/47] replcae string operation with exact match of (not like) --- src/Jenssegers/Mongodb/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 9752368e6..70b857293 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -980,7 +980,7 @@ protected function compileWhereBasic(array $where) // Replace like or not like with a Regex instance. if (in_array($operator, ['like', 'not like'])) { - if (Str::startsWith($operator, 'not')) { + if ($operator === 'not like') { $operator = 'not'; } else { $operator = '='; From 30098cd3b0c0a3f9f843bb9281ead2c5e07ae926 Mon Sep 17 00:00:00 2001 From: Ahmed Sayed Abdelsalam Date: Fri, 13 Sep 2019 11:25:08 +0200 Subject: [PATCH 46/47] add test to not like oepration fix --- tests/QueryTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/QueryTest.php b/tests/QueryTest.php index b697555cc..ab36d1886 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -71,6 +71,21 @@ public function testLike(): void $this->assertCount(1, $users); } + public function testNotLike(): void + { + $users = User::where('name', 'not like', '%doe')->get(); + $this->assertCount(7, $users); + + $users = User::where('name', 'not like', '%y%')->get(); + $this->assertCount(6, $users); + + $users = User::where('name', 'not LIKE', '%y%')->get(); + $this->assertCount(6, $users); + + $users = User::where('name', 'not like', 't%')->get(); + $this->assertCount(8, $users); + } + public function testSelect(): void { $user = User::where('name', 'John Doe')->select('name')->first(); From 045ebf08c273a4ac0c411afc8ac142238e363691 Mon Sep 17 00:00:00 2001 From: Denisson Leal Date: Wed, 9 Oct 2019 15:00:59 -0300 Subject: [PATCH 47/47] fix regex demo code --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a31c1330e..54db69769 100644 --- a/README.md +++ b/README.md @@ -549,13 +549,13 @@ User::where('name', 'regex', new \MongoDB\BSON\Regex("/.*doe/i"))->get(); **NOTE:** you can also use the Laravel regexp operations. These are a bit more flexible and will automatically convert your regular expression string to a MongoDB\BSON\Regex object. ```php -User::where('name', 'regexp', '/.*doe/i'))->get(); +User::where('name', 'regexp', '/.*doe/i')->get(); ``` And the inverse: ```php -User::where('name', 'not regexp', '/.*doe/i'))->get(); +User::where('name', 'not regexp', '/.*doe/i')->get(); ``` **Type**