PHP code example of chaplean / unit-bundle

1. Go to this page and download the library: Download chaplean/unit-bundle 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/ */

    

chaplean / unit-bundle example snippets


class MockService implements MockedServiceOnSetUpInterface
{
    /**
     * @return void
     */
    public static function getMockedServices()
    {
        $knpPdf = \Mockery::mock('Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator');
        $knpPdf->shouldReceive('getOutputFromHtml')->andReturn('example');
        $knpPdf->shouldReceive('getOutput')->andReturn('example');
       
        $mocks['knp_snappy.pdf'] = $knpPdf;
        
        $client = \Mockery::mock(Client::class);
        $client->shouldReceive('request')->andReturn(new Response());

        $mocks['guzzle.client.sor_api'] = $client;
        
        return $mocks;
    }
}

class ExampleTest extends FunctionalTestCase
{
    /**
     * @return array
     */
    public function rolesMustBeLoggedProvider()
    {
        return $this->rolesProvider(
            // rolesProvider is an utility to map your expectations with the
            // configured roles. It takes an array with the roles as keys and
            // your expectations as values.
            array(
                'NotLogged' => Response::HTTP_FORBIDDEN,
                'User'      => Response::HTTP_OK,
                'Admin'     => Response::HTTP_OK,
            )
        );
    }
    
    /**
     * @return array
     */
    public function rolesWithDifferentExpectations()
    {
        return $this->rolesProvider(
            // You can also give different expectations, see 3. Create a unittest
            // testWithDifferentExpectations to see how it translates in the test
            // function signature.
            array(
                'NotLogged' => Response::HTTP_FORBIDDEN,
                'User'      => array(Response::HTTP_OK),
                'Admin'     => array(Response::HTTP_OK, 'other expectation),
            )
        );
    }

    /**
     * @return array
     */
    public function rolesWithExtraRoles()
    {
        return $this->rolesProvider(
            array(
                'NotLogged' => Response::HTTP_FORBIDDEN,
                'User'      => Response::HTTP_OK,
                'Admin'     => Response::HTTP_OK,
            ),
            // You can also provide extra roles, thoses are added to the list
            // of default roles. Like with regular roles you provide the role
            // name as key and then the expectations as value, but the first
            // expectation must be the user to use to log in as.
            array(
                'SpecialCase' => array('user-3', Response::HTTP_OK)
            )
        );
    }
}

class ExampleTest extends FunctionalTestCase
{
    // Data provider ommited, see previous section
    
    /**
     * @dataProvider rolesMustBeLoggedProvider
     * 
     * @param string  $user
     * @param integer $expectedCode
     *
     * @return void
     */
    public function testRouteMustBeLogged($user, $expectedCode)
    {
        $client = $this->createClientWith($user);
        $client->request('/protected/url');
        
        $response = $client->getResponse();
        $this->assertEquals($expectedCode, $response->getStatusCode());
    }
    
    /**
     * @dataProvider rolesWithDifferentExpectations
     * 
     * @param string  $client
     * @param integer $expectedCode
     * @param string  $otherExpectation
     *
     * @return void
     */
    public function testWithDifferentExpectations($client, $expectedCode, $otherExpectation = null)
    {
        // $otherExpectation is not defined for every value in the provider so we must default to null
    }
}