New tests (FS writing is broken, fix it)

This commit is contained in:
flash 2018-01-02 01:14:55 +01:00
parent 4cc0d69351
commit 5ec90ec1ba
14 changed files with 328 additions and 75 deletions

View file

@ -8,7 +8,7 @@
},
"require": {
"php": ">=7.2",
"flashwave/aitemu": "dev-master#ac3050d17e3aa31a088caf15469bb51e5b165d40",
"flashwave/aitemu": "dev-master#3ddb8147ed809cfba59c46f6e3d3af9fb2a86ace",
"twig/twig": "~2.4",
"nesbot/carbon": "~1.22",
"illuminate/database": "~5.5",

77
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "e20f18d591572a55dce8ab1b98dc7b5a",
"content-hash": "4c9cc87ad91ef38f4333d4e9e80a0f39",
"packages": [
{
"name": "doctrine/annotations",
@ -590,17 +590,16 @@
"source": {
"type": "git",
"url": "https://github.com/flashwave/aitemu.git",
"reference": "ac3050d17e3aa31a088caf15469bb51e5b165d40"
"reference": "3ddb8147ed809cfba59c46f6e3d3af9fb2a86ace"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/flashwave/aitemu/zipball/ac3050d17e3aa31a088caf15469bb51e5b165d40",
"reference": "ac3050d17e3aa31a088caf15469bb51e5b165d40",
"url": "https://api.github.com/repos/flashwave/aitemu/zipball/3ddb8147ed809cfba59c46f6e3d3af9fb2a86ace",
"reference": "3ddb8147ed809cfba59c46f6e3d3af9fb2a86ace",
"shasum": ""
},
"require": {
"php": ">=7.2",
"twig/twig": "~2.0"
"php": ">=7.2"
},
"require-dev": {
"phpunit/phpunit": "~6.0"
@ -615,13 +614,13 @@
"license": [
"MIT"
],
"description": "PHP Framework, primarily for personal use.",
"description": "PHP router library, mainly for personal use.",
"homepage": "https://github.com/flashwave/aitemu",
"time": "2017-12-17T01:25:07+00:00"
"time": "2018-01-01T22:36:19+00:00"
},
{
"name": "illuminate/container",
"version": "v5.5.17",
"version": "v5.5.28",
"source": {
"type": "git",
"url": "https://github.com/illuminate/container.git",
@ -665,16 +664,16 @@
},
{
"name": "illuminate/contracts",
"version": "v5.5.17",
"version": "v5.5.28",
"source": {
"type": "git",
"url": "https://github.com/illuminate/contracts.git",
"reference": "d9e269284eba43bd2e9e8d1f1ba12362b00ec096"
"reference": "03e9014d2091a30b025c895aa6d39c2755576ea5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/contracts/zipball/d9e269284eba43bd2e9e8d1f1ba12362b00ec096",
"reference": "d9e269284eba43bd2e9e8d1f1ba12362b00ec096",
"url": "https://api.github.com/repos/illuminate/contracts/zipball/03e9014d2091a30b025c895aa6d39c2755576ea5",
"reference": "03e9014d2091a30b025c895aa6d39c2755576ea5",
"shasum": ""
},
"require": {
@ -705,20 +704,20 @@
],
"description": "The Illuminate Contracts package.",
"homepage": "https://laravel.com",
"time": "2017-09-19T13:09:37+00:00"
"time": "2017-11-22T19:01:14+00:00"
},
{
"name": "illuminate/database",
"version": "v5.5.17",
"version": "v5.5.28",
"source": {
"type": "git",
"url": "https://github.com/illuminate/database.git",
"reference": "9b3bdecfd74279d305ad1d3d56ae62b1b54a230d"
"reference": "e5f0c2ce642baf74fc126f0077032b13c50e8997"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/database/zipball/9b3bdecfd74279d305ad1d3d56ae62b1b54a230d",
"reference": "9b3bdecfd74279d305ad1d3d56ae62b1b54a230d",
"url": "https://api.github.com/repos/illuminate/database/zipball/e5f0c2ce642baf74fc126f0077032b13c50e8997",
"reference": "e5f0c2ce642baf74fc126f0077032b13c50e8997",
"shasum": ""
},
"require": {
@ -764,20 +763,20 @@
"orm",
"sql"
],
"time": "2017-10-17T12:16:50+00:00"
"time": "2017-12-19T14:02:10+00:00"
},
{
"name": "illuminate/filesystem",
"version": "v5.5.17",
"version": "v5.5.28",
"source": {
"type": "git",
"url": "https://github.com/illuminate/filesystem.git",
"reference": "9e45ea9a64787455944bca2c6588cf2da085c360"
"reference": "505d1a1e4a4d6e05db7f4d54bb1c9668e0afc754"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/filesystem/zipball/9e45ea9a64787455944bca2c6588cf2da085c360",
"reference": "9e45ea9a64787455944bca2c6588cf2da085c360",
"url": "https://api.github.com/repos/illuminate/filesystem/zipball/505d1a1e4a4d6e05db7f4d54bb1c9668e0afc754",
"reference": "505d1a1e4a4d6e05db7f4d54bb1c9668e0afc754",
"shasum": ""
},
"require": {
@ -814,20 +813,20 @@
],
"description": "The Illuminate Filesystem package.",
"homepage": "https://laravel.com",
"time": "2017-09-13T13:01:30+00:00"
"time": "2017-12-14T13:33:54+00:00"
},
{
"name": "illuminate/support",
"version": "v5.5.17",
"version": "v5.5.28",
"source": {
"type": "git",
"url": "https://github.com/illuminate/support.git",
"reference": "132b06edaab3808f63943004911d58785f164ab4"
"reference": "4db3cc82b483172b1b25d9dfcec684927f5c8cf9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/support/zipball/132b06edaab3808f63943004911d58785f164ab4",
"reference": "132b06edaab3808f63943004911d58785f164ab4",
"url": "https://api.github.com/repos/illuminate/support/zipball/4db3cc82b483172b1b25d9dfcec684927f5c8cf9",
"reference": "4db3cc82b483172b1b25d9dfcec684927f5c8cf9",
"shasum": ""
},
"require": {
@ -871,7 +870,7 @@
],
"description": "The Illuminate Support package.",
"homepage": "https://laravel.com",
"time": "2017-10-17T12:18:29+00:00"
"time": "2017-12-24T20:02:59+00:00"
},
{
"name": "nesbot/carbon",
@ -1989,16 +1988,16 @@
},
{
"name": "phpunit/phpunit",
"version": "6.5.4",
"version": "6.5.5",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "1b2f933d5775f9237369deaa2d2bfbf9d652be4c"
"reference": "83d27937a310f2984fd575686138597147bdc7df"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1b2f933d5775f9237369deaa2d2bfbf9d652be4c",
"reference": "1b2f933d5775f9237369deaa2d2bfbf9d652be4c",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/83d27937a310f2984fd575686138597147bdc7df",
"reference": "83d27937a310f2984fd575686138597147bdc7df",
"shasum": ""
},
"require": {
@ -2069,7 +2068,7 @@
"testing",
"xunit"
],
"time": "2017-12-10T08:06:19+00:00"
"time": "2017-12-17T06:31:19+00:00"
},
{
"name": "phpunit/phpunit-mock-objects",
@ -2177,16 +2176,16 @@
},
{
"name": "sebastian/comparator",
"version": "2.1.0",
"version": "2.1.1",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/comparator.git",
"reference": "1174d9018191e93cb9d719edec01257fc05f8158"
"reference": "b11c729f95109b56a0fe9650c6a63a0fcd8c439f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/1174d9018191e93cb9d719edec01257fc05f8158",
"reference": "1174d9018191e93cb9d719edec01257fc05f8158",
"url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/b11c729f95109b56a0fe9650c6a63a0fcd8c439f",
"reference": "b11c729f95109b56a0fe9650c6a63a0fcd8c439f",
"shasum": ""
},
"require": {
@ -2237,7 +2236,7 @@
"compare",
"equality"
],
"time": "2017-11-03T07:16:52+00:00"
"time": "2017-12-22T14:50:35+00:00"
},
{
"name": "sebastian/diff",

View file

@ -81,7 +81,6 @@ class Colour
$this->rawValue &= ~0xFF;
$this->rawValue |= $value;
break;
}
}

View file

@ -18,7 +18,7 @@ class File
/**
* Open + Write flag.
*/
public const OPEN_WRITE = 'xb';
public const OPEN_WRITE = 'cb';
/**
* Open + Read + Write flag.
@ -38,12 +38,12 @@ class File
/**
* Open + Write flag.
*/
public const OPEN_CREATE_WRITE = 'cb';
public const OPEN_CREATE_WRITE = 'xb';
/**
* Open or Create + Read + Write flag.
*/
public const OPEN_CREATE_READ_WRITE = 'cb+';
public const OPEN_CREATE_READ_WRITE = 'xb+';
/**
* Resource/stream container.
@ -318,6 +318,7 @@ class File
}
$this->updateMetaData();
$this->updatePosition();
}
/**

View file

@ -14,7 +14,7 @@ class CIDR
* @param string $range
* @return bool
*/
public static function match($ip, $range)
public static function match(string $ip, string $range): bool
{
[$net, $mask] = explode('/', $range);
@ -44,7 +44,7 @@ class CIDR
* @param int $mask
* @return bool
*/
private static function matchV4($ip, $net, $mask)
private static function matchV4(string $ip, string $net, int $mask): bool
{
$ip = ip2long($ip);
$net = ip2long($net);
@ -59,7 +59,7 @@ class CIDR
* @param int $mask
* @return bool
*/
private static function matchV6($ip, $net, $mask)
private static function matchV6(string $ip, string $net, int $mask): bool
{
$ip = inet_pton($ip);
$net = inet_pton($net);
@ -70,11 +70,11 @@ class CIDR
/**
* Converts an IPv6 mask to bytes.
* @param int $mask
* @return int
* @return string
*/
private static function createV6Mask($mask)
private static function createV6Mask(int $mask): string
{
$range = str_repeat("f", $mask / 4);
$range = str_repeat('f', $mask / 4);
switch ($mask % 4) {
case 1:

View file

@ -48,7 +48,7 @@ class IP
* @throws NetInvalidAddressException
* @return string
*/
public static function unpack($ip)
public static function unpack(string $ip): string
{
$ipv = static::version($ip);
@ -69,7 +69,7 @@ class IP
* @throws NetAddressTypeException
* @return string
*/
public static function pack($bin)
public static function pack(string $bin): string
{
$len = strlen($bin);

View file

@ -184,7 +184,7 @@ class TemplateEngine
* @param string $name
* @param Callable $callable
*/
public function addFunction(string $name, Callable $callable = null): void
public function addFunction(string $name, callable $callable = null): void
{
$this->twig->addFunction(new Twig_SimpleFunction($name, $callable === null ? $name : $callable));
}
@ -194,7 +194,7 @@ class TemplateEngine
* @param string $name
* @param Callable $callable
*/
public function addFilter(string $name, Callable $callable = null): void
public function addFilter(string $name, callable $callable = null): void
{
$this->twig->addFilter(new Twig_SimpleFilter($name, $callable === null ? $name : $callable));
}
@ -204,7 +204,7 @@ class TemplateEngine
* @param string $path
* @return string
*/
public function findNamespace(string $path): string
public function findNamespace(string $path): ?string
{
foreach ($this->loader->getNamespaces() as $namespace) {
if ($this->exists($path, $namespace)) {

View file

@ -76,8 +76,11 @@ class Zalgo
return $string;
}
public static function run(string $text, int $mode = self::ZALGO_MODE_MINI, int $direction = self::ZALGO_DIR_MID | self::ZALGO_DIR_DOWN): string
{
public static function run(
string $text,
int $mode = self::ZALGO_MODE_MINI,
int $direction = self::ZALGO_DIR_MID | self::ZALGO_DIR_DOWN
): string {
$text_length = strlen($text);
if (!$text_length || !$mode || !$direction || !in_array($mode, self::ZALGO_MODES)) {

23
tests/CIDRTest.php Normal file
View file

@ -0,0 +1,23 @@
<?php
namespace MisuzuTests;
use PHPUnit\Framework\TestCase;
use Misuzu\Net\CIDR;
class CIDRTest extends TestCase
{
public function testIPv4()
{
$this->assertTrue(CIDR::match('104.27.135.189', '104.16.0.0/12'));
$this->assertTrue(CIDR::match('104.27.154.200', '104.16.0.0/12'));
$this->assertTrue(CIDR::match('104.28.9.4', '104.16.0.0/12'));
}
public function testIPv6()
{
$this->assertTrue(CIDR::match('2400:cb00:2048:1:0:0:681b:9ac8', '2400:cb00::/32'));
$this->assertTrue(CIDR::match('2400:cb00:2048:1:0:0:681c:804', '2400:cb00::/32'));
$this->assertTrue(CIDR::match('2400:cb00:2048:1:0:0:681b:86bd', '2400:cb00::/32'));
$this->assertTrue(CIDR::match('2400:cb00:2048:1:0:0:681f:5e2a', '2400:cb00::/32'));
}
}

View file

@ -1,6 +1,7 @@
<?php
use PHPUnit\Framework\TestCase;
namespace MisuzuTests;
use PHPUnit\Framework\TestCase;
use Misuzu\Colour;
class ColourTest extends TestCase

132
tests/FileSystemTest.php Normal file
View file

@ -0,0 +1,132 @@
<?php
namespace MisuzuTests;
use PHPUnit\Framework\TestCase;
use Misuzu\IO\Directory;
use Misuzu\IO\File;
define('WORKING_DIR', sys_get_temp_dir() . '/MisuzuFileSystemTest' . time());
class FileSystemTest extends TestCase
{
public function testSlashFix()
{
$right_slash = DIRECTORY_SEPARATOR;
$wrong_slash = DIRECTORY_SEPARATOR == '/' ? '\\' : '/';
$this->assertEquals(
Directory::fixSlashes("test{$wrong_slash}dir{$wrong_slash}with{$wrong_slash}wrong{$wrong_slash}slashes"),
"test{$right_slash}dir{$right_slash}with{$right_slash}wrong{$right_slash}slashes"
);
}
public function testExists()
{
$this->assertTrue(Directory::exists(sys_get_temp_dir()));
$this->assertFalse(Directory::exists(WORKING_DIR));
}
public function testCreateDir()
{
$directory = Directory::create(WORKING_DIR);
$this->assertInstanceOf(Directory::class, $directory);
}
public function testCreateFile()
{
$file = new File(WORKING_DIR . '/file', File::CREATE_READ_WRITE);
$this->assertInstanceOf(File::class, $file);
$file->close();
}
public function testWriteFile()
{
$file = new File(WORKING_DIR . '/file', File::OPEN_WRITE);
$this->assertInstanceOf(File::class, $file);
$file->write('misuzu');
$this->assertEquals(6, $file->size);
$file->close();
}
public function testAppendFile()
{
$file = new File(WORKING_DIR . '/file', File::OPEN_WRITE);
$this->assertInstanceOf(File::class, $file);
$file->append(' test');
$this->assertEquals(11, $file->size);
$file->close();
}
public function testPosition()
{
$file = new File(WORKING_DIR . '/file', File::OPEN_READ);
$this->assertInstanceOf(File::class, $file);
$file->start();
$this->assertEquals(0, $file->position);
$file->end();
$this->assertEquals($file->size, $file->position);
$file->position(4);
$this->assertEquals(4, $file->position);
$file->position(4, true);
$this->assertEquals(8, $file->position);
$file->close();
}
public function testRead()
{
$file = new File(WORKING_DIR . '/file', File::OPEN_READ);
$this->assertInstanceOf(File::class, $file);
$this->assertEquals('misuzu test', $file->read());
$file->position(7);
$this->assertEquals('test', $file->read());
$file->close();
}
public function testFind()
{
$file = new File(WORKING_DIR . '/file', File::OPEN_READ);
$this->assertInstanceOf(File::class, $file);
$this->assertEquals(7, $file->find('test'));
$file->close();
}
public function testChar()
{
$file = new File(WORKING_DIR . '/file', File::OPEN_READ);
$this->assertInstanceOf(File::class, $file);
$file->position(3);
$this->assertEquals('s', $file->char());
$file->close();
}
public function testDirectoryFiles()
{
$dir = new Directory(WORKING_DIR);
$this->assertEquals([realpath(WORKING_DIR . DIRECTORY_SEPARATOR . 'file')], $dir->files());
}
public function testDelete()
{
File::delete(WORKING_DIR . '/file');
$this->assertFalse(File::exists(WORKING_DIR . '/file'));
Directory::delete(WORKING_DIR);
$this->assertFalse(Directory::exists(WORKING_DIR));
}
}

77
tests/IPTest.php Normal file
View file

@ -0,0 +1,77 @@
<?php
namespace MisuzuTests;
use PHPUnit\Framework\TestCase;
use Misuzu\Net\IP;
class IPTest extends TestCase
{
public function testVersion()
{
$this->assertEquals(IP::version('127.0.0.1'), IP::V4);
$this->assertEquals(IP::version('104.27.135.189'), IP::V4);
$this->assertEquals(IP::version('104.27.154.200'), IP::V4);
$this->assertEquals(IP::version('104.28.9.4'), IP::V4);
$this->assertEquals(IP::version('::1'), IP::V6);
$this->assertEquals(IP::version('2400:cb00:2048:1:0:0:681b:9ac8'), IP::V6);
$this->assertEquals(IP::version('2400:cb00:2048:1:0:0:681c:804'), IP::V6);
$this->assertEquals(IP::version('2400:cb00:2048:1:0:0:681b:86bd'), IP::V6);
$this->assertEquals(IP::version('2400:cb00:2048:1:0:0:681f:5e2a'), IP::V6);
$this->assertEquals(IP::version('not an ip address'), 0);
$this->assertEquals(IP::version('256.256.256.256'), 0);
}
public function testUnpack()
{
$this->assertEquals(IP::unpack('127.0.0.1'), "\x7f\x00\x00\x01");
$this->assertEquals(IP::unpack('104.27.135.189'), "\x68\x1b\x87\xbd");
$this->assertEquals(IP::unpack('104.27.154.200'), "\x68\x1b\x9a\xc8");
$this->assertEquals(IP::unpack('104.28.9.4'), "\x68\x1c\x09\x04");
$this->assertEquals(IP::unpack('::1'), "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01");
$this->assertEquals(
IP::unpack('2400:cb00:2048:1:0:0:681b:9ac8'),
"\x24\x00\xcb\x00\x20\x48\x00\x01\x00\x00\x00\x00\x68\x1b\x9a\xc8"
);
$this->assertEquals(
IP::unpack('2400:cb00:2048:1:0:0:681c:804'),
"\x24\x00\xcb\x00\x20\x48\x00\x01\x00\x00\x00\x00\x68\x1c\x08\x04"
);
$this->assertEquals(
IP::unpack('2400:cb00:2048:1:0:0:681b:86bd'),
"\x24\x00\xcb\x00\x20\x48\x00\x01\x00\x00\x00\x00\x68\x1b\x86\xbd"
);
$this->assertEquals(
IP::unpack('2400:cb00:2048:1:0:0:681f:5e2a'),
"\x24\x00\xcb\x00\x20\x48\x00\x01\x00\x00\x00\x00\x68\x1f\x5e\x2a"
);
}
public function testPack()
{
$this->assertEquals(IP::pack("\x7f\x00\x00\x01"), '127.0.0.1');
$this->assertEquals(IP::pack("\x68\x1b\x87\xbd"), '104.27.135.189');
$this->assertEquals(IP::pack("\x68\x1b\x9a\xc8"), '104.27.154.200');
$this->assertEquals(IP::pack("\x68\x1c\x09\x04"), '104.28.9.4');
$this->assertEquals(IP::pack("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"), '::1');
$this->assertEquals(
IP::pack("\x24\x00\xcb\x00\x20\x48\x00\x01\x00\x00\x00\x00\x68\x1b\x9a\xc8"),
'2400:cb00:2048:1::681b:9ac8'
);
$this->assertEquals(
IP::pack("\x24\x00\xcb\x00\x20\x48\x00\x01\x00\x00\x00\x00\x68\x1c\x08\x04"),
'2400:cb00:2048:1::681c:804'
);
$this->assertEquals(
IP::pack("\x24\x00\xcb\x00\x20\x48\x00\x01\x00\x00\x00\x00\x68\x1b\x86\xbd"),
'2400:cb00:2048:1::681b:86bd'
);
$this->assertEquals(
IP::pack("\x24\x00\xcb\x00\x20\x48\x00\x01\x00\x00\x00\x00\x68\x1f\x5e\x2a"),
'2400:cb00:2048:1::681f:5e2a'
);
}
}

17
tests/ZalgoTest.php Normal file
View file

@ -0,0 +1,17 @@
<?php
namespace MisuzuTests;
use PHPUnit\Framework\TestCase;
use Misuzu\Zalgo;
class ZalgoTest extends TestCase
{
public const TEST_STRING = 'This string will be put through the Zalgo function, and back to a regular string.';
public function testStrip()
{
$this->assertEquals(TEST_STRING, Zalgo::strip(Zalgo::run(TEST_STRING, Zalgo::ZALGO_MODE_MINI)));
$this->assertEquals(TEST_STRING, Zalgo::strip(Zalgo::run(TEST_STRING, Zalgo::ZALGO_MODE_NORMAL)));
$this->assertEquals(TEST_STRING, Zalgo::strip(Zalgo::run(TEST_STRING, Zalgo::ZALGO_MODE_MAX)));
}
}

View file

@ -1,5 +1,6 @@
<?php
// both of these are provided by illuminate/database already but i feel like it makes sense to add these definitions regardless
// both of these are provided by illuminate/database already
// but i feel like it makes sense to add these definitions regardless
if (!function_exists('starts_with')) {
function starts_with(string $string, string $text): bool