diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index e86bfe2..114027e 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -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 diff --git a/composer.json b/composer.json index b3b3329..9091f04 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -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" } -} +} \ No newline at end of file diff --git a/tests/SshTest.php b/tests/SshTest.php index 974c355..bf1a8d4 100644 --- a/tests/SshTest.php +++ b/tests/SshTest.php @@ -1,161 +1,113 @@ 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); +}); diff --git a/tests/__snapshots__/SshTest__it_can_enable_password_authentication__1.txt b/tests/__snapshots__/SshTest__it_it_can_enable_password_authentication__1.txt similarity index 100% rename from tests/__snapshots__/SshTest__it_can_enable_password_authentication__1.txt rename to tests/__snapshots__/SshTest__it_it_can_enable_password_authentication__1.txt