MongoDbProfilerStorageTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\Profiler;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  14. use Symfony\Component\HttpKernel\Profiler\MongoDbProfilerStorage;
  15. use Symfony\Component\HttpKernel\Profiler\Profile;
  16. class MongoDbProfilerStorageTestDataCollector extends DataCollector
  17. {
  18. public function setData($data)
  19. {
  20. $this->data = $data;
  21. }
  22. public function getData()
  23. {
  24. return $this->data;
  25. }
  26. public function collect(Request $request, Response $response, \Exception $exception = null)
  27. {
  28. }
  29. public function getName()
  30. {
  31. return 'test_data_collector';
  32. }
  33. }
  34. /**
  35. * @group legacy
  36. * @requires extension mongo
  37. */
  38. class MongoDbProfilerStorageTest extends AbstractProfilerStorageTest
  39. {
  40. private $storage;
  41. public function getDsns()
  42. {
  43. return array(
  44. array('mongodb://localhost/symfony_tests/profiler_data', array(
  45. 'mongodb://localhost/symfony_tests',
  46. 'symfony_tests',
  47. 'profiler_data',
  48. )),
  49. array('mongodb://user:password@localhost/symfony_tests/profiler_data', array(
  50. 'mongodb://user:password@localhost/symfony_tests',
  51. 'symfony_tests',
  52. 'profiler_data',
  53. )),
  54. array('mongodb://user:password@localhost/admin/symfony_tests/profiler_data', array(
  55. 'mongodb://user:password@localhost/admin',
  56. 'symfony_tests',
  57. 'profiler_data',
  58. )),
  59. array('mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin/symfony_tests/profiler_data', array(
  60. 'mongodb://user:password@localhost:27009,localhost:27010/?replicaSet=rs-name&authSource=admin',
  61. 'symfony_tests',
  62. 'profiler_data',
  63. )),
  64. );
  65. }
  66. public function testCleanup()
  67. {
  68. $dt = new \DateTime('-2 day');
  69. for ($i = 0; $i < 3; ++$i) {
  70. $dt->modify('-1 day');
  71. $profile = new Profile('time_'.$i);
  72. $profile->setTime($dt->getTimestamp());
  73. $profile->setMethod('GET');
  74. $this->storage->write($profile);
  75. }
  76. $records = $this->storage->find('', '', 3, 'GET');
  77. $this->assertCount(1, $records, '->find() returns only one record');
  78. $this->assertEquals($records[0]['token'], 'time_2', '->find() returns the latest added record');
  79. $this->storage->purge();
  80. }
  81. /**
  82. * @dataProvider getDsns
  83. */
  84. public function testDsnParser($dsn, $expected)
  85. {
  86. $m = new \ReflectionMethod($this->storage, 'parseDsn');
  87. $m->setAccessible(true);
  88. $this->assertEquals($expected, $m->invoke($this->storage, $dsn));
  89. }
  90. public function testUtf8()
  91. {
  92. $profile = new Profile('utf8_test_profile');
  93. $data = 'HЁʃʃϿ, ϢorЃd!';
  94. $nonUtf8Data = iconv('UTF-8', 'UCS-2', $data);
  95. $collector = new MongoDbProfilerStorageTestDataCollector();
  96. $collector->setData($nonUtf8Data);
  97. $profile->setCollectors(array($collector));
  98. $this->storage->write($profile);
  99. $readProfile = $this->storage->read('utf8_test_profile');
  100. $collectors = $readProfile->getCollectors();
  101. $this->assertCount(1, $collectors);
  102. $this->assertArrayHasKey('test_data_collector', $collectors);
  103. $this->assertEquals($nonUtf8Data, $collectors['test_data_collector']->getData(), 'Non-UTF8 data is properly encoded/decoded');
  104. }
  105. /**
  106. * @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
  107. */
  108. protected function getStorage()
  109. {
  110. return $this->storage;
  111. }
  112. protected function setUp()
  113. {
  114. $this->storage = new MongoDbProfilerStorage('mongodb://localhost/symfony_tests/profiler_data', '', '', 86400);
  115. $m = new \ReflectionMethod($this->storage, 'getMongo');
  116. $m->setAccessible(true);
  117. try {
  118. $m->invoke($this->storage);
  119. } catch (\MongoConnectionException $e) {
  120. $this->markTestSkipped('A MongoDB server on localhost is required.');
  121. }
  122. $this->storage->purge();
  123. }
  124. protected function tearDown()
  125. {
  126. $this->storage->purge();
  127. }
  128. }