@@ -17,25 +17,40 @@ class FileHandlerTest extends TestCase
1717 protected function setUp (): void
1818 {
1919 parent ::setUp ();
20-
2120 $ this ->fileHandler = new FileHandler ();
22- fopen (filename: " file " , mode: " w " );
21+ }
2322
23+ public static function setUpBeforeClass (): void
24+ {
25+ parent ::setUpBeforeClass ();
2426 $ content = "Film,Genre,Lead Studio,Audience score %,Profitability,Rotten Tomatoes %,Worldwide Gross,Year \n"
2527 . "Zack and Miri Make a Porno,Romance,The Weinstein Company,70,1.747541667,64,$41.94 ,2008 \n"
2628 . "Youth in Revolt,Comedy,The Weinstein Company,52,1.09,68,$19.62 ,2010 \n"
2729 . "Twilight,Romance,Independent,68,6.383363636,26,$702.17 ,2011 " ;
2830
31+ fopen (filename: "file " , mode: "w " );
32+ fopen (filename: "file1 " , mode: "w " );
2933 file_put_contents ('movie.csv ' , $ content );
3034 }
3135
36+ public static function tearDownAfterClass (): void
37+ {
38+ parent ::tearDownAfterClass ();
39+
40+ $ files = ["file " , "movie.csv " , 'file1 ' ];
41+
42+ foreach ($ files as $ file ) {
43+ if (file_exists ($ file )) {
44+ unlink (filename: $ file );
45+ }
46+ }
47+ }
48+
3249 protected function tearDown (): void
3350 {
3451 parent ::tearDown ();
3552
3653 $ this ->fileHandler = null ;
37- unlink (filename: "file " );
38- unlink (filename: "movie.csv " );
3954 }
4055
4156
@@ -67,34 +82,34 @@ public function shouldThrowExceptionIfFileIsNotWritable()
6782 $ this ->fileHandler ->write (data: "hello world " );
6883 }
6984
85+
7086 #[Test]
71- public function multipleFileCanBeWrittenSimultaneously ()
87+ public function fileIsClosedProperly ()
7288 {
7389 $ this ->fileHandler ->open (filename: 'file ' );
74-
75- $ this ->fileHandler ->open (filename: 'file1 ' , mode: 'w ' );
76-
7790 $ this ->fileHandler ->write (data: "hello world " );
91+ $ this ->fileHandler ->close ();
7892
79- $ this ->assertEquals ("hello world " , file_get_contents (filename: 'file ' ));
80-
81- $ this ->assertEquals ("hello world " , file_get_contents (filename: 'file1 ' ));
82-
83- unlink ("file1 " );
93+ $ this ->expectException (TypeError::class);
94+ $ this ->fileHandler ->write (data: "fwrite(): supplied resource is not a valid stream resource " );
8495 }
8596
8697
8798 #[Test]
88- public function fileIsClosedProperly ()
99+ public function multipleFileCanBeWrittenSimultaneously ()
89100 {
90101 $ this ->fileHandler ->open (filename: 'file ' );
102+
103+ $ this ->fileHandler ->open (filename: 'file1 ' );
104+
91105 $ this ->fileHandler ->write (data: "hello world " );
92- $ this ->fileHandler ->close ();
93106
94- $ this ->expectException (TypeError::class);
95- $ this ->fileHandler ->write (data: "fwrite(): supplied resource is not a valid stream resource " );
107+ $ this ->assertEquals ("hello world " , file_get_contents (filename: 'file ' ));
108+
109+ $ this ->assertEquals ("hello world " , file_get_contents (filename: 'file1 ' ));
96110 }
97111
112+
98113 #[Test]
99114 #[DataProvider('provideMovieNames ' )]
100115 #[TestDox('search result with name $keyword exists in file. ' )]
0 commit comments