5
5
namespace VeeWee \Tests \Xml \Writer \Opener ;
6
6
7
7
use PHPUnit \Framework \TestCase ;
8
+ use Psl \File ;
9
+ use Psl \Filesystem ;
10
+ use Psl \OS ;
8
11
use VeeWee \Tests \Xml \Helper \TmpFileTrait ;
9
12
use VeeWee \Xml \Exception \RuntimeException ;
10
13
use VeeWee \Xml \Writer \Writer ;
@@ -14,23 +17,46 @@ final class XmlFileOpenerTest extends TestCase
14
17
{
15
18
use TmpFileTrait;
16
19
17
-
18
20
public function test_it_can_open_a_file (): void
19
21
{
20
22
$ this ->createTmpFile (static function (string $ path ): void {
23
+ File \write ($ path , 'will be truncated ' , File \WriteMode::TRUNCATE );
24
+
21
25
$ writer = Writer::forFile ($ path );
22
26
$ writer ->write (element ('root ' ));
23
27
24
28
self ::assertXmlStringEqualsXmlFile ($ path , '<root /> ' );
25
29
});
26
30
}
27
31
28
-
32
+ public function test_it_can_write_to_a_new_file (): void
33
+ {
34
+ $ temporaryFile = Filesystem \create_temporary_file ();
35
+ Filesystem \delete_file ($ temporaryFile );
36
+
37
+ $ writer = Writer::forFile ($ temporaryFile );
38
+ $ writer ->write (element ('root ' ));
39
+ static ::assertXmlStringEqualsXmlFile ($ temporaryFile , '<root /> ' );
40
+
41
+ unlink ($ temporaryFile );
42
+ }
43
+
29
44
public function test_it_errors_if_file_is_not_writable (): void
30
45
{
46
+ if (OS \is_windows ()) {
47
+ static ::markTestSkipped ('Permissions are not reliable on windows. ' );
48
+ }
49
+
50
+ $ temporary_file = Filesystem \create_temporary_file ();
51
+ Filesystem \delete_file ($ temporary_file );
52
+ Filesystem \create_directory ($ temporary_file );
53
+ Filesystem \change_permissions ($ temporary_file , 0555 );
54
+
55
+ $ file = $ temporary_file . Filesystem \SEPARATOR . 'foo ' ;
56
+
31
57
$ this ->expectException (RuntimeException::class);
32
- $ this ->expectExceptionMessage ('The path "doesnotexist " is not writable. ' );
58
+ $ this ->expectExceptionMessage ('File " ' . $ file . ' " is not writable. ' );
33
59
34
- Writer::forFile (' doesnotexist ' );
60
+ Writer::forFile ($ file );
35
61
}
36
62
}
0 commit comments