diff --git a/tests/DnsServer/CustomDnsServerTest.php b/tests/DnsServer/CustomDnsServerTest.php new file mode 100644 index 0000000..dd427f1 --- /dev/null +++ b/tests/DnsServer/CustomDnsServerTest.php @@ -0,0 +1,68 @@ +assertInstanceOf( + DnsServerInterface::class, + new CustomDnsServer('8.8.8.8'), + sprintf( + 'Object %1$s should instanceof %2$s', + CustomDnsServer::class, + DnsServerInterface::class + ) + ); + } + + public function testCreate() + { + $dns = '8.8.8.8'; + $customDns = CustomDnsServer::create($dns); + $this->assertSame( + $customDns::class, + CustomDnsServer::class, + sprintf( + '%1$s::create("%2$s") should use object class of %1$s', + CustomDnsServer::class, + $dns + ) + ); + + $this->assertSame( + $customDns->getPrimaryServer(), + '8.8.8.8', + sprintf( + 'Object %1$s->getPrimaryServer() should %2$s', + CustomDnsServer::class, + $dns + ) + ); + + $this->assertNull( + $customDns->getSecondaryServer(), + sprintf( + 'Object %1$s->getSecondaryServer() should null if secondary not declared', + CustomDnsServer::class + ) + ); + + $this->assertSame( + $customDns->getPort(), + 53, + sprintf( + 'Object %1$s->getPort() should integer 53 as default port', + CustomDnsServer::class + ) + ); + } +} diff --git a/tests/Utils/AddressesTest.php b/tests/Utils/AddressesTest.php new file mode 100644 index 0000000..b0c9f32 --- /dev/null +++ b/tests/Utils/AddressesTest.php @@ -0,0 +1,289 @@ +assertSame( + $baseDomain, + Addresses::filterDomain($httpsDomainWithPort), + sprintf( + 'Addresses::filterDomain("%1$s") should return "%1$s"', + $httpsDomainWithPort + ) + ); + $this->assertSame( + $baseDomain, + Addresses::filterDomain($httpsDomainWithUser), + sprintf( + 'Addresses::filterDomain("%1$s") should return "%1$s"', + $httpsDomainWithUser + ) + ); + $this->assertSame( + $baseDomain, + Addresses::filterDomain($httpsDomainWithUserPassword), + sprintf( + 'Addresses::filterDomain("%1$s") should return "%1$s"', + $httpsDomainWithUserPassword + ) + ); + $this->assertSame( + $baseDomain, + Addresses::filterDomain($httpsDomainWithUserEmptyPassword), + sprintf( + 'Addresses::filterDomain("%1$s") should return "%1$s"', + $httpsDomainWithUserEmptyPassword + ) + ); + $this->assertSame( + $baseDomain, + Addresses::filterDomain($httpsDomainWithUserPasswordAndPort), + sprintf( + 'Addresses::filterDomain("%1$s") should return "%1$s"', + $httpsDomainWithUserPasswordAndPort + ) + ); + $this->assertSame( + $baseDomain, + Addresses::filterDomain($httpsDomainWithUserEmptyPasswordAndPort), + sprintf( + 'Addresses::filterDomain("%1$s") should return "%1$s"', + $httpsDomainWithUserEmptyPasswordAndPort + ) + ); + $this->assertSame( + $baseDomain, + Addresses::filterDomain($httpsDomainWithEmail), + sprintf( + 'Addresses::filterDomain("%1$s") should return "%1$s"', + $httpsDomainWithEmail + ) + ); + $invalidDomain = 'invalid-domain-name'; + $this->assertSame( + $baseDomain, + Addresses::filterDomain($httpsDomain), + sprintf( + 'Addresses::filterDomain("%1$s") should return "%1$s"', + $httpsDomain + ) + ); + // if invalid domain name + $this->assertNull( + Addresses::filterDomain($invalidDomain), + sprintf( + 'Addresses::filterDomain("%1$s") should return null', + $invalidDomain + ) + ); + } + + public function testFilterIp() + { + $invalidIP = '127.0.0.'; + $validLocalIP = '127.0.0.1'; + $validLocalIP6 = '::1'; + $invalidIPv6 = '::1:::'; + $validLocalIP61 = '::0:1'; // equal ::1 + $validPrivateIP = '192.168.0.1'; + $this->assertNull( + Addresses::filterIp($invalidIP), + sprintf( + 'Addresses::filterIp("%1$s") should return null cause invalid', + $invalidIP + ) + ); + $this->assertNull( + Addresses::filterIp($invalidIPv6), + sprintf( + 'Addresses::filterIp("%1$s") should return null cause invalid', + $invalidIPv6 + ) + ); + $this->assertSame( + $validLocalIP, + Addresses::filterIp($validLocalIP), + sprintf( + 'Addresses::filterIp("%1$s") should return "%1$s"', + $validLocalIP + ) + ); + $this->assertSame( + $validPrivateIP, + Addresses::filterIp($validPrivateIP), + sprintf( + 'Addresses::filterIp("%1$s") should return "%1$s"', + $validLocalIP + ) + ); + $this->assertSame( + $validLocalIP6, + Addresses::filterIp($validLocalIP6), + sprintf( + 'Addresses::filterIp("%1$s") should return "%1$s"', + $validLocalIP6 + ) + ); + $this->assertSame( + $validLocalIP6, + Addresses::filterIp($validLocalIP61), + sprintf( + 'Addresses::filterIp("%1$s") should return "%2$s"', + $validLocalIP61, + $validLocalIP6 + ) + ); + } + + public function testFilterIpv4() + { + $invalidIP = '::1'; + $validLocalIP = '127.0.0.1'; + $this->assertNull( + Addresses::filterIpv4($invalidIP), + sprintf( + 'Addresses::filterIpv4("%1$s") should return null cause invalid ip4', + $invalidIP + ) + ); + $this->assertSame( + $validLocalIP, + Addresses::filterIpv4($validLocalIP), + sprintf( + 'Addresses::filterIpv4("%1$s") should return "%1$s" ip4', + $validLocalIP + ) + ); + } + + public function testFilterIpv6() + { + $invalidIP = '127.0.0.1'; + $validLocalIP = '::1'; + $this->assertNull( + Addresses::filterIpv6($invalidIP), + sprintf( + 'Addresses::filterIpv6("%1$s") should return null cause invalid ip4', + $invalidIP + ) + ); + $this->assertSame( + $validLocalIP, + Addresses::filterIpv6($validLocalIP), + sprintf( + 'Addresses::filterIpv6("%1$s") should return "%1$s" ip4', + $validLocalIP + ) + ); + } + + public function testReverseIp() + { + $invalidIP = '127.0.0.'; + $invalidIPv6 = '::1:::'; + $validLocalIP = '127.0.0.1'; + $validLocalIP6 = '::1'; + $ip4Arpa = implode( + '.', + array_reverse(explode('.', $validLocalIP)) + ) .'.in-addr.arpa'; + $ip6Arpa = implode( + '.', + array_reverse(explode(':', $validLocalIP6)) + ) .'.ip6.arpa'; + $this->assertNull( + Addresses::reverseIp($invalidIP), + sprintf( + 'Addresses::reverseIp("%1$s") should return null cause invalid ip', + $invalidIP + ) + ); + $this->assertNull( + Addresses::reverseIp($invalidIPv6), + sprintf( + 'Addresses::reverseIp("%1$s") should return null cause invalid ip', + $invalidIPv6 + ) + ); + $this->assertSame( + Addresses::reverseIp($validLocalIP), + $ip4Arpa, + sprintf( + 'Addresses::reverseIp("%1$s") should return "%2$s"', + $validLocalIP, + $ip4Arpa + ) + ); + $this->assertSame( + Addresses::reverseIp($validLocalIP6), + $ip6Arpa, + sprintf( + 'Addresses::reverseIp("%1$s") should return "%2$s"', + $validLocalIP6, + $ip6Arpa + ) + ); + } + + public function testGuessDNSServer() + { + $invalidIP = '127.0.0.'; + $validIP = '127.0.0.1'; + $localhost = 'localhost'; + $localhostUpper = 'LOCALHOST'; + $invalidDomain = 'invalidDomain'; + $this->assertNull( + Addresses::guessDNSServer($invalidIP), + sprintf( + 'Addresses::guessDNSServer("%1$s") should return null cause invalid ip', + $invalidIP + ) + ); + $this->assertNull( + Addresses::guessDNSServer($invalidDomain), + sprintf( + 'Addresses::guessDNSServer("%1$s") should return null cause invalid ip', + $invalidDomain + ) + ); + + $this->assertSame( + Addresses::guessDNSServer($localhostUpper), + $localhost, + sprintf( + 'Addresses::guessDNSServer("%1$s") should return "%2$s"', + $localhostUpper, + $localhost + ) + ); + $this->assertSame( + Addresses::guessDNSServer($validIP), + $validIP, + sprintf( + 'Addresses::guessDNSServer("%1$s") should return "%1$s"', + $validIP + ) + ); + } +} diff --git a/tests/Utils/CallerTest.php b/tests/Utils/CallerTest.php new file mode 100644 index 0000000..9442763 --- /dev/null +++ b/tests/Utils/CallerTest.php @@ -0,0 +1,98 @@ +assertSame( + $code, + $erorCode, + sprintf( + 'Caller::track() use error code should equal with thrown : %d', + $code + ) + ); + $this->assertSame( + $message, + $errorMesage, + sprintf( + 'Caller::track() use error message should identical with thrown : %s', + $message + ) + ); + $message = 'lowercaseToUppercase'; + $this->assertSame( + Caller::track('strtoupper', $errorCode, $errorMessage, $message), + strtoupper($message), + sprintf( + 'Caller::track(\'strtoupper\', $errorCode, $errorMessage, "%1$s") should identical strtoupper(%1$s)', + $message + ) + ); + } + + #[WithoutErrorHandler] + public function testTrack() + { + $message = 'Message 1'; + @trigger_error($message, E_USER_WARNING); + Caller::call(function () { + // call trigger + trigger_error('Message 2'); + }); + $this->assertIsArray( + error_get_last(), + 'Caller::call() should not clear the last error & error_get_last() should array return' + ); + $this->assertSame( + $message, + error_get_last()['message'], + sprintf( + 'error_get_last()[\'message\'] should return string "%1$s"', + $message + ) + ); + $this->assertSame( + E_USER_WARNING, + error_get_last()['type'], + sprintf( + 'error_get_last()[\'type\'] should return code: %1$s', + E_USER_WARNING + ) + ); + + $message = 'lowercaseToUppercase'; + $this->assertSame( + Caller::call('strtoupper', $message), + strtoupper($message), + sprintf( + 'Caller::call(\'strtoupper\', "%1$s") should identical strtoupper(%1$s)', + $message + ) + ); + } +}