|
21 | 21 | use DCarbone\PHPConsulAPI\KV\KVPair; |
22 | 22 | use DCarbone\PHPConsulAPI\QueryMeta; |
23 | 23 | use DCarbone\PHPConsulAPI\WriteMeta; |
| 24 | +use DCarbone\PHPConsulAPITests\ConsulManager; |
24 | 25 |
|
25 | 26 | /** |
26 | 27 | * Class KVClientUsageTest |
27 | 28 | * @package DCarbone\PHPConsulAPITests\Usage\KV |
28 | 29 | */ |
29 | 30 | class KVClientUsageTest extends \PHPUnit_Framework_TestCase { |
| 31 | + |
| 32 | + const KVKey = 'testkey'; |
| 33 | + const KVValue = 'testvalue'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Sets up the fixture, for example, open a network connection. |
| 37 | + * This method is called before a test is executed. |
| 38 | + */ |
| 39 | + protected function setUp() { |
| 40 | + ConsulManager::startSingle(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Tears down the fixture, for example, close a network connection. |
| 45 | + * This method is called after a test is executed. |
| 46 | + */ |
| 47 | + protected function tearDown() { |
| 48 | + ConsulManager::stopSingle(); |
| 49 | + } |
| 50 | + |
30 | 51 | /** |
31 | 52 | * @return KVClient |
32 | 53 | */ |
33 | 54 | public function testCanConstructClient() { |
34 | | - $kv = new KVClient(Config::newDefaultConfig()); |
| 55 | + $kv = new KVClient(new Config()); |
35 | 56 | $this->assertInstanceOf(KVClient::class, $kv); |
36 | 57 | return $kv; |
37 | 58 | } |
38 | 59 |
|
39 | 60 | /** |
40 | 61 | * @depends testCanConstructClient |
41 | | - * @param KVClient $client |
42 | 62 | */ |
43 | | - public function testKVLifecycle(KVClient $client) { |
44 | | - $kvp = new KVPair(['Key' => 'testkey', 'Value' => 'testvalue']); |
| 63 | + public function testCanPutKey() { |
| 64 | + $client = new KVClient(new Config()); |
45 | 65 |
|
46 | | - list($wm, $err) = $client->put($kvp);; |
| 66 | + list($wm, $err) = $client->put(new KVPair(['Key' => self::KVKey, 'Value' => self::KVValue])); |
47 | 67 | $this->assertNull($err, sprintf('Unable to set kvp: %s', (string)$err)); |
48 | 68 | $this->assertInstanceOf(WriteMeta::class, $wm); |
| 69 | + } |
49 | 70 |
|
50 | | - list($kvp, $qm, $err) = $client->get('testkey'); |
51 | | - $this->assertNull($err, sprintf('Unable to get kvp: %s', (string)$err)); |
| 71 | + /** |
| 72 | + * @depends testCanPutKey |
| 73 | + */ |
| 74 | + public function testCanGetKey() { |
| 75 | + $client = new KVClient(new Config()); |
| 76 | + $client->put(new KVPair(['Key' => self::KVKey, 'Value' => self::KVValue])); |
| 77 | + |
| 78 | + list($kv, $qm, $err) = $client->get(self::KVKey); |
| 79 | + $this->assertNull($err, sprintf('KV::get returned error: %s', (string)$err)); |
52 | 80 | $this->assertInstanceOf(QueryMeta::class, $qm); |
53 | | - $this->assertInstanceOf(KVPair::class, $kvp); |
54 | | - $this->assertEquals('testvalue', $kvp->Value); |
| 81 | + $this->assertInstanceOf(KVPair::class, $kv); |
| 82 | + } |
55 | 83 |
|
56 | | - list($wm, $err) = $client->delete('testkey'); |
57 | | - $this->assertNull($err, sprintf('Unable to delete kvp: %s', (string)$err)); |
58 | | - $this->assertInstanceOf(WriteMeta::class, $wm); |
| 84 | + /** |
| 85 | + * @depends testCanPutKey |
| 86 | + */ |
| 87 | + public function testCanDeleteKey() { |
| 88 | + $client = new KVClient(new Config()); |
| 89 | + $client->put(new KVPair(['Key' => self::KVKey, 'Value' => self::KVValue])); |
59 | 90 |
|
60 | | - list($kvp, $qm, $err) = $client->get('testkey'); |
61 | | - $this->assertNull($err); |
62 | | - $this->assertInstanceOf(QueryMeta::class, $qm); |
63 | | - $this->assertNull($kvp); |
| 91 | + list($wm, $err) = $client->delete(self::KVKey); |
| 92 | + $this->assertNull($err, sprintf('KV::delete returned error: %s', $err)); |
| 93 | + $this->assertInstanceOf( |
| 94 | + WriteMeta::class, |
| 95 | + $wm, |
| 96 | + sprintf( |
| 97 | + 'expected "%s", saw "%s"', |
| 98 | + WriteMeta::class, |
| 99 | + is_object($wm) ? get_class($wm) : gettype($wm) |
| 100 | + )); |
64 | 101 | } |
65 | 102 | } |
0 commit comments