|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Nexmo Client Library for PHP |
| 4 | + * |
| 5 | + * @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com) |
| 6 | + * @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License |
| 7 | + */ |
| 8 | + |
| 9 | +namespace NexmoTest\Client\Credentials; |
| 10 | + |
| 11 | + |
| 12 | +use Nexmo\Client\Credentials\Container; |
| 13 | +use Nexmo\Client\Credentials\Keypair; |
| 14 | +use Nexmo\Client\Credentials\Basic; |
| 15 | +use Nexmo\Client\Credentials\OAuth; |
| 16 | +use Nexmo\Client\Credentials\SharedSecret; |
| 17 | +use Webmozart\Expression\Selector\Key; |
| 18 | + |
| 19 | +class ContainerTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + protected $types = [ |
| 22 | + Basic::class, |
| 23 | + SharedSecret::class, |
| 24 | + Keypair::class |
| 25 | + ]; |
| 26 | + |
| 27 | + protected $basic; |
| 28 | + protected $secret; |
| 29 | + protected $keypair; |
| 30 | + |
| 31 | + public function setUp() |
| 32 | + { |
| 33 | + $this->basic = new Basic('key', 'secret'); |
| 34 | + $this->secret = new SharedSecret('key', 'secret'); |
| 35 | + $this->keypair = new Keypair('key', 'app'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @dataProvider credentials |
| 40 | + */ |
| 41 | + public function testBasic($credential, $type) |
| 42 | + { |
| 43 | + $container = new Container($credential); |
| 44 | + |
| 45 | + $this->assertSame($credential, $container->get($type)); |
| 46 | + $this->assertSame($credential, $container[$type]); |
| 47 | + |
| 48 | + foreach($this->types as $class){ |
| 49 | + if($type == $class){ |
| 50 | + $this->assertTrue($container->has($class)); |
| 51 | + } else { |
| 52 | + $this->assertFalse($container->has($class)); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @dataProvider credentials |
| 59 | + */ |
| 60 | + public function testOnlyOneType($credential, $type) |
| 61 | + { |
| 62 | + $other = clone $credential; |
| 63 | + |
| 64 | + $this->expectException('RuntimeException'); |
| 65 | + |
| 66 | + $container = new Container($credential, $other); |
| 67 | + } |
| 68 | + |
| 69 | + public function testMultiple() |
| 70 | + { |
| 71 | + $container = new Container($this->basic, $this->secret, $this->keypair); |
| 72 | + |
| 73 | + foreach($this->types as $class){ |
| 74 | + $this->assertTrue($container->has($class)); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + public function credentials() |
| 80 | + { |
| 81 | + return [ |
| 82 | + [new Basic('key', 'secret'), Basic::class], |
| 83 | + [new SharedSecret('key', 'secret'), SharedSecret::class], |
| 84 | + [new Keypair('key', 'app'), Keypair::class] |
| 85 | + ]; |
| 86 | + } |
| 87 | +} |
0 commit comments