Browse Source

add one test

stanley-king 7 years ago
parent
commit
6652d14557
1 changed files with 140 additions and 0 deletions
  1. 140 0
      test/TestPhpLang.php

+ 140 - 0
test/TestPhpLang.php

@@ -0,0 +1,140 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2018/1/6
+ * Time: 下午10:55
+ */
+
+class Cart
+{
+    const PRICE_BUTTER  = 1.00;
+    const PRICE_MILK    = 3.00;
+    const PRICE_EGGS    = 6.95;
+
+    protected   $products = array();
+
+    public function add($product, $quantity)
+    {
+        $this->products[$product] = $quantity;
+    }
+
+    public function getQuantity($product)
+    {
+        return isset($this->products[$product]) ? $this->products[$product] : FALSE;
+    }
+
+    public function getTotal($tax)
+    {
+        $total = 0.00;
+
+        $callback = function ($quantity, $product) use ($tax, &$total) {
+            $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product));
+            $total += ($pricePerItem * $quantity) * ($tax + 1.0);
+        };
+
+        array_walk($this->products, $callback);
+
+        return round($total, 2);
+    }
+}
+
+class Test
+{
+    public function testing()
+    {
+        return function() {
+            var_dump($this);
+        };
+    }
+}
+
+class A
+{
+    public static $ClassNameX = 'HelloA';
+
+    function __construct($val) {
+        $this->val = $val;
+    }
+
+    function getClosure() {
+        //returns closure bound to this object and scope
+        return function() { return $this->val; };
+    }
+}
+
+class TestPhpLang extends PHPUnit_Framework_TestCase
+{
+    public function testCart()
+    {
+        $my_cart = new Cart;
+
+        // 往购物车里添加条目
+        $my_cart->add('butter', 1);
+        $my_cart->add('milk', 3);
+        $my_cart->add('eggs', 6);
+
+        // 打出出总价格,其中有 5% 的销售税.
+        print $my_cart->getTotal(0.05) . "\n";
+    }
+
+    public function testClass()
+    {
+        $object = new Test;
+        $function = $object->testing();
+        $function();
+    }
+
+    public function testBlock()
+    {
+        $message = 'hello';
+        $example = function () use($message){
+            var_dump($message);
+        };
+        $example();
+
+        $example = function () use (&$message) {
+            var_dump($message);
+        };
+
+        $this->block(function () {
+            echo "hello world";
+        });
+
+    }
+
+    private function block(callable $func)
+    {
+        $func();
+    }
+
+    public function testBind()
+    {
+        $func = function() {
+            echo __METHOD__;
+        };
+        $func = $func->bindTo(new StdClass);
+        $func();
+    }
+
+    public function testClosure()
+    {
+        $st_cl = function() {
+            return self::$ClassNameX;
+        };
+
+        $mem_cl = function() {
+            return $this->val;
+        };
+
+        $ob1 = new A(1);
+        $ob2 = new A(2);
+        echo A::class;
+
+        $cl2 = Closure::bind($st_cl,NULL,A::class);
+        echo $cl2();
+
+        $cl = $mem_cl->bindTo($ob1);
+        echo $cl();
+    }
+}