简体   繁体   中英

Unit testing DB:: call with PhpUnit in Laravel

I am new to TDD. I am writing a little package that interpolate a string with values from the database. So far, I managed to write most of the test but I am getting stuck now that I need to test calls to the Database. I want to test a class that retrieves a particular row in the database given a table, column and id. It should then return that instance column value.

class DatabaseResolver
{
    private $referredField;
    private $dependencies;

    public function __construct(ReferredField $referredField, array $dependencies)
    {
        $this->referredField = $referredField;
        $this->dependencies = $dependencies;
    }

    public function resolve() : string
    {
        if(!isset($this->dependencies['id'])) {
            throw new MissingDependencyException('id');
        }

        $instance = $this->getTargetedInstance();
        if ($instance) {
            return $instance->{$this->referredField->column};
        }
        return '';
    }

    public function getTargetedInstance()
    {
        return DB::table($this->referredField->table)
            ->select($this->referredField->column)
            ->where('id', $this->dependencies['id'])
            ->first();
    }
}

I have managed to test that it throws my exception when there is no id in the dependency array and that it returns the original sentence when a token is not found:

    /** @test */
    public function it_will_throw_if_no_id_is_present_in_dependency()
    {
        $this->expectException(MissingDependencyException::class);

        $rf = ReferredField::newFactory()->testAsTable()->create();
        $rf->getReferredValue([]);
    }

    /** @test */
    public function it_returns_blanked_tokens_when_referred_field_is_not_found()
    {
        $stringToInterpolate = 'This is a {{adj}} day!';
        $interpolatedString = $this->interpoler->interpolate($stringToInterpolate, ['adj' => ['id' => -1]]);

        $this->assertEquals('This is a  day!', $interpolatedString);
    }

EDIT : Here is the code of the Interpoler class:

<?php

namespace Armcanada\LaravelReferredField\Concerns;

use Armcanada\LaravelReferredField\Exceptions\MissingDependencyException;

class Interpoler
{
    private $tokenizer;
    private $mapper;

    public function __construct(Tokenizer $tokenizer = null, TokenReferredFieldMapper $mapper = null)
    {
        $this->tokenizer = $tokenizer ?: new Tokenizer;
        $this->mapper = $mapper ?: new TokenReferredFieldMapper;
    }

    public function interpolate(string $text, array $dependencies = []) : string
    {
        $map = $this->getReferredFieldsFromText($text);
        $replacedMap = $map->mapWithKeys(function($referredField, $token) use($dependencies) {
            if (!isset($dependencies[$token])) {
                throw new MissingDependencyException($token);
            }
            $key = str_replace(' ', $token, config('laravel-referred-field.interpolation_pattern'));
            return [$key => $referredField?->getReferredValue($dependencies[$token]) ?? null];
        });
        
        $replacedMap->map(function($newValue, $token) use(&$text) {
            $text = str_replace($token, $newValue, $text);
        });
        return $text;
    }

    public function getReferredFieldsFromText($text)
    {
        $tokens = $this->tokenizer->extract($text);
        return $this->mapper->mapTokensToReferredFields($tokens);
    }

}

And my ReferredField model:

<?php

namespace Armcanada\LaravelReferredField\Models;

use Armcanada\LaravelReferredField\Database\Factories\ReferredFieldFactory;
use Armcanada\LaravelReferredField\Resolvers\DatabaseResolver;
use Armcanada\LaravelReferredField\Traits\Referrable;
use Illuminate\Database\Eloquent\Model;

class ReferredField extends Model
{
    use Referrable;

    protected $guarded = [];
    protected $table = 'referred_fields';

    protected static function newFactory()
    {
        return ReferredFieldFactory::new();
    }

    public function getReferredValue(array $dependencies) : string
    {
        $resolver = $this->getResolver($dependencies);

        return $resolver->resolve();
    }

    private function getResolver($dependencies)
    {
        if ($this->table !== null) {
            return new DatabaseResolver($this, $dependencies);
        }
        //return new HandlerResolver($this, $dependencies);
    }

}

How can I test this path?

$instance = $this->getTargetedInstance();
if ($instance) {
    return $instance->{$this->referredField->field};
}

I'm not entirely sure I understand your code, but if you want to test the path where $instance is not null, you could partially mock the DatabaseResolver 's getTargetedInstance method.

public function test_insert_test_method_name_here()
{
    $rf = ReferredField::factory()->create();

    $this->partialMock(DatabaseResolver::class)
         ->shouldReceive('getTargetedInstance')
         ->andReturn((object) [$rf->column => 'nice']);

    $stringToInterpolate = 'This is a {{ adj }} day!';
    $interpolatedString = $this->interpoler->interpolate($stringToInterpolate, ['adj' => ['id' => -1]]);

    $this->assertEquals('This is a nice day!', $interpolatedString);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM