Skip to content

Commit ef32087

Browse files
authored
Merge pull request #20 from divine/pr/19
merge
2 parents b3ac78f + c7278e0 commit ef32087

File tree

12 files changed

+156
-11
lines changed

12 files changed

+156
-11
lines changed

.codacy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exclude_paths:
2+
- '.github/**'

.github/ISSUE_TEMPLATE/BUG_REPORT.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
name: "Bug report"
3+
about: 'Report errors or unexpected behavior.'
4+
---
5+
6+
- Laravel-mongodb Version: #.#.#
7+
- PHP Version: #.#.#
8+
- Database Driver & Version:
9+
10+
### Description:
11+
12+
### Steps to reproduce
13+
1.
14+
2.
15+
3.
16+
17+
### Expected behaviour
18+
Tell us what should happen
19+
20+
### Actual behaviour
21+
Tell us what happens instead
22+
23+
<details><summary><b>Logs</b>:</summary>
24+
Insert log.txt here (if necessary)
25+
</details>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea.
4+
title: "[Feature Request] "
5+
6+
---
7+
8+
### Is your feature request related to a problem?
9+
A clear and concise description of what the problem is.
10+
11+
### Describe the solution you'd like
12+
A clear and concise description of what you want to happen.
13+
14+
### Describe alternatives you've considered
15+
A clear and concise description of any alternative solutions or features you've considered.
16+
17+
### Additional context
18+
Add any other context or screenshots about the feature request here.

.github/ISSUE_TEMPLATE/QUESTION.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
name: Question
3+
about: Ask a question.
4+
title: "[Question] "
5+
labels: 'question'
6+
assignees: ''
7+
8+
---

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This package adds functionalities to the Eloquent model and Query builder for Mo
1616
- [Lumen](#lumen)
1717
- [Non-Laravel projects](#non-laravel-projects)
1818
- [Testing](#testing)
19+
- [Database Testing](#database-testing)
1920
- [Configuration](#configuration)
2021
- [Eloquent](#eloquent)
2122
- [Extending the base model](#extending-the-base-model)
@@ -113,6 +114,25 @@ To run the test for this package, run:
113114
docker-compose up
114115
```
115116

117+
Database Testing
118+
-------
119+
120+
To reset the database after each test, add:
121+
122+
```php
123+
use Illuminate\Foundation\Testing\DatabaseMigrations;
124+
```
125+
126+
Also inside each test classes, add:
127+
128+
```php
129+
use DatabaseMigrations;
130+
```
131+
132+
Keep in mind that these traits are not yet supported:
133+
- `use Database Transactions;`
134+
- `use RefreshDatabase;`
135+
116136
Configuration
117137
-------------
118138
You can use MongoDB either as the main database, either as a side database. To do so, add a new `mongodb` connection to `config/database.php`:
@@ -460,7 +480,7 @@ Selects documents where values match a specified regular expression.
460480
```php
461481
use MongoDB\BSON\Regex;
462482

463-
User::where('name', 'regex', new Regex("/.*doe/i"))->get();
483+
User::where('name', 'regex', new Regex('.*doe', 'i'))->get();
464484
```
465485

466486
**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.

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
"illuminate/container": "^5.8|^6.0|^7.0",
2424
"illuminate/database": "^5.8|^6.0|^7.0",
2525
"illuminate/events": "^5.8|^6.0|^7.0",
26-
"mongodb/mongodb": "^1.4",
27-
"cedx/coveralls": "^11.2"
26+
"mongodb/mongodb": "^1.4"
2827
},
2928
"require-dev": {
3029
"phpunit/phpunit": "^6.0|^7.0|^8.0",
3130
"orchestra/testbench": "^3.1|^4.0",
3231
"mockery/mockery": "^1.0",
3332
"doctrine/dbal": "^2.5",
34-
"phpunit/phpcov": "^6.0"
33+
"phpunit/phpcov": "^6.0",
34+
"cedx/coveralls": "^11.2"
3535
},
3636
"autoload": {
3737
"psr-0": {

src/Jenssegers/Mongodb/Connection.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(array $config)
3838
$this->connection = $this->createConnection($dsn, $config, $options);
3939

4040
// Select database
41-
$this->db = $this->connection->selectDatabase($config['database']);
41+
$this->db = $this->connection->selectDatabase($this->getDatabaseDsn($dsn, $config['database']));
4242

4343
$this->useDefaultPostProcessor();
4444

@@ -188,10 +188,21 @@ protected function getHostDsn(array $config)
188188

189189
// Check if we want to authenticate against a specific database.
190190
$auth_database = isset($config['options']) && !empty($config['options']['database']) ? $config['options']['database'] : null;
191-
192191
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
193192
}
194193

194+
/**
195+
* Get database name from DSN string, if there is no database in DSN path - returns back $database argument.
196+
* @param string $dsn
197+
* @param $database
198+
* @return string
199+
*/
200+
protected function getDatabaseDsn($dsn, $database)
201+
{
202+
$dsnDatabase = trim(parse_url($dsn, PHP_URL_PATH), '/');
203+
return trim($dsnDatabase) ? $dsnDatabase : $database;
204+
}
205+
195206
/**
196207
* Create a DSN string from a configuration.
197208
* @param array $config

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,12 @@ public function getFresh($columns = [])
384384
if ($this->limit) {
385385
$options['limit'] = $this->limit;
386386
}
387+
if ($this->hint) {
388+
$options['hint'] = $this->hint;
389+
}
387390
if ($columns) {
388391
$options['projection'] = $columns;
389392
}
390-
// if ($this->hint) $cursor->hint($this->hint);
391393

392394
// Fix for legacy support, converts the results to arrays instead of objects.
393395
$options['typeMap'] = ['root' => 'array', 'document' => 'array'];
@@ -698,7 +700,11 @@ public function from($collection, $as = null)
698700
*/
699701
public function truncate()
700702
{
701-
$result = $this->collection->drop();
703+
$options = [
704+
'typeMap' => ['root' => 'object', 'document' => 'object'],
705+
];
706+
707+
$result = $this->collection->drop($options);
702708

703709
return (1 == (int) $result->ok);
704710
}

src/Jenssegers/Mongodb/Schema/Blueprint.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,18 @@ protected function transformColumns($indexOrColumns)
129129
// Transform the columns to the index name.
130130
$transform = [];
131131

132-
foreach ($indexOrColumns as $column) {
133-
$transform[$column] = $column . '_1';
132+
foreach ($indexOrColumns as $key => $value) {
133+
if (is_int($key)) {
134+
// There is no sorting order, use the default.
135+
$column = $value;
136+
$sorting = '1';
137+
} else {
138+
// This is a column with sorting order e.g 'my_column' => -1.
139+
$column = $key;
140+
$sorting = $value;
141+
}
142+
143+
$transform[$column] = $column . "_" . $sorting;
134144
}
135145

136146
$indexOrColumns = implode('_', $transform);

tests/ModelTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,13 @@ public function testChunkById(): void
572572

573573
$this->assertEquals(3, $count);
574574
}
575+
576+
public function testTruncateModel()
577+
{
578+
User::create(['name' => 'John Doe']);
579+
580+
User::truncate();
581+
582+
$this->assertEquals(0, User::count());
583+
}
575584
}

tests/QueryBuilderTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ public function testDelete()
175175
public function testTruncate()
176176
{
177177
DB::collection('users')->insert(['name' => 'John Doe']);
178-
DB::collection('users')->truncate();
178+
$result = DB::collection('users')->truncate();
179+
$this->assertEquals(1, $result);
179180
$this->assertEquals(0, DB::collection('users')->count());
180181
}
181182

@@ -737,4 +738,25 @@ public function testValue()
737738
$this->assertEquals('Herman', DB::collection('books')->value('author.first_name'));
738739
$this->assertEquals('Melville', DB::collection('books')->value('author.last_name'));
739740
}
741+
742+
public function testHintOptions()
743+
{
744+
DB::collection('items')->insert([
745+
['name' => 'fork', 'tags' => ['sharp', 'pointy']],
746+
['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']],
747+
['name' => 'spoon', 'tags' => ['round', 'bowl']],
748+
]);
749+
750+
$results = DB::collection('items')->hint(['$natural' => -1])->get();
751+
752+
$this->assertEquals('spoon', $results[0]['name']);
753+
$this->assertEquals('spork', $results[1]['name']);
754+
$this->assertEquals('fork', $results[2]['name']);
755+
756+
$results = DB::collection('items')->hint(['$natural' => 1])->get();
757+
758+
$this->assertEquals('spoon', $results[2]['name']);
759+
$this->assertEquals('spork', $results[1]['name']);
760+
$this->assertEquals('fork', $results[0]['name']);
761+
}
740762
}

tests/SchemaTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ public function testDropIndex(): void
132132
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
133133
$this->assertFalse($index);
134134

135+
Schema::collection('newcollection', function ($collection) {
136+
$collection->index(['field_a' => -1, 'field_b' => 1]);
137+
});
138+
139+
$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
140+
$this->assertNotNull($index);
141+
142+
Schema::collection('newcollection', function ($collection) {
143+
$collection->dropIndex(['field_a' => -1, 'field_b' => 1]);
144+
});
145+
146+
$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
147+
$this->assertFalse($index);
148+
135149
Schema::collection('newcollection', function ($collection) {
136150
$collection->index(['field_a', 'field_b'], 'custom_index_name');
137151
});

0 commit comments

Comments
 (0)