PublishControllerTest.php 1.84 KB
Newer Older
mickadoo's avatar
mickadoo committed
1 2 3 4
<?php

namespace AppBundle\Tests\Controller;

5
use AppBundle\Model\Library;
mickadoo's avatar
mickadoo committed
6 7 8 9 10 11 12 13 14 15 16 17 18
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector;
use Symfony\Component\HttpFoundation\Response;

class PublishControllerTest extends WebTestCase {

  /**
   * Check that listen endpoint is working and sends mail
   */
  public function testListenAction() {
    $client = static::createClient();
    $client->enableProfiler();

19
    $hookBody = $this->getTestBookRequestBody();
mickadoo's avatar
mickadoo committed
20 21 22
    $headers = $this->getHeaders();
    $endpoint = '/admin/listen';

23 24 25
    $testLibrary = new Library(__DIR__ . '/../Files/books');
    $client->getContainer()->set('library', $testLibrary);

26
    $client->request('POST', $endpoint, [], [], $headers, $hookBody);
mickadoo's avatar
mickadoo committed
27 28 29 30 31 32 33 34 35 36
    $statusCode = $client->getResponse()->getStatusCode();

    $this->assertEquals(Response::HTTP_OK, $statusCode);

    /** @var MessageDataCollector $mailCollector */
    $mailCollector = $client->getProfile()->getCollector('swiftmailer');
    /** @var \Swift_Message[] $mails */
    $mails = $mailCollector->getMessages();
    $this->assertCount(1, $mails);

37 38
    $hookData = json_decode($hookBody, true);
    $sampleCommitHash = current($hookData['commits'])['id'];
mickadoo's avatar
mickadoo committed
39
    $sentMessage = array_shift($mails);
40

mickadoo's avatar
mickadoo committed
41
    $this->assertContains('Publishing Successful', $sentMessage->getBody());
42
    $this->assertContains($sampleCommitHash, $sentMessage->getBody());
mickadoo's avatar
mickadoo committed
43 44 45 46 47
  }

  /**
   * @return string
   */
48 49
  private function getTestBookRequestBody(): string {
    return file_get_contents(__DIR__ . '/../Files/webhook-gitlab-push-test-book.json');
mickadoo's avatar
mickadoo committed
50 51 52 53 54 55 56
  }

  /**
   * @return array
   */
  private function getHeaders(): array {
    $headers = [
57
      'HTTP_X-GitLab-Event' => 'Push Hook', // prefix required for non-standard
mickadoo's avatar
mickadoo committed
58 59 60 61 62 63 64
      'Content-Type' => 'application/json'
    ];

    return $headers;
  }

}