2

I am writing a plugin and want to unit test my services. I am following the example given here: https://github.com/amacneil/cocktailrecipes. The problem is when I run my application, I get a

PHP Fatal Error: Class 'TournamentData_IndividualService' not found.

Here is the problem portion of the test code:

<?php
namespace Craft;

use \Mockery as m;
use PHPUnit_Framework_TestCase;

class TournamentData_IndividualServiceTest extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->ingredientRecord = m::mock('Craft\TournamentData_IndividualRecord');
        $this->service = new TournamentData_IndividualService($this->ingredientRecord);
    }
}

Here is my service code:

<?php
namespace Craft;

class TournamentData_IndividualService extends BaseApplicationComponent
{
    protected $individualRecord;

    public function __construct($individualRecord = null)
    {
        $this->individualRecord = $individualRecord;
        if(is_null($this->individualRecord)) {
            $this->individualRecord = TournamentData_IndividualRecord::model();
        }
    }
}

One more note, just in case this has the answer to what I'm looking for, I have taken a look at https://github.com/selvinortiz/craft.loath, but I wasn't able to follow what its setup code was accomplishing.

Any help is greatly appreciated!

2

I figured out the problem, I needed to load in the service, and then my service needed to load in the model and record. So for the test code, I added the function:

protected function loadServices()
{
    require_once __DIR__ . '/../services/TournamentData_IndividualService.php';
}

Then for my setUp function, it became:

<?php
namespace Craft;

use \Mockery as m;
use PHPUnit_Framework_TestCase;
class TournamentData_IndividualServiceTest extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->loadServices();
        $this->ingredientRecord = m::mock('Craft\TournamentData_IndividualRecord');
        $this->service = new TournamentData_IndividualService($this->ingredientRecord);
    }
}

Then in my service method, I added require statements for the model and record like so:

<?php

require_once __DIR__ . '/../models/TournamentData_IndividualModel.php';
require_once __DIR__ . '/../records/TournamentData_IndividualRecord.php';

namespace Craft;

class TournamentData_IndividualService extends BaseApplicationComponent
{
    protected $individualRecord;

    public function __construct($individualRecord = null)
    {
        $this->individualRecord = $individualRecord;
        if(is_null($this->individualRecord)) {
            $this->individualRecord = TournamentData_IndividualRecord::model();
        }
    }
}

Ironically, I managed to find the answer to my problem almost immediately after deciding to ask the community, but hopefully this helps somebody else in the future!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.