public function testCollect()
 {
     $pheanstalkConnection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
     $pheanstalkConnection->expects($this->any())->method('getHost')->will($this->returnValue('127.0.0.1'));
     $pheanstalkConnection->expects($this->any())->method('getPort')->will($this->returnValue('11130'));
     $pheanstalkConnection->expects($this->any())->method('getConnectTimeout')->will($this->returnValue(60));
     $pheanstalkConnection->expects($this->any())->method('isServiceListening')->will($this->returnValue(false));
     $pheanstalkA = $this->getMockForAbstractClass(PheanstalkInterface::class);
     $pheanstalkB = $this->getMockForAbstractClass(PheanstalkInterface::class);
     $pheanstalkA->expects($this->any())->method('getConnection')->will($this->returnValue($pheanstalkConnection));
     $pheanstalkB->expects($this->any())->method('getConnection')->will($this->returnValue($pheanstalkConnection));
     $pheanstalkLocator = new PheanstalkLocator();
     $pheanstalkLocator->addPheanstalk('default', $pheanstalkA, true);
     $pheanstalkLocator->addPheanstalk('foo', $pheanstalkB);
     $request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock();
     $response = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock();
     $dataCollector = new PheanstalkDataCollector($pheanstalkLocator);
     $dataCollector->collect($request, $response);
     $this->assertArrayHasKey('default', $dataCollector->getPheanstalks());
     $this->assertArrayHasKey('foo', $dataCollector->getPheanstalks());
     $this->assertArrayNotHasKey('bar', $dataCollector->getPheanstalks());
     $data = $dataCollector->getPheanstalks();
     $this->assertArrayHasKey('name', $data['default']);
     $this->assertArrayHasKey('host', $data['default']);
     $this->assertArrayHasKey('port', $data['default']);
     $this->assertArrayHasKey('timeout', $data['default']);
     $this->assertArrayHasKey('default', $data['default']);
     $this->assertArrayHasKey('stats', $data['default']);
     $this->assertArrayHasKey('listening', $data['default']);
 }
 /**
  * @inheritdoc
  */
 public function collect(Request $request, Response $response, \Exception $exception = null)
 {
     $defaultPheanstalk = $this->pheanstalkLocator->getDefaultPheanstalk();
     // Collect the information
     foreach ($this->pheanstalkLocator->getPheanstalks() as $name => $pheanstalk) {
         // Get information about this connection
         $this->data['pheanstalks'][$name] = ['name' => $name, 'host' => $pheanstalk->getConnection()->getHost(), 'port' => $pheanstalk->getConnection()->getPort(), 'timeout' => $pheanstalk->getConnection()->getConnectTimeout(), 'default' => $defaultPheanstalk === $pheanstalk, 'stats' => [], 'listening' => $pheanstalk->getConnection()->isServiceListening()];
         // If connection is not listening, there is a connection problem.
         // Skip next steps which require an established connection
         if (!$pheanstalk->getConnection()->isServiceListening()) {
             continue;
         }
         $pheanstalkStatistics = $pheanstalk->stats()->getArrayCopy();
         // Get information about this connection
         $this->data['pheanstalks'][$name]['stats'] = $pheanstalkStatistics;
         // Increment the number of jobs
         $this->data['jobCount'] += $pheanstalkStatistics['current-jobs-ready'];
         // Get information about the tubes of this connection
         $tubes = $pheanstalk->listTubes();
         foreach ($tubes as $tubeName) {
             // Fetch next ready job and next buried job for this tube
             $this->fetchJobs($pheanstalk, $tubeName);
             $this->data['tubes'][] = ['pheanstalk' => $name, 'name' => $tubeName, 'stats' => $pheanstalk->statsTube($tubeName)->getArrayCopy()];
         }
     }
 }
 /**
  * @param string $name
  *
  * @return PheanstalkInterface
  */
 protected function getPheanstalk(&$name = null)
 {
     $pheanstalk = $this->locator->getPheanstalk($name);
     if (null === $name) {
         $name = 'default';
     }
     if (null === $pheanstalk) {
         throw new \RuntimeException('Pheanstalk not found: ' . $name);
     }
     if (!$pheanstalk->getConnection()->isServiceListening()) {
         throw new \RuntimeException('Pheanstalk not connected: ' . $name);
     }
     return $pheanstalk;
 }
 public function testGetDefaultPheanstalk()
 {
     $pheanstalkA = $this->getMockForAbstractClass(PheanstalkInterface::class);
     $pheanstalkB = $this->getMockForAbstractClass(PheanstalkInterface::class);
     $pheanstalkLocator = new PheanstalkLocator();
     $pheanstalkLocator->addPheanstalk('default', $pheanstalkA, true);
     $pheanstalkLocator->addPheanstalk('foo', $pheanstalkB);
     $this->assertEquals($pheanstalkA, $pheanstalkLocator->getPheanstalk());
     $this->assertEquals($pheanstalkA, $pheanstalkLocator->getDefaultPheanstalk());
     $this->assertEquals($pheanstalkA, $pheanstalkLocator->getPheanstalk('default'));
     $this->assertEquals($pheanstalkB, $pheanstalkLocator->getPheanstalk('foo'));
     $this->assertEquals(['default' => $pheanstalkA, 'foo' => $pheanstalkB], $pheanstalkLocator->getPheanstalks());
 }
 public function register(Container $app)
 {
     $app['pheanstalks.options.initializer'] = $app->protect(function () use($app) {
         static $initialized = false;
         if ($initialized) {
             return;
         }
         $initialized = true;
         if (!isset($app['pheanstalks.options'])) {
             $app['pheanstalks.options'] = ['default' => isset($app['pheanstalk.options']) ? $app['pheanstalk.options'] : []];
         }
         $app['pheanstalks.options'] = array_map(function ($options) use($app) {
             return array_replace($app['pheanstalk.default_options'], $options);
         }, $app['pheanstalks.options']);
         if (!isset($app['pheanstalks.default'])) {
             $app['pheanstalks.default'] = array_keys(array_slice($app['pheanstalks.options'], 0, 1))[0];
         }
     });
     $app['pheanstalk.listener.log'] = function () use($app) {
         $listener = new PheanstalkLogListener();
         if ($app['logger']) {
             $listener->setLogger($app['logger']);
         }
         return $listener;
     };
     $app['pheanstalk.proxy.factory'] = $app->protect(function ($name, PheanstalkInterface $pheanstalk) use($app) {
         $proxy = new PheanstalkProxy();
         $proxy->setName($name);
         $proxy->setPheanstalk($pheanstalk);
         $proxy->setDispatcher($app['dispatcher']);
         return $proxy;
     });
     $app['pheanstalks'] = function (Container $app) {
         $app['pheanstalks.options.initializer']();
         $locator = new PheanstalkLocator();
         foreach ($app['pheanstalks.options'] as $name => $options) {
             $pheanstalk = new Pheanstalk($options['server'], $options['port'], $options['timeout']);
             $locator->addPheanstalk($name, $app['pheanstalk.proxy.factory']($name, $pheanstalk), $app['pheanstalks.default'] === $name);
         }
         return $locator;
     };
     $app['pheanstalk.commands'] = $app->protect(function () use($app) {
         $locator = $app['pheanstalks'];
         return [new DeleteJobCommand($locator), new FlushTubeCommand($locator), new KickCommand($locator), new KickJobCommand($locator), new ListTubeCommand($locator), new NextReadyCommand($locator), new PauseTubeCommand($locator), new PeekCommand($locator), new PeekTubeCommand($locator), new PutCommand($locator), new StatsCommand($locator), new StatsJobCommand($locator), new StatsTubeCommand($locator)];
     });
     // shortcuts for the "first" pheanstalk
     $app['pheanstalk'] = function (Container $app) {
         $pheanstalks = $app['pheanstalks'];
         return $pheanstalks->getPheanstalk($app['pheanstalks.default']);
     };
     $app['pheanstalk.default_options'] = ['server' => '127.0.0.1', 'port' => PheanstalkInterface::DEFAULT_PORT, 'timeout' => PheanstalkInterface::DEFAULT_TTR];
     if (isset($app['console'])) {
         $app['console'] = $app->extend('console', function (ConsoleApplication $console, Container $app) {
             $commands = $app['pheanstalk.commands']();
             foreach ($commands as $command) {
                 $console->add($command);
             }
             return $console;
         });
     }
 }