public function setUp()
 {
     parent::setUp();
     $dbOptions = new ThrottleDbAdapterOptions();
     $this->gateway = new TableGateway($dbOptions->getDbTable(), $this->getAdapter());
     $this->throttleDbAdapter = new ThrottleDbAdapter($this->gateway, $dbOptions, new LockHandleFactory());
     $throttleOptions = new ThrottleOptions();
     $this->throttle = new Throttle($this->throttleDbAdapter, $throttleOptions);
 }
Example #2
0
 /**
  * @param string $key
  * @param DateTime $endDate
  * @return mixed Lock\Handle or false
  */
 public function setLock($key, DateTime $endDate)
 {
     try {
         $result = $this->gateway->insert(['key' => $key, 'end_datetime' => $endDate->format($this->options->getDbDateTimeFormat())]);
         if ($result) {
             return $this->createLockHandle($key);
         }
     } catch (\Exception $e) {
     }
     return false;
 }
Example #3
0
 public function testCreateLockHandleLooped()
 {
     $dbOptions = new ThrottleDbAdapterOptions();
     $gateway = new TableGateway($dbOptions->getDbTable(), $this->getAdapter());
     $factoryMock = $this->getMock('BeaucalLongThrottle\\Factory\\LockHandleFactory');
     $factoryMock->expects($this->any())->method('createHandle')->will($this->returnValue(new Lock\Handle()));
     $throttleDbAdapter = new ThrottleDbAdapter($gateway, $dbOptions, $factoryMock);
     $throttle = new Throttle($throttleDbAdapter, new ThrottleOptions());
     /**
      * First lock works, second tries to get a new handle but can't.
      */
     $ttl = new DateTimeUnit(80, 'seconds');
     $throttle->takeLock('handleOk', $ttl);
     $this->assertFalse($throttle->takeLock('handleRepeats', $ttl));
 }
 public function testConfigOverrides()
 {
     $config = (require __DIR__ . '/data/beaucallongthrottle.local.php');
     $config = $config['beaucallongthrottle']['BeaucalLongThrottle\\Adapter\\Db'];
     unset($config['options_class']);
     $options = new DbAdapterOptions($config);
     $this->assertEquals('use_transactions_another', $options->getUseTransactions());
     $this->assertEquals('db_adapter_class_another', $options->getDbAdapterClass());
     $this->assertEquals('db_table_another', $options->getDbTable());
     $this->assertEquals('db_date_time_format_another', $options->getDbDateTimeFormat());
     $this->assertEquals('clear_all_is_cheap_another', $options->getClearAllIsCheap());
 }