phpunit testing:

  • Installation:
    • composer require –dev phpunit/phpunit
  • Usage on cli:
    • If it’s installed with composer in your project folder:
      • go to your project folder
        • .\vendor\bin\phpunit phptest.php 
  • phpunit config file:
    • create phpunit.xml
    • specify the testsuite (filename, location etc)
    • from the cli just use .\vendor\bin\phpunit
    • file content
  • Can use gruntjs for running phpunit
  • Accessing Main Class from Test Class
    • Autoload the main class location in composer.json
      • “autoload”:{
      • where ‘app’ is the folder location of the main class
      • PSR 4 app loading:
      • after specifying autoload execute following composer command to work with autoloaded classes
        • composer dump-autoload -o
    • Using the main class in Test class
      • create object $obj= new \App\ClassName; (fresh model for each method)
      • User the $obj to access the class properties
    • Naming for test methods:
      • ‘test’ has to be pre-fixed for all test class methods and has camel case methods
      • dot block method:
        • /** @test */
        • inserting this dot block before a method which has no ‘test’ pre-fix will run the test like other test cases which has ‘test’ pref-fix
    • Using setUp as a constructor method for creating a model with a protected variable
      • setUp runs before each method
      • protected $user;public function setUp(){
        $this->user = new \App\Models\User;
    • Collection class
      • convenient wrapper for any kind of objects
      • for iterating purposes
    • Important concepts:
    • Assertions:
      1. assertTrue
      2. assertEquals
      3. assertEmpty
      4. assertInstanceOf
      5. assertCount
      6. assertInternalType
    • Exceptions:
      1. expectExceptions


Tutorial series

Package Managers

Package – A package can be a GitHub shorthand, a Git endpoint, a URL, and more. Eg: bootstrap

Package Manager – Package manager is collection of software  tools that automates the process of installing, configuring, upgrading and removing any programs

  1. npm – node package manager
  2. bower – package manager for web
    • bower is a command line utility
    • has to be installed with npm
    • Installing bower: (-g installs it globally)
      • npm install -g bower
    • bower requires node, npm and git
    • bower init to initialize bower.json file
    • Installing packages with bower
      • bower install <package>
      • bower install <package> -S (for saving in bower.json)
      • bower install <package> -D (for saving in bower.json as devdependency)
    • Issues git not found event after having git client tool
      • solution:
        • locate your git.exe file
        • It’s usually located here – C:\Users\user\AppData\Local\GitHub\PortableGit_dGUID\cmd
        • GUID changes when the git gets updated
        • copy the path and paste it in command line
        • enter ‘git’ command, if you see git help options that means git is working
        • now copy the path, remove ‘.git.exe’ at the end and paste into PATH variable
        • close and reopen the command line and try entering git command again. If everything goes right, git works!
      • package.json file gets created with all dependencies. If not just enter, ‘npm init’ command
  3. composer – A dependency manager for php
    • Installing composer
      • The installer will download composer for you and set up your PATH environment variable so you can simply call composer from any directory.
    • Installing packages with composer:
      • composer require twbs/bootstrap
    • For php
      • Eg:
        • composer require phpmd/phpmd
        • composer require –dev phpunit/phpunit
          • Using phpunit from commandline
            • .\vendor\bin\phpunit testFile.php
            • Same pattern of execution for other modules installed with composer
    • composer.json file gets created with all dependencies