123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- <?php
- namespace spec\Gaufrette;
- use PhpSpec\ObjectBehavior;
- use Prophecy\Argument;
- class FilesystemSpec extends ObjectBehavior
- {
- /**
- * @param \Gaufrette\Adapter $adapter
- */
- function let($adapter)
- {
- $this->beConstructedWith($adapter);
- }
- function it_is_initializable()
- {
- $this->shouldBeAnInstanceOf('Gaufrette\Filesystem');
- }
- /**
- * @param \Gaufrette\Adapter $adapter
- */
- function it_gives_access_to_adapter($adapter)
- {
- $this->getAdapter()->shouldBe($adapter);
- }
- /**
- * @param \Gaufrette\Adapter $adapter
- */
- function it_check_if_file_exists_using_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->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->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->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->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->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->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->exists('filename')->willReturn(false);
- $this->get('filename', true)->shouldBeAnInstanceOf('Gaufrette\File');
- }
- /**
- * @param \spec\Gaufrette\Adapter $extendedAdapter
- * @param \Gaufrette\File $file
- */
- function it_delegates_file_creation_to_adapter_when_adapter_is_file_factory($extendedAdapter, $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->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->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->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->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->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->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->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->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->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->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)
- {
- $keys = array('filename', 'filename1', 'filename2');
- $adapter->keys()->willReturn($keys);
- $this->keys()->shouldReturn($keys);
- }
- /**
- * @param \Gaufrette\Adapter $adapter
- */
- function it_match_listed_keys_using_specified_pattern($adapter)
- {
- $keys = array('filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey');
- $adapter->keys()->willReturn($keys);
- $adapter->isDirectory(Argument::any())->willReturn(false);
- $this->listKeys()->shouldReturn(
- array(
- 'keys' => array('filename', 'filename1', 'filename2', 'testKey', 'KeyTest', 'testkey'),
- 'dirs' => array()
- )
- );
- $this->listKeys('filename')->shouldReturn(
- array(
- 'keys' => array('filename', 'filename1', 'filename2'),
- 'dirs' => array()
- )
- );
- $this->listKeys('Key')->shouldReturn(
- array(
- 'keys' => array('KeyTest'),
- 'dirs' => array()
- )
- );
- }
- /**
- * @param \Gaufrette\Adapter $adapter
- */
- function it_listing_directories_using_adapter_is_directory_method($adapter)
- {
- $keys = array('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(
- array(
- 'keys' => array('filename', 'filename2', 'KeyTest', 'testkey'),
- 'dirs' => array('filename1', 'testKey')
- )
- );
- $this->listKeys('filename')->shouldReturn(
- array(
- 'keys' => array('filename', 'filename2'),
- 'dirs' => array('filename1')
- )
- );
- $this->listKeys('Key')->shouldReturn(
- array(
- 'keys' => array('KeyTest'),
- 'dirs' => array()
- )
- );
- }
- /**
- * @param \Gaufrette\Adapter $adapter
- */
- function it_gets_mtime_of_file_using_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->exists('filename')->willReturn(false);
- $this
- ->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
- ->duringMtime('filename')
- ;
- }
- /**
- * @param \Gaufrette\Adapter $adapter
- */
- function it_calculates_file_checksum($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->exists('filename')->shouldBeCalled()->willReturn(false);
- $this
- ->shouldThrow(new \Gaufrette\Exception\FileNotFound('filename'))
- ->duringChecksum('filename');
- }
- /**
- * @param \spec\Gaufrette\Adapter $extendedAdapter
- */
- function it_delegates_checksum_calculation_to_adapter_when_adapter_is_checksum_calculator($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\Adapter $extendedAdapter
- */
- function it_delegates_mime_type_resolution_to_adapter_when_adapter_is_mime_type_provider($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->exists('filename')->willReturn(true);
- $this
- ->shouldThrow(new \LogicException(sprintf('Adapter "%s" cannot provide MIME type', get_class($adapter->getWrappedObject()))))
- ->duringMimeType('filename');
- }
- }
- interface Adapter extends \Gaufrette\Adapter,
- \Gaufrette\Adapter\FileFactory,
- \Gaufrette\Adapter\StreamFactory,
- \Gaufrette\Adapter\ChecksumCalculator,
- \Gaufrette\Adapter\MetadataSupporter,
- \Gaufrette\Adapter\MimeTypeProvider
- {}
|