S
S
sl1m_dogg2016-03-15 20:31:08
PHP
sl1m_dogg, 2016-03-15 20:31:08

What's wrong with my PHP tests?

<?php

namespace Tasks\tests;

/*
require_once '../tasks/task3/Lesson.php';
require_once '../tasks/task3/Grammar.php';
require_once '../tasks/task3/Speaking.php';
require_once '../Tasks/task3/Strategy/CostStrategy.php';
require_once '../tasks/task3/Strategy/TimeStrategy.php';
require_once '../tasks/task3/Strategy/FixedStrategy.php';
*/
use Tasks\task3\Grammar;
use Tasks\task3\Speaking;
use Tasks\task3\Strategy\FixedStrategy;
use Tasks\task3\Strategy\TimeStrategy;

class TestLessons extends \PHPUnit_Framework_TestCase
{
    public function testLessonsGrammarFixedCostGood()
    {
        for ($i = 0; $i < 2; $i++) {
            $grammar = new Grammar(2, new FixedStrategy());
            $this->assertEquals(600, $grammar->cost());
        }
    }

    public function testLessonsSpeakingTimedCostGood()
    {
        for ($i = 0; $i < 2; $i++) {
            $spaeking = new Speaking(3, new TimeStrategy());
            $this->assertEquals(900, $spaeking->cost());
        }
    }

    public function testLessonsGrammarTimedCostBad()
    {
        for ($i = 0; $i < 2; $i++) {
            $grammar = new Grammar(3, new TimeStrategy());
            $this->assertNotEquals(900, $grammar->cost());
        }
    }
}

When I run phpunit TestLessons.php it says Class Tasks\task3\Grammar not found, psr-0 is set in the composer, I tried phpunit --bootstrap path_to_autoload, I tried to connect the retriever directly to the tests, everything is the same

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Entelis, 2016-03-15
@DmitriyEntelis

Telepath mode:
1. Instead of use Tasks\task3\Grammar;all the same, it’s more correct to write use \Tasks\task3\Grammar;
2. Show the beginning of the tasks/task3/Grammar.php file
3. As far as I remember, autoloading will not work without explicit --bootstrap - but since everything crashes and the problem is clearly with require_once not in this.

S
sl1m_dogg, 2016-03-15
@sl1m_dogg

<?php

namespace Tasks\task3;

use Tasks\task3\Strategy\CostStrategy;

class Lesson
{
    protected $duration;

    protected $costStrategy;

    public function __construct($duration, CostStrategy $strategy)
    {
        $this->duration = $duration;
        $this->costStrategy = $strategy;
    }

    public function cost()
    {
        return $this->costStrategy->cost($this);
    }

    public function chargeType()
    {
        return $this->costStrategy->chargeType();
    }

    public function getDuration()
    {
        return $this->duration;
    }
}

Grammar just extends from this class

@
@smple, 2016-03-29
_

after adding the psr entry maybe you forgot to do a composer update ?
post the files:
autoload_classmap.php
autoload_namespaces.php
autoload_psr4.php
autoload_real.php
see if your namespaces are declared there and where the composer is looking for them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question