|
| 1 | +
|
| 2 | +/** |
| 3 | + * Copyright 2016 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +namespace Google\Cloud\Test; |
| 18 | + |
| 19 | +use Silex\WebTestCase; |
| 20 | +use GeckoPackages\MemcacheMock\MemcachedMock; |
| 21 | + |
| 22 | +class LocalTest extends WebTestCase |
| 23 | +{ |
| 24 | + public function setUp() |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + $this->client = $this->createClient(); |
| 28 | + } |
| 29 | + |
| 30 | + public function createApplication() |
| 31 | + { |
| 32 | + $app = require __DIR__ . '/../app.php'; |
| 33 | + $app['memcached'] = new MemcachedMock; |
| 34 | + $app['memcached']->addServer("localhost", 11211); |
| 35 | + return $app; |
| 36 | + } |
| 37 | + |
| 38 | + public function testIndex() |
| 39 | + { |
| 40 | + // Access the modules app top page. |
| 41 | + $client = $this->client; |
| 42 | + $client->request('GET', '/'); |
| 43 | + $this->assertTrue($client->getResponse()->isOk()); |
| 44 | + |
| 45 | + // Make sure it handles a POST request too, which will increment the |
| 46 | + // counter. |
| 47 | + $this->client->request('POST', '/'); |
| 48 | + $this->assertTrue($this->client->getResponse()->isOk()); |
| 49 | + } |
| 50 | + |
| 51 | + public function testGetAndPut() |
| 52 | + { |
| 53 | + // Use a random key to avoid colliding with simultaneous tests. |
| 54 | + $key = rand(0, 1000); |
| 55 | + |
| 56 | + // Test the /memcached REST API. |
| 57 | + $this->put("/memcached/test$key", "sour"); |
| 58 | + $this->assertEquals("sour", $this->get("/memcached/test$key")); |
| 59 | + $this->put("/memcached/test$key", "sweet"); |
| 60 | + $this->assertEquals("sweet", $this->get("/memcached/test$key")); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * HTTP PUTs the body to the url path. |
| 65 | + * @param $path string |
| 66 | + * @param $body string |
| 67 | + */ |
| 68 | + private function put($path, $body) |
| 69 | + { |
| 70 | + $this->client->request('PUT', $path, array(), array(), array(), $body); |
| 71 | + return $this->client->getResponse()->getContent(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * HTTP GETs the url path. |
| 76 | + * @param $path string |
| 77 | + * @return string The HTTP Response. |
| 78 | + */ |
| 79 | + private function get($path) |
| 80 | + { |
| 81 | + $this->client->request('GET', $path); |
| 82 | + return $this->client->getResponse()->getContent(); |
| 83 | + } |
| 84 | +} |
0 commit comments