Sunday, 28 February 2021

Introduction to PHP

 

PHP stands for php hypertext processor, it is powerful language designed for creating HTML content.

 

Php is one of the popular language to create dynamic websites. One of the key advantage of php is, it can talk to backend infrastructure like databases, file systems etc.,

 

Some notable features

a.   PHP has built-in support to generate pdf, jpg, png and gif images.

b.   PHP has support to communicate with variety of databases (Ex: Oracle, MySQL, Postgres etc.,).

 

Where can I use PHP?

a.   Server side scripting

b.   Command line scripting

c.    Form processing

d.   Reading file contents

e.   Talk to databases like MySQL, Oracle, Postgres etc.,

 

 

Can PHP run on multiple operating systems?

Yes, php can run on operating systems like Linux, Mac and Windows.

 

Is php support object oriented principles (Oops)?

Yes, oops features are added in PHP V5.0 onwards.

 

You can download the complete working application from below link.

https://github.com/harikrishna553/php_core

Previous                                                    Next                                                    Home

PHP core tutorial

      Introduction to PHP
      How PHP works?
      Install php on Mac
      Hello World application
      Php: apache: hello world application
      Access query parameters
      post form example
      Basic syntax
      variables
      Variable references
      echo: print the data to console
      comments: Documenting the code
      Php: data types
      Booleans
      is_bool: check variable holds Boolean value or not
      strings
      Single vs double quotes
      in place substitution of variables in a string
      Concatenate strings
      Escape characters
      built-in string functions
            strtolower: Get the string in lowercase form
            strtoupper: Get the string in uppercase form
            ucfirst: Make a string's first character uppercase
            lcfirst: Make a string's first character lowercase
            ucwords: Uppercase the first character of each word in a string
            strlen: Get string length
            ltrim, rtrim, trim : Trim the string
            strrev: Reverse a string
            substr: Return part of a string
      NULL: Represent nothing
      Emptiness
      Constants
      Arrays
            Add an element to the array
            Add an element to associate array
            Update values of array using reference
            Array pointers
            Basic array functions
                  array: count: count number of elements in the array
                  Array: max, min: get maximum and minimum value of array
                  sort: Sort the elements of array in ascending order
                  rsort: Sort array contents in descending order
                  is_array(): Chek whether a variable is an array
                  in_array: Check for existence of an element
      Operators
            Comparison operators
            <=>: Spaceship operator
            Logical operators
            Arithmetic operators
            what is the sign of modulo operator result?
            ++, --: increment and decrement operators
      Conditional statements
      if statement
      php is loosely coupled: === operator
      else statement
      if – else if - else statement
      Ternary operator
      switch statement
      while loop
      do-while loop
      for-each loop
      break statement: exit from the loop
      continue statement
      multi-dimensional arrays
            Access elements in a multi-dimensional array
      functions
            Access variables defined outside of a function
            Set default value to function argument
            functions: return a value from function
            Functions: pass the argument by reference
            Anonymous function
            Classes and objects
            How to define a class?
            get_declared_classes: Get all the classes that Php knows
            class_exists: Check whether class is defined or not
            How to get an object from class?
            Add properties to class definition
            Add methods to the class definition
            $this: refer to current instance properties
            Convert an object to string
            Class inheritance
            Override method definitions in subclass
            Access specifiers (or) visibility modifiers
            Adding dynamic properties, overloading
            Setter and getter methods
            Static properties
            static methods
            Access static properties from super classes
            new self(): Return instance of the class where new statement is defined
            new self() vs new static()
            Class level constants
            How to refer parent class properties, methods from sub class
            __construct: Constructor method
            Constructors and inheritance
            Why to use array as an argument to constructor definition
            clone an object
            __clone called while cloning an object
            Object are assigned by reference
            Implement singleton design pattern
            How to compare two objects?
      Return multiple values from a function
      Organizing your code
      include_once, require_once
      Interfaces
            class implement multiple interfaces
      Traits
      Abstract classes
      namespaces
      namespace: access entities using aliases
      Exception handling
      How to throw an exception
      Handle Exceptions
      try and multiple catch statements
      finally block
      uncaught exception handler
Previous                                                    Next                                                    Home

Friday, 26 February 2021

EnvironmentVariables: Set and clear Environment Variables

'EnvironmentVariables' rule allows you to set environment variables for your test. All changes to environment variables are reverted after the test.

 

Example

@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

environmentVariables.set(COUNTRY_KEY, "India");
environmentVariables.set(STATE_KEY, "Andhra Pradesh");

assertEquals("India", System.getenv(COUNTRY_KEY));
assertEquals("Andhra Pradesh", System.getenv(STATE_KEY));

Find the below working application.

 

EnvironmentVariablesTest.java

package com.sample.app.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;

public class EnvironmentVariablesTest {
	@Rule
	public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

	private static final String COUNTRY_KEY = "country";
	private static final String STATE_KEY = "state";

	private void printEnvProps() {
		System.out.println(COUNTRY_KEY + "-> " + System.getenv(COUNTRY_KEY));
		System.out.println(STATE_KEY + "-> " + System.getenv(STATE_KEY));
	}

	@Test
	public void test() {
		printEnvProps();

		System.out.println("\nSetting Environment Variables\n");
		environmentVariables.set(COUNTRY_KEY, "India");
		environmentVariables.set(STATE_KEY, "Andhra Pradesh");

		assertEquals("India", System.getenv(COUNTRY_KEY));
		assertEquals("Andhra Pradesh", System.getenv(STATE_KEY));

		printEnvProps();

		System.out.println("\nClearning the key 'country'\n");
		environmentVariables.clear(COUNTRY_KEY);

		assertNull(System.getenv(COUNTRY_KEY));
		assertEquals("Andhra Pradesh", System.getenv(STATE_KEY));

		printEnvProps();
	}

}


Output

country-> null
state-> null

Setting Environment Variables

country-> India
state-> Andhra Pradesh

Clearning the key 'country'

country-> null
state-> Andhra Pradesh


You can download complete working application from this link.

https://github.com/harikrishna553/system-rules





 

 

Previous                                                    Next                                                    Home

ExpectedSystemExit: Test System exits

Follow below steps to test system exit codes.

 

Step 1: Create a rule of type ExpectedSystemExit.

@Rule

public final ExpectedSystemExit exit = ExpectedSystemExit.none();

 

Step 2: Use 'expectSystemExitWithStatus' method to test system exit with status code.

exit.expectSystemExitWithStatus(0);

 

SystemExitApp.java

package com.sample.app;

public class SystemExitApp {

	public void exitApp() {
		System.exit(0);
	}

}

 

SystemExitAppTest.java

package com.sample.app.tests;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;

import com.sample.app.SystemExitApp;

public class SystemExitAppTest {
	@Rule
	public final ExpectedSystemExit exit = ExpectedSystemExit.none();

	@Test
	public void exitApp() {
		exit.expectSystemExitWithStatus(0);

		SystemExitApp app = new SystemExitApp();

		app.exitApp();
	}

}

 

You can download complete working application from this link.

https://github.com/harikrishna553/system-rules

 

 

 

  

Previous                                                    Next                                                    Home

DisallowWriteToSystemErr: Disable System error messages

‘DisallowWriteToSystemErr’ class make the test fail, if the test tries to write some data to System.err.

 

DisallowWriteToSystemErrTest.java

package com.sample.app.tests;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.DisallowWriteToSystemErr;

public class DisallowWriteToSystemErrTest {
	@Rule
	public final DisallowWriteToSystemErr disallowWriteToSystemErr = new DisallowWriteToSystemErr();

	@Test
	public void thisTestFail() {
		System.err.println("Hello World");
	}
}

 

You can download complete working application from this link.

https://github.com/harikrishna553/system-rules

 

 

 

  

Previous                                                    Next                                                    Home

DisallowWriteToSystemOut: Disable System output messages

‘DisallowWriteToSystemOut’ class make the test fail, if the test tries to write some data to System.out.

 

DisallowWriteToSystemOutTest.java

package com.sample.app.tests;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.DisallowWriteToSystemOut;

public class DisallowWriteToSystemOutTest {
	@Rule
	public final DisallowWriteToSystemOut disallowWriteToSystemOut = new DisallowWriteToSystemOut();

	@Test
	public void thisTestFail() {
		System.out.println("Hello World");
	}
}

 

 

 

Previous                                                    Next                                                    Home

TextFromStandardInputStream: Create tests that depends on input (System.in)

‘TextFromStandardInputStream’ class is used to create tests that depends on System input (System.in).

 

'provideLines' method is used to provide input to the program.

 

Arithmetic.java

package com.sample.app;

import java.util.Scanner;

public class Arithmetic {

	public int sum() {
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		return a + b;
	}
}

As you see sum() method, it takes two integers a and b, return sum of a and b.

 

We can pass input 1, 2 using provideLines method like below.

@Rule
public final TextFromStandardInputStream systemInMock = emptyStandardInputStream();

systemInMock.provideLines("1", "2");


ArithmeticTest.java

package com.sample.app.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.contrib.java.lang.system.TextFromStandardInputStream.emptyStandardInputStream;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.TextFromStandardInputStream;

import com.sample.app.Arithmetic;

public class ArithmeticTest {
	@Rule
	public final TextFromStandardInputStream systemInMock = emptyStandardInputStream();

	private Arithmetic arithemtic = new Arithmetic();

	@Test
	public void sumOf1And2() {
		systemInMock.provideLines("1", "2");
		assertEquals(3, arithemtic.sum());
	}
}


You can download complete working application from this link.

https://github.com/harikrishna553/system-rules

 





 

  

Previous                                                    Next                                                    Home