PHP code example of we-bridge / functionnal-test-helpers

1. Go to this page and download the library: Download we-bridge/functionnal-test-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

we-bridge / functionnal-test-helpers example snippets




namespace YourBundle\Tests\Controller;

use WeBridge\TestHelpers\ApiHelpersTrait;
use Liip\FunctionalTestBundle\Test\WebTestCase;

class AnalystControllerTest extends WebTestCase
{
    use ApiHelpersTrait;

    private static $ANALYST_FIELDS = [
        'id',
        'title',
        'job_title',
        'biography',
        'registration_code',
        'videos',
        'subscribed',
        'users_subscribed',
    ];

    private $analyst = null;

    public function setUp()
    {
        $this->client = static::createClient();
    }

    public function testGetAnalystsWithoutSortReturnBadParameter()
    {
        $this->performGET('/api/analysts');
        $this->assertBadRequestError();
    }

    public function testGetAnalystsWithdValidSortReturnListAnalysts()
    {
        //TODO replace by phpunit stuff to feed with data
        foreach (['hot', 'recommended', 'newest'] as $validSort) {
            $this->performGET('/api/analysts?sort='.$sort)
            $this->assertOkSuccess();
            $this->assertArrayHasKeys(
                self::$ANALYST_FIELDS,
                $this->responseJson[0]
            );
        }
    }
}




namespace YourBundle\Tests\Controller;

use Liip\FunctionalTestBundle\Test\WebTestCase;
use WeBridge\TestHelpers\IsControllerTestTrait;

class CommentsControllerTest extends WebTestCase
{
    use IsControllerTestTrait;

    const COMMENT_OF_VIDEO_PAGE = '/backend/comments/of-video/';

    private $video;

    // if you don't use any fixtures declare this array as empty
    // in latter version it will not be needed to declare it if not used
    protected $fixturelist = [
        'YourBundle\DataFixtures\ORM\LoadCommentData',
        'YourBundle\DataFixtures\ORM\LoadBackendCommentData',
    ];

    public function testOpenCommentOfVideoPageShouldHaveAlistOfComment()
    {
        $this->givenVideo('commented-video');
        $this->openCommentOfVideoPage();
        $this->assertPageOpenedSuccessfully();

        $this->assertListofCommentsPresents();
    }

    // conveniency methods

    private function givenVideo($fixturesName)
    {
        $video = $this->fixtures->getReference($fixturesName);
        $this->video = $video;
    }

    private function openCommentOfVideoPage()
    {
        $id = $this->video->getId();
        $this->openPage(self::COMMENT_OF_VIDEO_PAGE."$id");
    }


    // assert

    private function assertListofCommentsPresents()
    {
        $this->assertEquals(
            1,
            $this->getFirstElementByTestName('table-comments-list')->count(),
            'The table containing the list of comments was not found.'
        );

        $this->assertEquals(
            1,
            $this->getFirstElementByTestName('comments-list-record')->count(),
            'There is no comment listed.'
        );
    }
}