You are here

class MongoDbProfilerStorageTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-kernel/Tests/Profiler/MongoDbProfilerStorageTest.php \Symfony\Component\HttpKernel\Tests\Profiler\MongoDbProfilerStorageTest

Hierarchy

Expanded class hierarchy of MongoDbProfilerStorageTest

File

vendor/symfony/http-kernel/Tests/Profiler/MongoDbProfilerStorageTest.php, line 50

Namespace

Symfony\Component\HttpKernel\Tests\Profiler
View source
class MongoDbProfilerStorageTest extends AbstractProfilerStorageTest {
  protected static $storage;
  public static function setUpBeforeClass() {
    if (extension_loaded('mongo')) {
      self::$storage = new DummyMongoDbProfilerStorage('mongodb://localhost/symfony_tests/profiler_data', '', '', 86400);
      try {
        self::$storage
          ->getMongo();
      } catch (\MongoConnectionException $e) {
        self::$storage = null;
      }
    }
  }
  public static function tearDownAfterClass() {
    if (self::$storage) {
      self::$storage
        ->purge();
      self::$storage = null;
    }
  }
  public function getDsns() {
    return array(
      array(
        'mongodb://localhost/symfony_tests/profiler_data',
        array(
          'mongodb://localhost/symfony_tests',
          'symfony_tests',
          'profiler_data',
        ),
      ),
      array(
        'mongodb://user:password@localhost/symfony_tests/profiler_data',
        array(
          'mongodb://user:password@localhost/symfony_tests',
          'symfony_tests',
          'profiler_data',
        ),
      ),
      array(
        'mongodb://user:password@localhost/admin/symfony_tests/profiler_data',
        array(
          'mongodb://user:password@localhost/admin',
          'symfony_tests',
          'profiler_data',
        ),
      ),
      array(
        'mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin/symfony_tests/profiler_data',
        array(
          'mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin',
          'symfony_tests',
          'profiler_data',
        ),
      ),
    );
  }
  public function testCleanup() {
    $dt = new \DateTime('-2 day');
    for ($i = 0; $i < 3; ++$i) {
      $dt
        ->modify('-1 day');
      $profile = new Profile('time_' . $i);
      $profile
        ->setTime($dt
        ->getTimestamp());
      $profile
        ->setMethod('GET');
      self::$storage
        ->write($profile);
    }
    $records = self::$storage
      ->find('', '', 3, 'GET');
    $this
      ->assertCount(1, $records, '->find() returns only one record');
    $this
      ->assertEquals($records[0]['token'], 'time_2', '->find() returns the latest added record');
    self::$storage
      ->purge();
  }

  /**
   * @dataProvider getDsns
   */
  public function testDsnParser($dsn, $expected) {
    $m = new \ReflectionMethod(self::$storage, 'parseDsn');
    $m
      ->setAccessible(true);
    $this
      ->assertEquals($expected, $m
      ->invoke(self::$storage, $dsn));
  }
  public function testUtf8() {
    $profile = new Profile('utf8_test_profile');
    $data = 'HЁʃʃϿ, ϢorЃd!';
    $nonUtf8Data = mb_convert_encoding($data, 'UCS-2');
    $collector = new MongoDbProfilerStorageTestDataCollector();
    $collector
      ->setData($nonUtf8Data);
    $profile
      ->setCollectors(array(
      $collector,
    ));
    self::$storage
      ->write($profile);
    $readProfile = self::$storage
      ->read('utf8_test_profile');
    $collectors = $readProfile
      ->getCollectors();
    $this
      ->assertCount(1, $collectors);
    $this
      ->assertArrayHasKey('test_data_collector', $collectors);
    $this
      ->assertEquals($nonUtf8Data, $collectors['test_data_collector']
      ->getData(), 'Non-UTF8 data is properly encoded/decoded');
  }

  /**
   * @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
   */
  protected function getStorage() {
    return self::$storage;
  }
  protected function setUp() {
    if (self::$storage) {
      self::$storage
        ->purge();
    }
    else {
      $this
        ->markTestSkipped('MongoDbProfilerStorageTest requires the mongo PHP extension and a MongoDB server on localhost');
    }
  }

}

Members