vendredi 26 août 2016

Laravel 5 - Unable to bind interface

I am trying to implement a custom class (regarding business bank holiday days in England/Wales) that implements an interface.

My folder structure is:

App\Library\WorkDay.php
App\Library\Holiday\HolidayInterface.php
App\Library\Holiday\HolidayEnglandWales.php
App\Providers\HolidayInterfaceProvider.php
Tests\WorkDayTest.php

HolidayInterface:

<?php

namespace  App\Library\Holiday;

/**
 * Holiday Interface
 * 
 * @author andy.roberts
 */

interface HolidayInterface {

/**
 * Retrieve a list of holidays for supplied year
 * 
 * @param int $year Holiday year
 * @param int $subsitute Subsitute holiday on weekend
 */
public function getHoliday($year);

/**
 * Add a single holiday event
 * 
 * @param string $name
 * @param int $timestamp
 */
public function addHoliday($name, $timestamp);

/**
 * Remove single holiday event
 * 
 * @param int $timestamp
 */
public function removeHoliday($timestamp);
}

HolidayEnglandWales:

<?php

namespace  App\Library\Holiday;

use App\Library\Holiday\HolidayInterface;


/**
 * Public and bank holidays in England and Wales
 * 
 * @author andy.roberts
 */

class HolidayEnglandWales implements HolidayInterface {
    ....
}

HolidayInterfaceProvider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use App\Library\Holiday\HolidayEnglandWales;

class HolidayInterfaceProvider extends ServiceProvider
{

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
       $this->app->bind('App\Library\Holiday\HolidayInterface', function(){

            return new HolidayEnglandWales();

        });
    }


}

WorkDay:

<?php


namespace  App\Library;

/**
 * Calculate the number of working days between two dates
 * 
 * This is achieved by a simple algorithm which calculates 
 * the number of complete weeks and remaining days within 
 * any given period.
 * 
 * Each complete week is multiplied by the number of
 * working days, and the remaining days enumerated.
 * 
 * Public holidays are included in the calculation.
 * 
 * @author andy.roberts
 */

class WorkDay {

    const DAY = 86400;
    const WEEK = 604800;
    const MONDAY = 1;
    const TUESDAY = 2;
    const WEDNESDAY = 3;
    const THURSDAY = 4;
    const FRIDAY = 5;
    const SATURDAY = 6;
    const SUNDAY = 7;

    ...

    public function __construct(HolidayInterface $holiday, $params = array()) {

        $this->_nonWorkingDay = array(self::SATURDAY, self::SUNDAY);

        if(isset($params['includeEndDay'])) {
            $this->_includeEndDay = ($params['includeEndDay'] == true) ? true : false;
        }

        $this->_holiday = $holiday;
    }

}

WorkDayTest:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Models\User;
use Carbon\Carbon;
use Symfony\Component\Console\Output\ConsoleOutput;

use App\Library\WorkDay;
use App\Library\Holiday\HolidayEnglandWales;


/**
 * Working Days Test Case
 */
class WorkDayTest extends TestCase
{

    /**
     * Prepares the environment before running a test.
     */
     /*
    protected function setUp() {
        parent::setUp ();
        date_default_timezone_set('Europe/London');
    }
    */


    /**
     * Working days in a single month
     * 
     * January 2010
     * 
     * 31 days
     * 21 working days
     * 1 holiday (New Years Day)
     */
    public function testWorkingDayInSingleMonth() {
        $workDay = new WorkDay(new HolidayEnglandWales());
        $this->assertEquals($workDay->count('2010-01-01', '2010-01-31'), 20);
    }
}

In WorkDayTest when this line runs:

$workDay = new WorkDay(new HolidayEnglandWales());

This error is produced:

enter image description here

The class does not seem to be bound to the interface and I am not sure what the problem is. I have tried composer update to no avail. I have added the provider to the providers array in App\Config, btw.

Any help would be appreciated.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2bSKP7v
via IFTTT

Aucun commentaire:

Enregistrer un commentaire