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