I have a service the consumes data from an external source. I would like to unit test the service and I am not sure how to get the test set up. To start with I would like to just test 1 method. Here is my service:
<?php
namespace Drupal\data_provider;
use Drupal\data_provider\Security\DataProviderCrypt;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\ClientInterface;
use Drupal\Core\Cache\CacheBackendInterface;
/**
* DataProviderServic.
*
* @ingroup data_provider
*
* @group data_provider
*/
class DataProviderService {
public $baseUri;
protected $username;
protected $password;
/**
* The HTTP client to fetch the feed data with.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* The cache.default cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;
/**
* Constructs a database object.
*
* @param \GuzzleHttp\ClientInterface $http_client
* The Guzzle HTTP client.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache object associated with the default bin.
*/
public function __construct(ClientInterface $http_client, CacheBackendInterface $cache_backend) {
$this->httpClient = $http_client;
$this->cacheBackend = $cache_backend;
$this->baseUri = \Drupal::config('data_provider.settings')->get('base_uri');
$this->username = \Drupal::config('data_provider.settings')
->get('username');
$this->password = \Drupal::config('data_provider.settings')
->get('password');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Forms that require a Drupal service or a custom service should access
// the service using dependency injection.
// @link https://www.drupal.org/node/2203931.
// Those services are passed in the $container through the static create
// method.
return new static(
$container->get('http_client'),
$container->get('cache.default')
);
}
/**
* Create a formatted request based on options provided
*
* @param $url
* Requested URl.
* @param array $options
* Query parameter options.
* @param bool $reset
* Rest the cache.
*
* @return bool|string
*/
public function doRequest($url, $options = array(), $reset = FALSE) {
// request from api
return $content;
}
/**
* Return the data from the API in json format.
*
* @return bool|mixed
*/
public function getSubscriptions() {
// do_request and from the api and return info
return $subscriptions;
}
}
and here is my test:
<?php
namespace Drupal\data_provider\Tests\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\data_provider\DataProviderService;
/**
* DataProviderServic unit test.
*
* @ingroup data_provider
*
* @group data_provider
*/
class DataProviderServiceTest extends UnitTestCase {
/**
* Very simple test of DataProviderService::getSubscriptions().
* @todo write dataprovider tests.
*/
public function testGetSubscriptions() {
$dp = new DataProviderService();
$subs = $dp->getSubscriptions();
$this->assertEquals(TRUE, TRUE);
}
}
when I run the tests I get these errors:
PHPUnit 4.8.36 by Sebastian Bergmann and contributors.
Testing
E
Time: 42.35 seconds, Memory: 610.25MB
There was 1 error:
1) Drupal\data_provider\Tests\Unit\DataProviderServiceTest::testGetSubscriptions
Argument 1 passed to Drupal\data_provider\DataProviderService::__construct() must be an instance of GuzzleHttp\ClientInterface, none given, called in /var/www/drupal8/docroot/modules/custom/data_provider/tests/src/Unit/DataProviderServiceTest.php on line 22 and defined
/var/www/drupal8/docroot/vendor/symfony/phpunit-bridge/DeprecationErrorHandler.php:73
/var/www/drupal8/docroot/modules/custom/data_provider/src/DataProviderService.php:63
/var/www/drupal8/docroot/modules/custom/data_provider/tests/src/Unit/DataProviderServiceTest.php:22
I believe the issue has to do with DI and how to properly do it. So my questions:
1. How do I start with testing methods on the service?
2. Should my unit tests make actual api requests or mock them?
new Class()
and passing no arguments to it. The constructor is expecting arguments. If this is a service registered to Drupal (w a services.yml file) you can bring in this service already constructed for you.\Drupal
.