ImageClientTest::makeMock()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
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']);
0 ignored issues
show
Bug introduced by
The method getFields() does not exist on Buzz\Message\RequestInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Buzz\Message\Request. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        $this->assertInstanceOf('Buzz\Message\Form\FormUpload', $this->request->/** @scrutinizer ignore-call */ getFields()['file']);
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit_Framework_TestCase::getMock() has been deprecated: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

82
        return /** @scrutinizer ignore-deprecated */ $this->getMock($originalClassName);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
83
    }
84
}
85