This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
<?php
namespace spec\Gaufrette\Adapter;
use AsyncAws\SimpleS3\SimpleS3Client;
use Gaufrette\Adapter\MimeTypeProvider;
use PhpSpec\ObjectBehavior;
class AsyncAwsS3Spec extends ObjectBehavior
{
/**
* @param \AsyncAws\SimpleS3\SimpleS3Client $service
*/
function let(SimpleS3Client $service)
{
$this->beConstructedWith($service, 'bucketName');
}
function it_is_initializable()
{
$this->shouldHaveType('Gaufrette\Adapter\AsyncAwsS3');
}
function it_is_adapter()
{
$this->shouldHaveType('Gaufrette\Adapter');
}
function it_supports_metadata()
{
$this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter');
}
function it_supports_sizecalculator()
{
$this->shouldHaveType('Gaufrette\Adapter\SizeCalculator');
}
function it_provides_mime_type()
{
$this->shouldHaveType(MimeTypeProvider::class);
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace spec\Gaufrette\Adapter;
use Aws\S3\S3Client;
use Gaufrette\Adapter\MimeTypeProvider;
use PhpSpec\ObjectBehavior;
class AwsS3Spec extends ObjectBehavior
{
/**
* @param \Aws\S3\S3Client $service
*/
function let(S3Client $service)
{
$this->beConstructedWith($service, 'bucketName');
}
function it_is_initializable()
{
$this->shouldHaveType('Gaufrette\Adapter\AwsS3');
}
function it_is_adapter()
{
$this->shouldHaveType('Gaufrette\Adapter');
}
function it_supports_metadata()
{
$this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter');
}
function it_supports_sizecalculator()
{
$this->shouldHaveType('Gaufrette\Adapter\SizeCalculator');
}
function it_provides_mime_type()
{
$this->shouldHaveType(MimeTypeProvider::class);
}
}

View File

@@ -0,0 +1,505 @@
<?php
namespace spec\Gaufrette\Adapter;
use PhpSpec\ObjectBehavior;
use WindowsAzure\Blob\Models\Blob;
use WindowsAzure\Common\ServiceException;
class AzureBlobStorage extends ObjectBehavior
{
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
*/
function let($blobProxyFactory)
{
$this->beConstructedWith($blobProxyFactory, 'containerName');
}
function it_should_be_initializable()
{
$this->shouldHaveType('Gaufrette\Adapter\AzureBlobStorage');
$this->shouldHaveType('Gaufrette\Adapter');
$this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter');
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
* @param \WindowsAzure\Blob\Models\GetBlobResult $getBlobResult
*/
function it_should_read_file($blobProxyFactory, $blobProxy, $getBlobResult)
{
$getBlobResult
->getContentStream()
->shouldBeCalled()
//azure blob content is handled as stream so we need to fake it
->willReturn(fopen('data://text/plain,some content', 'r'));
$blobProxy
->getBlob('containerName', 'filename')
->shouldBeCalled()
->willReturn($getBlobResult);
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$this->read('filename')->shouldReturn('some content');
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_return_false_when_cannot_read($blobProxyFactory, $blobProxy)
{
$blobProxy
->getBlob('containerName', 'filename')
->shouldBeCalled()
->willThrow(new ServiceException(500));
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$this->read('filename')->shouldReturn(false);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_not_mask_exception_when_read($blobProxyFactory, $blobProxy)
{
$blobProxy
->getBlob('containerName', 'filename')
->shouldBeCalled()
->willThrow(new \RuntimeException('read'));
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$this->shouldThrow(new \RuntimeException('read'))->duringRead('filename');
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_rename_file($blobProxyFactory, $blobProxy)
{
$blobProxy
->copyBlob('containerName', 'filename2', 'containerName', 'filename1')
->shouldBeCalled();
$blobProxy
->deleteBlob('containerName', 'filename1')
->shouldBeCalled();
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$this->rename('filename1', 'filename2')->shouldReturn(true);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_return_false_when_cannot_rename($blobProxyFactory, $blobProxy)
{
$blobProxy
->copyBlob('containerName', 'filename2', 'containerName', 'filename1')
->shouldBeCalled()
->willThrow(new ServiceException(500));
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$this->rename('filename1', 'filename2')->shouldReturn(false);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_not_mask_exception_when_rename($blobProxyFactory, $blobProxy)
{
$blobProxy
->copyBlob('containerName', 'filename2', 'containerName', 'filename1')
->shouldBeCalled()
->willThrow(new \RuntimeException('rename'));
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$this->shouldThrow(new \RuntimeException('rename'))->duringRename('filename1', 'filename2');
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_write_file($blobProxyFactory, $blobProxy)
{
$blobProxy
->createBlockBlob(
'containerName',
'filename',
'some content',
\Mockery::type('\WindowsAzure\Blob\Models\CreateBlobOptions')
)
->shouldBeCalled();
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$this->write('filename', 'some content')->shouldReturn(12);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_return_false_when_cannot_write($blobProxyFactory, $blobProxy)
{
$blobProxy
->createBlockBlob(
'containerName',
'filename',
'some content',
\Mockery::type('\WindowsAzure\Blob\Models\CreateBlobOptions')
)
->willThrow(new ServiceException(500));
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$this->write('filename', 'some content')->shouldReturn(false);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_not_mask_exception_when_write($blobProxyFactory, $blobProxy)
{
$blobProxy
->createBlockBlob(
'containerName',
'filename',
'some content',
\Mockery::type('\WindowsAzure\Blob\Models\CreateBlobOptions')
)
->willThrow(new \RuntimeException('write'));
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$this->shouldThrow(new \RuntimeException('write'))->duringWrite('filename', 'some content');
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
* @param \WindowsAzure\Blob\Models\GetBlobResult $getBlobResult
*/
function it_should_check_if_file_exists($blobProxyFactory, $blobProxy, $getBlobResult)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->getBlob('containerName', 'filename')
->shouldBeCalled()
->willThrow(new ServiceException(404));
$this->exists('filename')->shouldReturn(false);
$blobProxy
->getBlob('containerName', 'filename2')
->shouldBeCalled()
->willReturn($getBlobResult);
$this->exists('filename2')->shouldReturn(true);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_not_mask_exception_when_check_if_file_exists($blobProxyFactory, $blobProxy)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->getBlob('containerName', 'filename')
->shouldBeCalled()
->willThrow(new \RuntimeException('exists'));
$this->shouldThrow(new \RuntimeException('exists'))->duringExists('filename');
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
* @param \WindowsAzure\Blob\Models\GetBlobPropertiesResult $getBlobPropertiesResult
* @param \WindowsAzure\Blob\Models\BlobProperties $blobProperties
*/
function it_should_get_file_mtime($blobProxyFactory, $blobProxy, $getBlobPropertiesResult, $blobProperties)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->getBlobProperties('containerName', 'filename')
->shouldBeCalled()
->willReturn($getBlobPropertiesResult);
$getBlobPropertiesResult
->getProperties()
->shouldBeCalled()
->willReturn($blobProperties);
$blobProperties
->getLastModified()
->shouldBeCalled()
->willReturn(new \DateTime('1987-12-28 20:00:00'));
$this->mtime('filename')->shouldReturn(strtotime('1987-12-28 20:00:00'));
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_return_false_when_cannot_mtime($blobProxyFactory, $blobProxy)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->getBlobProperties('containerName', 'filename')
->shouldBeCalled()
->willThrow(new ServiceException(500));
$this->mtime('filename')->shouldReturn(false);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_not_mask_exception_when_get_mtime($blobProxyFactory, $blobProxy)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->getBlobProperties('containerName', 'filename')
->shouldBeCalled()
->willThrow(new \RuntimeException('mtime'));
$this->shouldThrow(new \RuntimeException('mtime'))->duringMtime('filename');
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_delete_file($blobProxyFactory, $blobProxy)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->deleteBlob('containerName', 'filename')
->shouldBeCalled();
$this->delete('filename')->shouldReturn(true);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_return_false_when_cannot_delete_file($blobProxyFactory, $blobProxy)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->deleteBlob('containerName', 'filename')
->shouldBeCalled()
->willThrow(new ServiceException(500));
$this->delete('filename')->shouldReturn(false);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_not_mask_exception_when_delete($blobProxyFactory, $blobProxy)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->deleteBlob('containerName', 'filename')
->shouldBeCalled()
->willThrow(new \RuntimeException('delete'));
$this->shouldThrow(new \RuntimeException('delete'))->duringDelete('filename');
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
* @param \WindowsAzure\Blob\Models\ListBlobsResult $listBlobResult
*/
function it_should_get_keys($blobProxyFactory, $blobProxy, $listBlobResult)
{
$fileNames = ['aaa', 'aaa/filename', 'filename1', 'filename2'];
$blobs = [];
foreach ($fileNames as $fileName) {
$blob = new Blob();
$blob->setName($fileName);
$blobs[] = $blob;
}
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->listBlobs('containerName')
->shouldBeCalled()
->willReturn($listBlobResult);
$listBlobResult
->getBlobs()
->shouldBeCalled()
->willReturn($blobs);
$this->keys()->shouldReturn(['aaa', 'aaa/filename', 'filename1', 'filename2']);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_not_mask_exception_when_get_keys($blobProxyFactory, $blobProxy)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->listBlobs('containerName')
->shouldBeCalled()
->willThrow(new \RuntimeException('keys'));
$this->shouldThrow(new \RuntimeException('keys'))->duringKeys();
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_handle_dirs($blobProxyFactory, $blobProxy)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->getBlob('containerName', 'filename')
->shouldNotBeCalled();
$blobProxy
->getBlob('containerName', 'filename/')
->shouldBeCalled()
->willThrow(new ServiceException(404));
$blobProxy
->getBlob('containerName', 'dirname/')
->shouldBeCalled();
$this->isDirectory('filename')->shouldReturn(false);
$this->isDirectory('dirname')->shouldReturn(true);
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_create_container($blobProxyFactory, $blobProxy)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->createContainer('containerName', null)
->shouldBeCalled();
$this->createContainer('containerName');
}
/**
* @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
* @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
*/
function it_should_fail_when_cannot_create_container($blobProxyFactory, $blobProxy)
{
$blobProxyFactory
->create()
->shouldBeCalled()
->willReturn($blobProxy);
$blobProxy
->createContainer('containerName', null)
->shouldBeCalled()
->willThrow(new ServiceException(500));
$this->shouldThrow(new \RuntimeException('Failed to create the configured container "containerName": 0 ().', null))->duringCreateContainer('containerName');
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace spec\Gaufrette\Adapter\AzureBlobStorage;
use PhpSpec\ObjectBehavior;
class BlobProxyFactory extends ObjectBehavior
{
/**
* @param string $connectionString
*/
function let($connectionString)
{
$this->beConstructedWith($connectionString);
}
function it_should_be_initializable()
{
$this->shouldHaveType('Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactory');
$this->shouldHaveType('Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface');
}
}

View File

@@ -0,0 +1,270 @@
<?php
namespace spec\Gaufrette\Adapter;
//hack - mock php built-in functions
require_once 'functions.php';
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class DoctrineDbalSpec extends ObjectBehavior
{
/**
* @param \Doctrine\DBAL\Connection $connection
*/
function let(Connection $connection)
{
$this->beConstructedWith($connection, 'someTableName');
}
function it_is_adapter()
{
$this->shouldHaveType('Gaufrette\Adapter');
}
function it_is_checksum_calculator()
{
$this->shouldHaveType('Gaufrette\Adapter\ChecksumCalculator');
}
function it_does_not_handle_directories()
{
$this->isDirectory('filename')->shouldReturn(false);
}
/**
* @param \Doctrine\DBAL\Connection $connection
*/
function it_checks_if_file_exists(Connection $connection)
{
$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}
$connection
->$method('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(12);
$this->exists('filename')->shouldReturn(true);
$connection
->$method('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(0);
$this->exists('filename')->shouldReturn(false);
}
/**
* @param \Doctrine\DBAL\Connection $connection
*/
function it_writes_to_new_file(Connection $connection)
{
$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}
$connection
->$method('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(false);
$connection
->insert(
'someTableName',
[
'"content"' => 'some content',
'"mtime"' => strtotime('2012-10-10 23:10:10'),
'"checksum"' => '9893532233caff98cd083a116b013c0b',
'"key"' => 'filename',
]
)
->shouldBeCalled();
$this->write('filename', 'some content')->shouldReturn(12);
}
/**
* @param \Doctrine\DBAL\Connection $connection
*/
function it_write_file(Connection $connection)
{
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}
$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->$method('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(true);
$connection
->update(
'someTableName',
[
'"content"' => 'some content',
'"mtime"' => strtotime('2012-10-10 23:10:10'),
'"checksum"' => '9893532233caff98cd083a116b013c0b',
],
[
'"key"' => 'filename',
]
)
->shouldBeCalled();
$this->write('filename', 'some content')->shouldReturn(12);
}
/**
* @param \Doctrine\DBAL\Connection $connection
*/
function it_reads_file(Connection $connection)
{
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}
$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->$method('SELECT "content" FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn('some content');
$this->read('filename')->shouldReturn('some content');
}
/**
* @param \Doctrine\DBAL\Connection $connection
*/
function it_calculates_checksum(Connection $connection)
{
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}
$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->$method('SELECT "checksum" FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(1234);
$this->checksum('filename')->shouldReturn(1234);
}
/**
* @param \Doctrine\DBAL\Connection $connection
*/
function it_gets_mtime(Connection $connection)
{
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}
$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->$method('SELECT "mtime" FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(1234);
$this->mtime('filename')->shouldReturn(1234);
}
/**
* @param \Doctrine\DBAL\Connection $connection
*/
function it_renames_file(Connection $connection)
{
$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->update(
'someTableName',
[
'"key"' => 'newFile',
],
[
'"key"' => 'filename',
]
)
->shouldBeCalled()
->willReturn(1);
$this->rename('filename', 'newFile')->shouldReturn(true);
}
/**
* @param \Doctrine\DBAL\Connection $connection
*/
function it_get_keys(Connection $connection, $result)
{
if (class_exists(Result::class)) {
// dbal 3.x
$result->beADoubleOf(Result::class);
$result->fetchFirstColumn()->willReturn(['filename', 'filename1', 'filename2']);
} else {
// BC layer for dbal 2.x
$result->beADoubleOf(\Doctrine\DBAL\Statement::class);
$result->fetchAll(\PDO::FETCH_COLUMN)->willReturn(['filename', 'filename1', 'filename2']);
}
$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->executeQuery('SELECT "key" FROM "someTableName"')
->willReturn($result);
$this->keys()->shouldReturn(['filename', 'filename1', 'filename2']);
}
/**
* @param \Doctrine\DBAL\Connection $connection
*/
function it_deletes_file(Connection $connection)
{
$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->delete('someTableName', ['"key"' => 'filename'])
->shouldBeCalled()
->willReturn(1);
$this->delete('filename')->shouldReturn(true);
}
}

View File

@@ -0,0 +1,106 @@
<?php
namespace spec\Gaufrette\Adapter;
use PhpSpec\ObjectBehavior;
use League\Flysystem\AdapterInterface;
use League\Flysystem\Config;
class FlysystemSpec extends ObjectBehavior
{
function let(AdapterInterface $adapter, Config $config)
{
$this->beConstructedWith($adapter, $config);
}
function it_is_adapter()
{
$this->shouldImplement('Gaufrette\Adapter');
}
function it_is_list_keys_aware()
{
$this->shouldImplement('Gaufrette\Adapter\ListKeysAware');
}
function it_reads_file(AdapterInterface $adapter)
{
$adapter->read('filename')->willReturn(['contents' => 'Hello.']);
$this->read('filename')->shouldReturn('Hello.');
}
function it_writes_file(AdapterInterface $adapter, Config $config)
{
$adapter->write('filename', 'Hello.', $config)->willReturn([]);
$this->write('filename', 'Hello.')->shouldReturn([]);
}
function it_checks_if_file_exists(AdapterInterface $adapter)
{
$adapter->has('filename')->willReturn(true);
$this->exists('filename')->shouldReturn(true);
}
function it_checks_if_file_exists_when_flysystem_returns_array(AdapterInterface $adapter)
{
$adapter->has('filename')->willReturn(['type' => 'file']);
$this->exists('filename')->shouldReturn(true);
}
function it_fetches_keys(AdapterInterface $adapter)
{
$adapter->listContents()->willReturn([[
'path' => 'folder',
'timestamp' => 1457104978,
'size' => 22,
'type' => 'dir',
]]);
$this->keys()->shouldReturn(['folder']);
}
function it_lists_keys(AdapterInterface $adapter)
{
$adapter->listContents()->willReturn([[
'path' => 'folder',
'timestamp' => 1457104978,
'size' => 22,
'type' => 'dir',
]]);
$this->listKeys()->shouldReturn([
'keys' => [],
'dirs' => ['folder'],
]);
}
function it_fetches_mtime(AdapterInterface $adapter)
{
$adapter->getTimestamp('filename')->willReturn(1457104978);
$this->mtime('filename')->shouldReturn(1457104978);
}
function it_deletes_file(AdapterInterface $adapter)
{
$adapter->delete('filename')->willReturn(true);
$this->delete('filename')->shouldReturn(true);
}
function it_renames_file(AdapterInterface $adapter)
{
$adapter->rename('oldfilename', 'newfilename')->willReturn(true);
$this->rename('oldfilename', 'newfilename')->shouldReturn(true);
}
function it_does_not_support_is_directory(AdapterInterface $adapter)
{
$this->shouldThrow('Gaufrette\Exception\UnsupportedAdapterMethodException')->duringisDirectory('folder');
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace spec\Gaufrette\Adapter;
use PhpSpec\ObjectBehavior;
class GoogleCloudStorageSpec extends ObjectBehavior
{
function let(\Google_Service_Storage $service)
{
$this->beConstructedWith($service, 'bucketName');
}
function it_is_adapter()
{
$this->shouldHaveType('Gaufrette\Adapter');
}
function it_supports_metadata()
{
$this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter');
}
function it_is_list_keys_aware()
{
$this->shouldHaveType('Gaufrette\Adapter\ListKeysAware');
}
}

View File

@@ -0,0 +1,169 @@
<?php
namespace spec\Gaufrette\Adapter;
use MongoDB\BSON\UTCDateTime;
use MongoDB\GridFS\Bucket;
use MongoDB\GridFS\Exception\FileNotFoundException;
use MongoDB\Model\BSONDocument;
use PhpSpec\ObjectBehavior;
class GridFSSpec extends ObjectBehavior
{
private $resources = [];
function let(Bucket $bucket)
{
$this->beConstructedWith($bucket);
}
function letGo()
{
array_map(function ($res) {
if (is_resource($res)) {
@fclose($res);
}
}, $this->resources);
}
function it_is_adapter()
{
$this->shouldHaveType('Gaufrette\Adapter');
}
function it_is_checksum_calculator()
{
$this->shouldHaveType('Gaufrette\Adapter\ChecksumCalculator');
}
function it_supports_metadata()
{
$this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter');
}
function it_supports_native_list_keys()
{
$this->shouldHaveType('Gaufrette\Adapter\ListKeysAware');
}
function it_reads_file($bucket)
{
$this->resources[] = $readable = fopen('php://memory', 'rw');
fwrite($readable, 'some content');
fseek($readable, 0);
$bucket
->openDownloadStreamByName('filename')
->shouldBeCalled()
->willReturn($readable)
;
$this->read('filename')->shouldReturn('some content');
}
function it_does_not_fail_when_cannot_read($bucket)
{
$bucket->openDownloadStreamByName('filename')->willThrow(FileNotFoundException::class);
$this->read('filename')->shouldReturn(false);
}
function it_checks_if_file_exists($bucket, BSONDocument $file)
{
$bucket
->findOne(['filename' => 'filename'])
->willReturn($file)
;
$bucket
->findOne(['filename' => 'filename2'])
->willReturn(null)
;
$this->exists('filename')->shouldReturn(true);
$this->exists('filename2')->shouldReturn(false);
}
function it_deletes_file($bucket)
{
$bucket
->findOne(['filename' => 'filename'], ['projection' => ['_id' => 1]])
->willReturn($file = new BSONDocument(['_id' => 123]))
;
$bucket->delete(123)->shouldBeCalled();
$this->delete('filename')->shouldReturn(true);
}
function it_does_not_delete_file($bucket)
{
$bucket->findOne(['filename' => 'filename'], ['projection' => ['_id' => 1]])->willReturn(null);
$this->delete('filename')->shouldReturn(false);
}
function it_writes_file($bucket)
{
$this->resources[] = $writable = fopen('php://memory', 'rw');
$bucket
->openUploadStream('filename', ['metadata' => ['someother' => 'metadata']])
->willReturn($writable)
;
$this->setMetadata('filename', ['someother' => 'metadata']);
$this
->write('filename', 'some content')
->shouldReturn(12)
;
}
function it_renames_file($bucket)
{
$this->resources[] = $writable = fopen('php://memory', 'rw');
$this->resources[] = $readable = fopen('php://memory', 'rw');
fwrite($readable, 'some content');
fseek($readable, 0);
$bucket->openUploadStream('otherFilename', ['metadata' => ['some' => 'metadata']])->willReturn($writable);
$bucket->downloadToStreamByName('filename', $writable)->shouldBeCalled();
$bucket
->findOne(['filename' => 'filename'], ['projection' => ['_id' => 1]])
->willReturn($toDelete = new BSONDocument(['_id' => 1234]))
;
$bucket->delete(1234)->shouldBeCalled();
$this->setMetadata('filename', ['some' => 'metadata']);
$this->rename('filename', 'otherFilename')->shouldReturn(true);
}
function it_fetches_keys($bucket)
{
$bucket
->find([], ['projection' => ['filename' => 1]])
->willReturn([new BSONDocument(['filename' => 'filename']), new BSONDocument(['filename' => 'otherFilename'])])
;
$this->keys()->shouldReturn(['filename', 'otherFilename']);
}
function it_fetches_mtime($bucket)
{
$bucket
->findOne(['filename' => 'filename'], ['projection' => ['uploadDate' => 1]])
->willReturn(new BSONDocument(['uploadDate' => new UTCDateTime(12345000)]))
;
$this->mtime('filename')->shouldReturn(12345);
}
function it_calculates_checksum($bucket)
{
$bucket
->findOne(['filename' => 'filename'], ['projection' => ['md5' => 1]])
->willReturn(new BSONDocument(['md5' => 'md5123']))
;
$this->checksum('filename')->shouldReturn('md5123');
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace spec\Gaufrette\Adapter;
use PhpSpec\ObjectBehavior;
class InMemorySpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith([
'filename' => ['mtime' => 12345, 'content' => 'content'],
'filename2' => 'other content',
]);
}
function it_is_adapter()
{
$this->shouldHaveType('Gaufrette\Adapter');
}
function it_is_a_mime_type_provider()
{
$this->shouldHaveType('Gaufrette\Adapter\MimeTypeProvider');
}
function it_gets_the_file_mime_type()
{
$this->mimeType('filename')->shouldReturn('text/plain');
}
function it_reads_file()
{
$this->read('filename')->shouldReturn('content');
}
function it_writes_file()
{
$this->write('filename', 'some content')->shouldReturn(12);
}
function it_renames_file()
{
$this->rename('filename', 'aaa/filename2')->shouldReturn(true);
$this->exists('filename')->shouldReturn(false);
$this->exists('aaa/filename2')->shouldReturn(true);
}
function it_checks_if_file_exists()
{
$this->exists('filename')->shouldReturn(true);
$this->exists('filenameTest')->shouldReturn(false);
}
function it_fetches_keys()
{
$this->keys()->shouldReturn(['filename', 'filename2']);
}
function it_fetches_mtime()
{
$this->mtime('filename')->shouldReturn(12345);
}
function it_deletes_file()
{
$this->delete('filename')->shouldReturn(true);
$this->exists('filename')->shouldReturn(false);
}
function it_does_not_handle_dirs()
{
$this->isDirectory('filename')->shouldReturn(false);
$this->isDirectory('filename2')->shouldReturn(false);
}
}

View File

@@ -0,0 +1,166 @@
<?php
namespace spec\Gaufrette\Adapter;
use org\bovigo\vfs\vfsStream;
use PhpSpec\ObjectBehavior;
class LocalSpec extends ObjectBehavior
{
function let()
{
vfsStream::setup('test');
vfsStream::copyFromFileSystem(__DIR__ . '/MockFilesystem');
$this->beConstructedWith(vfsStream::url('test'));
}
function it_is_adapter()
{
$this->shouldHaveType('Gaufrette\Adapter');
}
function it_is_checksum_calculator()
{
$this->shouldHaveType('Gaufrette\Adapter\ChecksumCalculator');
}
function it_is_a_mime_type_provider()
{
$this->shouldHaveType('Gaufrette\Adapter\MimeTypeProvider');
}
function it_gets_the_file_mime_type()
{
$this->mimeType('filename')->shouldReturn('text/plain');
}
function it_is_stream_factory()
{
$this->shouldHaveType('Gaufrette\Adapter\StreamFactory');
}
function it_reads_file()
{
$this->read('filename')->shouldReturn("content\n");
}
function it_writes_file()
{
$this->write('filename', 'some content')->shouldReturn(12);
}
function it_renames_file()
{
$this->rename('filename', 'aaa/filename2')->shouldReturn(true);
}
function it_checks_if_file_exists()
{
$this->exists('filename')->shouldReturn(true);
$this->exists('filename1')->shouldReturn(false);
}
function it_fetches_keys()
{
$expectedKeys = ['filename', 'dir', 'dir/file'];
sort($expectedKeys);
$this->keys()->shouldReturn($expectedKeys);
}
function it_fetches_mtime()
{
$mtime = filemtime(vfsStream::url('test/filename'));
$this->mtime('filename')->shouldReturn($mtime);
}
function it_deletes_file()
{
$this->delete('filename')->shouldReturn(true);
$this->delete('filename1')->shouldReturn(false);
}
function it_deletes_dir()
{
$this->delete('dir')->shouldReturn(true);
}
function it_checks_if_given_key_is_directory()
{
$this->isDirectory('dir')->shouldReturn(true);
$this->isDirectory('filename')->shouldReturn(false);
}
function it_creates_local_stream()
{
$this->createStream('filename')->shouldReturnAnInstanceOf('Gaufrette\Stream\Local');
}
function it_does_not_allow_to_read_path_above_main_file_directory()
{
$this
->shouldThrow(new \OutOfBoundsException(sprintf('The path "%s" is out of the filesystem.', vfsStream::url('filename'))))
->duringRead('../filename')
;
$this
->shouldThrow(new \OutOfBoundsException(sprintf('The path "%s" is out of the filesystem.', vfsStream::url('filename'))))
->duringExists('../filename')
;
}
function it_fails_when_directory_does_not_exists()
{
$this->beConstructedWith(vfsStream::url('other'));
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringRead('filename')
;
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringWrite('filename', 'some content')
;
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringRename('filename', 'otherFilename')
;
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringExists('filename')
;
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringKeys()
;
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringMtime('filename')
;
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringDelete('filename')
;
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringIsDirectory('filename')
;
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringCreateStream('filename')
;
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringChecksum('filename')
;
$this
->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
->duringMimeType('filename')
;
}
function it_creates_directory_when_does_not_exists()
{
$this->beConstructedWith(vfsStream::url('test/other'), true);
$this->isDirectory('/')->shouldReturn(true);
}
}

View File

@@ -0,0 +1 @@
content

View File

@@ -0,0 +1,157 @@
<?php
namespace spec\Gaufrette\Adapter;
if (!defined('NET_SFTP_TYPE_REGULAR')) {
define('NET_SFTP_TYPE_REGULAR', 1);
}
if (!defined('NET_SFTP_TYPE_DIRECTORY')) {
define('NET_SFTP_TYPE_DIRECTORY', 2);
}
use Gaufrette\Filesystem;
use phpseclib\Net\SFTP as Base;
use PhpSpec\ObjectBehavior;
class SFTP extends Base
{
public function __construct()
{
}
}
class PhpseclibSftpSpec extends ObjectBehavior
{
/**
* @param \spec\Gaufrette\Adapter\SFTP $sftp
*/
function let(SFTP $sftp)
{
$this->beConstructedWith($sftp, '/home/l3l0', false, 'l3lo', 'password');
}
function it_is_adapter()
{
$this->shouldHaveType('Gaufrette\Adapter');
}
function it_is_file_factory()
{
$this->shouldHaveType('Gaufrette\Adapter\FileFactory');
}
function it_supports_native_list_keys()
{
$this->shouldHaveType('Gaufrette\Adapter\ListKeysAware');
}
/**
* @param \spec\Gaufrette\Adapter\SFTP $sftp
*/
function it_fetches_keys(SFTP $sftp)
{
$sftp
->file_exists('/home/l3l0/')
->willReturn(true);
$sftp
->rawlist('/home/l3l0/')
->willReturn([
'filename' => ['type' => NET_SFTP_TYPE_REGULAR],
'filename1' => ['type' => NET_SFTP_TYPE_REGULAR],
'aaa' => ['type' => NET_SFTP_TYPE_DIRECTORY],
]);
$sftp
->file_exists('/home/l3l0/aaa')
->willReturn(true);
$sftp
->rawlist('/home/l3l0/aaa')
->willReturn([
'filename' => ['type' => NET_SFTP_TYPE_REGULAR],
]);
$this->keys()->shouldReturn(['filename', 'filename1', 'aaa', 'aaa/filename']);
}
/**
* @param \spec\Gaufrette\Adapter\SFTP $sftp
*/
function it_reads_file(SFTP $sftp)
{
$sftp->get('/home/l3l0/filename')->willReturn('some content');
$this->read('filename')->shouldReturn('some content');
}
/**
* @param \spec\Gaufrette\Adapter\SFTP $sftp
*/
function it_creates_and_writes_file(SFTP $sftp)
{
$sftp->pwd()->willReturn('/home/l3l0');
$sftp->chdir('/home/l3l0')->willReturn(true);
$sftp->put('/home/l3l0/filename', 'some content')->willReturn(true);
$sftp->size('/home/l3l0/filename')->willReturn(12);
$this->write('filename', 'some content')->shouldReturn(12);
}
/**
* @param \spec\Gaufrette\Adapter\SFTP $sftp
*/
function it_renames_file(SFTP $sftp)
{
$sftp->pwd()->willReturn('/home/l3l0');
$sftp->chdir('/home/l3l0')->willReturn(true);
$sftp
->rename('/home/l3l0/filename', '/home/l3l0/filename1')
->willReturn(true)
;
$this->rename('filename', 'filename1')->shouldReturn(true);
}
/**
* @param \spec\Gaufrette\Adapter\SFTP $sftp
*/
function it_should_check_if_file_exists(SFTP $sftp)
{
$sftp->pwd()->willReturn('/home/l3l0');
$sftp->chdir('/home/l3l0')->willReturn(true);
$sftp->stat('/home/l3l0/filename')->willReturn([
'name' => '/home/l3l0/filename',
]);
$sftp->stat('/home/l3l0/filename1')->willReturn(false);
$this->exists('filename')->shouldReturn(true);
$this->exists('filename1')->shouldReturn(false);
}
/**
* @param \spec\Gaufrette\Adapter\SFTP $sftp
*/
function it_should_check_is_directory(SFTP $sftp)
{
$sftp->pwd()->willReturn('/home/l3l0');
$sftp->chdir('/home/l3l0')->willReturn(true);
$sftp->chdir('/home/l3l0/aaa')->willReturn(true);
$sftp->chdir('/home/l3l0/filename')->willReturn(false);
$this->isDirectory('aaa')->shouldReturn(true);
$this->isDirectory('filename')->shouldReturn(false);
}
/**
* @param \spec\Gaufrette\Adapter\SFTP $sftp
* @param \Gaufrette\Filesystem $filesystem
*/
function it_should_create_file(SFTP $sftp, Filesystem $filesystem)
{
$sftp->stat('/home/l3l0/filename')->willReturn([
'name' => '/home/l3l0/filename',
'size' => '30',
]);
$this->createFile('filename', $filesystem)->beAnInstanceOf('Gaufrette\File');
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace spec\Gaufrette\Adapter;
use org\bovigo\vfs\vfsStream;
use PhpSpec\ObjectBehavior;
class SafeLocalSpec extends ObjectBehavior
{
function let()
{
vfsStream::setup('test');
vfsStream::copyFromFileSystem(__DIR__ . '/MockFilesystem');
$this->beConstructedWith(vfsStream::url('test'));
}
function it_is_local_adapter()
{
$this->shouldHaveType('Gaufrette\Adapter\Local');
}
function it_computes_path_using_base64()
{
rename(vfsStream::url('test/filename'), vfsStream::url('test/' . base64_encode('filename')));
$this->read('filename')->shouldReturn("content\n");
}
function it_computes_key_back_using_base64()
{
$this->keys()->shouldReturn([base64_decode('dir'), base64_decode('dir/file'), base64_decode('filename')]);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace spec\Gaufrette\Adapter;
use PhpSpec\ObjectBehavior;
class ZipSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('somefile');
}
function it_is_adapter()
{
$this->shouldHaveType('Gaufrette\Adapter');
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Gaufrette\Adapter;
global $createdDirectory;
function time()
{
return \strtotime('2012-10-10 23:10:10');
}
function file_exists($path)
{
//fake it for ssh+ssl: protocol for SFTP testing, otherwise delegate to global
if (strpos($path, 'ssh+ssl:') === 0) {
return in_array($path, ['/home/l3l0/filename', '/home/somedir/filename', 'ssh+ssl://localhost/home/l3l0/filename']) ? true : false;
}
return \file_exists($path);
}
function extension_loaded($name)
{
global $extensionLoaded;
if (is_null($extensionLoaded)) {
return true;
}
return $extensionLoaded;
}
function opendir($url)
{
return true;
}
function apc_fetch($path)
{
return sprintf('%s content', $path);
}
function apc_store($path, $content, $ttl)
{
if ('prefix-apc-test/invalid' === $path) {
return false;
}
return sprintf('%s content', $path);
}
function apc_delete($path)
{
if ('prefix-apc-test/invalid' === $path) {
return false;
}
return true;
}
function apc_exists($path)
{
if ('prefix-apc-test/invalid' === $path) {
return false;
}
return true;
}

View File

@@ -0,0 +1,191 @@
<?php
namespace spec\Gaufrette;
use Gaufrette\Filesystem;
use PhpSpec\ObjectBehavior;
interface MetadataAdapter extends \Gaufrette\Adapter,
\Gaufrette\Adapter\MetadataSupporter
{
}
class FileSpec extends ObjectBehavior
{
/**
* @param \Gaufrette\Filesystem $filesystem
*/
function let(Filesystem $filesystem)
{
$this->beConstructedWith('filename', $filesystem);
}
function it_is_initializable()
{
$this->shouldHaveType('Gaufrette\File');
}
function it_gives_access_to_key()
{
$this->getKey()->shouldReturn('filename');
}
/**
* @param \Gaufrette\Filesystem $filesystem
*/
function it_gets_content(Filesystem $filesystem)
{
$filesystem->read('filename')->shouldBeCalled()->willReturn('Some content');
$this->getContent()->shouldReturn('Some content');
}
/**
* @param \Gaufrette\Filesystem $filesystem
*/
function it_gets_mtime(Filesystem $filesystem)
{
$filesystem->mtime('filename')->shouldBeCalled()->willReturn(1358797854);
$this->getMtime()->shouldReturn(1358797854);
}
/**
* @param \Gaufrette\Filesystem $filesystem
* @param \spec\Gaufrette\MetadataAdapter $adapter
*/
function it_pass_metadata_when_write_content(Filesystem $filesystem, MetadataAdapter $adapter)
{
$metadata = ['id' => '123'];
$adapter->setMetadata('filename', $metadata)->shouldBeCalled();
$filesystem->write('filename', 'some content', true)->willReturn(12);
$filesystem->getAdapter()->willReturn($adapter);
$this->setContent('some content', $metadata);
}
/**
* @param \Gaufrette\Filesystem $filesystem
* @param \spec\Gaufrette\MetadataAdapter $adapter
*/
function it_pass_metadata_when_read_content(Filesystem $filesystem, MetadataAdapter $adapter)
{
$metadata = ['id' => '123'];
$adapter->setMetadata('filename', $metadata)->shouldBeCalled();
$filesystem->read('filename')->willReturn('some content');
$filesystem->getAdapter()->willReturn($adapter);
$this->getContent($metadata);
}
/**
* @param \Gaufrette\Filesystem $filesystem
* @param \spec\Gaufrette\MetadataAdapter $adapter
*/
function it_pass_metadata_when_delete_content(Filesystem $filesystem, MetadataAdapter $adapter)
{
$metadata = ['id' => '123'];
$adapter->setMetadata('filename', $metadata)->shouldBeCalled();
$filesystem->delete('filename')->willReturn(true);
$filesystem->getAdapter()->willReturn($adapter);
$this->delete($metadata);
}
/**
* @param \Gaufrette\Filesystem $filesystem
* @param \spec\Gaufrette\MetadataAdapter $adapter
*/
function it_sets_content_of_file(Filesystem $filesystem, MetadataAdapter $adapter)
{
$adapter->setMetadata('filename', [])->shouldNotBeCalled();
$filesystem->getAdapter()->willReturn($adapter);
$filesystem->write('filename', 'some content', true)->shouldBeCalled()->willReturn(21);
$this->setContent('some content')->shouldReturn(21);
$this->getContent('filename')->shouldReturn('some content');
}
function it_sets_key_as_name_by_default()
{
$this->getName()->shouldReturn('filename');
}
function it_sets_name()
{
$this->setName('name');
$this->getName()->shouldReturn('name');
}
/**
* @param \Gaufrette\Filesystem $filesystem
*/
function it_sets_size_for_new_file(Filesystem $filesystem)
{
$filesystem->write('filename', 'some content', true)->shouldBeCalled()->willReturn(21);
$this->setContent('some content');
$this->getSize()->shouldReturn(21);
}
/**
* @param \Gaufrette\Filesystem $filesystem
*/
function it_calculates_size_from_filesystem(Filesystem $filesystem)
{
$filesystem->size('filename')->shouldBeCalled()->willReturn(12);
$this->getSize()->shouldReturn(12);
}
/**
* @param \Gaufrette\Filesystem $filesystem
*/
function it_allows_to_set_size(Filesystem $filesystem)
{
$filesystem->read('filename')->shouldNotBeCalled();
$this->setSize(21);
$this->getSize()->shouldReturn(21);
}
/**
* @param \Gaufrette\Filesystem $filesystem
*/
function it_gets_zero_size_when_file_not_found(Filesystem $filesystem)
{
$filesystem->size('filename')->willThrow(new \Gaufrette\Exception\FileNotFound('filename'));
$this->getSize()->shouldReturn(0);
}
/**
* @param \Gaufrette\Filesystem $filesystem
*/
function it_check_if_file_with_key_exists_in_filesystem(Filesystem $filesystem)
{
$filesystem->has('filename')->willReturn(true);
$this->exists()->shouldReturn(true);
$filesystem->has('filename')->willReturn(false);
$this->exists()->shouldReturn(false);
}
/**
* @param \Gaufrette\Filesystem $filesystem
*/
function it_deletes_file_from_filesystem(Filesystem $filesystem)
{
$filesystem->delete('filename')->shouldBeCalled()->willReturn(true);
$this->delete()->shouldReturn(true);
}
/**
* @param \Gaufrette\Filesystem $filesystem
*/
function it_renames_file_from_filesystem(Filesystem $filesystem)
{
$filesystem->rename('filename', 'newname')->shouldBeCalled();
$this->rename('newname');
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace spec\Gaufrette;
use Gaufrette\Filesystem;
use PhpSpec\ObjectBehavior;
class FilesystemMapSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Gaufrette\FilesystemMap');
}
/**
* @param Gaufrette\Filesystem $filesystem
*/
function it_checks_if_has_mapped_filesystem(Filesystem $filesystem)
{
$this->set('some', $filesystem);
$this->has('some')->shouldReturn(true);
$this->has('other')->shouldReturn(false);
}
/**
* @param Gaufrette\Filesystem $filesystem
*/
function it_sets_mapped_filesystem(Filesystem $filesystem)
{
$this->set('some', $filesystem);
$this->get('some')->shouldReturn($filesystem);
}
function it_fails_when_get_filesystem_which_was_not_mapped()
{
$this
->shouldThrow(new \InvalidArgumentException('There is no filesystem defined having "some" name.'))
->duringGet('some')
;
}
/**
* @param Gaufrette\Filesystem $filesystem
*/
function it_removes_mapped_filesystem(Filesystem $filesystem)
{
$this->set('some', $filesystem);
$this->remove('some');
$this->has('some')->shouldReturn(false);
}
function it_fails_when_try_to_remove_filesystem_which_was_not_mapped()
{
$this
->shouldThrow(new \InvalidArgumentException('Cannot remove the "some" filesystem as it is not defined.'))
->duringRemove('some')
;
}
/**
* @param Gaufrette\Filesystem $filesystem
*/
function it_removes_all_filesystems(Filesystem $filesystem)
{
$this->set('some', $filesystem);
$this->set('other', $filesystem);
$this->clear();
$this->has('some')->shouldReturn(false);
$this->has('other')->shouldReturn(false);
$this->all()->shouldReturn([]);
}
}

View File

@@ -0,0 +1,435 @@
<?php
namespace spec\Gaufrette;
use Gaufrette\Adapter;
use Gaufrette\File;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
interface ExtendedAdapter extends \Gaufrette\Adapter,
\Gaufrette\Adapter\FileFactory,
\Gaufrette\Adapter\StreamFactory,
\Gaufrette\Adapter\ChecksumCalculator,
\Gaufrette\Adapter\MetadataSupporter,
\Gaufrette\Adapter\MimeTypeProvider
{
}
class FilesystemSpec extends ObjectBehavior
{
/**
* @param \Gaufrette\Adapter $adapter
*/
function let(Adapter $adapter)
{
$this->beConstructedWith($adapter);
}
function it_is_initializable()
{
$this->shouldBeAnInstanceOf('Gaufrette\Filesystem');
$this->shouldBeAnInstanceOf('Gaufrette\FilesystemInterface');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_gives_access_to_adapter(Adapter $adapter)
{
$this->getAdapter()->shouldBe($adapter);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_check_if_file_exists_using_adapter(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->exists('otherFilename')->willReturn(false);
$this->has('filename')->shouldReturn(true);
$this->has('otherFilename')->shouldReturn(false);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_renames_file(Adapter $adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(true);
$adapter->exists('otherFilename')->shouldBeCalled()->willReturn(false);
$adapter->rename('filename', 'otherFilename')->shouldBeCalled()->willReturn(true);
$this->rename('filename', 'otherFilename')->shouldReturn(true);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_fails_when_renamed_source_file_does_not_exist(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringRename('filename', 'otherFilename')
;
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_fails_when_renamed_target_file_exists(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->exists('otherFilename')->willReturn(true);
$this
->shouldThrow(new \Gaufrette\Exception\UnexpectedFile('otherFilename'))
->duringRename('filename', 'otherFilename')
;
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_fails_when_rename_is_not_successful(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->exists('otherFilename')->willReturn(false);
$adapter->rename('filename', 'otherFilename')->willReturn(false);
$this
->shouldThrow(new \RuntimeException('Could not rename the "filename" key to "otherFilename".'))
->duringRename('filename', 'otherFilename')
;
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_creates_file_object_for_key(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(true);
$this->get('filename')->shouldBeAnInstanceOf('Gaufrette\File');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_does_not_get_file_object_when_file_with_key_does_not_exist(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringGet('filename')
;
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_gets_file_object_when_file_does_not_exist_but_can_be_created(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(false);
$this->get('filename', true)->shouldBeAnInstanceOf('Gaufrette\File');
}
/**
* @param \spec\Gaufrette\ExtendedAdapter $extendedAdapter
* @param \Gaufrette\File $file
*/
function it_delegates_file_creation_to_adapter_when_adapter_is_file_factory(ExtendedAdapter $extendedAdapter, File $file)
{
$this->beConstructedWith($extendedAdapter);
$extendedAdapter->exists('filename')->willReturn(true);
$extendedAdapter->createFile('filename', $this)->willReturn($file);
$this->get('filename')->shouldBe($file);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_writes_content_to_new_file(Adapter $adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(false);
$adapter->write('filename', 'some content to write')->shouldBeCalled()->willReturn(21);
$this->write('filename', 'some content to write')->shouldReturn(21);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_updates_content_of_file(Adapter $adapter)
{
$adapter->write('filename', 'some content to write')->shouldBeCalled()->willReturn(21);
$this->write('filename', 'some content to write', true)->shouldReturn(21);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_does_not_update_content_of_file_when_file_cannot_be_overwriten(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->write('filename', 'some content to write')->shouldNotBeCalled();
$this
->shouldThrow(new \Gaufrette\Exception\FileAlreadyExists('filename'))
->duringWrite('filename', 'some content to write')
;
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_fails_when_write_is_not_successful(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(false);
$adapter->write('filename', 'some content to write')->shouldBeCalled()->willReturn(false);
$this
->shouldThrow(new \RuntimeException('Could not write the "filename" key content.'))
->duringWrite('filename', 'some content to write')
;
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_read_file(Adapter $adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(true);
$adapter->read('filename')->shouldBeCalled()->willReturn('Some content');
$this->read('filename')->shouldReturn('Some content');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_does_not_read_file_which_does_not_exist(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringRead('filename');
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_fails_when_read_is_not_successful(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->read('filename')->willReturn(false);
$this
->shouldThrow(new \RuntimeException('Could not read the "filename" key content.'))
->duringRead('filename')
;
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_deletes_file(Adapter $adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(true);
$adapter->delete('filename')->shouldBeCalled()->willReturn(true);
$this->delete('filename')->shouldReturn(true);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_does_not_delete_file_which_does_not_exist(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringDelete('filename')
;
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_fails_when_delete_is_not_successful(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->delete('filename')->willReturn(false);
$this
->shouldThrow(new \RuntimeException('Could not remove the "filename" key.'))
->duringDelete('filename')
;
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_should_get_all_keys(Adapter $adapter)
{
$keys = ['filename', 'filename1', 'filename2'];
$adapter->keys()->willReturn($keys);
$this->keys()->shouldReturn($keys);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_match_listed_keys_using_specified_pattern(Adapter $adapter)
{
$keys = ['filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey'];
$adapter->keys()->willReturn($keys);
$adapter->isDirectory(Argument::any())->willReturn(false);
$this->listKeys()->shouldReturn(
[
'keys' => ['filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey'],
'dirs' => [],
]
);
$this->listKeys('filename')->shouldReturn(
[
'keys' => ['filename', 'filename1', 'filename2'],
'dirs' => [],
]
);
$this->listKeys('Key')->shouldReturn(
[
'keys' => ['KeyTest'],
'dirs' => [],
]
);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_listing_directories_using_adapter_is_directory_method(Adapter $adapter)
{
$keys = ['filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey'];
$adapter->keys()->willReturn($keys);
$adapter->isDirectory('filename')->willReturn(false);
$adapter->isDirectory('filename2')->willReturn(false);
$adapter->isDirectory('KeyTest')->willReturn(false);
$adapter->isDirectory('testkey')->willReturn(false);
$adapter->isDirectory('filename1')->willReturn(true);
$adapter->isDirectory('testKey')->willReturn(true);
$this->listKeys()->shouldReturn(
[
'keys' => ['filename', 'filename2', 'KeyTest', 'testkey'],
'dirs' => ['filename1', 'testKey'],
]
);
$this->listKeys('filename')->shouldReturn(
[
'keys' => ['filename', 'filename2'],
'dirs' => ['filename1'],
]
);
$this->listKeys('Key')->shouldReturn(
[
'keys' => ['KeyTest'],
'dirs' => [],
]
);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_gets_mtime_of_file_using_adapter(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(true);
$adapter->mtime('filename')->willReturn(1234567);
$this->mtime('filename')->shouldReturn(1234567);
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_does_not_get_mtime_of_file_which_does_not_exist(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringMtime('filename')
;
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_calculates_file_checksum(Adapter $adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(true);
$adapter->read('filename')->willReturn('some content');
$this->checksum('filename')->shouldReturn(md5('some content'));
}
/**
* @param \Gaufrette\Adapter $adapter
*/
function it_does_not_calculate_checksum_of_file_which_does_not_exist(Adapter $adapter)
{
$adapter->exists('filename')->shouldBeCalled()->willReturn(false);
$this
->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
->duringChecksum('filename');
}
/**
* @param \spec\Gaufrette\ExtendedAdapter $extendedAdapter
*/
function it_delegates_checksum_calculation_to_adapter_when_adapter_is_checksum_calculator(ExtendedAdapter $extendedAdapter)
{
$this->beConstructedWith($extendedAdapter);
$extendedAdapter->exists('filename')->shouldBeCalled()->willReturn(true);
$extendedAdapter->read('filename')->shouldNotBeCalled();
$extendedAdapter->checksum('filename')->shouldBeCalled()->willReturn(12);
$this->checksum('filename')->shouldReturn(12);
}
/**
* @param \spec\Gaufrette\ExtendedAdapter $extendedAdapter
*/
function it_delegates_mime_type_resolution_to_adapter_when_adapter_is_mime_type_provider(ExtendedAdapter $extendedAdapter)
{
$this->beConstructedWith($extendedAdapter);
$extendedAdapter->exists('filename')->willReturn(true);
$extendedAdapter->mimeType('filename')->willReturn('text/plain');
$this->mimeType('filename')->shouldReturn('text/plain');
}
function it_cannot_resolve_mime_type_if_the_adapter_cannot_provide_it(Adapter $adapter)
{
$adapter->exists('filename')->willReturn(true);
$this
->shouldThrow(new \LogicException(sprintf('Adapter "%s" cannot provide MIME type', get_class($adapter->getWrappedObject()))))
->duringMimeType('filename');
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace spec\Gaufrette\Stream;
use PhpSpec\ObjectBehavior;
use Gaufrette\StreamMode;
use org\bovigo\vfs\vfsStream;
class LocalSpec extends ObjectBehavior
{
function it_throws_runtime_exception_when_file_doesnt_exists()
{
$this->beConstructedWith(vfsStream::url('other'));
$this->shouldThrow('\RuntimeException')->duringOpen(new StreamMode('r'));
}
function it_throws_runtime_exception_when_file_doesnt_exists_and_custom_error_handler_specified()
{
$custom_error_handler = function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
};
set_error_handler($custom_error_handler);
$this->beConstructedWith(vfsStream::url('other'));
$this->shouldThrow('\RuntimeException')->duringOpen(new StreamMode('r'));
restore_error_handler();
}
}

View File

@@ -0,0 +1,142 @@
<?php
namespace spec\Gaufrette;
use PhpSpec\ObjectBehavior;
class StreamModeSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->beConstructedWith('r');
$this->shouldHaveType('Gaufrette\StreamMode');
}
function it_gives_access_to_mode()
{
$this->beConstructedWith('r+');
$this->getMode()->shouldReturn('r+');
}
function it_allows_write_only()
{
$this->beConstructedWith('w');
$this->allowsWrite()->shouldReturn(true);
$this->allowsRead()->shouldReturn(false);
}
function it_allows_write_and_read()
{
$this->beConstructedWith('w+');
$this->allowsWrite()->shouldReturn(true);
$this->allowsRead()->shouldReturn(true);
}
function it_allows_read_only()
{
$this->beConstructedWith('r');
$this->allowsWrite()->shouldReturn(false);
$this->allowsRead()->shouldReturn(true);
}
function it_allows_to_existing_file_opening()
{
$this->beConstructedWith('r');
$this->allowsExistingFileOpening()->shouldReturn(true);
}
function it_does_not_allow_to_existing_file_opening()
{
$this->beConstructedWith('x');
$this->allowsExistingFileOpening()->shouldReturn(false);
}
function it_allows_new_file_opening()
{
$this->beConstructedWith('w');
$this->allowsNewFileOpening()->shouldReturn(true);
}
function it_does_not_allow_new_file_opening()
{
$this->beConstructedWith('r');
$this->allowsNewFileOpening()->shouldReturn(false);
}
function it_implies_existing_content_deletion()
{
$this->beConstructedWith('w+');
$this->allowsNewFileOpening()->shouldReturn(true);
}
function it_does_not_implies_existing_content_deletion()
{
$this->beConstructedWith('r+');
$this->allowsNewFileOpening()->shouldReturn(false);
}
function it_implies_positioning_cursor_at_the_beginning()
{
$this->beConstructedWith('r+');
$this->impliesPositioningCursorAtTheBeginning()->shouldReturn(true);
}
function it_does_no_implies_positioning_cursor_at_the_beginning()
{
$this->beConstructedWith('a');
$this->impliesPositioningCursorAtTheBeginning()->shouldReturn(false);
}
function it_implies_positioning_cursor_at_the_end()
{
$this->beConstructedWith('a');
$this->impliesPositioningCursorAtTheEnd()->shouldReturn(true);
}
function it_does_no_implies_positioning_cursor_at_the_end()
{
$this->beConstructedWith('w');
$this->impliesPositioningCursorAtTheEnd()->shouldReturn(false);
}
function it_should_be_binary()
{
$this->beConstructedWith('wb+');
$this->isBinary()->shouldReturn(true);
}
function it_should_not_be_binary()
{
$this->beConstructedWith('w+');
$this->isBinary()->shouldReturn(false);
}
function it_should_not_be_text()
{
$this->beConstructedWith('wb+');
$this->isText()->shouldReturn(false);
}
function it_should_be_text()
{
$this->beConstructedWith('w+');
$this->isText()->shouldReturn(true);
}
}

View File

@@ -0,0 +1,299 @@
<?php
namespace spec\Gaufrette;
use Gaufrette\FilesystemMap;
use Gaufrette\Filesystem;
use Gaufrette\Stream;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class StreamWrapperSpec extends ObjectBehavior
{
/**
* @param \Gaufrette\FilesystemMap $map
* @param \Gaufrette\Filesystem $filesystem
* @param \Gaufrette\Stream $stream
*/
function let(FilesystemMap $map, Filesystem $filesystem, Stream $stream)
{
$filesystem->createStream('filename')->willReturn($stream);
$map->get('some')->willReturn($filesystem);
$this->setFilesystemMap($map);
}
function it_is_initializable()
{
$this->shouldHaveType('Gaufrette\StreamWrapper');
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_opens_stream(Stream $stream)
{
$stream->open(Argument::any())->willReturn(true);
$this->stream_open('gaufrette://some/filename', 'r+')->shouldReturn(true);
}
function it_does_not_open_stream_when_key_is_not_defined()
{
$this
->shouldThrow(new \InvalidArgumentException('The specified path (gaufrette://some) is invalid.'))
->duringStream_open('gaufrette://some', 'r+');
}
function it_does_not_open_stream_when_host_is_not_defined()
{
$this
->shouldThrow(new \InvalidArgumentException('The specified path (gaufrette:///somefile) is invalid.'))
->duringStream_open('gaufrette:///somefile', 'r+')
;
}
function it_does_not_read_from_stream_when_is_not_opened()
{
$this->stream_read(10)->shouldReturn(false);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_does_not_read_from_stream(Stream $stream)
{
$stream->open(Argument::any())->willReturn(true);
$stream->read(4)->willReturn('some');
$this->stream_open('gaufrette://some/filename', 'r+');
$this->stream_read(4)->shouldReturn('some');
}
function it_does_not_write_to_stream_when_is_not_opened()
{
$this->stream_write('some content')->shouldReturn(0);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_writes_to_stream(Stream $stream)
{
$stream->open(Argument::any())->willReturn(true);
$stream->write('some content')->shouldBeCalled()->willReturn(12);
$this->stream_open('gaufrette://some/filename', 'w+');
$this->stream_write('some content')->shouldReturn(12);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_does_not_close_stream_when_is_not_opened($stream)
{
$stream->close()->shouldNotBeCalled();
$this->stream_close();
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_closes_stream(Stream $stream)
{
$stream->open(Argument::any())->willReturn(true);
$stream->close()->shouldBeCalled();
$this->stream_open('gaufrette://some/filename', 'w+');
$this->stream_close();
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_does_not_flush_stream_when_is_not_opened(Stream $stream)
{
$stream->flush()->shouldNotBeCalled();
$this->stream_flush();
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_flushes_stream(Stream $stream)
{
$stream->open(Argument::any())->willReturn(true);
$stream->flush()->shouldBeCalled();
$this->stream_open('gaufrette://some/filename', 'w+');
$this->stream_flush();
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_does_not_seek_in_stream_when_is_not_opened(Stream $stream)
{
$stream->seek(12, SEEK_SET)->shouldNotBeCalled();
$this->stream_seek(12, SEEK_SET);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_seeks_in_stream(Stream $stream)
{
$stream->open(Argument::any())->willReturn(true);
$stream->seek(12, SEEK_SET)->shouldBeCalled()->willReturn(true);
$this->stream_open('gaufrette://some/filename', 'w+');
$this->stream_seek(12, SEEK_SET)->shouldReturn(true);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_does_not_tell_about_position_in_stream_when_is_not_opened(Stream $stream)
{
$stream->tell()->shouldNotBeCalled();
$this->stream_tell();
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_does_tell_about_position_in_stream(Stream $stream)
{
$stream->open(Argument::any())->willReturn(true);
$stream->tell()->shouldBeCalled()->willReturn(12);
$this->stream_open('gaufrette://some/filename', 'w+');
$this->stream_tell()->shouldReturn(12);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_does_not_mark_as_eof_if_stream_is_not_opened(Stream $stream)
{
$stream->eof()->shouldNotBeCalled();
$this->stream_eof();
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_checks_if_eof(Stream $stream)
{
$stream->open(Argument::any())->willReturn(true);
$this->stream_open('gaufrette://some/filename', 'w+');
$stream->eof()->willReturn(false);
$this->stream_eof()->shouldReturn(false);
$stream->eof()->willReturn(true);
$this->stream_eof()->shouldReturn(true);
}
function it_does_not_get_stat_when_is_not_open()
{
$this->stream_stat()->shouldReturn(false);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_stats_file(Stream $stream)
{
$stat = [
'dev' => 1,
'ino' => 12,
'mode' => 0777,
'nlink' => 0,
'uid' => 123,
'gid' => 1,
'rdev' => 0,
'size' => 666,
'atime' => 1348030800,
'mtime' => 1348030800,
'ctime' => 1348030800,
'blksize' => 5,
'blocks' => 1,
];
$stream->open(Argument::any())->willReturn(true);
$stream->stat()->willReturn($stat);
$this->stream_open('gaufrette://some/filename', 'w+');
$this->stream_stat()->shouldReturn($stat);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_should_stat_from_url(Stream $stream)
{
$stat = [
'dev' => 1,
'ino' => 12,
'mode' => 0777,
'nlink' => 0,
'uid' => 123,
'gid' => 1,
'rdev' => 0,
'size' => 666,
'atime' => 1348030800,
'mtime' => 1348030800,
'ctime' => 1348030800,
'blksize' => 5,
'blocks' => 1,
];
$stream->open(Argument::any())->willReturn(true);
$stream->stat()->willReturn($stat);
$this->url_stat('gaufrette://some/filename', STREAM_URL_STAT_LINK)->shouldReturn($stat);
}
/**
* @param \Gaufrette\Filesystem $stream
* @param \Gaufrette\Stream $stream
*/
function it_stats_even_if_it_cannot_be_open(Filesystem $filesystem, Stream $stream)
{
$filesystem->createStream('dir/')->willReturn($stream);
$stream->open(Argument::any())->willThrow(new \RuntimeException);
$stream->stat(Argument::any())->willReturn(['mode' => 16893]);
$this->url_stat('gaufrette://some/dir/', STREAM_URL_STAT_LINK)->shouldReturn(['mode' => 16893]);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_does_not_unlink_when_cannot_open(Stream $stream)
{
$stream->open(Argument::any())->willThrow(new \RuntimeException);
$this->unlink('gaufrette://some/filename')->shouldReturn(false);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_unlinks_file(Stream $stream)
{
$stream->open(Argument::any())->willReturn(true);
$stream->unlink()->willReturn(true);
$this->unlink('gaufrette://some/filename')->shouldReturn(true);
}
function it_does_not_cast_stream_if_is_not_opened()
{
$this->stream_cast(STREAM_CAST_FOR_SELECT)->shouldReturn(false);
}
/**
* @param \Gaufrette\Stream $stream
*/
function it_casts_stream(Stream $stream)
{
$stream->open(Argument::any())->willReturn(true);
$stream->cast(STREAM_CAST_FOR_SELECT)->willReturn('resource');
$this->stream_open('gaufrette://some/filename', 'w+');
$this->stream_cast(STREAM_CAST_FOR_SELECT)->shouldReturn('resource');
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace spec\Gaufrette\Util;
use PhpSpec\ObjectBehavior;
class ChecksumSpec extends ObjectBehavior
{
function let()
{
file_put_contents($this->getTestFilePath(), 'some other content');
}
function letGo()
{
@unlink($this->getTestFilePath());
}
function it_calculates_checksum_from_content()
{
$this->fromContent('some content')
->shouldReturn(md5('some content'))
;
}
function it_calculates_checksum_from_filepath()
{
$this->fromFile($this->getTestFilePath())
->shouldReturn(md5('some other content'))
;
}
private function getTestFilePath(): string
{
return __DIR__ . DIRECTORY_SEPARATOR . 'testFile';
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace spec\Gaufrette\Util;
use PhpSpec\ObjectBehavior;
class PathSpec extends ObjectBehavior
{
function it_checks_if_path_is_absolute()
{
$this->isAbsolute('/home/path')->shouldBe(true);
$this->isAbsolute('home/path')->shouldBe(false);
$this->isAbsolute('../home/path')->shouldBe(false);
$this->isAbsolute('protocol://home/path')->shouldBe(true);
}
function it_normalizes_file_path()
{
$this->normalize('C:\\some\other.txt')->shouldReturn('c:/some/other.txt');
$this->normalize('..\other.txt')->shouldReturn('../other.txt');
$this->normalize('..\other.txt')->shouldReturn('../other.txt');
$this->normalize('/home/other/../new')->shouldReturn('/home/new');
$this->normalize('/home/other/./new')->shouldReturn('/home/other/new');
$this->normalize('protocol://home/other.txt')->shouldReturn('protocol://home/other.txt');
}
function it_returns_unix_style_dirname()
{
$this->dirname('a/test/path')->shouldReturn('a/test');
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace spec\Gaufrette\Util;
use PhpSpec\ObjectBehavior;
class SizeSpec extends ObjectBehavior
{
function it_calculates_size_of_content()
{
$this->fromContent('some content')->shouldReturn(12);
$this->fromContent('some other content')->shouldReturn(18);
$this->fromContent('some')->shouldReturn(4);
}
}