K
K
kaxa32012018-12-25 18:01:02
PHPUnit
kaxa3201, 2018-12-25 18:01:02

How to write phpunit tests correctly?

I am learning to write phpunit tests, I want to try to write a test of
my code on the existing code

<?php
class ContentService implements ContentServiceInterface
{
    private $client;

    /**
     * ContentServiceInterface constructor.
     * @param Client $client
     */
    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    /**
     * @param $query
     * @return mixed
     */
    public function getContent($query): ?string
    {
        $queryEncode = urlencode($query);

        if ($result = $this->loadFromCache($queryEncode)) {
            return $result['content'];
        }

        return null;
    }

    public function sqrt($x)
    {
        return sqrt($x);
    }

}

here is my trial test
<?php

namespace Tests\Feature;

use App\Services\ContentService;
use GuzzleHttp\Client;
use Tests\TestCase;

class ContentServiceTest extends TestCase
{
    public $client;

    public function __construct()
    {
        parent:: __construct();


    }


    public function testsqrt($client): void
    {
        $o = new ContentService($client);
        $this->assertEquals(4, $o->sqrt(16));
    }
}

I get an error
ArgumentCountError: Too few arguments to function Tests\Feature\ContentServiceTest::testsqrt(), 0 passed

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey, 2018-12-25
@kaxa3201

Correct test for this code:

<?php

namespace Tests\Feature;

use App\Services\ContentService;
use GuzzleHttp\Client;
use Tests\TestCase;

class ContentServiceTest extends TestCase
{
    public function testSqrt(): void
    {
        $client = new Client();
        $o = new ContentService($client);
        $this->assertEquals(4, $o->sqrt(16));
    }
}

It's also a good idea to annotate tests in order to accurately map what is being tested. If you do not specify annotations, then when building a report on code coverage, not only the tested methods will be taken into account, but in general all the calls that were used in the framework of testing
<?php

namespace Tests\Feature;

use App\Services\ContentService;
use GuzzleHttp\Client;
use Tests\TestCase;

/**
 * @coversDefaultClass \App\Services\ContentService
 */
class ContentServiceTest extends TestCase
{
    /**
     * @covers ::sqrt
     */
    public function testSqrt(): void
    {
        $client = new Client();
        $o = new ContentService($client);
        $this->assertEquals(4, $o->sqrt(16));
    }
}

G
GTRxShock, 2018-12-25
@GTRxShock

well, as it were
and that says it all)
who will inject the client into the test for you?
public function testsqrt($client): void

P
Pavel Tereshchenko, 2018-12-25
@l2ping

<?php

namespace Tests\Feature;

use App\Services\ContentService;
use GuzzleHttp\Client;
use Tests\TestCase;

class ContentServiceTest extends TestCase
{
    public function testsqrt(): void
    {
        $config = [];
        $client = new GuzzleHttp\Client($config);
        $o = new ContentService($client);
        $this->assertEquals(4, $o->sqrt(16));
    }
}

If $client will be used somewhere else in the test class, take out its initialization.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question