Hoa central
Attachment.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\File;
41 use Hoa\Test;
42 
52 {
53  public function case_basic()
54  {
55  $this
56  ->given(
57  $stream = new File\ReadWrite(resolve('hoa://Test/Vfs/Foo.text')),
58  $stream->writeAll('foobar'),
59  $name = 'Hello.txt'
60  )
61  ->when($result = new SUT($stream, $name))
62  ->then
63  ->string($result->getFormattedContent())
64  ->isEqualTo(
65  'content-transfer-encoding: base64' . CRLF .
66  'content-disposition: attachment; filename="Hello.txt"; size=6' . CRLF .
67  'content-type: text/plain' . CRLF .
68  CRLF .
69  'Zm9vYmFy'
70  );
71  }
72 
73  public function case_only_stream()
74  {
75  $this
76  ->given(
77  $filename = 'Foo.txt',
78  $stream = new File\ReadWrite(resolve('hoa://Test/Vfs/' . $filename)),
79  $stream->writeAll('<strong>foobar</strong>')
80  )
81  ->when($result = new SUT($stream))
82  ->then
83  ->string($result['content-type'])
84  ->isEqualTo('text/plain')
85  ->string($result['content-disposition'])
86  ->isEqualTo('attachment; filename="' . $filename . '"; size=23');
87  }
88 
89  public function case_get_stream()
90  {
91  $this
92  ->given(
93  $filename = 'Foo.txt',
94  $stream = new File\ReadWrite(resolve('hoa://Test/Vfs/' . $filename)),
95  $stream->writeAll('<strong>foobar</strong>')
96  )
97  ->when($result = new SUT($stream))
98  ->then
99  ->object($result->getStream())
100  ->isIdenticalTo($stream);
101  }
102 
103  public function case_force_name()
104  {
105  $this
106  ->given(
107  $filename = 'Foo.txt',
108  $stream = new File\ReadWrite(resolve('hoa://Test/Vfs/' . $filename)),
109  $stream->writeAll('<strong>foobar</strong>'),
110  $name = 'Bar.txt'
111  )
112  ->when($result = new SUT($stream, $name))
113  ->then
114  ->string($result['content-disposition'])
115  ->isEqualTo('attachment; filename="' . $name . '"; size=23');
116  }
117 
119  {
120  $this
121  ->given(
122  $filename = 'Foo.txt',
123  $stream = new File\ReadWrite(resolve('hoa://Test/Vfs/' . $filename))
124  )
125  ->when($result = new SUT($stream, 'B"a"r.txt'))
126  ->then
127  ->string($result['content-disposition'])
128  ->isEqualTo('attachment; filename="B-a-r.txt"; size=0');
129  }
130 
131  public function case_force_mime_type()
132  {
133  $this
134  ->given(
135  $filename = 'Foo.txt',
136  $stream = new File\ReadWrite(resolve('hoa://Test/Vfs/' . $filename)),
137  $stream->writeAll('<strong>foobar</strong>'),
138  $name = 'Bar.txt',
139  $mimeType = 'text/x-test-plain'
140  )
141  ->when($result = new SUT($stream, $name, $mimeType))
142  ->then
143  ->string($result['content-type'])
144  ->isEqualTo($mimeType)
145  ->string($result['content-disposition'])
146  ->isEqualTo('attachment; filename="' . $name . '"; size=23');
147  }
148 
149  public function case_unknown_mime_type()
150  {
151  $this
152  ->given(
153  $stream = new File\ReadWrite(resolve('hoa://Test/Vfs/Foo.hoa-test')),
154  $stream->writeAll('foobar')
155  )
156  ->when($result = new SUT($stream))
157  ->then
158  ->string($result['content-type'])
159  ->isEqualTo('application/octet-stream');
160  }
161 
163  {
164  $this
165  ->given(
166  $text = 'foobar',
167  $stream = new File\ReadWrite(resolve('hoa://Test/Vfs/Foo.text')),
168  $stream->writeAll($text)
169  )
170  ->when($result = new SUT($stream))
171  ->then
172  ->string($result['content-transfer-encoding'])
173  ->isEqualTo('base64')
174  ->string($result->getFormattedContent(false))
175  ->isEqualTo(base64_encode($text));
176  }
177 }