ControllerWithGcsTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. use Google\Auth\HttpHandler\HttpHandlerFactory;
  3. use GuzzleHttp\Client;
  4. use PrivateBin\Data\GoogleCloudStorage;
  5. use PrivateBin\Persistence\ServerSalt;
  6. use PrivateBin\Persistence\TrafficLimiter;
  7. require_once 'ControllerTest.php';
  8. class ControllerWithGcsTest extends ControllerTest
  9. {
  10. private static $_client;
  11. private static $_bucket;
  12. private $_options = array();
  13. public static function setUpBeforeClass(): void
  14. {
  15. $httpClient = new Client(array('debug'=>false));
  16. $handler = HttpHandlerFactory::build($httpClient);
  17. $name = 'pb-';
  18. $alphabet = 'abcdefghijklmnopqrstuvwxyz';
  19. for ($i = 0; $i < 29; ++$i) {
  20. $name .= $alphabet[rand(0, strlen($alphabet) - 1)];
  21. }
  22. self::$_client = new StorageClientStub(array());
  23. self::$_bucket = self::$_client->createBucket($name);
  24. }
  25. public function setUp(): void
  26. {
  27. /* Setup Routine */
  28. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  29. if (!is_dir($this->_path)) {
  30. mkdir($this->_path);
  31. }
  32. $this->_options = array(
  33. 'bucket' => self::$_bucket->name(),
  34. 'prefix' => 'pastes',
  35. );
  36. $this->_data = new GoogleCloudStorage($this->_options);
  37. ServerSalt::setStore($this->_data);
  38. TrafficLimiter::setStore($this->_data);
  39. $this->reset();
  40. }
  41. public function reset()
  42. {
  43. parent::reset();
  44. // but then inject a db config
  45. $options = parse_ini_file(CONF, true);
  46. $options['model'] = array(
  47. 'class' => 'GoogleCloudStorage',
  48. );
  49. $options['model_options'] = $this->_options;
  50. Helper::createIniFile(CONF, $options);
  51. }
  52. }