vendredi 23 août 2019

Laravel package publishes a class - how can I write a test in the package as the pat will be wrong

I have a Laravel package which publishes some classes to the the app folder.

Part of the package needs to dynamically instantiate this or another class. I want to test that the correct class is being created. However, when running the tests in the package they haven’t been published to the final folder / namespace yet so can’t be found.

Package

/app/Blocks/DefaultPage.php //this file will be moved using Laravel’s publishing commands

Laravel app

/app/Blocks/DefaultPage.php // final location

This is the code I am trying to test, specifically the getPageClass method. I will be checking it returns the correct class.

<?php


namespace Riclep\Storyblok;

use Illuminate\Support\Str;

class Storyblok
{

    public function read()
    {
        $pageClass = $this->getPageClass($this->rawStory['content']['component']);

        return new $pageClass($this->rawStory);
    }

    private function getPageClass($component) {
        if (class_exists('App\Blocks\\' . Str::studly($component) . 'Page')) {

            return 'App\Blocks\\' . Str::studly($component) . 'Page';
        }

        return 'App\Blocks\DefaultPage';
    }

    /**
     * @param $data
     */
    public function setRawStory($data) {
        $this->rawStory = $data;
    }
}

But obviously the namespace / path is wrong. Is there some way I can do this?

Aucun commentaire:

Enregistrer un commentaire