public function testStartsWith() { $this->assertTrue(StrUtil::startsWith('foobar', 'foo'), 'foobar starts with foo'); $this->assertTrue(StrUtil::startsWith('foobar', ''), 'foobar starts with empty string'); $this->assertTrue(StrUtil::startsWith('', ''), 'empty string starts with empty string'); $this->assertFalse(StrUtil::startsWith('foobar', 'baz'), 'foobar does not start with baz'); $this->assertFalse(StrUtil::startsWith('foobar', 'bar'), 'foobar does not start with bar'); $this->assertFalse(StrUtil::startsWith('foobar', 'foobars'), 'foobar does not start with foobars'); }
/** * Ensure that all fake data we use are valid FileStorage * data files. This should ensure, that fake backend and * other services depending on fake data will not encounter * broken data. */ public function testAllData() { $dir = FakeData::getDirectory(); $storage = new FileStorage(array("root_path" => $dir . '/')); $it = new RecursiveDirectoryIterator($dir); $nonrecursiveIt = new RecursiveIteratorIterator($it); $regexIt = new RegexIterator($nonrecursiveIt, '@\\.dat$@'); foreach ($regexIt as $file) { $this->assertTrue($file->isFile()); $key = $file->getPathname(); assert(StrUtil::startsWith($key, $dir)); // remove $dir from beginning of $key $key = substr($key, strlen($dir)); $key = preg_replace("@\\.dat\$@", "", $key); $data = $storage->read($key); $this->assertTrue(null !== $data); } }
/** * Joins two or more path components. * If joining more than two path components, the result is * the same as calling two-argument joinPath successively. * Moreover, this function is associative, i.e. * joinPath('a','b','c') has the same effect as * joinPath(joinPath('a', 'b'), 'c') or joinPath('a', joinPath('b', 'c')) * * Path components of zero length are ignored * * @param string $a first path component * @param string $b second path component * @param string ... any other path components to join * @returns string all the paths joined using a directory separator */ public static function joinPath($a, $b) { $args = func_get_args(); $num_args = count($args); $shouldAddDS = true; // start with $a, omit trailing directory separator if ($a == DIRECTORY_SEPARATOR || $a == '') { $shouldAddDS = false; $path = $a; } else { if (StrUtil::endsWith($a, DIRECTORY_SEPARATOR)) { $path = substr($a, 0, strlen($a) - 1); } else { $path = $a; } } // add other components for ($i = 1; $i < $num_args; $i++) { $part = $args[$i]; // DIRECTORY_SEPARATOR or empty string is a special case if ($part == DIRECTORY_SEPARATOR) { continue; } // first extract range of part without leading or trailing DS if (StrUtil::startsWith($part, DIRECTORY_SEPARATOR)) { $start = 1; } else { $start = 0; } if (StrUtil::endsWith($part, DIRECTORY_SEPARATOR)) { $end = strlen($part) - 1; } else { $end = strlen($part); } // append a path component if ($shouldAddDS) { $path .= DIRECTORY_SEPARATOR; } $shouldAddDS = true; $path .= substr($part, $start, $end); } return $path; }