Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord\Testing; |
| 4 | |
| 5 | use LdapRecord\Container; |
| 6 | |
| 7 | class DirectoryFake |
| 8 | { |
| 9 | /** |
| 10 | * Setup the fake connection. |
| 11 | * |
| 12 | * @param string|null $name |
| 13 | * |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 14 | * @return ConnectionFake |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 15 | * |
| 16 | * @throws \LdapRecord\ContainerException |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 17 | */ |
| 18 | public static function setup($name = null) |
| 19 | { |
| 20 | $connection = Container::getConnection($name); |
| 21 | |
| 22 | $fake = static::makeConnectionFake( |
| 23 | $connection->getConfiguration()->all() |
| 24 | ); |
| 25 | |
| 26 | // Replace the connection with a fake. |
| 27 | Container::addConnection($fake, $name); |
| 28 | |
| 29 | return $fake; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Reset the container. |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public static function tearDown() |
| 38 | { |
| 39 | Container::reset(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Make a connection fake. |
| 44 | * |
| 45 | * @param array $config |
| 46 | * |
| 47 | * @return ConnectionFake |
| 48 | */ |
| 49 | public static function makeConnectionFake(array $config = []) |
| 50 | { |
| 51 | return ConnectionFake::make($config)->shouldBeConnected(); |
| 52 | } |
| 53 | } |