-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/internet extension #8
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Xefi\Faker\Extensions; | ||
|
||
class InternetExtension extends Extension | ||
{ | ||
protected $tld = ['com', 'biz', 'info', 'net', 'org', 'edu', 'gov', 'mil', 'co', 'io']; | ||
|
||
public function sdl(): string | ||
{ | ||
return $this->randomizer->getBytesFromString( | ||
implode(range('a', 'z')), | ||
$this->randomizer->getInt(5, 10) | ||
); | ||
} | ||
|
||
public function tld(): string | ||
{ | ||
return $this->pickArrayRandomElement($this->tld); | ||
} | ||
|
||
public function domain(): string | ||
{ | ||
return sprintf('%s.%s', $this->sdl(), $this->tld()); | ||
} | ||
|
||
public function ipv4(): string | ||
{ | ||
return long2ip($this->randomizer->getInt(0, 1) === 0 ? $this->randomizer->getInt(-2147483648, -2) : $this->randomizer->getInt(16777216, 2147483647)); | ||
} | ||
|
||
public function ipv6(): string | ||
{ | ||
$res = []; | ||
|
||
for ($i = 0; $i < 8; $i++) { | ||
$res[] = dechex($this->randomizer->getInt(0, 65535)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you prefer this way to ensure compatibility, or to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did have a look at this but i'm not familiar enough with this function and documentation didn't help me on that, but this isn't a reason, will need to have a look |
||
} | ||
|
||
return implode(':', $res); | ||
} | ||
|
||
public function macAddress() | ||
{ | ||
return implode(':', str_split(substr(md5($this->randomizer->getInt(0, 2147483647)), 0, 12), 2)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace Extensions; | ||
|
||
use ReflectionClass; | ||
use Xefi\Faker\Container\Container; | ||
use Xefi\Faker\Tests\Unit\Extensions\TestCase; | ||
|
||
final class InternetExtensionTest extends TestCase | ||
{ | ||
protected array $tld = []; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$internetExtension = new \Xefi\Faker\Extensions\InternetExtension(new \Random\Randomizer()); | ||
$this->tld = (new ReflectionClass($internetExtension))->getProperty('tld')->getValue($internetExtension); | ||
} | ||
|
||
public function testSdl(): void | ||
{ | ||
$faker = new Container(false); | ||
|
||
$results = []; | ||
|
||
for ($i = 0; $i < 50; $i++) { | ||
$results[] = $faker->sdl(); | ||
} | ||
|
||
foreach ($results as $result) { | ||
$this->assertTrue(ctype_alnum($result)); | ||
} | ||
} | ||
|
||
public function testTld(): void | ||
{ | ||
$faker = new Container(false); | ||
|
||
$results = []; | ||
|
||
for ($i = 0; $i < count($this->tld); $i++) { | ||
$results[] = $faker->unique()->tld(); | ||
} | ||
|
||
$this->assertEqualsCanonicalizing($results, $this->tld); | ||
} | ||
|
||
public function testDomain(): void | ||
{ | ||
$faker = new Container(false); | ||
|
||
$results = []; | ||
|
||
for ($i = 0; $i < 50; $i++) { | ||
$results[] = $faker->domain(); | ||
} | ||
|
||
foreach ($results as $result) { | ||
$this->assertMatchesRegularExpression('/^([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,}$/', $result); | ||
} | ||
} | ||
|
||
public function testIpv4(): void | ||
{ | ||
$faker = new Container(false); | ||
|
||
$results = []; | ||
|
||
for ($i = 0; $i < 50; $i++) { | ||
$results[] = $faker->ipv4(); | ||
} | ||
|
||
foreach ($results as $result) { | ||
$this->assertNotFalse(filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)); | ||
} | ||
} | ||
|
||
public function testIpv6(): void | ||
{ | ||
$faker = new Container(false); | ||
|
||
$results = []; | ||
|
||
for ($i = 0; $i < 50; $i++) { | ||
$results[] = $faker->ipv6(); | ||
} | ||
|
||
foreach ($results as $result) { | ||
$this->assertNotFalse(filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)); | ||
} | ||
} | ||
|
||
public function testMacAddress(): void | ||
{ | ||
$faker = new Container(false); | ||
|
||
$results = []; | ||
|
||
for ($i = 0; $i < 50; $i++) { | ||
$results[] = $faker->macAddress(); | ||
} | ||
|
||
foreach ($results as $result) { | ||
$this->assertNotFalse(filter_var($result, FILTER_VALIDATE_MAC)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The negative numbers ensures compatibility with 32 bits systems, but I think we should check all the positive numbers to ensure compatibility with 64 bits systems. Cause
ip2long
(invert oflong2ip
) always return positive numbers so a lot of 64 bits systems are storing IPs as int from0
to4294967295
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems you are right, the documentation is poor on that side, I don't know if I should be avoiding local ips, like in this case, what do you think ?