1+ <?php
2+
3+ use TRMS \Carousel \Server \API ;
4+ use TRMS \Carousel \Requests \FileUploadRequest ;
5+ use TRMS \Carousel \Models \Bulletin ;
6+
7+ use TRMS \Carousel \Exceptions \CarouselAPIException ;
8+
9+ use CarouselTests \MockData \MockResponder ;
10+
11+ use GuzzleHttp \Handler \MockHandler ;
12+ use GuzzleHttp \HandlerStack ;
13+ use GuzzleHttp \Psr7 \Response ;
14+ use GuzzleHttp \Psr7 \Request ;
15+ use GuzzleHttp \Exception \RequestException ;
16+
17+ class TestCase extends PHPUnit_Framework_TestCase
18+ {
19+ function test_you_can_upload_a_bulletin_from_a_file ()
20+ {
21+ $ mockResponder = new MockResponder ;
22+ $ mock = new MockHandler ([
23+ new Response (200 ,[],json_encode ([
24+ 'Bulletins ' =>[['id ' =>'1 ' ]]
25+ ])),
26+ ]);
27+
28+ $ handler = HandlerStack::create ($ mock );
29+
30+ $ filepath = 'Tests/MockData/mock_file.png ' ;
31+ $ fileUpload = new FileUploadRequest (Bulletin::class,['ZoneID ' =>1 ]);
32+ $ fileUpload ->addFile ($ filepath );
33+
34+ $ server = new API ();
35+ $ server
36+ ->addMockHandler ($ handler )
37+ ->connect ('server ' ,'username ' ,'password ' );
38+
39+ $ bulletins = $ server ->upload ($ fileUpload );
40+
41+ $ this ->assertInstanceOf (Bulletin::class, $ bulletins ->first ());
42+ $ this ->assertEquals ($ bulletins ->first ()->id , '1 ' );
43+ $ this ->assertEquals ('server/carouselapi/v1/bulletins ' , (string ) $ mock ->getLastRequest ()->getUri ());
44+ $ this ->assertEquals ('POST ' , (string ) $ mock ->getLastRequest ()->getMethod ());
45+ $ this ->assertGreaterThan (150000 ,$ mock ->getLastRequest ()->getBody ()->stream ->getSize (), 'the file is big ' );
46+ }
47+
48+ function test_you_can_upload_multiple_bulletins_at_once ()
49+ {
50+ $ mockResponder = new MockResponder ;
51+ $ mock = new MockHandler ([
52+ new Response (200 ,[],json_encode ([
53+ 'Bulletins ' =>[['id ' =>'1 ' ]]
54+ ])),
55+ new Response (200 ,[],json_encode ([
56+ 'Bulletins ' =>[['id ' =>'2 ' ]]
57+ ])),
58+ ]);
59+
60+ $ handler = HandlerStack::create ($ mock );
61+
62+ $ filepath = 'Tests/MockData/mock_file.png ' ;
63+ $ fileUpload = new FileUploadRequest (Bulletin::class,['ZoneID ' =>1 ]);
64+ $ fileUpload ->addFile ($ filepath )->addFile ($ filepath );
65+
66+ $ server = new API ();
67+ $ server
68+ ->addMockHandler ($ handler )
69+ ->connect ('server ' ,'username ' ,'password ' );
70+
71+ $ bulletins = $ server ->upload ($ fileUpload );
72+
73+ $ this ->assertInstanceOf (Bulletin::class, $ bulletins [0 ]);
74+ $ this ->assertInstanceOf (Bulletin::class, $ bulletins [1 ]);
75+ $ this ->assertEquals ($ bulletins [0 ]->id , '1 ' );
76+ $ this ->assertEquals ($ bulletins [1 ]->id , '2 ' );
77+ }
78+
79+ function test_failed_uploads_will_return_an_exception ()
80+ {
81+ $ mockResponder = new MockResponder ;
82+ $ mock = new MockHandler ([
83+ new Response (500 ,[],json_encode (['Message ' =>'File Upload Failed ' ])),
84+ ]);
85+
86+ $ handler = HandlerStack::create ($ mock );
87+
88+ $ filepath = 'Tests/MockData/mock_file.png ' ;
89+ $ fileUpload = new FileUploadRequest (Bulletin::class,['ZoneID ' =>1 ]);
90+ $ fileUpload ->addFile ($ filepath );
91+
92+ $ server = new API ();
93+ $ server
94+ ->addMockHandler ($ handler )
95+ ->connect ('server ' ,'username ' ,'password ' );
96+
97+ $ bulletins = $ server ->upload ($ fileUpload );
98+
99+ $ this ->assertInstanceOf ('TRMS\Carousel\Exceptions\CarouselAPIException ' , $ bulletins ->first ());
100+ }
101+
102+ }
0 commit comments