Skip to content
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

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Extensions/InternetExtension.php
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));
Copy link
Collaborator

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 of long2ip) always return positive numbers so a lot of 64 bits systems are storing IPs as int from 0 to 4294967295

Copy link
Contributor Author

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 ?

}

public function ipv6(): string
{
$res = [];

for ($i = 0; $i < 8; $i++) {
$res[] = dechex($this->randomizer->getInt(0, 65535));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you prefer this way to ensure compatibility, or to use inet_ntop with a randomizer, which is the prefered way of using IPv6 when PHP is compiled with IPv6 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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));
}
}
2 changes: 2 additions & 0 deletions src/FakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Xefi\Faker\Extensions\DateTimeExtension;
use Xefi\Faker\Extensions\HashExtension;
use Xefi\Faker\Extensions\HtmlExtension;
use Xefi\Faker\Extensions\InternetExtension;
use Xefi\Faker\Extensions\LoremExtension;
use Xefi\Faker\Extensions\NumbersExtension;
use Xefi\Faker\Extensions\StringsExtension;
Expand All @@ -21,6 +22,7 @@ public function boot(): void
StringsExtension::class,
HashExtension::class,
DateTimeExtension::class,
InternetExtension::class,
]);
}
}
108 changes: 108 additions & 0 deletions tests/Unit/Extensions/InternetExtensionTest.php
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));
}
}
}