|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Devmachine\Bundle\OntheioBundle\Tests\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Buzz\Browser; |
|
6
|
|
|
use Buzz\Listener\ListenerInterface; |
|
7
|
|
|
use Buzz\Message\MessageInterface; |
|
8
|
|
|
use Buzz\Message\RequestInterface; |
|
9
|
|
|
use Devmachine\Bundle\OntheioBundle\Client\Image\ImageClient; |
|
10
|
|
|
use Devmachine\Bundle\OntheioBundle\Client\Image\Signer; |
|
11
|
|
|
|
|
12
|
|
|
class ImageClientTest extends \PHPUnit_Framework_TestCase implements ListenerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private $signer; |
|
15
|
|
|
private $buzz; |
|
16
|
|
|
|
|
17
|
|
|
/** @var RequestInterface */ |
|
18
|
|
|
private $request; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->signer = new Signer('key', 'secret'); |
|
23
|
|
|
$this->buzz = new Browser($this->makeMock('Buzz\Client\ClientInterface')); |
|
24
|
|
|
$this->buzz->addListener($this); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function preSend(RequestInterface $request) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->request = $request; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function postSend(RequestInterface $request, MessageInterface $response) |
|
33
|
|
|
{ |
|
34
|
|
|
$response->setContent(json_encode([ |
|
35
|
|
|
'key' => 'abc', |
|
36
|
|
|
'size' => '200x150', |
|
37
|
|
|
'full_size' => 'http://original.url', |
|
38
|
|
|
])); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testUploadByUrl() |
|
42
|
|
|
{ |
|
43
|
|
|
$client = new ImageClient($this->buzz, $this->signer, 'http://base.url'); |
|
44
|
|
|
|
|
45
|
|
|
$result = $client->uploadByUrl('http://upload.url'); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertInstanceOf('Devmachine\Bundle\OntheioBundle\Client\Image\Result', $result); |
|
48
|
|
|
$this->assertEquals('http://base.url', $this->request->getHost()); |
|
49
|
|
|
$this->assertEquals('/upload.php?app=key&s=4880768bfa54ae301b69770b79190e40', $this->request->getResource()); |
|
50
|
|
|
$this->assertEquals(['url' => 'http://upload.url'], $this->request->getContent()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @expectedException \RuntimeException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testUploadByFileExpectsValidFile() |
|
57
|
|
|
{ |
|
58
|
|
|
(new ImageClient($this->buzz, $this->signer, 'http://base.url'))->uploadByFile('no-such-file'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testUploadByFile() |
|
62
|
|
|
{ |
|
63
|
|
|
$client = new ImageClient($this->buzz, $this->signer, 'http://base.url'); |
|
64
|
|
|
|
|
65
|
|
|
$result = $client->uploadByFile($file = tempnam(sys_get_temp_dir(), 'image_')); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertInstanceOf('Devmachine\Bundle\OntheioBundle\Client\Image\Result', $result); |
|
68
|
|
|
$this->assertEquals('http://base.url', $this->request->getHost()); |
|
69
|
|
|
$this->assertEquals('/upload.php?app=key&s=4ca8dbe87d76306ae00cce35967b619b', $this->request->getResource()); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertInstanceOf('Buzz\Message\Form\FormRequest', $this->request); |
|
72
|
|
|
$this->assertInstanceOf('Buzz\Message\Form\FormUpload', $this->request->getFields()['file']); |
|
|
|
|
|
|
73
|
|
|
$this->assertEquals($file, $this->request->getFields()['file']->getFile()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function makeMock($originalClassName) |
|
77
|
|
|
{ |
|
78
|
|
|
if (method_exists($this, 'createMock')) { |
|
79
|
|
|
return $this->createMock($originalClassName); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->getMock($originalClassName); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|