|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AppBundle\Tests\Controller; |
| 4 | + |
| 5 | +use Bazinga\Bundle\RestExtraBundle\Test\WebTestCase; |
| 6 | +use PDO; |
| 7 | + |
| 8 | +class VideoControllerTest extends WebTestCase |
| 9 | +{ |
| 10 | + private $connection; |
| 11 | + |
| 12 | + protected function setUp() |
| 13 | + { |
| 14 | + $this->connection = $this->getClient()->getContainer()->get('database_connection'); |
| 15 | + $this->connection->query("TRUNCATE video"); |
| 16 | + } |
| 17 | + |
| 18 | + public function testPostVideo() |
| 19 | + { |
| 20 | + $client = $this->getClient(true); |
| 21 | + |
| 22 | + $client->request('POST', '/videos.json', [ |
| 23 | + 'title' => 'Testing en frontend', |
| 24 | + 'url' => 'https://pro.codely.tv/library/testing-frontend', |
| 25 | + 'course_id' => 5 |
| 26 | + ]); |
| 27 | + $response = $client->getResponse(); |
| 28 | + |
| 29 | + $this->assertJsonResponse($response); |
| 30 | + $expectedId = "1"; |
| 31 | + $expectedTitle = "Testing en frontend"; |
| 32 | + $expectedUrl = "https://pro.codely.tv/library/testing-frontend"; |
| 33 | + $expectedCourseId = 5; |
| 34 | + $expectedResponse = [ |
| 35 | + "id" => $expectedId, |
| 36 | + "title" => $expectedTitle, |
| 37 | + "url" => $expectedUrl, |
| 38 | + "course_id" => $expectedCourseId |
| 39 | + ]; |
| 40 | + |
| 41 | + $this->assertEquals(json_encode($expectedResponse), $response->getContent()); |
| 42 | + |
| 43 | + $stmt = $this->connection->prepare("SELECT * FROM video WHERE id = 1"); |
| 44 | + $stmt->execute(); |
| 45 | + $result = $stmt->fetch(PDO::FETCH_ASSOC); |
| 46 | + |
| 47 | + $this->assertEquals($expectedTitle, $result['title']); |
| 48 | + $this->assertEquals($expectedUrl, $result['url']); |
| 49 | + $this->assertEquals($expectedCourseId, $result['course_id']); |
| 50 | + } |
| 51 | + |
| 52 | + private function getClient($authenticated = false) |
| 53 | + { |
| 54 | + $params = array(); |
| 55 | + if ($authenticated) { |
| 56 | + $params = array_merge($params, array( |
| 57 | + 'PHP_AUTH_USER' => 'restapi', |
| 58 | + 'PHP_AUTH_PW' => 'secretpw', |
| 59 | + )); |
| 60 | + } |
| 61 | + |
| 62 | + return static::createClient(array(), $params); |
| 63 | + } |
| 64 | +} |
0 commit comments