| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Tests\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ds\Cache\CacheStorageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Ds\Cache\Storage\MemcacheStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Class MemcacheStorageTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @package Tests\Cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class MemcacheStorageTest extends \PHPUnit\Framework\TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var \DateInterval | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public $ttl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var \PHPUnit_Framework_MockObject_MockObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public $memcacheMock; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var \Memcached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public $memcacheStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Set up. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->ttl = new \DateInterval('P1M'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->memcacheMock = $this->getMockBuilder('\Memcached')->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->memcacheStorage = new MemcacheStorage($this->memcacheMock,'localhost',11211, $this->ttl); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function testAddServer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $server = 'localhost'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $port = '11211'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->memcacheMock->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->method('addServer') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 $this->equalTo($server), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 $this->equalTo($port) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->memcacheStorage->addServer($server, $port); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 58 |  | View Code Duplication |     public function testFlush() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $delay = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->memcacheMock->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ->method('flush') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $this->equalTo($delay) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->memcacheStorage->flush($delay); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @dataProvider hasCacheProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function testHas($expected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $key = 'key'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->memcacheMock->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ->method('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $this->equalTo($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ->willReturn($expected); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->assertEquals($expected, $this->memcacheStorage->has($key)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     public function hasCacheProvider() | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         return array( | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             [true], | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             [false] | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function testSet() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $key = 'key'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $value = 'value'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $expires = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->memcacheMock->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ->method('set') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $this->equalTo($key), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $this->equalTo($value), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $this->equalTo($expires) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $this->memcacheStorage->set($key, $value, $expires); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 104 |  | View Code Duplication |     public function testGet() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $key = 'key'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->memcacheMock->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ->method('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $this->equalTo($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->memcacheStorage->get($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 114 |  | View Code Duplication |     public function testDelete() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $key = 'key'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->memcacheMock->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             ->method('delete') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $this->equalTo($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->memcacheStorage->delete($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.