1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mykees\MediaBundle\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use Liip\FunctionalTestBundle\Test\WebTestCase; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
7
|
|
|
|
8
|
|
|
class MediasControllerTest extends WebTestCase |
9
|
|
|
{ |
10
|
|
|
protected $client; |
11
|
|
|
protected $container; |
12
|
|
|
protected $manager; |
13
|
|
|
protected $image; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
//Load fixtures |
18
|
|
|
$fixtures = array( |
19
|
|
|
'Mykees\MediaBundle\DataFixtures\ORM\LoadMedias', |
20
|
|
|
'Mvc\BlogBundle\DataFixtures\ORM\LoadPostsData', |
21
|
|
|
); |
22
|
|
|
$this->loadFixtures($fixtures); |
23
|
|
|
|
24
|
|
|
$this->client = static::createClient(); |
25
|
|
|
$this->container = $this->client->getContainer(); |
26
|
|
|
$this->manager = $this->container->get('mk.media.manager'); |
27
|
|
|
$this->image = $this->container->get('kernel')->getRootDir().'/../web'; |
28
|
|
|
|
29
|
|
|
parent::setUp(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testListMedias() |
33
|
|
|
{ |
34
|
|
|
$crawler = $this->client->request('GET', '/admin/medias/Post/MvcBlogBundle/1'); |
35
|
|
|
$this->assertEquals(200,$this->client->getResponse()->getStatusCode()); |
36
|
|
|
$this->assertEquals(2,$crawler->filter('.count-tr')->count()); |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
$crawler = $this->client->request('GET', '/admin/medias/Post/MvcBlogBundle/2'); |
40
|
|
|
$this->assertEquals(200,$this->client->getResponse()->getStatusCode()); |
41
|
|
|
$this->assertEquals(1,$crawler->filter('.count-tr')->count()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testFindAllMediasForAModel() |
45
|
|
|
{ |
46
|
|
|
$medias = $this->manager->findMediasByModelAndId('Post',1); |
47
|
|
|
$this->assertGreaterThan(1,count($medias)); |
48
|
|
|
$this->assertEquals(2, count($medias)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testRemoveFile() |
52
|
|
|
{ |
53
|
|
|
$this->client->request('GET', '/admin/medias/delete/Post/MvcBlogBundle/9'); |
54
|
|
|
$this->assertEquals(200,$this->client->getResponse()->getStatusCode()); |
55
|
|
|
|
56
|
|
|
$medias = $this->manager->findMediasByModelAndId('Post',2); |
57
|
|
|
$this->assertEquals(0, count($medias)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
public function testValidUploadFileFormat() |
62
|
|
|
{ |
63
|
|
|
$filePath = dirname(__DIR__).'/../Resources/public/images/elly.jpg'; |
64
|
|
|
$file = new UploadedFile( |
65
|
|
|
$filePath, |
66
|
|
|
'elly.jpg', |
67
|
|
|
'image/jpeg', |
68
|
|
|
123 |
69
|
|
|
); |
70
|
|
|
$this->client->request( |
71
|
|
|
'POST', |
72
|
|
|
'/admin/medias/add/Post/MvcBlogBundle/13', |
73
|
|
|
['name'=>'Elly'], |
74
|
|
|
['file'=>$file], |
75
|
|
|
['HTTP_X-Requested-With' => 'XMLHttpRequest'] |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$this->assertEquals(200,$this->client->getResponse()->getStatusCode()); |
79
|
|
|
$medias = $this->manager->findMediasByModelAndId('Post',13); |
80
|
|
|
|
81
|
|
|
$this->assertEquals(1, count($medias)); |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testInvalidUploadFileFormat() |
86
|
|
|
{ |
87
|
|
|
$filePath = dirname(__DIR__).'/../Resources/public/images/thumb.png'; |
88
|
|
|
$file = new UploadedFile( |
89
|
|
|
$filePath, |
90
|
|
|
'thumb.png', |
91
|
|
|
'image/png', |
92
|
|
|
123 |
93
|
|
|
); |
94
|
|
|
$this->client->request( |
95
|
|
|
'POST', |
96
|
|
|
'/admin/medias/add/Post/MvcBlogBundle/17', |
97
|
|
|
['name'=>'thumb'], |
98
|
|
|
['file'=>$file], |
99
|
|
|
['HTTP_X-Requested-With' => 'XMLHttpRequest'] |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$this->assertEquals(500,$this->client->getResponse()->getStatusCode()); |
103
|
|
|
|
104
|
|
|
$this->manager = $this->container->get('mk.media.manager'); |
105
|
|
|
$medias = $this->manager->findMediasByModelAndId('Post',17); |
106
|
|
|
$this->assertEquals(0, count($medias)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|