Skip to content

Commit

Permalink
perf(Concerns): improve error handling in setSoarBinary
Browse files Browse the repository at this point in the history
- Refactor error handling in setSoarBinary method
- Combine file existence and executability checks
- Throw exception when file does not exist or is not executable
  • Loading branch information
guanguans committed Jan 16, 2025
1 parent cb13eec commit 7ca9c9d
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/Concerns/HasSoarBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public function getSoarBinary(): string

public function setSoarBinary(string $soarBinary): self
{
if (!file_exists($soarBinary)) {
throw new InvalidArgumentException("The file [$soarBinary] does not exist.");
}

if (!is_executable($soarBinary)) {
throw new InvalidArgumentException("The file [$soarBinary] is not executable.");
if (
!is_file($soarBinary)
// || !file_exists($soarBinary)
|| !is_executable($soarBinary)
) {
throw new InvalidArgumentException("The file [$soarBinary] does not exist or is not executable.");
}

$this->soarBinary = realpath($soarBinary);
Expand Down
1 change: 1 addition & 0 deletions src/Support/OS.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
/**
* This file was modified from https://github.com/jolicode/JoliNotif.
* This file was modified from https://github.com/utopia-php/system.
* This file was modified from https://github.com/loophp/phposinfo.
*/
class OS
{
Expand Down
8 changes: 0 additions & 8 deletions tests/Concerns/ConcreteScoresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* @see https://github.com/guanguans/soar-php
*/

use Guanguans\SoarPHP\Exceptions\InvalidArgumentException;
use Guanguans\SoarPHP\Soar;
use Guanguans\SoarPHP\Support\OS;

Expand Down Expand Up @@ -111,13 +110,6 @@
});
});

it('will throw InvalidArgumentException when sqls is boolean', function (): void {
Soar::create()->scores(true);
})
->group(__DIR__, __FILE__)
->throws(InvalidArgumentException::class, \gettype(true))
->skip();

it('can get scores', function (): void {
expect(Soar::create())
->scores('select * from users;')
Expand Down
1 change: 1 addition & 0 deletions tests/Concerns/HasSoarBinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
->skip('This test is skipped. Because is not supported in github actions.');

it('will throw InvalidArgumentException when set invalid binary', function (): void {
Soar::create()->setSoarBinary('/');
Soar::create()->setSoarBinary('soar.path');
})
->group(__DIR__, __FILE__)
Expand Down
8 changes: 0 additions & 8 deletions tests/Concerns/WithRunableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@

namespace Guanguans\SoarPHPTests\Concerns;

use Guanguans\SoarPHP\Exceptions\InvalidArgumentException;
use Guanguans\SoarPHP\Exceptions\ProcessFailedException;
use Guanguans\SoarPHP\Soar;
use Symfony\Component\Process\Process;

it('will throw InvalidArgumentException when with-options is boolean', function (): void {
Soar::create()->run(true);
})
->group(__DIR__, __FILE__)
->throws(InvalidArgumentException::class, \gettype(true))
->skip();

it('will throw ProcessFailedException when sqls is invalid sql', function (): void {
Soar::create()->setOnlySyntaxCheck(true)->setQuery('invalid sql')->run();
})
Expand Down

0 comments on commit 7ca9c9d

Please sign in to comment.