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 all 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
41 changes: 41 additions & 0 deletions src/Extensions/InternetExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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, PHP_INT_MAX));
}

public function ipv6(): string
{
return inet_ntop($this->randomizer->getBytes(16));
}

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 @@ -7,6 +7,7 @@
use Xefi\Faker\Extensions\FinancialExtension;
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\PersonExtension;
Expand All @@ -24,6 +25,7 @@ public function boot(): void
StringsExtension::class,
HashExtension::class,
DateTimeExtension::class,
InternetExtension::class,
ColorsExtension::class,
PersonExtension::class,
FinancialExtension::class,
Expand Down
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));
}
}
}