1+ <?php namespace io \unittest ;
2+
3+ use ArrayObject ;
4+ use io \streams \{MemoryInputStream , InputStream };
5+ use io \{Blob , OperationNotSupportedException };
6+ use lang \IllegalArgumentException ;
7+ use test \verify \Runtime ;
8+ use test \{Assert , Expect , Test , Values };
9+ use util \Bytes ;
10+
11+ class BlobTest {
12+
13+ /** @return iterable */
14+ private function cases () {
15+ yield [new Blob (), []];
16+ yield [new Blob ('Test ' ), ['Test ' ]];
17+ yield [new Blob (['Über ' ]), ['Über ' ]];
18+ yield [new Blob ([new Blob (['Test ' ]), 'ed ' ]), ['Test ' , 'ed ' ]];
19+ yield [new Blob (['Test ' , 'ed ' ]), ['Test ' , 'ed ' ]];
20+ yield [new Blob ((function () { yield 'Test ' ; yield 'ed ' ; })()), ['Test ' , 'ed ' ]];
21+ yield [new Blob (new ArrayObject (['Test ' , 'ed ' ])), ['Test ' , 'ed ' ]];
22+ yield [new Blob (new Bytes ('Test ' )), ['Test ' ]];
23+ yield [new Blob (new MemoryInputStream ('Test ' )), ['Test ' ]];
24+ }
25+
26+ #[Test]
27+ public function can_create () {
28+ new Blob ();
29+ }
30+
31+ #[Test, Expect(IllegalArgumentException::class)]
32+ public function not_from_null () {
33+ new Blob (null );
34+ }
35+
36+ #[Test]
37+ public function meta_empty_by_default () {
38+ Assert::equals ([], (new Blob ('Test ' ))->meta );
39+ }
40+
41+ #[Test]
42+ public function meta () {
43+ $ meta = ['type ' => 'text/plain ' ];
44+ Assert::equals ($ meta , (new Blob ('Test ' , $ meta ))->meta );
45+ }
46+
47+ #[Test, Values(from: 'cases ' )]
48+ public function iteration ($ fixture , $ expected ) {
49+ Assert::equals ($ expected , iterator_to_array ($ fixture ));
50+ }
51+
52+ #[Test, Values(from: 'cases ' )]
53+ public function bytes ($ fixture , $ expected ) {
54+ Assert::equals (new Bytes ($ expected ), $ fixture ->bytes ());
55+ }
56+
57+ #[Test, Values(from: 'cases ' )]
58+ public function stream ($ fixture , $ expected ) {
59+ $ stream = $ fixture ->stream ();
60+ $ data = [];
61+ while ($ stream ->available ()) {
62+ $ data []= $ stream ->read ();
63+ }
64+ Assert::equals ($ expected , $ data );
65+ }
66+
67+ #[Test, Values(from: 'cases ' )]
68+ public function string_cast ($ fixture , $ expected ) {
69+ Assert::equals (implode ('' , $ expected ), (string )$ fixture );
70+ }
71+
72+ #[Test, Values([[1 , ['T ' , 'e ' , 's ' , 't ' ]], [2 , ['Te ' , 'st ' ]], [3 , ['Tes ' , 't ' ]], [4 , ['Test ' ]]])]
73+ public function slices ($ size , $ expected ) {
74+ Assert::equals ($ expected , iterator_to_array ((new Blob ('Test ' ))->slices ($ size )));
75+ }
76+
77+ #[Test]
78+ public function fill_slice () {
79+ Assert::equals (['Test ' ], iterator_to_array ((new Blob (['Te ' , 'st ' ]))->slices ()));
80+ }
81+
82+ #[Test]
83+ public function fetch_slice_twice () {
84+ $ fixture = new Blob ('Test ' );
85+
86+ Assert::equals (['Test ' ], iterator_to_array ($ fixture ->slices ()));
87+ Assert::equals (['Test ' ], iterator_to_array ($ fixture ->slices ()));
88+ }
89+
90+ #[Test]
91+ public function cannot_fetch_slices_twice_from_non_seekable () {
92+ $ fixture = new Blob (new class () implements InputStream {
93+ private $ input = ['Test ' ];
94+ public function available () { return strlen (current ($ this ->input )); }
95+ public function read ($ limit = 8192 ) { return array_shift ($ this ->input ); }
96+ public function close () { $ this ->input = []; }
97+ });
98+ iterator_to_array ($ fixture ->slices ());
99+
100+ Assert::throws (OperationNotSupportedException::class, fn () => iterator_to_array ($ fixture ->slices ()));
101+ }
102+
103+ /** @see https://bugs.php.net/bug.php?id=77069 */
104+ #[Test, Runtime(php: '>=7.4.14 ' )]
105+ public function base64_encoded () {
106+ $ base64 = (new Blob ('Test ' ))->encoded ('convert.base64-encode ' );
107+
108+ Assert::equals (['convert.base64-encode ' ], $ base64 ->meta ['encoding ' ]);
109+ Assert::equals ('VGVzdA== ' , (string )$ base64 );
110+ }
111+
112+ #[Test]
113+ public function custom_encoding () {
114+ $ base64 = (new Blob ('Test ' ))->encoded ('uppercase ' , fn ($ chunk ) => strtoupper ($ chunk ));
115+
116+ Assert::equals (['uppercase ' ], $ base64 ->meta ['encoding ' ]);
117+ Assert::equals ('TEST ' , (string )$ base64 );
118+ }
119+ }
0 commit comments