Unit Tests

Issue #27 resolved
Gerry Hall created an issue

some of the current unit tests need to be re-developed as it would appear they designed to pass. for example

/**
     * @ticket 26
     * @expectedException \InvalidArgumentException
     */
    public function testCreateRepositoryWithWrongParamsType()
    {
        /** @var \Bitbucket\API\Repositories\Repository $repo */
        $repo   = $this->getApiMock('Bitbucket\API\Repositories\Repository');

        $repo->create('gentle', 'new-repo', '');
    }

The Above Passes yet the below fails yet all that was changed was blank to a number 1

/**
     * @ticket 26
     * @expectedException \InvalidArgumentException
     */
    public function testCreateRepositoryWithWrongParamsType()
    {
        /** @var \Bitbucket\API\Repositories\Repository $repo */
        $repo   = $this->getApiMock('Bitbucket\API\Repositories\Repository');

        $repo->create('gentle', 'new-repo', 1);
    }

Also the current Doc Block does not mention that the function accepts a json string.

/**
     * Create a new repository
     *
     * If `$params` are omitted, a private git repository will be created,
     * with a "no forking" policy.
     *
     * @access public
     * @param  string           $account The team or individual account owning the repository.
     * @param  string           $repo    The repository identifier.
     * @param  array            $params  Additional parameters
     * @return MessageInterface
     *
     * @throws \InvalidArgumentException If invalid JSON is provided.
     *
     * @see https://confluence.atlassian.com/x/WwZAGQ
     */
    public function create($account, $repo, $params = array())
    {
        // Keep BC for now.
        // @todo[1]: to be removed.
        if (is_array($repo)) {
            return $this->createLegacy($account, $repo);
        }

        $defaults = array(
            'scm'               => 'git',
            'name'              => $repo,
            'is_private'        => true,
            'description'       => 'My secret repo',
            'forking_policy'    => 'no_forks',
        );

        // allow developer to directly specify params as json if (s)he wants.
        if ('string' === gettype($params)) {
            if (empty($params)) {
                throw new \InvalidArgumentException('Invalid JSON provided.');
            }

            $params = json_decode($params, true);

            if (JSON_ERROR_NONE !== json_last_error()) {
                throw new \InvalidArgumentException('Invalid JSON provided.');
            }
        }

        $params = json_encode(array_merge($defaults, $params));

        return $this->getClient()->setApiVersion('2.0')->post(
            sprintf('repositories/%s/%s', $account, $repo),
            $params,
            array('Content-Type' => 'application/json')
        );
    }

below's test, tests more possible variables and yields Failures: 4.

    /**
     * @return array of invalid value for repo creations parameter
     */
    public function invalidCreateProvider()
    {
        return array(
            array(''),
            array("\t"),
            array("\n"),
            array(' '),
            array("{ 'bar': 'baz' }"),
            array('{ repo: "company", }'),
            array('{ repo: "company" }'),
            array(
                substr(
                    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
                    mt_rand( 0 ,50 ) ,1 ) .substr( md5( time() ), 1)
            ),
            array(1),
            array(false),
            array(1.234),
            array(null),
        );

    }

    /**
     * @param $check
     * @param $expectation
     * @return mixed
     * @expectedException \InvalidArgumentException
     *  @dataProvider invalidCreateProvider
     */
    public function testInvalidCreate($check)
    {

        $client     = $this->getHttpClientMock();

        /** @var \Bitbucket\API\Repositories\Repository $repo */
        $repo   = $this->getClassMock('Bitbucket\API\Repositories\Repository', $client);
        $this->setExpectedException('\InvalidArgumentException');
        $repo->create('gentle', 'new-repo', $check);
    }

and the follow altercation to the function passes all tests.

/**
     * Create a new repository
     *
     * If `$params` are omitted, a private git repository will be created,
     * with a "no forking" policy.
     *
     * @access public
     * @param  string           $account The team or individual account owning the repository.
     * @param  string           $repo    The repository identifier.
     * @param  array|string     $params  Additional parameters can be array or json string
     * @return MessageInterface
     *
     * @throws \InvalidArgumentException If invalid JSON is provided.
     *
     * @see https://confluence.atlassian.com/x/WwZAGQ
     */
    public function create($account, $repo, $params = array())
    {
        // Keep BC for now.
        // @todo[1]: to be removed.
        if (is_array($repo)) {
            return $this->createLegacy($account, $repo);
        }

        $defaults = array(
            'scm'               => 'git',
            'name'              => $repo,
            'is_private'        => true,
            'description'       => 'My secret repo',
            'forking_policy'    => 'no_forks',
        );

        // allow developer to directly specify params as json if (s)he wants.
        if ('string' === gettype($params)) {
            if (!($params = json_decode($params, true))) {
                throw new \InvalidArgumentException('Invalid JSON provided.');
            }
        }

        if(!$params = @array_merge($defaults, $params)){
            throw new \InvalidArgumentException('Invalid JSON provided.');
        }

        return $this->getClient()->setApiVersion('2.0')->post(
            sprintf('repositories/%s/%s', $account, $repo),
            json_encode($params),
            array('Content-Type' => 'application/json')
        );
    }

Comments (4)

  1. Log in to comment