Demo entry 3975702

dsaf

   

Submitted by anonymous on Mar 10, 2016 at 15:31
Language: PHP. Code size: 5.8 kB.

<?php
require __DIR__ . '/../../../vendor/autoload.php';

/**
 * Class AdminTest
 * Her mocker vi Admin klassen. vi tester av funkjonene blir suksessfult kalt og returnerer korrekte verdier
 *
 * Metodene er laget med god hjelp fra phpunit sin manual Kap 9
 * https://phpunit.de/manual/current/en/test-doubles.html
 * http://php.net/manual/en/spl.exceptions.php
 */
class AdminTest extends PHPUnit_Framework_TestCase
{
    private static $kunde;
    private static $expected = 'OK';

    /**
     * sets $kunde object
     */
    public static function setUpBeforeClass()
    {
        self::$kunde = new Kunde('12345678', 'arne', 'går', 'mot', 2341, 'døra', '99933222', 'passord');
    }

    /**
     * Returns a mock for Admin Class with param as methodname
     * @param $functionName
     * @return PHPUnit_Framework_MockObject_MockObject
     */
    private function createAdminMock($functionName)
    {
        return $this->getMockBuilder('Admin')
            ->disableOriginalConstructor()
            ->setMethods(array($functionName))
            ->getMock();
    }

    /**
     * Factory for making a mock of a function that expectsts a customer and returns ok
     * @param $method
     * @return PHPUnit_Framework_MockObject_MockObject
     */
    private function mockMethodFactory($method)
    {
        $mock = self::createAdminMock($method);

        $mock->expects(static::once())
            ->method($method)
            ->with(static::equalTo(static::$kunde))
            ->willReturn(static::$expected);

        return $mock;
    }

    /**
     * tester at metoden blir kallt
     * @covers /BLL/Admin::hentAlleKunder
     * @uses /BLL/Admin
     */
    public function testHentAlleKunder()
    {
        $method = 'hentAlleKunder';
        $admindatabase = $this->createAdminMock($method);
        $array = array(
            array('01010110523', 'Lene', 'Jensen', 'Askerveien 22', 3270, '22224444', 'deasa', 'Heihei'),
            array('12345678901', 'Per', 'Hansen', 'Osloveien 82', 1234, '12345678', 'sasaf', 'Heihei')
        );

        $admindatabase->expects(static::once())
            ->method($method)
            ->willReturn($array);

        static::assertTrue(is_array($admindatabase->hentAlleKunder()));
    }

    /**
     * tester at metoden blir kalt og får svar
     * @covers /BLL/Admin::endreKundeInfo
     * @uses /BLL/Admin
     * @uses /Model/Kunde
     */
    public function testendreKundeInfo()
    {
        $method = 'endreKundeInfo';
        $mock = self::createAdminMock($method);

        $kundefeil = new Kunde('12345678', 'ikkearne', 'går', 'mot', 2341, 'døra', '99933222', 'passord');
        $error = 'Feil';

        $mock->expects(static::exactly(2))
            ->method($method)
            ->withConsecutive(
                array(static::equalTo(static::$kunde)),
                array(static::equalTo($kundefeil))
            )
            ->willReturnOnConsecutiveCalls(static::$expected, $error);

        static::assertEquals(static::$expected, $mock->endreKundeInfo(static::$kunde));
        static::assertEquals($error, $mock->endreKundeInfo($kundefeil));
    }

    /**
     * tester at metoden blir kalt og får svar
     * @covers /BLL/Admin::registrerKunde
     * @uses /BLL/Admin
     * @uses /Model/Kunde
     */
    public function testregistrerKunde()
    {
        $mock = $this->mockMethodFactory('registrerKunde');
        static::assertEquals(static::$expected, $mock->registrerKunde(static::$kunde));
    }

    /**
     * tester at metoden blir kalt og får svar
     * @covers /BLL/Admin::slettKunde
     * @uses /BLL/Admin
     * @uses /Model/Kunde
     */
    public function testslettKunde()
    {
        $method = 'slettKunde';
        $mock = self::createAdminMock($method);

        $mock->expects(static::once())
            ->method($method)
            ->with(static::equalTo(static::$kunde->personnummer))
            ->willReturn(static::$expected);

        static::assertEquals(static::$expected, $mock->slettKunde(static::$kunde->personnummer));
    }

    /**
     * tester at metoden blir kalt og får svar
     * @covers /BLL/Admin::registerKonto
     * @uses /BLL/Admin
     * @uses /Model/Kunde
     */
    public function testregisterKonto()
    {
        $mock = $this->mockMethodFactory('registerKonto');
        static::assertEquals(static::$expected, $mock->registerKonto(static::$kunde));
    }

    /**
     * tester at metoden blir kalt og får svar
     * @covers /BLL/Admin::endreKonto
     * @uses /BLL/Admin
     * @uses /Model/Konto
     */
    public function testendreKonto()
    {
        $method = 'endreKonto';
        $mock = self::createAdminMock($method);
        $konto = new Konto();

        $mock->expects(static::once())
            ->method($method)
            ->with(static::equalTo($konto))
            ->willReturn(static::$expected);

        static::assertEquals(static::$expected, $mock->endreKonto($konto));
    }

    /** tester at metoden blir kalt og får svar
     * @covers /BLL/Admin::endreKonto
     * @uses /BLL/Admin
     */
    public function testhentAlleKonti()
    {
        $method = 'hentAlleKonti';
        $mock = self::createAdminMock($method);
        $array = array(
            array('105010123456', '01010110523', 720, 'Lønnskonto', 'NOK'),
            array('105020123456', '01010110523', 100500, 'Sparekonto', 'NOK')
        );

        $mock->expects(static::once())
            ->method($method)
            ->willReturn($array);

        static::assertTrue(is_array($mock->hentAlleKonti()));
    }
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).