Chris@17
|
1 <?php
|
Chris@17
|
2 namespace TYPO3\PharStreamWrapper;
|
Chris@17
|
3
|
Chris@17
|
4 /*
|
Chris@17
|
5 * This file is part of the TYPO3 project.
|
Chris@17
|
6 *
|
Chris@17
|
7 * It is free software; you can redistribute it and/or modify it under the terms
|
Chris@17
|
8 * of the MIT License (MIT). For the full copyright and license information,
|
Chris@17
|
9 * please read the LICENSE file that was distributed with this source code.
|
Chris@17
|
10 *
|
Chris@17
|
11 * The TYPO3 project - inspiring people to share!
|
Chris@17
|
12 */
|
Chris@17
|
13
|
Chris@17
|
14 class Behavior implements Assertable
|
Chris@17
|
15 {
|
Chris@17
|
16 const COMMAND_DIR_OPENDIR = 'dir_opendir';
|
Chris@17
|
17 const COMMAND_MKDIR = 'mkdir';
|
Chris@17
|
18 const COMMAND_RENAME = 'rename';
|
Chris@17
|
19 const COMMAND_RMDIR = 'rmdir';
|
Chris@17
|
20 const COMMAND_STEAM_METADATA = 'stream_metadata';
|
Chris@17
|
21 const COMMAND_STREAM_OPEN = 'stream_open';
|
Chris@17
|
22 const COMMAND_UNLINK = 'unlink';
|
Chris@17
|
23 const COMMAND_URL_STAT = 'url_stat';
|
Chris@17
|
24
|
Chris@17
|
25 /**
|
Chris@17
|
26 * @var string[]
|
Chris@17
|
27 */
|
Chris@17
|
28 private $availableCommands = array(
|
Chris@17
|
29 self::COMMAND_DIR_OPENDIR,
|
Chris@17
|
30 self::COMMAND_MKDIR,
|
Chris@17
|
31 self::COMMAND_RENAME,
|
Chris@17
|
32 self::COMMAND_RMDIR,
|
Chris@17
|
33 self::COMMAND_STEAM_METADATA,
|
Chris@17
|
34 self::COMMAND_STREAM_OPEN,
|
Chris@17
|
35 self::COMMAND_UNLINK,
|
Chris@17
|
36 self::COMMAND_URL_STAT,
|
Chris@17
|
37 );
|
Chris@17
|
38
|
Chris@17
|
39 /**
|
Chris@17
|
40 * @var Assertable[]
|
Chris@17
|
41 */
|
Chris@17
|
42 private $assertions;
|
Chris@17
|
43
|
Chris@17
|
44 /**
|
Chris@17
|
45 * @param Assertable $assertable
|
Chris@17
|
46 * @return static
|
Chris@17
|
47 */
|
Chris@17
|
48 public function withAssertion(Assertable $assertable)
|
Chris@17
|
49 {
|
Chris@17
|
50 $commands = func_get_args();
|
Chris@17
|
51 array_shift($commands);
|
Chris@17
|
52 $this->assertCommands($commands);
|
Chris@17
|
53 $commands = $commands ?: $this->availableCommands;
|
Chris@17
|
54
|
Chris@17
|
55 $target = clone $this;
|
Chris@17
|
56 foreach ($commands as $command) {
|
Chris@17
|
57 $target->assertions[$command] = $assertable;
|
Chris@17
|
58 }
|
Chris@17
|
59 return $target;
|
Chris@17
|
60 }
|
Chris@17
|
61
|
Chris@17
|
62 /**
|
Chris@17
|
63 * @param string $path
|
Chris@17
|
64 * @param string $command
|
Chris@17
|
65 * @return bool
|
Chris@17
|
66 */
|
Chris@17
|
67 public function assert($path, $command)
|
Chris@17
|
68 {
|
Chris@17
|
69 $this->assertCommand($command);
|
Chris@17
|
70 $this->assertAssertionCompleteness();
|
Chris@17
|
71
|
Chris@17
|
72 return $this->assertions[$command]->assert($path, $command);
|
Chris@17
|
73 }
|
Chris@17
|
74
|
Chris@17
|
75 /**
|
Chris@17
|
76 * @param array $commands
|
Chris@17
|
77 */
|
Chris@17
|
78 private function assertCommands(array $commands)
|
Chris@17
|
79 {
|
Chris@17
|
80 $unknownCommands = array_diff($commands, $this->availableCommands);
|
Chris@17
|
81 if (empty($unknownCommands)) {
|
Chris@17
|
82 return;
|
Chris@17
|
83 }
|
Chris@17
|
84 throw new \LogicException(
|
Chris@17
|
85 sprintf(
|
Chris@17
|
86 'Unknown commands: %s',
|
Chris@17
|
87 implode(', ', $unknownCommands)
|
Chris@17
|
88 ),
|
Chris@17
|
89 1535189881
|
Chris@17
|
90 );
|
Chris@17
|
91 }
|
Chris@17
|
92
|
Chris@17
|
93 private function assertCommand($command)
|
Chris@17
|
94 {
|
Chris@17
|
95 if (in_array($command, $this->availableCommands, true)) {
|
Chris@17
|
96 return;
|
Chris@17
|
97 }
|
Chris@17
|
98 throw new \LogicException(
|
Chris@17
|
99 sprintf(
|
Chris@17
|
100 'Unknown command "%s"',
|
Chris@17
|
101 $command
|
Chris@17
|
102 ),
|
Chris@17
|
103 1535189882
|
Chris@17
|
104 );
|
Chris@17
|
105 }
|
Chris@17
|
106
|
Chris@17
|
107 private function assertAssertionCompleteness()
|
Chris@17
|
108 {
|
Chris@17
|
109 $undefinedAssertions = array_diff(
|
Chris@17
|
110 $this->availableCommands,
|
Chris@17
|
111 array_keys($this->assertions)
|
Chris@17
|
112 );
|
Chris@17
|
113 if (empty($undefinedAssertions)) {
|
Chris@17
|
114 return;
|
Chris@17
|
115 }
|
Chris@17
|
116 throw new \LogicException(
|
Chris@17
|
117 sprintf(
|
Chris@17
|
118 'Missing assertions for commands: %s',
|
Chris@17
|
119 implode(', ', $undefinedAssertions)
|
Chris@17
|
120 ),
|
Chris@17
|
121 1535189883
|
Chris@17
|
122 );
|
Chris@17
|
123 }
|
Chris@17
|
124 }
|