|
| 1 | +<?php |
| 2 | + |
| 3 | +use Psr\Container\ContainerInterface; |
| 4 | +use Psr\Container\NotFoundExceptionInterface; |
| 5 | +use Technically\ArrayContainer\Exceptions\ServiceNotFound; |
| 6 | +use Technically\ArrayContainer\ArrayContainer; |
| 7 | + |
| 8 | +describe('ArrayContainer', function() { |
| 9 | + |
| 10 | + it('should instantiate a new instance', function() { |
| 11 | + $container = new ArrayContainer(); |
| 12 | + assert($container instanceof ArrayContainer); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should implement PSR container interface', function() { |
| 16 | + $container = new ArrayContainer(); |
| 17 | + assert($container instanceof ContainerInterface); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should instantiate a new instance with array of entries', function() { |
| 21 | + $container = new ArrayContainer([ |
| 22 | + 'a' => 'A', |
| 23 | + 'b' => 'B', |
| 24 | + ]); |
| 25 | + assert($container->has('a')); |
| 26 | + assert($container->has('b')); |
| 27 | + assert($container->toArray() === [ |
| 28 | + 'a' => 'A', |
| 29 | + 'b' => 'B', |
| 30 | + ]); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('->has()', function() { |
| 34 | + it('should return true for defined entries', function () { |
| 35 | + $container = new ArrayContainer([ |
| 36 | + 'a' => 'A', |
| 37 | + 'b' => 'B', |
| 38 | + ]); |
| 39 | + |
| 40 | + assert($container->has('a') === true); |
| 41 | + assert($container->has('b') === true); |
| 42 | + }); |
| 43 | + it('should return false for entries not defined', function () { |
| 44 | + $container = new ArrayContainer([ |
| 45 | + 'a' => 'A', |
| 46 | + 'b' => 'B', |
| 47 | + ]); |
| 48 | + |
| 49 | + assert($container->has('A') === false); |
| 50 | + assert($container->has('B') === false); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('->get()', function() { |
| 55 | + it('should return entries by identifier', function () { |
| 56 | + $now = new DateTime('now'); |
| 57 | + $container = new ArrayContainer([ |
| 58 | + 'greeting' => 'Hello', |
| 59 | + 'date' => $now, |
| 60 | + ]); |
| 61 | + |
| 62 | + assert($container->get('greeting') === 'Hello'); |
| 63 | + assert($container->get('date') === $now); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should throw ServiceNotFound exception for entries not defined', function () { |
| 67 | + $container = new ArrayContainer(['a' => 'A']); |
| 68 | + |
| 69 | + assert($container->has('b') === false); |
| 70 | + |
| 71 | + try { |
| 72 | + $container->get('b'); |
| 73 | + } catch (ServiceNotFound $exception) { |
| 74 | + // passthru |
| 75 | + } |
| 76 | + |
| 77 | + assert(isset($exception)); |
| 78 | + assert($exception instanceof ServiceNotFound); |
| 79 | + assert($exception instanceof NotFoundExceptionInterface); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('->set()', function() { |
| 84 | + it('should set entries for identifier', function () { |
| 85 | + $now = new DateTime('now'); |
| 86 | + $container = new ArrayContainer(); |
| 87 | + |
| 88 | + assert($container->has('date') === false); |
| 89 | + |
| 90 | + $container->set('date', $now); |
| 91 | + |
| 92 | + assert($container->has('date') === true); |
| 93 | + assert($container->get('date') === $now); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should overwrite previously defined entries for the same identifier', function () { |
| 97 | + $container = new ArrayContainer(['a' => 'A']); |
| 98 | + |
| 99 | + assert($container->get('a') === 'A'); |
| 100 | + |
| 101 | + $container->set('a', 'B'); |
| 102 | + |
| 103 | + assert($container->get('a') === 'B'); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('->toArray()', function() { |
| 108 | + it('should return all container entries in array', function () { |
| 109 | + $container = new ArrayContainer(['a' => 'A']); |
| 110 | + |
| 111 | + assert($container->toArray() === ['a' => 'A']); |
| 112 | + |
| 113 | + $container->set('b', 'B'); |
| 114 | + |
| 115 | + assert($container->toArray() === ['a' => 'A', 'b' => 'B']); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments