Skip to content
This repository was archived by the owner on Apr 1, 2023. It is now read-only.

Commit

Permalink
Convert all tests to Pest (spatie#188)
Browse files Browse the repository at this point in the history
* feat: install Pest

* refactor: MongoDbTest

* refactor: MySqlTest

* refactor: PostgreSqlTest

* refactor: SqliteTest

* feat: replace test suite in Github action and `composer.json`
  • Loading branch information
alexmanase authored Feb 6, 2023
1 parent 7f75307 commit 53e1bbd
Show file tree
Hide file tree
Showing 7 changed files with 853 additions and 978 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/pest
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
}
],
"require": {
"php" : "^8.0",
"php": "^8.0",
"symfony/process": "^5.0|^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"pestphp/pest": "^1.22"
},
"autoload": {
"psr-4": {
Expand All @@ -36,6 +36,11 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/pest"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
}
232 changes: 104 additions & 128 deletions tests/MongoDbTest.php
Original file line number Diff line number Diff line change
@@ -1,135 +1,111 @@
<?php

namespace Spatie\DbDumper\Test;

use PHPUnit\Framework\TestCase;
use Spatie\DbDumper\Compressors\Bzip2Compressor;
use Spatie\DbDumper\Compressors\GzipCompressor;
use Spatie\DbDumper\Databases\MongoDb;
use Spatie\DbDumper\Exceptions\CannotStartDump;

class MongoDbTest extends TestCase
{
/** @test */
public function it_provides_a_factory_method()
{
$this->assertInstanceOf(MongoDb::class, MongoDb::create());
}

/** @test */
public function it_will_throw_an_exception_when_no_credentials_are_set()
{
$this->expectException(CannotStartDump::class);

MongoDb::create()->dumpToFile('test.gz');
}

/** @test */
public function it_can_generate_a_dump_command()
{
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname'
.' --archive --host localhost --port 27017 > "dbname.gz"', $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_gzip_compressor_enabled()
{
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->useCompressor(new GzipCompressor())
->getDumpCommand('dbname.gz');

$expected = '((((\'mongodump\' --db dbname --archive --host localhost --port 27017; echo $? >&3) | gzip > "dbname.gz") 3>&1) | (read x; exit $x))';

$this->assertSame($expected, $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_bzip2_compressor_enabled()
{
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->useCompressor(new Bzip2Compressor())
->getDumpCommand('dbname.bz2');

$expected = '((((\'mongodump\' --db dbname --archive --host localhost --port 27017; echo $? >&3) | bzip2 > "dbname.bz2") 3>&1) | (read x; exit $x))';

$this->assertSame($expected, $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_absolute_path_having_space_and_brackets()
{
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->getDumpCommand('/save/to/new (directory)/dbname.gz');

$this->assertSame('\'mongodump\' --db dbname --archive --host localhost --port 27017 > "/save/to/new (directory)/dbname.gz"', $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_username_and_password()
{
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname --archive'
.' --username \'username\' --password \'password\' --host localhost --port 27017 > "dbname.gz"', $dumpCommand);
}

/** @test */
public function it_can_generate_a_command_with_custom_host_and_port()
{
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->setHost('mongodb.test.com')
->setPort(27018)
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname --archive'
.' --host mongodb.test.com --port 27018 > "dbname.gz"', $dumpCommand);
}

/** @test */
public function it_can_generate_a_backup_command_for_a_single_collection()
{
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->setCollection('mycollection')
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname --archive'
.' --host localhost --port 27017 --collection mycollection > "dbname.gz"', $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_custom_binary_path()
{
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->setDumpBinaryPath('/custom/directory')
->getDumpCommand('dbname.gz');

$this->assertSame('\'/custom/directory/mongodump\' --db dbname --archive'
.' --host localhost --port 27017 > "dbname.gz"', $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_authentication_database()
{
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->setAuthenticationDatabase('admin')
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname --archive'
.' --host localhost --port 27017 --authenticationDatabase admin > "dbname.gz"', $dumpCommand);
}
}
it('provides a factory method')
->expect(MongoDb::create())
->toBeInstanceOf(MongoDb::class);

it('will_throw_an_exception_when_no_credentials_are_set')
->tap(fn () => MongoDb::create()->dumpToFile('test.gz'))
->throws(CannotStartDump::class);

it('can generate a dump command', function () {
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->getDumpCommand('dbname.gz');

expect($dumpCommand)->toEqual('\'mongodump\' --db dbname'
. ' --archive --host localhost --port 27017 > "dbname.gz"');
});

it('can generate a dump command with gzip compressor enabled', function () {
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->useCompressor(new GzipCompressor())
->getDumpCommand('dbname.gz');

expect($dumpCommand)->toEqual(
'((((\'mongodump\' --db dbname --archive --host localhost --port 27017; echo $? >&3) | gzip > "dbname.gz") 3>&1) | (read x; exit $x))'
);
});

it('can generate a dump command with bzip2 compressor enabled', function () {
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->useCompressor(new Bzip2Compressor())
->getDumpCommand('dbname.bz2');

expect($dumpCommand)->toEqual(
'((((\'mongodump\' --db dbname --archive --host localhost --port 27017; echo $? >&3) | bzip2 > "dbname.bz2") 3>&1) | (read x; exit $x))'
);
});

it('can generate a dump command with absolute path having space and brackets', function () {
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->getDumpCommand('/save/to/new (directory)/dbname.gz');

expect($dumpCommand)->toEqual(
'\'mongodump\' --db dbname --archive --host localhost --port 27017 > "/save/to/new (directory)/dbname.gz"'
);
});

it('can generate a dump command with username and password', function () {
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->getDumpCommand('dbname.gz');

expect($dumpCommand)->toEqual('\'mongodump\' --db dbname --archive'
. ' --username \'username\' --password \'password\' --host localhost --port 27017 > "dbname.gz"');
});

it('can generate a command with custom host and port', function () {
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->setHost('mongodb.test.com')
->setPort(27018)
->getDumpCommand('dbname.gz');

expect($dumpCommand)->toEqual('\'mongodump\' --db dbname --archive'
. ' --host mongodb.test.com --port 27018 > "dbname.gz"');
});

it('can generate a backup command for a single collection', function () {
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->setCollection('mycollection')
->getDumpCommand('dbname.gz');

expect($dumpCommand)->toEqual('\'mongodump\' --db dbname --archive'
. ' --host localhost --port 27017 --collection mycollection > "dbname.gz"');
});

it('can generate a dump command with custom binary path', function () {
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->setDumpBinaryPath('/custom/directory')
->getDumpCommand('dbname.gz');

expect($dumpCommand)->toEqual(
'\'/custom/directory/mongodump\' --db dbname --archive'
. ' --host localhost --port 27017 > "dbname.gz"'
);
});

it('can generate a dump command with authentication database', function () {
$dumpCommand = MongoDb::create()
->setDbName('dbname')
->setAuthenticationDatabase('admin')
->getDumpCommand('dbname.gz');

expect($dumpCommand)->toEqual(
'\'mongodump\' --db dbname --archive'
. ' --host localhost --port 27017 --authenticationDatabase admin > "dbname.gz"'
);
});
Loading

0 comments on commit 53e1bbd

Please sign in to comment.