phpunit episode iv.iii: return of the tests

40
PHPunit Episode IV.III Return of the tests

Upload: michelangelo-van-dam

Post on 15-Jul-2015

112 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: PHPUnit Episode iv.iii: Return of the tests

PHPunitEpisode IV.III!

Return of the tests

Page 2: PHPUnit Episode iv.iii: Return of the tests

A long time ago, in a code base not so far away…

Page 3: PHPUnit Episode iv.iii: Return of the tests

For years people have been advocating the usage of unit tests and slowly developers around the world have adopted the skills to write good tests. But since the crisis businesses have been battling for customers and profits, leaving less room for proper QA and testing. In this race for money, things are looking dim and unfavourable for businesses that have cut corners and went straight for the gold.

Page 4: PHPUnit Episode iv.iii: Return of the tests

Fortunately it’s not to late for development teams to turn the tide and reclaim their honour and ensure businesses aren’t disposing money out of the window because of easily preventable failures. The gloves are on and developers take action to test what’s most important: the core of the applications!

Page 5: PHPUnit Episode iv.iii: Return of the tests
Page 6: PHPUnit Episode iv.iii: Return of the tests
Page 7: PHPUnit Episode iv.iii: Return of the tests
Page 8: PHPUnit Episode iv.iii: Return of the tests

Auth ACL

Products

CustomerDetails

Front-End

Back-End

BusinessLogic

ExternalServices

RDBMS

Files

NoSQL

Streams

Page 9: PHPUnit Episode iv.iii: Return of the tests

Auth ACL

BusinessLogic

ExternalServices

Page 10: PHPUnit Episode iv.iii: Return of the tests

Auth ACL

BusinessLogic

ExternalServices

Mocked!External!Services

Page 11: PHPUnit Episode iv.iii: Return of the tests

http

s://w

ww.

flick

r.com

/pho

tos/

nood

lepi

e/38

8651

9163

Page 12: PHPUnit Episode iv.iii: Return of the tests

http

s://w

ww.

flick

r.com

/pho

tos/

csee

man

/111

0192

0756

Page 13: PHPUnit Episode iv.iii: Return of the tests

http

s://w

ww.

flick

r.com

/pho

tos/

st3f

4n/3

7529

9477

8

Your class contains 10K lines!I sense lots of anger in you

Page 14: PHPUnit Episode iv.iii: Return of the tests

http

s://w

ww.

flick

r.com

/pho

tos/

clem

ent1

27/1

6162

2272

60

Page 15: PHPUnit Episode iv.iii: Return of the tests

• createOrder

• updateOrder

• payOrder

• cancelOrder

• findOrderById

• findOrderByCustomerId

• findOrderByPaymentId

Page 16: PHPUnit Episode iv.iii: Return of the tests

• createOrder

• updateOrder

• payOrder

• findOrderByPaymentId

• findOrderByCustomerId

• findOrderById

• cancelOrder

} High importance

} Normal importance

} Low importance

Page 17: PHPUnit Episode iv.iii: Return of the tests

    /** "     * Creates an order based on provided data "     * "     * @param array $data Associative array with order information "     * @return bool|int Will return the order ID if storage was successful "     * or false when storage failed "     */ "    public function createOrder($data) "    { "        $db = Registry::getInstance()->get('db'); "        $sql = sprintf( "            'INSERT INTO `order` (`productId`, `customerId`, "            `productPrice`, `quantity`) VALUES (%d, %d, %f, %f)', "            $data['productId'], $data['customerId'], "            $data['productPrice'], $data['quantity'] "        ); "        $id = false; "        try { "            $id = $db->exec($sql); "        } catch (Exception $e) { "            Registry::get('logger')->log($e->getMessage(), CRIT); "            Registry::get('logger')->log($e->getTraceAsString(), INFO); "        } "        return $id; "    }

Page 18: PHPUnit Episode iv.iii: Return of the tests

http

s://w

ww.

flick

r.com

/pho

tos/

sim

onon

ly/1

6445

2784

75

Page 19: PHPUnit Episode iv.iii: Return of the tests

/** " * Creates an order based on provided data " * " * @param array $data Associative array with order information " * @return bool|int Will return the order ID if storage was successful  " * or false when storage failed " */ "public function createOrder($data) "{ "    $db = Registry::get('db'); "    $sql = sprintf( "        'INSERT INTO `order` (`productId`, `customerId`,  "        `productPrice`, `quantity`) VALUES (%d, %d, %f, %f)', "        $data['productId'], $data['customerId'],  "        $data['productPrice'], $data['quantity'] "    ); "    $id = false; "    try { "        $id = $db->query($sql); "    } catch (Exception $e) { "        Registry::get('logger')->log($e->getMessage(), CRIT); "        Registry::get('logger')->log($e->getTraceAsString(), INFO); "    } "    return $id; "}

Page 20: PHPUnit Episode iv.iii: Return of the tests

/** " * Creates an order based on provided data " * " * @param array $data Associative array with order information " * @return bool|int Will return the order ID if storage was successful  " * or false when storage failed " */ "public function createOrder($data) "{ "    $db = Registry::get('db'); "    $sql = sprintf( "        'INSERT INTO `order` (`productId`, `customerId`,  "        `productPrice`, `quantity`) VALUES (%d, %d, %f, %f)', "        $data['productId'], $data['customerId'],  "        $data['productPrice'], $data['quantity'] "    ); "    $id = false; "    try { "        $id = $db->query($sql); "    } catch (Exception $e) { "        Registry::get('logger')->log($e->getMessage(), CRIT); "        Registry::get('logger')->log($e->getTraceAsString(), INFO); "    } "    return $id; "}

Page 21: PHPUnit Episode iv.iii: Return of the tests

/** " * Creates an order based on provided data " * " * @param array $data Associative array with order information " * @return bool|int Will return the order ID if storage was successful  " * or false when storage failed " */ "public function createOrder($data) "{ "    $db = Registry::get('db'); "    $sql = sprintf( "        'INSERT INTO `order` (`productId`, `customerId`,  "        `productPrice`, `quantity`) VALUES (%d, %d, %f, %f)', "        $data['productId'], $data['customerId'],  "        $data['productPrice'], $data['quantity'] "    ); "    $id = false; "    try { "        $id = $db->query($sql); "    } catch (Exception $e) { "        Registry::get('logger')->log($e->getMessage(), CRIT); "        Registry::get('logger')->log($e->getTraceAsString(), INFO); "    } "    return $id; "}

Page 22: PHPUnit Episode iv.iii: Return of the tests

What do we know?

Page 23: PHPUnit Episode iv.iii: Return of the tests

What do we know?

• We provide an array with values in

Page 24: PHPUnit Episode iv.iii: Return of the tests

What do we know?

• We provide an array with values in

• We get inserted ID or false back

Page 25: PHPUnit Episode iv.iii: Return of the tests

What do we know?

• We provide an array with values in

• We get inserted ID or false back

• The code is crappy

Page 26: PHPUnit Episode iv.iii: Return of the tests

class OrderTest extends \PHPUnit_Framework_TestCase "{ "    public function testCreateOrder() "    { "        $data = array ( "            'productId' => 1, "            'customerId' => 1, "            'productPrice' => 4.95, "            'quantity' => 2, "        ); "        $order = new Order(); "        $result = $order->createOrder($data); "        $this->assertGreaterThanOrEqual(1, $result); "    } "}

Page 27: PHPUnit Episode iv.iii: Return of the tests
Page 28: PHPUnit Episode iv.iii: Return of the tests

    public function testCreateOrder() "    { "        $db = new \PDO('sqlite::memory:'); "        $db->exec('CREATE TABLE `order` ( "          `orderId` INTEGER PRIMARY KEY NOT NULL, "          `productId` INTEGER NOT NULL, "          `customerId` INTEGER NOT NULL, "          `productPrice` REAL NOT NULL DEFAULT 0.0, "          `quantity` REAL NOT NULL DEFAULT 1.0);'); "        Registry::getInstance()->register('db', $db); "        $data = array ( "            'productId' => 1, "            'customerId' => 1, "            'productPrice' => 4.95, "            'quantity' => 2, "        ); "        $order = new Order(); "        $result = $order->createOrder($data); "        $this->assertGreaterThanOrEqual(1, $result); "    }

Page 29: PHPUnit Episode iv.iii: Return of the tests
Page 30: PHPUnit Episode iv.iii: Return of the tests

http

s://w

ww.

flick

r.com

/pho

tos/

jurv

etso

n/83

1769

15

Page 31: PHPUnit Episode iv.iii: Return of the tests

    public function testCreateOrder() "    { "        $db = $this->getMock('\\PDO',  "            array ('exec'), array ('sqlite::memory:')); "        $db->expects($this->once()) "            ->method('exec') "            ->will($this->returnValue(2)); "!        Registry::getInstance()->register('db', $db); "        $data = array ( "            'productId' => 1, "            'customerId' => 1, "            'productPrice' => 4.95, "            'quantity' => 2, "        ); "        $order = new Order(); "        $result = $order->createOrder($data); "        $this->assertGreaterThanOrEqual(1, $result); "    }

Page 32: PHPUnit Episode iv.iii: Return of the tests
Page 33: PHPUnit Episode iv.iii: Return of the tests

http

s://w

ww.

flick

r.com

/pho

tos/

lego

fenr

is/4

5784

5356

9

Page 34: PHPUnit Episode iv.iii: Return of the tests

    /** "     * @var \PDO "     */ "    protected $db; "!    /** "     * @return \PDO "     */ "    public function getDb() "    { "        if (null === $this->db) { "            // fallback to old functions using this method "            $this->setDb(Registry::getInstance()->get('db')); "        } "        return $this->db; "    } "!    /** "     * @param \PDO $db "     */ "    public function setDb($db) "    { "        $this->db = $db; "    }

Page 35: PHPUnit Episode iv.iii: Return of the tests

    /** "     * Creates an order based on provided data "     * "     * @param array $data Associative array with order information "     * @return bool|int Will return the order ID if storage was successful "     * or false when storage failed "     */ "    public function createOrder($data) "    { "        $db = $this->getDb(); "        $sql = sprintf( "            'INSERT INTO `order` (`productId`, `customerId`, "            `productPrice`, `quantity`) VALUES (%d, %d, %f, %f)', "            $data['productId'], $data['customerId'], "            $data['productPrice'], $data['quantity'] "        ); "        $id = false; "        try { "            $id = $db->exec($sql); "        } catch (Exception $e) { "            Registry::get('logger')->log($e->getMessage(), CRIT); "            Registry::get('logger')->log($e->getTraceAsString(), INFO); "        } "        return $id; "    }

Page 36: PHPUnit Episode iv.iii: Return of the tests
Page 37: PHPUnit Episode iv.iii: Return of the tests

http

s://w

ww.

flick

r.com

/pho

tos/

pasu

karu

76/5

1524

9797

3

Page 38: PHPUnit Episode iv.iii: Return of the tests

Michelangelo van Dam

dragonbe@gmail

T DragonBe

F DragonBe

www.dragonbe.com

Intergalactic PHP Ninja Consultant

http

s://w

ww.

flick

r.com

/pho

tos/

1301

6024

6@N

02/1

6465

3675

56

Page 39: PHPUnit Episode iv.iii: Return of the tests

joind.in/event/view/3701

Rate my talk If you liked it, thanks.

If you don’t, tell me how to improve it

Page 40: PHPUnit Episode iv.iii: Return of the tests

http

s://w

ww.

flick

r.com

/pho

tos/

toom

uchd

ew/1

4508

5647

45