Coder Social home page Coder Social logo

phpunitsamples's Introduction

PHPUnitSamples

Create composer.json
{
    "require-dev": {
            "phpunit/phpunit":"^5.7"
    },
    "autoload": {
        "psr-4":{
            "App\\":"app/"
        }
    }
}
To install the requried packages composer.json
$ composer install

autoload.php is generated in the vendor folder [Autoloading allows us to use PHP classes without the need to require() or include() them]

PSR-4 is the newest standard of autoloading in PHP, mandates us to use namespaces
  • Put the classes that we want to autoload in a dedicated directory.
  • Give the classes a namespace
  • Point the namespace to the app/ directory in the composer.json file
  • Update the Composer autoloader using
$ composer dumpautoload -o

Reference : Site

PHPUnit.xml
  • The attributes of the element can be used to configure PHPUnit's core functionality
  • Bootstrap the autoload.php in phpunit.xml
  • The element and its one or more children can be used to compose a test suite out of test suites and test cases. PHPUnit Manual
<testsuites>
  <testsuite name="My Test Suite">
    <directory>/path/to/*Test.php files</directory>
    <file>/path/to/MyTest.php</file>
    <exclude>/path/to/exclude</exclude>
  </testsuite>
</testsuites>
  • For Code coverage add below code
   <filter>
      <whitelist processUncoveredFilesFromWhitelist="true">
         <directory suffix=".php">./app</directory>
      </whitelist>
   </filter>
   <logging>
      <log type="coverage-html"
      	target="./tmp/coverage/html/" 
      	charset="UTF-8" 
        highlight="true" 
        lowUpperBound="60" 	
        highLowerBound="90" />
      <log type="coverage-clover" target="./tmp/coverage/clover.xml" />
   </logging>

Running all tests

C:/xampp/htdocs/PHPUnitSamples/vendor/phpunit/phpunit/phpunit --configuration C:\xampp\htdocs\PHPUnitSamples\phpunit.xml C:\xampp\htdocs\PHPUnitTestStudy\tests

Sample Stub [Reference : Lynda - Test-Driven Development in PHP with PHPUnit]

    public function testPostTaxTotal() {
        $Receipt = $this->getMockBuilder('App\Receipt')
            ->setMethods(['tax', 'total'])
            ->getMock();
        $Receipt->method('total')->will($this->returnValue(10.00));
        $Receipt->method('tax')->will($this->returnValue(1.0));
        $result = $Receipt->postTaxTotal([1, 2, 5, 8], 0.20, null);
        $this->assertEquals(11.00, $result);
    }

Sample Mock

    //Mock has expectations as to which stub methods are called and the inputs to the stub methods
    public function testPostTaxTotalMock() {
        $items = [1,2,5,8];
        $coupon = null;
        $tax = 0.20;
        $Receipt = $this->getMockBuilder('App\Receipt')
            ->setMethods(['tax', 'total'])
            ->getMock();
        $Receipt->expects($this->once())
            ->method('total')
            ->with($items, $coupon)
            ->will($this->returnValue(10.00));
        $Receipt
            ->expects($this->once())
            ->method('tax')
            ->with(10.0, $tax)
            ->will($this->returnValue(1.0));
        $result = $Receipt->postTaxTotal([1, 2, 5, 8], 0.20, null);
        $this->assertEquals(11.00, $result);
    }

Adding Data Providers to the test

    public function provideTotalData() {
        return [
            'indexdataset#1'=> [[1,2,5,8],16],
            'indexdataset#2'=> [[1,2,0,8],11]
        ];
    }

    /** @test
     * @dataProvider provideTotalData
     */
    public function testTotal($input, $expected) {
        $coupon = null;
        $output = $this->receipt->total($input, $coupon);
        $this->assertEquals($expected, $output, "When sunmming total to be {$expected}");
    }

phpunitsamples's People

Contributors

stavan2003 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.