66use PHPUnit \Framework \TestCase ;
77use rcsofttech85 \FileHandler \Exception \CouldNotWriteFileException ;
88use rcsofttech85 \FileHandler \Exception \FileNotFoundException ;
9- use rcsofttech85 \FileHandler \Exception \InvalidFileException ;
109use rcsofttech85 \FileHandler \FileHandler ;
1110
1211class FileHandlerTest extends TestCase
@@ -32,7 +31,6 @@ protected function tearDown(): void
3231
3332
3433 #[Test]
35- #[TestDox("file was written successfully! " )]
3634 public function file_successfully_written ()
3735 {
3836 $ this ->fileHandler ->open (filename: 'file ' );
@@ -43,7 +41,6 @@ public function file_successfully_written()
4341 }
4442
4543 #[Test]
46- #[TestDox("should throw an exception if file is not found " )]
4744 public function should_throw_exception_if_file_is_not_Found ()
4845 {
4946 $ this ->expectException (FileNotFoundException::class);
@@ -52,7 +49,6 @@ public function should_throw_exception_if_file_is_not_Found()
5249 }
5350
5451 #[Test]
55- #[TestDox("should throw an exception if file is not writable " )]
5652 public function should_throw_exception_if_file_is_not_writable ()
5753 {
5854 $ this ->fileHandler ->open (filename: 'file ' , mode: 'r ' );
@@ -63,7 +59,6 @@ public function should_throw_exception_if_file_is_not_writable()
6359 }
6460
6561 #[Test]
66- #[TestDox("multiple files can be written simultaneously " )]
6762 public function multiple_file_can_be_written_simultaneously ()
6863 {
6964 $ this ->fileHandler ->open (filename: 'file ' );
@@ -81,7 +76,6 @@ public function multiple_file_can_be_written_simultaneously()
8176
8277
8378 #[Test]
84- #[TestDox("checks if a movie exists in a collection by a name " )]
8579 public function file_is_closed_properly ()
8680 {
8781 $ this ->fileHandler ->open (filename: 'file ' );
@@ -94,28 +88,69 @@ public function file_is_closed_properly()
9488
9589 #[Test]
9690 #[DataProvider('provide_movie_names ' )]
97- #[TestDox('Movie with name $keyword exists in collection . ' )]
98- public function movie_is_found_for_exact_name_match (string $ keyword )
91+ #[TestDox('search result with name $keyword exists in file . ' )]
92+ public function result_found_for_exact_name_match (string $ keyword )
9993 {
100- $ isMovieAvailable = $ this ->fileHandler ->open (filename: 'movie.csv ' )->searchInCsvFile (keyword: $ keyword );
94+ $ isMovieAvailable = $ this ->fileHandler ->open (filename: 'movie.csv ' )->searchInCsvFile (
95+ keyword: $ keyword ,
96+ column: 'Film '
97+ );
10198 $ this ->assertTrue ($ isMovieAvailable );
10299 }
103100
104101 #[Test]
105102 #[DataProvider('provide_studio_names ' )]
106- #[TestDox('Studio with name $keyword exists in collection . ' )]
103+ #[TestDox('search result with name $keyword exists in file . ' )]
107104 public function studio_is_found_for_exact_name_match (string $ keyword )
108105 {
109- $ isStudioFound = $ this ->fileHandler ->open (filename: 'movie.csv ' )->searchInCsvFile (keyword: $ keyword , offset: 2 );
106+ $ isStudioFound = $ this ->fileHandler ->open (filename: 'movie.csv ' )->searchInCsvFile (
107+ keyword: $ keyword ,
108+ column: 'Lead Studio '
109+ );
110110 $ this ->assertTrue ($ isStudioFound );
111111 }
112112
113113 #[Test]
114- public function should_throw_exception_if_not_valid_csv ()
114+ public function to_array_method_returns_valid_array ()
115115 {
116- $ this ->expectException (InvalidFileException::class);
117- $ this ->expectExceptionMessage ("invalid file format " );
118- $ this ->fileHandler ->open (filename: 'invalid.csv ' )->searchInCsvFile (keyword: 'hello ' );
116+ $ data = $ this ->fileHandler ->open (filename: 'movie.csv ' )->toArray ();
117+
118+ $ expected = [
119+ 'Film ' => 'Zack and Miri Make a Porno ' ,
120+ 'Genre ' => 'Romance ' ,
121+ 'Lead Studio ' => 'The Weinstein Company ' ,
122+ 'Audience score % ' => '70 ' ,
123+ 'Profitability ' => '1.747541667 ' ,
124+ 'Rotten Tomatoes % ' => '64 ' ,
125+ 'Worldwide Gross ' => '$41.94 ' ,
126+ 'Year ' => '2008 '
127+
128+ ];
129+
130+ $ this ->assertEquals ($ expected , $ data [0 ]);
131+ }
132+
133+ public function search_by_keyword_and_return_array ()
134+ {
135+ $ expected = [
136+ 'Film ' => 'Zack and Miri Make a Porno ' ,
137+ 'Genre ' => 'Romance ' ,
138+ 'Lead Studio ' => 'The Weinstein Company ' ,
139+ 'Audience score % ' => '70 ' ,
140+ 'Profitability ' => '1.747541667 ' ,
141+ 'Rotten Tomatoes % ' => '64 ' ,
142+ 'Worldwide Gross ' => '$41.94 ' ,
143+ 'Year ' => '2008 '
144+
145+ ];
146+
147+ $ data = $ this ->fileHandler ->open (filename: 'movie.csv ' )->searchInCsvFile (
148+ keyword: 'Zack and Miri Make a Porno ' ,
149+ column: 'Film ' ,
150+ format: FileHandler::ARRAY_FORMAT
151+ );
152+
153+ $ this ->assertEquals ($ expected , $ data );
119154 }
120155
121156 public static function provide_studio_names (): iterable
0 commit comments