TestOssClientBase.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\OssClient;
  4. require_once __DIR__ . DIRECTORY_SEPARATOR . 'Common.php';
  5. class TestOssClientBase extends \PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @var OssClient
  9. */
  10. protected $ossClient;
  11. /**
  12. * @var string
  13. */
  14. protected $bucket;
  15. public function setUp()
  16. {
  17. $this->bucket = Common::getBucketName() . rand(100000, 999999);
  18. $this->ossClient = Common::getOssClient();
  19. $this->ossClient->createBucket($this->bucket);
  20. Common::waitMetaSync();
  21. }
  22. public function tearDown()
  23. {
  24. if (!$this->ossClient->doesBucketExist($this->bucket)) {
  25. return;
  26. }
  27. $objects = $this->ossClient->listObjects(
  28. $this->bucket, array('max-keys' => 1000, 'delimiter' => ''))->getObjectList();
  29. $keys = array();
  30. foreach ($objects as $obj) {
  31. $keys[] = $obj->getKey();
  32. }
  33. if (count($keys) > 0) {
  34. $this->ossClient->deleteObjects($this->bucket, $keys);
  35. }
  36. $uploads = $this->ossClient->listMultipartUploads($this->bucket)->getUploads();
  37. foreach ($uploads as $up) {
  38. $this->ossClient->abortMultipartUpload($this->bucket, $up->getKey(), $up->getUploadId());
  39. }
  40. $this->ossClient->deleteBucket($this->bucket);
  41. }
  42. }