-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35983: Run command usage example #2852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ccho-mongodb
merged 13 commits into
mongodb:4.1
from
norareidy:DOCSP-35983-run-command-usage
Apr 19, 2024
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bb7af03
DOCSP-35983: Run command usage example
norareidy 822e37d
apply phpcbf formatting
norareidy c87fa9b
adding content
norareidy 3f059be
assertions
norareidy 937db89
apply phpcbf formatting
norareidy 322fdb0
PRR fixes
0771a82
PRR fixes 2
572ca04
Merge branch '4.1' into DOCSP-35983-run-command-usage
b0f0e53
add DB facade to test imports
bc68863
disable phpcs to allow printing output
e17ea5e
apply phpcbf formatting
ccho-mongodb 29b8d2c
fix phpcs command
2219d45
Fix test and example
GromNaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use MongoDB\Driver\Cursor; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class RunCommandTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testRunCommand(): void | ||
{ | ||
// phpcs:disable | ||
// begin-command | ||
$cursor = DB::connection('mongodb') | ||
->command(['listCollections' => 1]); | ||
|
||
foreach ($cursor as $coll) { | ||
echo $coll['name'] . '<br>'; | ||
} | ||
|
||
// end-command | ||
// phpcs:enable | ||
|
||
$this->assertNotNull($cursor); | ||
$this->assertInstanceOf(Cursor::class, $cursor); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
.. _laravel-run-command-usage: | ||
|
||
============= | ||
Run a Command | ||
============= | ||
|
||
You can run a MongoDB command directly on a database by calling the ``command()`` | ||
method on a database connection instance. | ||
|
||
To run a command, call the ``command()`` method and pass it a document that | ||
contains the command and its parameters. | ||
|
||
Example | ||
------- | ||
|
||
This usage example performs the following actions on the database connection | ||
instance that uses the ``sample_mflix`` database: | ||
|
||
- Creates a database connection instance that references the ``sample_mflix`` | ||
database | ||
- Specifies a command to retrieve a list of collections and views in the | ||
``sample_mflix`` database | ||
- Prints the value of the ``name`` field of each result returned by the command | ||
|
||
The example calls the ``command()`` method to run the ``listCollections`` command. This method | ||
returns a cursor that contains a result document for each collection in the database. | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: ../includes/usage-examples/RunCommandTest.php | ||
:start-after: begin-command | ||
:end-before: end-command | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:language: console | ||
:visible: false | ||
|
||
sessions | ||
movies | ||
theaters | ||
comments | ||
embedded_movies | ||
users | ||
|
||
To learn how to edit your Laravel application to run the usage example, see the | ||
:ref:`Usage Examples landing page <laravel-usage-examples>`. | ||
|
||
.. tip:: | ||
|
||
To learn more about running MongoDB database commands, see | ||
:manual:`Database Commands </reference/command/>` in the {+server-docs-name+}. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question for @GromNaN:
I'm not able to get the tests to pass since printing any output causes a CI test failure. Is there a way to keep the output code and pass the CI tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed the test by adding
$this->expectOutputRegex('/<br>/');
The
phpcs:disable
statement was not necessary.