Skip to content

Commit

Permalink
Merge pull request #72 from alexmanase/convert-all-tests-to-pest
Browse files Browse the repository at this point in the history
Convert all tests to pest
  • Loading branch information
freekmurze authored Jan 9, 2023
2 parents 10bc681 + 35dfc1d commit 4bae2a3
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 150 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest

- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/pest
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
"symfony/process": "^4.4|^5.3|^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"spatie/phpunit-snapshot-assertions": "^4.2",
"pestphp/pest": "^1.22",
"spatie/pest-plugin-snapshots": "^1.1",
"symfony/var-dumper": "^5.3|6.0"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"autoload": {
"psr-4": {
Expand All @@ -38,7 +41,7 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage-html coverage"
}
}
}
238 changes: 95 additions & 143 deletions tests/SshTest.php
Original file line number Diff line number Diff line change
@@ -1,161 +1,113 @@
<?php

namespace Spatie\Ssh\Tests;

use Exception;
use PHPUnit\Framework\TestCase;
use Spatie\Snapshots\MatchesSnapshots;
use function Spatie\Snapshots\assertMatchesSnapshot;
use Spatie\Ssh\Ssh;

use Symfony\Component\Process\Process;

class SshTest extends TestCase
{
use MatchesSnapshots;
uses(PHPUnit\Framework\TestCase::class);

beforeEach(function () {
$this->ssh = (new Ssh('user', 'example.com'));
});

it('can run a single command', function () {
$command = $this->ssh->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});

it('can run multiple commands', function () {
$command = $this->ssh->getExecuteCommand(['whoami', 'cd /var/log']);

assertMatchesSnapshot($command);
});

it('can use a specific private key', function () {
$command = $this->ssh->usePrivateKey('/home/user/.ssh/id_rsa')->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});

it('can set the port via the constructor', function () {
$command = (new Ssh('user', 'example.com', 123))->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});

it('can set the port via the dedicated function', function () {
$command = (new Ssh('user', 'example.com'))->usePort(123)->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});

it('can set the multiplex path via the dedicated function', function () {
$command = (new Ssh('user', 'example.com'))->useMultiplexing('/home/test/control_masters/%C', '15m')->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});

it('can instantiate via the create method')
->expect(Ssh::create('user', 'example.com'))
->toBeInstanceOf(Ssh::class);

it('can enable strict host checking', function () {
$command = (new Ssh('user', 'example.com'))->enableStrictHostKeyChecking()->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});

it('can enable quiet mode', function () {
$command = (new Ssh('user', 'example.com'))->enableQuietMode()->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});

it('can disable password authentication', function () {
$command = (new Ssh('user', 'example.com'))->disablePasswordAuthentication()->getExecuteCommand('whoami');

assertMatchesSnapshot($command);
});

private Ssh $ssh;
it('it can enable password authentication', function () {
$command = (new Ssh('user', 'example.com'))->enablePasswordAuthentication()->getExecuteCommand('whoami');

public function setUp(): void
{
parent::setUp();
assertMatchesSnapshot($command);
});

$this->ssh = (new Ssh('user', 'example.com'));
}
test('zero is a valid port number', function () {
$command = (new Ssh('user', 'example.com'))->usePort(0)->getExecuteCommand('whoami');

/** @test */
public function it_can_run_a_single_command()
{
$command = $this->ssh->getExecuteCommand('whoami');
assertMatchesSnapshot($command);
});

$this->assertMatchesSnapshot($command);
}
it('throws an exception if a port is negative')
->tap(fn () => (new Ssh('user', 'example.com'))->usePort(-45)->getExecuteCommand('whoami'))
->throws(Exception::class, 'Port must be a positive integer.');

/** @test */
public function it_can_run_multiple_commands()
{
$command = $this->ssh->getExecuteCommand(['whoami', 'cd /var/log']);
it('can download a file', function () {
$command = $this->ssh->getDownloadCommand('spatie.be/current/.env', '.env');

$this->assertMatchesSnapshot($command);
}
assertMatchesSnapshot($command);
});

/** @test */
public function it_can_use_a_specific_private_key()
{
$command = $this->ssh->usePrivateKey('/home/user/.ssh/id_rsa')->getExecuteCommand('whoami');
it('can upload a file', function () {
$command = $this->ssh->getUploadCommand('.env', 'spatie.be/current/.env');

$this->assertMatchesSnapshot($command);
}
assertMatchesSnapshot($command);
});

/** @test */
public function it_can_set_the_port_via_the_constructor()
{
$command = (new Ssh('user', 'example.com', 123))->getExecuteCommand('whoami');
it('can run a command locally', function () {
$local = new Ssh('user', '127.0.0.1');
$command = $local->execute('whoami');

$this->assertMatchesSnapshot($command);
}
expect(get_current_user())->toEqual(trim($command->getOutput()));
});

/** @test */
public function it_can_set_the_port_via_the_dedicated_function()
{
$command = (new Ssh('user', 'example.com'))->usePort(123)->getExecuteCommand('whoami');
it('can configure the used process', function () {
$command = $this->ssh->configureProcess(function (Process $process) {
$process->setTimeout(0);
})->getExecuteCommand('whoami');

$this->assertMatchesSnapshot($command);
}

/** @test */
public function it_can_set_the_multiplex_path_via_the_dedicated_function()
{
$command = (new Ssh('user', 'example.com'))->useMultiplexing('/home/test/control_masters/%C', '15m')->getExecuteCommand('whoami');

$this->assertMatchesSnapshot($command);
}

/** @test */
public function it_can_instantiate_via_the_create_method()
{
$this->assertInstanceOf(Ssh::class, Ssh::create('user', 'example.com'));
}

/** @test */
public function it_can_enable_strict_host_checking()
{
$command = (new Ssh('user', 'example.com'))->enableStrictHostKeyChecking()->getExecuteCommand('whoami');

$this->assertMatchesSnapshot($command);
}

/** @test */
public function it_can_enable_quiet_mode()
{
$command = (new Ssh('user', 'example.com'))->enableQuietMode()->getExecuteCommand('whoami');

$this->assertMatchesSnapshot($command);
}

/** @test */
public function it_can_disable_password_authentication()
{
$command = (new Ssh('user', 'example.com'))->disablePasswordAuthentication()->getExecuteCommand('whoami');

$this->assertMatchesSnapshot($command);
}

/** @test */
public function it_can_enable_password_authentication()
{
$command = (new Ssh('user', 'example.com'))->enablePasswordAuthentication()->getExecuteCommand('whoami');

$this->assertMatchesSnapshot($command);
}

/** @test */
public function zero_is_a_valid_port_number()
{
$command = (new Ssh('user', 'example.com'))->usePort(0)->getExecuteCommand('whoami');

$this->assertMatchesSnapshot($command);
}

/** @test */
public function it_throws_an_exception_if_a_port_is_negative()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Port must be a positive integer.');

$command = (new Ssh('user', 'example.com'))->usePort(-45)->getExecuteCommand('whoami');
}

/** @test */
public function it_can_download_a_file()
{
$command = $this->ssh->getDownloadCommand('spatie.be/current/.env', '.env');

$this->assertMatchesSnapshot($command);
}

/** @test */
public function it_can_upload_a_file()
{
$command = $this->ssh->getUploadCommand('.env', 'spatie.be/current/.env');

$this->assertMatchesSnapshot($command);
}

/** @test */
public function it_can_configure_the_used_process()
{
$command = $this->ssh->configureProcess(function (Process $process) {
$process->setTimeout(0);
})->getExecuteCommand('whoami');

$this->assertMatchesSnapshot($command);
}

/** @test */
public function it_can_run_a_command_locally()
{
$local = new Ssh('user', '127.0.0.1');
$command = $local->execute('whoami');

$this->assertEquals(trim($command->getOutput()), get_current_user());
}
}
assertMatchesSnapshot($command);
});

0 comments on commit 4bae2a3

Please sign in to comment.