How to use testIsDir method of file class

Best Atoum code snippet using file.testIsDir

FileTest.php

Source:FileTest.php Github

copy

Full Screen

...168 *169 * @param string|null $input170 * @param bool $expected171 */172 public function testIsDir(?string $input, bool $expected) {173 clearstatcache();174 $this->assertSame($expected, CRM_Utils_File::isDir($input));175 }176 /**177 * Test isDir with invalid args.178 *179 * @dataProvider isDirInvalidArgsProvider180 *181 * @param mixed $input182 * @param bool $expected183 */184 public function testIsDirInvalidArgs($input, bool $expected) {185 $this->assertSame($expected, CRM_Utils_File::isDir($input));186 }187 /**188 * Just trying to include some of the same tests as php itself and189 * this doesn't fit in well to a dataprovider so is separate.190 */191 public function testIsDirMkdir() {192 $a_dir = sys_get_temp_dir() . '/testIsDir';193 // I think temp is global to the test node, so if any test failed on this194 // in the past it doesn't get cleaned up and so already exists.195 system('rm -rf ' . escapeshellarg($a_dir));196 mkdir($a_dir);197 $this->assertTrue(CRM_Utils_File::isDir($a_dir));198 mkdir($a_dir . '/aSubDir');199 $this->assertTrue(CRM_Utils_File::isDir($a_dir . '/aSubDir'));200 clearstatcache();201 $this->assertTrue(CRM_Utils_File::isDir($a_dir));202 rmdir($a_dir . '/aSubDir');203 rmdir($a_dir);204 }205 /**206 * testIsDirSlashVariations207 */208 public function testIsDirSlashVariations() {209 $a_dir = sys_get_temp_dir() . '/testIsDir';210 // I think temp is global to the test node, so if any test failed on this211 // in the past it doesn't get cleaned up and so already exists.212 system('rm -rf ' . escapeshellarg($a_dir));213 mkdir($a_dir);214 $old_cwd = getcwd();215 $this->assertTrue(chdir(sys_get_temp_dir()));216 $this->assertTrue(CRM_Utils_File::isDir("./testIsDir"));217 clearstatcache();218 $this->assertTrue(CRM_Utils_File::isDir("testIsDir/"));219 clearstatcache();220 $this->assertTrue(CRM_Utils_File::isDir("./testIsDir/"));221 clearstatcache();222 $this->assertTrue(CRM_Utils_File::isDir("testIsDir//"));223 clearstatcache();224 $this->assertTrue(CRM_Utils_File::isDir("./testIsDir//"));225 clearstatcache();226 $this->assertTrue(CRM_Utils_File::isDir(".//testIsDir//"));227 clearstatcache();228 $this->assertFalse(CRM_Utils_File::isDir('testIsDir*'));229 // Note that in php8 is_dir changed in php itself to return false with no warning for these. It used to give `is_dir() expects parameter 1 to be a valid path, string given`. See https://github.com/php/php-src/commit/7bc7a80445f2bb349891d3cccfef2d589c48607e230 clearstatcache();231 if (version_compare(PHP_VERSION, '8.0.0', '<')) {232 $this->assertNull(CRM_Utils_File::isDir('./testIsDir/' . chr(0)));233 clearstatcache();234 $this->assertNull(CRM_Utils_File::isDir("testIsDir\0"));235 }236 else {237 $this->assertFalse(CRM_Utils_File::isDir('./testIsDir/' . chr(0)));238 clearstatcache();239 $this->assertFalse(CRM_Utils_File::isDir("testIsDir\0"));240 }241 $this->assertTrue(chdir($old_cwd));242 rmdir($a_dir);243 }244 /**245 * Test hard and soft links with isDir246 * Note hard links to directories aren't allowed so can only test with file.247 */248 public function testIsDirLinks() {249 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {250 $this->markTestSkipped('Windows has links but not the same.');251 }252 $a_dir = sys_get_temp_dir() . '/testIsDir';253 // I think temp is global to the test node, so if any test failed on this254 // in the past it doesn't get cleaned up and so already exists.255 system('rm -rf ' . escapeshellarg($a_dir));256 mkdir($a_dir);257 symlink($a_dir, $a_dir . '_symlink');258 $this->assertTrue(CRM_Utils_File::isDir($a_dir . '_symlink'));259 $a_file = $a_dir . '/testFile';260 touch($a_file);261 $this->assertFalse(CRM_Utils_File::isDir($a_file));262 clearstatcache();263 symlink($a_file, $a_file . '_symlink');264 $this->assertFalse(CRM_Utils_File::isDir($a_file . '_symlink'));265 clearstatcache();266 link($a_file, $a_file . '_hardlink');267 $this->assertFalse(CRM_Utils_File::isDir($a_file . '_hardlink'));268 unlink($a_file . '_symlink');269 unlink($a_file . '_hardlink');270 unlink($a_file);271 unlink($a_dir . '_symlink');272 rmdir($a_dir);273 }274 /**275 * Test isDir with open_basedir276 *277 * @link https://github.com/php/php-src/blob/5b01c4863fe9e4bc2702b2bbf66d292d23001a18/tests/security/open_basedir_is_dir.phpt278 *279 * @dataProvider isDirBasedirProvider280 *281 * @param string|null $input282 * @param bool $expected283 */284 public function testIsDirWithOpenBasedir(?string $input, bool $expected) {285 $originalOpenBasedir = ini_get('open_basedir');286 // This might not always be under cms root, but let's see how it goes.287 $a_dir = \Civi::paths()->getPath('[civicrm.compile]/');288 if (file_exists("{$a_dir}/isDirTest/ok/ok.txt")) {289 unlink("{$a_dir}/isDirTest/ok/ok.txt");290 }291 if (is_dir("{$a_dir}/isDirTest/ok")) {292 rmdir("{$a_dir}/isDirTest/ok");293 }294 if (is_dir("{$a_dir}/isDirTest")) {295 rmdir("{$a_dir}/isDirTest");296 }297 // We want the cms root path, but in headless tests even though there is298 // a real cms strictly speaking the cms is "UNITTESTS", which might return299 // something made up (currently NULL).300 // \Civi::paths()->getPath('[cms.root]/')301 // For now let's try this, assuming a drupal 7 structure where we know302 // where this file is:303 $cms_root = realpath(__DIR__ . '/../../../../../../../..');304 // We also need temp dir because phpunit creates files in there as it does stuff before we can reset basedir.305 ini_set('open_basedir', $cms_root . PATH_SEPARATOR . sys_get_temp_dir());306 $this->assertTrue(mkdir("{$a_dir}/isDirTest"));307 $this->assertTrue(mkdir("{$a_dir}/isDirTest/ok"));308 file_put_contents("{$a_dir}/isDirTest/ok/ok.txt", 'Hello World!');309 // hmm the "bad" isn't going to work the same way php's own tests work. We310 // need to find a directory outside both cms_root and the sys temp dir.311 // Let's just use some known unix files that always exist instead.312 // mkdir("{$a_dir}/isDirTest/bad");313 $old_cwd = getcwd();314 $this->assertTrue(chdir("{$a_dir}/isDirTest/ok"));315 clearstatcache();316 if ($expected) {317 $this->assertTrue(CRM_Utils_File::isDir($input));318 }319 else {320 // Note that except for 'ok.txt', the real is_dir() would give an321 // error for these. For 'ok.txt' it would return false, but no error.322 // So this is what we are changing about the real function.323 $this->assertFalse(CRM_Utils_File::isDir($input));324 }325 ini_set('open_basedir', $originalOpenBasedir);326 $this->assertTrue(chdir($old_cwd));327 unlink("{$a_dir}/isDirTest/ok/ok.txt");328 rmdir("{$a_dir}/isDirTest/ok");329 rmdir("{$a_dir}/isDirTest");330 }331 /**332 * dataprovider for testIsDir333 *334 * @return array335 */336 public function isDirProvider(): array {337 return [338 // explicit indices to make it easier to see which one failed339 0 => [340 // input value341 NULL,342 // expected value343 FALSE,344 ],345 1 => ['.', TRUE],346 2 => ['..', TRUE],347 3 => [__FILE__, FALSE],348 4 => [__DIR__, TRUE],349 5 => ['dontexist', FALSE],350 6 => ['/no/such/dir', FALSE],351 7 => [' ', FALSE],352 ];353 }354 /**355 * dataprovider for testIsDirInvalidArgs356 *357 * @return array358 */359 public function isDirInvalidArgsProvider(): array {360 return [361 // explicit indices to make it easier to see which one failed362 0 => [-2.34555, FALSE],363 1 => [TRUE, FALSE],364 2 => [FALSE, FALSE],365 3 => [0, FALSE],366 4 => [1234, FALSE],367 ];368 }369 /**370 * dataprovider for testIsDirWithOpenBasedir371 *372 * @return array373 */374 public function isDirBasedirProvider(): array {375 return [376 // explicit indices to make it easier to see which one failed377 0 => [378 // input value379 strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'C:/windows' : '/etc',380 // expected value381 FALSE,382 ],383 1 => [strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'C:/windows/win.ini' : '/etc/group', FALSE],384 // This assumes a known location for template compile dir relative to...

Full Screen

Full Screen

ResourceExistenceChecker.php

Source:ResourceExistenceChecker.php Github

copy

Full Screen

...49 $this->assertFalse($this->checkResource('does-not-exist', 'file'));50 $this->assertFalse($this->checkResource('does-not-exist', 'fileAndDir'));51 }52 /**53 * testIsDir54 */55 public function testIsDir()56 {57 $this->assertTrue($this->isDir($this->testDir));58 $this->assertFalse($this->isDir('does-not-exist'));59 }60 /**61 * testIsFile62 */63 public function testIsFile()64 {65 $this->assertTrue($this->isFile($this->testFile));66 $this->assertFalse($this->isFile($this->testDir . 'does-not-exist'));67 }68}...

Full Screen

Full Screen

testIsDir

Using AI Code Generation

copy

Full Screen

1require_once 'file.php';2$file = new File();3$dir = 'test';4if($file->testIsDir($dir))5{6echo 'true';7}8{9echo 'false';10}11require_once 'file.php';12$file = new File();13$dir = 'test';14if($file->testIsDir($dir))15{16echo 'true';17}18{19echo 'false';20}

Full Screen

Full Screen

testIsDir

Using AI Code Generation

copy

Full Screen

1include_once("file.php");2$objFile = new File();3if($objFile->testIsDir("test"))4{5echo "Given path is directory";6}7{8echo "Given path is not directory";9}

Full Screen

Full Screen

testIsDir

Using AI Code Generation

copy

Full Screen

1require_once 'file.php';2$file = new file();3$file->testIsDir('D:/xampp/htdocs/test/');4Related Posts: PHP: Create a directory using mkdir() function5PHP: Get the list of files in a directory using glob() function6PHP: Get the list of files in a directory using scandir() function

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful