Skip to content
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"require-dev": {
"phpunit/phpunit": "7.*",
"php-coveralls/php-coveralls": "2.*"
"php-coveralls/php-coveralls": "2.*",
"ext-curl": "*"
},
"autoload": {
"psr-4": {
Expand Down
97 changes: 97 additions & 0 deletions tests/Curl/CurlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Brick\Std\Tests\Curl;

use Brick\Std\Curl\Curl;
use PHPUnit\Framework\TestCase;

class CurlTest extends TestCase
{
public function testGetInfoWithSpecificOption()
{
$curl = new Curl();

$this->assertSame(0, $curl->getInfo(CURLINFO_HTTP_CODE));
}

public function testGetInfo()
{
$expectedArray = [
'url' => '',
'content_type' => null,
'http_code' => 0,
'header_size' => 0,
'request_size' => 0,
'filetime' => 0,
'ssl_verify_result' => 0,
'redirect_count' => 0,
'total_time' => 0.0,
'namelookup_time' => 0.0,
'connect_time' => 0.0,
'pretransfer_time' => 0.0,
'size_upload' => 0.0,
'size_download' => 0.0,
'speed_download' => 0.0,
'speed_upload' => 0.0,
'download_content_length' => -1.0,
'upload_content_length' => -1.0,
'starttransfer_time' => 0.0,
'redirect_time' => 0.0,
'redirect_url' => '',
'primary_ip' => '',
'certinfo' => [],
'primary_port' => 0,
'local_ip' => '',
'local_port' => 0,
];
$curl = new Curl();

$this->assertSame($expectedArray, $curl->getInfo());
}

public function testSetOption()
{
$curl = new Curl();
$curl->setOption(CURLOPT_URL, 'http://example.com');

$this->assertSame('http://example.com', $curl->getInfo(CURLINFO_EFFECTIVE_URL));
}

public function testSetOptions()
{
$curlOpts = [
CURLOPT_URL => 'http://example.com',
];
$curl = new Curl();
$curl->setOptions($curlOpts);

$this->assertSame('http://example.com', $curl->getInfo(CURLINFO_EFFECTIVE_URL));
}

public function testGetVersion()
{
$curl = new Curl();

$this->assertInternalType('array', $curl->getVersion());
$this->assertContains('version_number', $curl->getVersion());
}

public function testExecute()
{
$curl = new Curl('file://' . __FILE__);

$this->assertSame('<?php', substr($curl->execute(), 0, 5));
}

/**
* @expectedException Brick\Std\Curl\CurlException
* @expectedExceptionMessage cURL request failed: No URL set!.
*/
public function testExecuteShouldThrowCurlException()
{
$curl = new Curl();
$curl->execute();
}
}
62 changes: 62 additions & 0 deletions tests/FixedArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,68 @@ public function providerShiftDown() : array
];
}

public function testCreate()
{
$fixedArray = FixedArray::create(4);

$this->assertInstanceOf(FixedArray::class, $fixedArray);
$this->assertSame(4, $fixedArray->getSize());
}

public function testToArray()
{
$expectedArray = [1, 2, 3, 4];
$fixedArray = FixedArray::fromArray($expectedArray);
$result = $fixedArray->toArray();

$this->assertSame($expectedArray, $result);
}

public function testGetSize()
{
$fixedArray = FixedArray::fromArray([1, 2, 3, 4]);

$this->assertSame(4, $fixedArray->getSize());
}

public function testSetSize()
{
$fixedArray = FixedArray::fromArray([1, 2, 3, 4]);
$fixedArray->setSize(5);

$this->assertSame(5, $fixedArray->getSize());
}

public function offsetExistsProvider()
{
return [
[
[1, 2, 3, 4], 0, true
],
[
[1, 2, 3, 4], 5, false
],
];
}

/**
* @dataProvider offsetExistsProvider
*/
public function testOffsetExists($array, $index, $expected)
{
$fixedArray = FixedArray::fromArray($array);

$this->assertSame($expected, $fixedArray->offsetExists($index));
}

public function testOffsetUnset()
{
$fixedArray = FixedArray::fromArray([1, 2, 3, 4]);
$fixedArray->offsetUnset(3);

$this->assertFalse($fixedArray->offsetExists(3));
}

/**
* @dataProvider providerShiftTo
*
Expand Down
127 changes: 127 additions & 0 deletions tests/Io/FileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,133 @@ public function tearDown()
$this->exec('rm -rf ' . $this->tmp);
}

/**
* @expectedException Brick\Std\Io\IoException
* @expectedExceptionMessage Error getting real path of invalid_path, check that the path exists
*/
public function testGetRealPathWithInvalidPath()
{
FileSystem::getRealPath('invalid_path');
}

public function testGetRealPath()
{
$filePath = __DIR__ . '/../../composer.json';
$expectedRealPath = realpath($filePath);

$this->assertSame($expectedRealPath, FileSystem::getRealPath($filePath));
}

public function testWriteWithAppendFlag()
{
FileSystem::write('temp_file', 'data1' . PHP_EOL);
FileSystem::write('temp_file', 'data2', true);

$this->assertSame('data1' . PHP_EOL . 'data2', FileSystem::read('temp_file'));
}

public function testWriteWithLockFlag()
{
$this->assertSame(5, FileSystem::write('temp_lock_file', '12345', false, true));
}

public function testReadWithMaxLength()
{
FileSystem::write('temp_lock_file', 'data');

$this->assertSame('dat', FileSystem::read('temp_lock_file', 0, 3));
}

/**
* @expectedException Brick\Std\Io\IoException
* @expectedExceptionMessage Error copying temp_lock_file to non_existing_dir/temp_lock_file
*/
public function testCopyShouldThrowIOException()
{
FileSystem::write('temp_lock_file', 'data');
FileSystem::copy('temp_lock_file', 'non_existing_dir/temp_lock_file');
}

/**
* @expectedException Brick\Std\Io\IoException
* @expectedExceptionMessage Error moving temp_lock_file to non_existing_dir/temp_lock_file
*/
public function testMoveShouldThrowIOException()
{
FileSystem::write('temp_lock_file', 'data');
FileSystem::move('temp_lock_file', 'non_existing_dir/temp_lock_file');
}

/**
* @expectedException Brick\Std\Io\IoException
* @expectedExceptionMessage Error deleting non_existing_dir/temp_lock_file
*/
public function testDeleteShouldThrowIOException()
{
FileSystem::delete('non_existing_dir/temp_lock_file');
}

/**
* @expectedException Brick\Std\Io\IoException
* @expectedExceptionMessage Error creating directory non_existing_dir/temp_lock_file
*/
public function testCreateDirectoryShouldThrowIOException()
{
FileSystem::createDirectory('non_existing_dir/temp_lock_file');
}

/**
* @expectedException Brick\Std\Io\IoException
* @expectedExceptionMessage Error creating directories new_file/temp_directory
*/
public function testCreateDirectoriesShouldThrowIOException()
{
FileSystem::write('new_file', '');
FileSystem::createDirectories('new_file/temp_directory');
}

public function testCreateDirectoriesTwice()
{
FileSystem::createDirectories('temp_directory');
FileSystem::createDirectories('temp_directory');
}

/**
* @expectedException Brick\Std\Io\IoException
* @expectedExceptionMessage Error creating link invalid_link to non_existing_dir/invalid_target
*/
public function testCreateLinkWithInvalidFileLink()
{
FileSystem::createLink('invalid_link', 'non_existing_dir/invalid_target');
}

/**
* @expectedException Brick\Std\Io\IoException
* @expectedExceptionMessage Error reading symbolic link invalid_path
*/
public function testReadSymbolicLinkWithInvalidFileLink()
{
FileSystem::readSymbolicLink('invalid_path');
}

/**
* @expectedException Brick\Std\Io\IoException
* @expectedExceptionMessage Error writing to non_existing_dir/invalid_path
*/
public function testWriteWithInvalidFilePath()
{
FileSystem::write('non_existing_dir/invalid_path', 'data');
}

/**
* @expectedException Brick\Std\Io\IoException
* @expectedExceptionMessage Error reading from non_existing_dir/invalid_path
*/
public function testReadWithInvalidFilePath()
{
FileSystem::read('non_existing_dir/invalid_path');
}

public function testCopy()
{
$this->file_put_contents('a', 'Hello World');
Expand Down
9 changes: 9 additions & 0 deletions tests/Iterator/CsvFileIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
*/
class CsvFileIteratorTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot open file for reading: NonExistentFile
*/
public function testConstructorWithNonExistentFile()
{
new CsvFileIterator('NonExistentFile');
}

/**
* @dataProvider providerIterator
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Iterator/CsvJsonFileIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@
*/
class CsvJsonFileIteratorTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot open file for reading: NonExistentFile
*/
public function testConstructorWithNonExistentFile()
{
new CsvJsonFileIterator('NonExistentFile');
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Syntax error
*/
public function testReadRowShouldThrowRuntimeException()
{
$fp = fopen('php://memory', 'rb+');
fwrite($fp, 'this,is,');
fseek($fp, 0);

$iterator = new CsvJsonFileIterator($fp);

fclose($fp);
}

/**
* @dataProvider providerIterator
*
Expand Down
18 changes: 18 additions & 0 deletions tests/ObjectStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ public function testSetSecondObject(ObjectStorage $storage) : ObjectStorage
return $storage;
}

public function testGetObjects()
{
$storage = new ObjectStorage();

$a = new \stdClass();
$b = new \stdClass();
$c = new \stdClass();

$objects = [$a, $b, $c];
foreach($objects as $value) {
$storage->set($value, null);
}

$result = $storage->getObjects();

$this->assertSame($objects, $result);
}

/**
* @depends testSetSecondObject
*
Expand Down