AwsS3Test.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Gaufrette\Functional\Adapter;
  3. use Gaufrette\Adapter\AwsS3;
  4. use Aws\S3\S3Client;
  5. use Guzzle\Plugin\Mock\MockPlugin;
  6. use Guzzle\Http\Message\Response;
  7. /**
  8. * @todo move to phpspec
  9. */
  10. class AwsS3Test extends \PHPUnit_Framework_TestCase
  11. {
  12. protected function getClient()
  13. {
  14. return S3Client::factory(array(
  15. 'key' => 'foo',
  16. 'secret' => 'bar'
  17. ));
  18. }
  19. public function testCreatesBucketIfMissing()
  20. {
  21. $mock = new MockPlugin(array(
  22. new Response(404), // Head bucket response
  23. new Response(200), // Create bucket response
  24. new Response(200, array(), 'foo') // Get object response
  25. ));
  26. $client = $this->getClient();
  27. $client->addSubscriber($mock);
  28. $adapter = new AwsS3($client, 'bucket', array('create' => true));
  29. $this->assertEquals('foo', $adapter->read('foo'));
  30. $requests = $mock->getReceivedRequests();
  31. $this->assertEquals('HEAD', $requests[0]->getMethod());
  32. $this->assertEquals('PUT', $requests[1]->getMethod());
  33. $this->assertEquals('GET', $requests[2]->getMethod());
  34. $this->assertEquals('bucket.s3.amazonaws.com', $requests[0]->getHost());
  35. }
  36. /**
  37. * @expectedException \RuntimeException
  38. */
  39. public function testThrowsExceptionIfBucketMissingAndNotCreating()
  40. {
  41. $mock = new MockPlugin(array(new Response(404)));
  42. $client = $this->getClient();
  43. $client->addSubscriber($mock);
  44. $adapter = new AwsS3($client, 'bucket');
  45. $adapter->read('foo');
  46. }
  47. public function testWritesObjects()
  48. {
  49. $mock = new MockPlugin(array(
  50. new Response(200), // HEAD bucket response
  51. new Response(201) // PUT object response
  52. ));
  53. $client = $this->getClient();
  54. $client->addSubscriber($mock);
  55. $adapter = new AwsS3($client, 'bucket');
  56. $this->assertEquals(7, $adapter->write('foo', 'testing'));
  57. $requests = $mock->getReceivedRequests();
  58. $this->assertEquals('bucket.s3.amazonaws.com', $requests[1]->getHost());
  59. $this->assertEquals('PUT', $requests[1]->getMethod());
  60. }
  61. public function testChecksForObjectExistence()
  62. {
  63. $mock = new MockPlugin(array(new Response(200)));
  64. $client = $this->getClient();
  65. $client->addSubscriber($mock);
  66. $adapter = new AwsS3($client, 'bucket');
  67. $this->assertTrue($adapter->exists('foo'));
  68. $requests = $mock->getReceivedRequests();
  69. $this->assertEquals('bucket.s3.amazonaws.com', $requests[0]->getHost());
  70. $this->assertEquals('HEAD', $requests[0]->getMethod());
  71. $this->assertEquals('/foo', $requests[0]->getResource());
  72. }
  73. public function testGetsObjectUrls()
  74. {
  75. $client = $this->getClient();
  76. $adapter = new AwsS3($client, 'bucket');
  77. $this->assertEquals('https://bucket.s3.amazonaws.com/foo', $adapter->getUrl('foo'));
  78. }
  79. public function testChecksForObjectExistenceWithDirectory()
  80. {
  81. $mock = new MockPlugin(array(new Response(200)));
  82. $client = $this->getClient();
  83. $client->addSubscriber($mock);
  84. $adapter = new AwsS3($client, 'bucket', array('directory' => 'bar'));
  85. $this->assertTrue($adapter->exists('foo'));
  86. $requests = $mock->getReceivedRequests();
  87. $this->assertEquals('bucket.s3.amazonaws.com', $requests[0]->getHost());
  88. $this->assertEquals('HEAD', $requests[0]->getMethod());
  89. $this->assertEquals('/bar/foo', $requests[0]->getResource());
  90. }
  91. public function testGetsObjectUrlsWithDirectory()
  92. {
  93. $client = $this->getClient();
  94. $adapter = new AwsS3($client, 'bucket', array('directory' => 'bar'));
  95. $this->assertEquals('https://bucket.s3.amazonaws.com/bar/foo', $adapter->getUrl('foo'));
  96. }
  97. }