---
# System prepended metadata

title: Testing Use Cases
tags: [phpunit, UnitTests]

---

# Testing Use Cases

View the book with "<i class="fa fa-book fa-fw"></i> Book Mode".

## Integration
### Laravel
We use [Testbench Component](https://packages.tools/testbench/getting-started/introduction.html) for testing Process Packages (Laravel Package).
#### Service Providers
Here we verify that when we resolve an interface, we get an item of the correct class back:
```
<?php

namespace AboutYou\MWC\BAPI\Client\Tests\Integration\Laravel;

use AboutYou\MWC\BAPI\Client\ClientInterface;
use AboutYou\MWC\BAPI\Client\Laravel\ClientProviderInterface;
use AboutYou\MWC\BAPI\Client\Laravel\Provider\BAPIClientProvider;
use LogicException;
use Orchestra\Testbench\TestCase;

class ClientProviderTest extends TestCase
{

    private ClientProviderInterface $clientProvider;

    public function setUp(): void
    {
        parent::setUp();
        $this->clientProvider = $this->app->make(ClientProviderInterface::class);
    }

    protected function getPackageProviders($app): array
    {
        return [BAPIClientProvider::class];
    }

    protected function getEnvironmentSetUp($app)
    {
        $payload = include __DIR__ . '/fixtures/MwcBapiClientConfig.php';
        $app['config']->set('mwc.bapi.client', $payload);
    }

    public function testRegisterSetsTheDependencies()
    {
        $this->assertInstanceOf(ClientInterface::class, $this->clientProvider->getClient(1));
    }
}
```
#### Repository
##### Database 
###### Connections
We can easily mock connections through the facade:
```
        $pdo = $this->getMockBuilder(PDO::class)
            ->disableOriginalConstructor()
            ->getMock();
        $pdo->expects($this->once())
            ->method('prepare')
            ->willReturn(false);

        $connection = $this->getMockBuilder(Connection::class)
            ->disableOriginalConstructor()
            ->getMock();
        $connection->expects($this->once())
            ->method('getPdo')
            ->willReturn($pdo);

        DB::shouldReceive('connection')->once()->andReturn($connection);
```
###### Has data
To check if data was saved correctly in database
```
$this->assertDatabaseHas(
            'table_name',
            [
                'shop_id' => 3,
                'product_id' => 24
            ]
        );
```
#### Commands
##### No much controll on mocking command dependecies
[Laravel Documentation for Console tests](https://laravel.com/docs/9.x/console-tests)
##### Full controll on mocking command dependecies
We will test command class as a normal class, we will use **PHPUnit\Framework\TestCase** and not **Orchestra\Testbench\TestCase**. 
We can mock all class dependencies. And to controll command options/arguments, we will use **Symfony\Component\Console\Input**.
eg:
```
$definition = new InputDefinition();
 
$inputOptionFilter = new InputOption('filter', null, 4);
$definition->addOption($inputOptionFilter);

$inputArgumentFilter = new InputArgument('filter');
$definition->addArgument($inputArgumentFilter);
  
$input = new ArrayInput(['--filter' => '4'],$definition);

$batchProcessorCommand= new BatchProcessorCommand();
$batchProcessorCommand->setInput($input);

batchProcessorCommand->handle();
```
## Unit
---
###### tags: `UnitTests` `phpunit`
