123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- <?php
- namespace spec\Gaufrette\Adapter;
- use PhpSpec\ObjectBehavior;
- use Prophecy\Argument;
- class AmazonS3Spec extends ObjectBehavior
- {
- /**
- * @param \AmazonS3 $service
- */
- function let($service)
- {
- $this->beConstructedWith($service, 'bucketName');
- }
- function it_is_adapter()
- {
- $this->shouldHaveType('Gaufrette\Adapter');
- }
- function it_supports_metadata()
- {
- $this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_reads_file($service)
- {
- $options = array(
- 'range' => 12,
- 'response' => array(
- 'content-language' => 'pl-pl'
- )
- );
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->get_object(
- 'bucketName',
- 'filename',
- $options
- )
- ->shouldBeCalled()
- ->willReturn(new \CFResponse('header', 'some content', 200))
- ;
- $service->set_region(Argument::any())->shouldBeCalled();
- $this->setMetadata('filename', $options);
- $this->read('filename')->shouldReturn('some content');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_returns_false_when_cannot_read($service)
- {
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->get_object(
- 'bucketName',
- 'filename',
- array()
- )
- ->shouldBeCalled()
- ->willReturn(new \CFResponse('header', 'some content', 500))
- ;
- $service->set_region(Argument::any())->shouldBeCalled();
- $this->read('filename')->shouldReturn(false);
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_is_verbose_and_throws_exceptions_when_read($service)
- {
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->get_object(
- 'bucketName',
- 'filename',
- array()
- )
- ->willThrow(new \RuntimeException('read'))
- ;
- $service->set_region(Argument::any())->shouldBeCalled();
- $this->shouldThrow(new \RuntimeException('read'))->duringRead('filename');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_rename_file($service)
- {
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->copy_object(
- array(
- 'bucket' => 'bucketName',
- 'filename' => 'filename1',
- ),
- array(
- 'bucket' => 'bucketName',
- 'filename' => 'filename2'
- ),
- array('acl' => \AmazonS3::ACL_OWNER_READ)
- )
- ->shouldBeCalled()
- ->willReturn(new \CFResponse('header', 'some content', 200))
- ;
- $service
- ->delete_object(
- 'bucketName',
- 'filename1',
- Argument::any()
- )
- ->shouldBeCalled()
- ->willReturn(new \CFResponse(array(), 'some', 200))
- ;
- $this->setMetadata('filename1', array('acl' => \AmazonS3::ACL_OWNER_READ));
- $this->rename('filename1', 'filename2')->shouldReturn(true);
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_is_verbose_and_throws_exceptions_when_rename($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->copy_object(Argument::cetera())
- ->willThrow(new \RuntimeException('rename'))
- ;
- $this->shouldThrow(new \RuntimeException('rename'))->duringRename('filename', 'filename1');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_returns_false_when_cannot_rename($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->copy_object(
- array(
- 'bucket' => 'bucketName',
- 'filename' => 'filename1',
- ),
- array(
- 'bucket' => 'bucketName',
- 'filename' => 'filename2'
- ),
- array()
- )
- ->shouldBeCalled()
- ->willReturn(new \CFResponse('header', 'some content', 500))
- ;
- $this->rename('filename1', 'filename2')->shouldReturn(false);
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_should_write_file($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->create_object(
- 'bucketName',
- 'filename',
- array(
- 'acl' => \AmazonS3::ACL_PRIVATE,
- 'body' => 'some content'
- )
- )
- ->shouldBeCalled()
- ->willReturn(new \CFResponse(array('x-aws-requestheaders' => array('Content-Length' => 12)), 'some content', 200))
- ;
- $this->setMetadata('filename', array('acl' => \AmazonS3::ACL_PRIVATE, 'body' => 'other content'));
- $this->write('filename', 'some content')->shouldReturn(12);
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_returns_false_when_cannot_write($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->create_object(
- 'bucketName',
- 'filename',
- array(
- 'acl' => \AmazonS3::ACL_PUBLIC,
- 'body' => 'some content'
- )
- )
- ->shouldBeCalled()
- ->willReturn(new \CFResponse(array('x-aws-requestheaders' => array('Content-Length' => 12)), 'some content', 500))
- ;
- $this->write('filename', 'some content')->shouldReturn(false);
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_is_verbose_and_throws_exceptions_when_write($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->create_object(Argument::cetera())
- ->willThrow(new \RuntimeException('write'))
- ;
- $this->shouldThrow(new \RuntimeException('write'))->duringWrite('filename', 'some content');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_should_check_if_file_exists($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service->if_object_exists('bucketName', 'filename')->willReturn(true);
- $this->exists('filename')->shouldReturn(true);
- $service->if_object_exists('bucketName', 'filename')->willReturn(false);
- $this->exists('filename')->shouldReturn(false);
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_is_verbose_and_throws_exceptions_when_file_exists($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->if_object_exists('bucketName', 'filename')
- ->willThrow(new \RuntimeException('exists'))
- ;
- $this->shouldThrow(new \RuntimeException('exists'))->duringExists('filename');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_should_get_file_mtime($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $metadata = array('acl' => \AmazonS3::ACL_PUBLIC);
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->get_object_metadata(
- 'bucketName',
- 'filename',
- $metadata
- )
- ->shouldBeCalled()
- ->willReturn(array('Headers' => array('last-modified' => '2012-01-01 23:10:10')))
- ;
- $this->setMetadata('filename', $metadata);
- $this->mtime('filename')->shouldReturn(strtotime('2012-01-01 23:10:10'));
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_returns_false_when_cannot_fetch_mtime($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->get_object_metadata(
- 'bucketName',
- 'filename',
- array()
- )
- ->shouldBeCalled()
- ->willReturn(array('Headers' => array()))
- ;
- $this->mtime('filename')->shouldReturn(false);
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_is_verbose_and_throws_exceptions_when_fetch_mtime($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->get_object_metadata('bucketName', 'filename', Argument::any())
- ->willThrow(new \RuntimeException('mtime'))
- ;
- $this->shouldThrow(new \RuntimeException('mtime'))->duringMtime('filename');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_should_delete_file($service)
- {
- $metadata = array('acl' => \AmazonS3::ACL_PRIVATE);
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->delete_object(
- 'bucketName',
- 'filename',
- $metadata
- )
- ->willReturn(new \CFResponse(array(), 'some', 200))
- ;
- $this->setMetadata('filename', $metadata);
- $this->delete('filename')->shouldReturn(true);
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_is_verbose_and_throws_exceptions_when_fetch_delete($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->willReturn(true)
- ;
- $service
- ->delete_object(
- 'bucketName',
- 'filename',
- Argument::any()
- )
- ->willThrow(new \RuntimeException('delete'))
- ;
- $this->shouldThrow(new \RuntimeException('delete'))->duringDelete('filename');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_returns_false_when_cannot_delete($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->delete_object(
- 'bucketName',
- 'filename',
- array()
- )
- ->willReturn(new \CFResponse(array(), 'some', 500))
- ;
- $this->delete('filename')->shouldReturn(false);
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_should_get_keys($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->get_object_list('bucketName')
- ->shouldBeCalled()
- ->willReturn(array('filename2', 'aaa/filename', 'filename1'))
- ;
- $this->keys()->shouldReturn(array('aaa', 'aaa/filename', 'filename1', 'filename2'));
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_is_verbose_and_throws_exceptions_when_fetch_keys($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->willReturn(true)
- ;
- $service
- ->get_object_list('bucketName')
- ->willThrow(new \RuntimeException('keys'))
- ;
- $this->shouldThrow(new \RuntimeException('keys'))->duringKeys();
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_should_handle_dirs($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->willReturn(true)
- ;
- $service
- ->if_object_exists('bucketName', 'filename')
- ->shouldNotBeCalled()
- ;
- $service
- ->if_object_exists('bucketName', 'filename/')
- ->shouldBeCalled()
- ->willReturn(false)
- ;
- $service
- ->if_object_exists('bucketName', 'dirname/')
- ->willReturn(true)
- ;
- $this->isDirectory('filename')->shouldReturn(false);
- $this->isDirectory('dirname')->shouldReturn(true);
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_should_fail_when_bucket_does_not_exist($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->willReturn(false)
- ;
- $this
- ->shouldThrow(new \RuntimeException('The configured bucket "bucketName" does not exist.'))
- ->duringRead('filename')
- ;
- $this
- ->shouldThrow(new \RuntimeException('The configured bucket "bucketName" does not exist.'))
- ->duringWrite('filename', 'content')
- ;
- $this
- ->shouldThrow(new \RuntimeException('The configured bucket "bucketName" does not exist.'))
- ->duringDelete('filename')
- ;
- $this
- ->shouldThrow(new \RuntimeException('The configured bucket "bucketName" does not exist.'))
- ->duringExists('filename')
- ;
- $this
- ->shouldThrow(new \RuntimeException('The configured bucket "bucketName" does not exist.'))
- ->duringMtime('filename')
- ;
- $this
- ->shouldThrow(new \RuntimeException('The configured bucket "bucketName" does not exist.'))
- ->duringRename('filename', 'filename2')
- ;
- $this
- ->shouldThrow(new \RuntimeException('The configured bucket "bucketName" does not exist.'))
- ->duringKeys()
- ;
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_creates_bucket_if_create_mode_is_enabled($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->willReturn(false)
- ;
- $service
- ->create_bucket('bucketName', \AmazonS3::REGION_US_E1)
- ->shouldBeCalled()
- ->willReturn(new \CFResponse(array(), 'created', 201))
- ;
- $service
- ->if_object_exists('bucketName', 'filename')
- ->willReturn(false)
- ;
- $this->beConstructedWith($service, 'bucketName', array('create' => true));
- $this->exists('filename');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_fails_when_cannot_create_bucket($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->willReturn(false)
- ;
- $service
- ->create_bucket('bucketName', \AmazonS3::REGION_US_E1)
- ->shouldBeCalled()
- ->willReturn(new \CFResponse(array(), 'created', 500))
- ;
- $this->beConstructedWith($service, 'bucketName', array('create' => true));
- $this
- ->shouldThrow(new \RuntimeException('Failed to create the configured bucket "bucketName".'))
- ->duringExists('filename')
- ;
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_allows_to_configure_reqion($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->willReturn(true)
- ;
- $service
- ->set_region(\AmazonS3::REGION_EU_W1)
- ->shouldBeCalled()
- ;
- $service
- ->if_object_exists('bucketName', 'filename')
- ->willReturn(true)
- ;
- $this->beConstructedWith($service, 'bucketName', array('region' => \AmazonS3::REGION_EU_W1));
- $this->exists('filename');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_allows_to_configure_region_for_bucket($service)
- {
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->willReturn(false)
- ;
- $service
- ->create_bucket('bucketName', \AmazonS3::REGION_EU_W1)
- ->shouldBeCalled()
- ->willReturn(new \CFResponse(array(), 'created', 201))
- ;
- $service
- ->if_object_exists('bucketName', 'filename')
- ->willReturn(false)
- ;
- $this->beConstructedWith($service, 'bucketName', array('create' => true, 'region' => \AmazonS3::REGION_EU_W1));
- $this->exists('filename');
- }
- /**
- * @param \AmazonS3 $service
- */
- function it_allows_to_configure_acl($service)
- {
- $this->setAcl('123abc');
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->create_object(
- 'bucketName',
- 'filename',
- array(
- 'acl' => '123abc',
- 'body' => 'some content'
- )
- )
- ->shouldBeCalled()
- ->willReturn(new \CFResponse(array('x-aws-requestheaders' => array('Content-Length' => 12)), 'some content', 200))
- ;
- $this->write('filename', 'some content')->shouldReturn(12);
- $this->getAcl()->shouldBe('123abc');
- }
- /**
- * @param \AmazonS3 $service
- */
- function its_file_metadata_acl_are_more_important_than_global_acl_config($service)
- {
- $this->setAcl('123abc');
- $service->set_region(Argument::any())->shouldBeCalled();
- $service
- ->if_bucket_exists('bucketName')
- ->shouldBeCalled()
- ->willReturn(true)
- ;
- $service
- ->create_object(
- 'bucketName',
- 'filename',
- array(
- 'acl' => 'more important acl',
- 'body' => 'some content'
- )
- )
- ->shouldBeCalled()
- ->willReturn(new \CFResponse(array('x-aws-requestheaders' => array('Content-Length' => 12)), 'some content', 200))
- ;
- $this->setMetadata('filename', array('acl' => 'more important acl'));
- $this->write('filename', 'some content')->shouldReturn(12);
- }
- }
|