Tests for TempFSFile
authorAryeh Gregor <ayg@aryeh.name>
Tue, 20 Aug 2019 08:22:31 +0000 (11:22 +0300)
committerAryeh Gregor <ayg@aryeh.name>
Tue, 20 Aug 2019 08:25:02 +0000 (11:25 +0300)
These are in preparation for making a TempFSFileFactory service, thus
the odd break-up into two files. I split it into a separate commit so
that we could verify that the same tests pass before and after the
conversion to service.

Tests cover everything except getUsableTempDirectory() (which I don't
see how to test), and register_shutdown_function()-related stuff (which
seems actually impossible to test without starting a new PHP process).

Change-Id: If61b7ea3e332adc2bceefc8e6879a9e9443c99dd

tests/common/TestsAutoLoader.php
tests/phpunit/includes/libs/filebackend/fsfile/TempFSFileIntegrationTest.php [new file with mode: 0644]
tests/phpunit/unit/includes/libs/filebackend/fsfile/TempFSFileTestTrait.php [new file with mode: 0644]

index fd418f7..e71cc88 100644 (file)
@@ -217,6 +217,9 @@ $wgAutoloadClasses += [
        'MockSearchResultSet' => "$testDir/phpunit/mocks/search/MockSearchResultSet.php",
        'MockSearchResult' => "$testDir/phpunit/mocks/search/MockSearchResult.php",
 
+       # tests/phpunit/unit/includes/libs/filebackend/fsfile
+       'TempFSFileTestTrait' => "$testDir/phpunit/unit/includes/libs/filebackend/fsfile/TempFSFileTestTrait.php",
+
        # tests/suites
        'ParserTestFileSuite' => "$testDir/phpunit/suites/ParserTestFileSuite.php",
        'ParserTestTopLevelSuite' => "$testDir/phpunit/suites/ParserTestTopLevelSuite.php",
diff --git a/tests/phpunit/includes/libs/filebackend/fsfile/TempFSFileIntegrationTest.php b/tests/phpunit/includes/libs/filebackend/fsfile/TempFSFileIntegrationTest.php
new file mode 100644 (file)
index 0000000..42805b2
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+class TempFSFileIntegrationTest extends MediaWikiIntegrationTestCase {
+       use TempFSFileTestTrait;
+
+       private function newFile() {
+               return TempFSFile::factory( 'tmp' );
+       }
+}
diff --git a/tests/phpunit/unit/includes/libs/filebackend/fsfile/TempFSFileTestTrait.php b/tests/phpunit/unit/includes/libs/filebackend/fsfile/TempFSFileTestTrait.php
new file mode 100644 (file)
index 0000000..d32e01e
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+/**
+ * Code shared between the unit and integration tests
+ */
+trait TempFSFileTestTrait {
+       abstract protected function newFile();
+
+       /**
+        * @covers TempFSFile::__construct
+        * @covers TempFSFile::purge
+        */
+       public function testPurge() {
+               $file = $this->newFile();
+               $this->assertTrue( file_exists( $file->getPath() ) );
+               $file->purge();
+               $this->assertFalse( file_exists( $file->getPath() ) );
+       }
+
+       /**
+        * @covers TempFSFile::__construct
+        * @covers TempFSFile::bind
+        * @covers TempFSFile::autocollect
+        * @covers TempFSFile::__destruct
+        */
+       public function testBind() {
+               $file = $this->newFile();
+               $path = $file->getPath();
+               $this->assertTrue( file_exists( $path ) );
+               $obj = new stdclass;
+               $file->bind( $obj );
+               unset( $file );
+               $this->assertTrue( file_exists( $path ) );
+               unset( $obj );
+               $this->assertFalse( file_exists( $path ) );
+       }
+
+       /**
+        * @covers TempFSFile::__construct
+        * @covers TempFSFile::preserve
+        * @covers TempFSFile::__destruct
+        */
+       public function testPreserve() {
+               $file = $this->newFile();
+               $path = $file->getPath();
+               $this->assertTrue( file_exists( $path ) );
+               $file->preserve();
+               unset( $file );
+               $this->assertTrue( file_exists( $path ) );
+               Wikimedia\suppressWarnings();
+               unlink( $path );
+               Wikimedia\restoreWarnings();
+       }
+}