1<?php 2 3require_once('CouchbaseMock.php'); 4 5class CouchbaseTestCase extends \PHPUnit_Framework_TestCase { 6 public $testDsn; 7 public $testBucket; 8 public $testAdminUser; 9 public $testAdminPassword; 10 public $testUser; 11 public $testPassword; 12 public $testAuthenticator; 13 public $mock = null; 14 15 protected function setUp() { 16 if (getenv('CB_MOCK')) { 17 $this->mock = \CouchbaseMock::get(); 18 $this->mock->start(); 19 $this->mock->setCCCP(true); 20 21 $this->testDsn = $this->mock->connectionString(); 22 $this->testBucket = 'default'; 23 $this->testAdminUser = 'Administrator'; 24 $this->testAdminPassword = 'password'; 25 $this->testUser = 'default'; 26 $this->testPassword = ''; 27 } else { 28 $this->testDsn = getenv('CB_DSN'); 29 if ($this->testDsn === FALSE) { 30 $this->testDsn = 'couchbase://localhost/default'; 31 } 32 33 $this->testBucket = getenv('CB_BUCKET'); 34 if ($this->testBucket === FALSE) { 35 $this->testBucket = 'default'; 36 } 37 38 $this->testAdminUser = getenv('CB_ADMIN_USER'); 39 if ($this->testAdminUser === FALSE) { 40 $this->testAdminUser = 'Administrator'; 41 } 42 43 $this->testAdminPassword = getenv('CB_ADMIN_PASSWORD'); 44 if ($this->testAdminPassword === FALSE) { 45 $this->testAdminPassword = 'password'; 46 } 47 48 $this->testUser = getenv('CB_USER'); 49 if ($this->testUser === FALSE) { 50 $this->testUser = 'default'; 51 } 52 53 $this->testPassword = getenv('CB_PASSWORD'); 54 if ($this->testPassword === FALSE) { 55 $this->testPassword = ''; 56 } 57 } 58 if ($this->serverVersion() >= 5.0) { 59 $this->testAuthenticator = new \Couchbase\PasswordAuthenticator(); 60 $this->testAuthenticator->username($this->testUser)->password($this->testPassword); 61 } else { 62 $this->testAuthenticator = new \Couchbase\ClassicAuthenticator(); 63 $this->testAuthenticator->bucket($this->testBucket, $this->testPassword); 64 } 65 } 66 67 public function usingMock() { 68 return $this->mock != null; 69 } 70 71 public function serverVersion() { 72 $version = getenv('CB_VERSION'); 73 if ($version === FALSE) { 74 $version = '4.6'; 75 } 76 return floatval($version); 77 } 78 79 function setTimeouts($bucket) { 80 $val = getenv("CB_OPERATION_TIMEOUT"); 81 if ($val !== FALSE) { 82 $bucket->operationTimeout = intval($val); 83 } else { 84 $bucket->operationTimeout = 5000000; 85 } 86 $val = getenv("CB_VIEW_TIMEOUT"); 87 if ($val !== FALSE) { 88 $bucket->viewTimeout = intval($val); 89 } 90 $val = getenv("CB_DURABILITY_INTERVAL"); 91 if ($val !== FALSE) { 92 $bucket->durabilityInterval = intval($val); 93 } 94 $val = getenv("CB_DURABILITY_TIMEOUT"); 95 if ($val !== FALSE) { 96 $bucket->durabilityTimeout = intval($val); 97 } 98 $val = getenv("CB_HTTP_TIMEOUT"); 99 if ($val !== FALSE) { 100 $bucket->httpTimeout = intval($val); 101 } 102 $val = getenv("CB_CONFIG_TIMEOUT"); 103 if ($val !== FALSE) { 104 $bucket->configTimeout = intval($val); 105 } 106 $val = getenv("CB_CONFIG_DELAY"); 107 if ($val !== FALSE) { 108 $bucket->configDelay = intval($val); 109 } 110 $val = getenv("CB_CONFIG_NODE_TIMEOUT"); 111 if ($val !== FALSE) { 112 $bucket->configNodeTimeout = intval($val); 113 } 114 $val = getenv("CB_HTTP_CONFIG_IDLE_TIMEOUT"); 115 if ($val !== FALSE) { 116 $bucket->htconfigIdleTimeout = intval($val); 117 } 118 if (getenv("REPORT_TIMEOUT_SETTINGS")) { 119 printf("\n[TIMEOUTS] OT=%d, VT=%d, DI=%d, DT=%d, HT=%d, CT=%d, CD=%d, CNT=%d, HCIT=%d\n", 120 $bucket->operationTimeout, 121 $bucket->viewTimeout, 122 $bucket->durabilityInterval, 123 $bucket->durabilityTimeout, 124 $bucket->httpTimeout, 125 $bucket->configTimeout, 126 $bucket->configDelay, 127 $bucket->configNodeTimeout, 128 $bucket->htconfigIdleTimeout); 129 } 130 } 131 132 function makeKey($prefix) { 133 return uniqid($prefix); 134 } 135 136 function assertValidMetaDoc($metadoc) { 137 $this->assertInstanceOf('\Couchbase\Document', $metadoc); 138 139 // Check it has all the fields it should. 140 for ($i = 1; $i < func_num_args(); ++$i) { 141 $attr = func_get_arg($i); 142 $this->assertObjectHasAttribute($attr, $metadoc); 143 $this->assertNotNull($metadoc->{$attr}, "Expected document to have not NULL $attr"); 144 } 145 } 146 147 function assertErrorMetaDoc($metadoc, $type, $code) { 148 $this->assertValidMetaDoc($metadoc, 'error'); 149 150 $this->assertInstanceOf($type, $metadoc->error); 151 $this->assertEquals($code, $metadoc->error->getCode()); 152 } 153 154 function wrapException($cb, $type = NULL, $code = NULL, $message = NULL) { 155 PHPUnit_Framework_Error_Notice::$enabled = false; 156 $exOut = NULL; 157 try { 158 $cb(); 159 } catch (Exception $ex) { 160 $exOut = $ex; 161 } 162 PHPUnit_Framework_Error_Notice::$enabled = true; 163 164 if ($type !== NULL) { 165 $this->assertErrorType($type, $exOut); 166 } 167 if ($code !== NULL) { 168 $this->assertErrorCode($code, $exOut); 169 } 170 if ($message !== NULL) { 171 $this->assertErrorMessage($message, $exOut); 172 } 173 174 return $exOut; 175 } 176 177 function assertError($type, $code, $ex) { 178 $this->assertErrorType($type, $ex); 179 $this->assertErrorCode($code, $ex); 180 } 181 182 function assertErrorType($type, $ex) { 183 $this->assertInstanceOf($type, $ex); 184 } 185 186 function assertErrorMessage($msg, $ex) { 187 $this->assertRegexp($msg, $ex->getMessage()); 188 } 189 190 function assertErrorCode($code, $ex) { 191 $this->assertEquals($code, $ex->getCode(), "Exception code does not match: {$ex->getCode()} != {$code}, exception message: '{$ex->getMessage()}'"); 192 } 193} 194