Skip to content

Commit

Permalink
fix: cli tool + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SRWieZ committed Dec 9, 2024
1 parent 072dbe2 commit 729e9ab
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 59 deletions.
37 changes: 0 additions & 37 deletions bin/publicip
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,3 @@ use KnotsPHP\PublicIP\Finders\PublicIPv6;
include $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php';

require __DIR__.'/../cli/publicip.php';


$ipVersion = null;

// if --help or -h is passed, show help
if (in_array('--help', $argv) || in_array('-h', $argv)) {
echo 'Usage: publicip [options]' . PHP_EOL;
echo PHP_EOL;
echo 'Options:' . PHP_EOL;
echo ' --v4, -4 Use IPv4' . PHP_EOL;
echo ' --v6, -6 Use IPv6' . PHP_EOL;
echo ' --help, -h Show this help' . PHP_EOL;
exit(0);
}

// if --v4 or -4 is passed, use IPv4
if (in_array('--v4', $argv) || in_array('-4', $argv)) {
$ipVersion = 'ipv4';
}

// if --v6 or -6 is passed, use IPv6
if (in_array('--v6', $argv) || in_array('-6', $argv)) {
$ipVersion = 'ipv6';
}

$ip = match ($ipVersion) {
'ipv4' => PublicIPv4::get(),
'ipv6' => PublicIPv6::get(),
default => PublicIP::get(),
};

if($ip === null) {
echo 'Could not find public IP address.' . PHP_EOL;
exit(1);
}

echo $ip . PHP_EOL;
40 changes: 40 additions & 0 deletions cli/publicip.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env php
<?php

use KnotsPHP\PublicIP\Finders\PublicIP;
use KnotsPHP\PublicIP\Finders\PublicIPv4;
use KnotsPHP\PublicIP\Finders\PublicIPv6;

error_reporting(E_ALL ^ E_DEPRECATED ^ E_NOTICE);

if (! class_exists('\Composer\InstalledVersions')) {
Expand All @@ -10,3 +14,39 @@
if (class_exists('\NunoMaduro\Collision\Provider')) {
(new \NunoMaduro\Collision\Provider)->register();
}

$ipVersion = null;

// if --help or -h is passed, show help
if (in_array('--help', $argv) || in_array('-h', $argv)) {
echo 'Usage: publicip [options]'.PHP_EOL;
echo PHP_EOL;
echo 'Options:'.PHP_EOL;
echo ' --v4, -4 Use IPv4'.PHP_EOL;
echo ' --v6, -6 Use IPv6'.PHP_EOL;
echo ' --help, -h Show this help'.PHP_EOL;
exit(0);
}

// if --v4 or -4 is passed, use IPv4
if (in_array('--v4', $argv) || in_array('-4', $argv)) {
$ipVersion = 'ipv4';
}

// if --v6 or -6 is passed, use IPv6
if (in_array('--v6', $argv) || in_array('-6', $argv)) {
$ipVersion = 'ipv6';
}

$ip = match ($ipVersion) {
'ipv4' => PublicIPv4::get(),
'ipv6' => PublicIPv6::get(),
default => PublicIP::get(),
};

if ($ip === null) {
echo 'Could not find public IP address.'.PHP_EOL;
exit(1);
}

echo $ip.PHP_EOL;
22 changes: 0 additions & 22 deletions tests/Feature/CliTool.php

This file was deleted.

29 changes: 29 additions & 0 deletions tests/Feature/CommandLineToolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

it('returns an ip', function () {
exec('php cli/publicip.php', $output, $result_code);

dump(trim($output[0]));
$filtered = filter_var(trim($output[0]), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6);

expect($result_code)->toBe(0)
->and($filtered)->toBeString();
});

it('returns an ipv4', function () {
exec('php cli/publicip.php --v4', $output, $result_code);

$filtered = filter_var(trim($output[0]), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);

expect($result_code)->toBe(0)
->and($filtered)->toBeString();
});

it('returns an ipv6', function () {
exec('php cli/publicip.php --v6', $output, $result_code);

$filtered = filter_var(trim($output[0]), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);

expect($result_code)->toBe(0)
->and($filtered)->toBeString();
});

0 comments on commit 729e9ab

Please sign in to comment.