Example #1
0
 /**
  * Regiter built-in matchers.
  */
 public static function registerMatchers()
 {
     Matcher::register('toBe', 'Kahlan\\Matcher\\ToBe');
     Matcher::register('toBeA', 'Kahlan\\Matcher\\ToBeA');
     Matcher::register('toBeAn', 'Kahlan\\Matcher\\ToBeA');
     Matcher::register('toBeAnInstanceOf', 'Kahlan\\Matcher\\ToBeAnInstanceOf');
     Matcher::register('toBeCloseTo', 'Kahlan\\Matcher\\ToBeCloseTo');
     Matcher::register('toBeEmpty', 'Kahlan\\Matcher\\ToBeFalsy');
     Matcher::register('toBeFalsy', 'Kahlan\\Matcher\\ToBeFalsy');
     Matcher::register('toBeGreaterThan', 'Kahlan\\Matcher\\ToBeGreaterThan');
     Matcher::register('toBeLessThan', 'Kahlan\\Matcher\\ToBeLessThan');
     Matcher::register('toBeNull', 'Kahlan\\Matcher\\ToBeNull');
     Matcher::register('toBeTruthy', 'Kahlan\\Matcher\\ToBeTruthy');
     Matcher::register('toContain', 'Kahlan\\Matcher\\ToContain');
     Matcher::register('toContainKey', 'Kahlan\\Matcher\\ToContainKey');
     Matcher::register('toContainKeys', 'Kahlan\\Matcher\\ToContainKey');
     Matcher::register('toEcho', 'Kahlan\\Matcher\\ToEcho');
     Matcher::register('toEqual', 'Kahlan\\Matcher\\ToEqual');
     Matcher::register('toHaveLength', 'Kahlan\\Matcher\\ToHaveLength');
     Matcher::register('toMatch', 'Kahlan\\Matcher\\ToMatch');
     Matcher::register('toReceive', 'Kahlan\\Matcher\\ToReceive');
     Matcher::register('toReceiveNext', 'Kahlan\\Matcher\\ToReceiveNext');
     Matcher::register('toThrow', 'Kahlan\\Matcher\\ToThrow');
     Matcher::register('toMatchEcho', 'Kahlan\\Matcher\\ToMatchEcho');
 }
Example #2
0
     Matcher::reset();
     foreach ($this->matchers as $name => $value) {
         foreach ($value as $for => $class) {
             Matcher::register($name, $class, $for);
         }
     }
 });
 describe("->__call()", function () {
     it("throws an exception when using an undefined matcher name", function () {
         $closure = function () {
             $result = Expectation::expect(true)->toHelloWorld(true);
         };
         expect($closure)->toThrow(new Exception("Unexisting matcher attached to `'toHelloWorld'`."));
     });
     it("throws an exception when a specific class matcher doesn't match", function () {
         Matcher::register('toEqualCustom', Stub::classname(['extends' => 'Kahlan\\Matcher\\ToEqual']), 'stdClass');
         $closure = function () {
             $result = Expectation::expect([])->toEqualCustom(new stdClass());
         };
         expect($closure)->toThrow(new Exception("Unexisting matcher attached to `'toEqualCustom'` for `stdClass`."));
     });
     it("doesn't wait when the spec passes", function () {
         $start = microtime(true);
         $result = Expectation::expect(true, 1)->toBe(true);
         $end = microtime(true);
         expect($end - $start)->toBeLessThan(1);
     });
     it("loops until the timeout is reached on failure", function () {
         $start = microtime(true);
         $result = Expectation::expect(true, 0.1)->toBe(false);
         $end = microtime(true);
Example #3
0
<?php

use kahlan\Matcher;
class NoNamespace
{
    public function hello()
    {
        return "Hello World!";
    }
}
function test()
{
    return "It's a test";
}
if (true) {
    echo "Hello World!";
}
Matcher::register('toBe', 'kahlan\\matcher\\ToBe');
Box::share('kahlan.suite', function () {
    return new Suite();
});
?>

Outside PHP Tags

<?php 
for ($i = 0; $i < 10; $i++) {
    echo "Success";
}
Example #4
0
        });
        it("throws an exception when using an undefined matcher name for a specific class", function () {
            $closure = function () {
                Matcher::get('toHelloWorld', 'stdClass');
            };
            expect($closure)->toThrow(new Exception("Unexisting matcher attached to `'toHelloWorld'` for `stdClass`."));
        });
    });
    describe("::unregister()", function () {
        it("unregisters a matcher", function () {
            Matcher::register('toBeOrNotToBe', Double::classname(['extends' => 'Kahlan\\Matcher\\ToBe']));
            expect(Matcher::exists('toBeOrNotToBe'))->toBe(true);
            Matcher::unregister('toBeOrNotToBe');
            expect(Matcher::exists('toBeOrNotToBe'))->toBe(false);
        });
        it("unregisters all matchers", function () {
            expect(Matcher::get())->toBeGreaterThan(1);
            Matcher::unregister(true);
            Matcher::register('toHaveLength', 'Kahlan\\Matcher\\ToHaveLength');
            expect(Matcher::get())->toHaveLength(1);
        });
    });
    describe("::reset()", function () {
        it("unregisters all matchers", function () {
            expect(Matcher::get())->toBeGreaterThan(1);
            Matcher::reset();
            Matcher::register('toHaveLength', 'Kahlan\\Matcher\\ToHaveLength');
            expect(Matcher::get())->toHaveLength(1);
        });
    });
});
Example #5
0
<?php

use Kahlan\Matcher;
class NoNamespace
{
    public function hello()
    {
        return "Hello World!";
    }
}
function test()
{
    return "It's a test";
}
if (true) {
    echo "Hello World!";
}
Matcher::register('toBe', 'Kahlan\\Matcher\\ToBe');
Box::share('kahlan.suite', function () {
    return new Suite();
});
?>

Outside PHP Tags

<?php 
for ($i = 0; $i < 10; $i++) {
    echo "Success";
}
Example #6
0
 /**
  * Returns a compatible matcher class name according to a passed actual value.
  *
  * @param  string $name   The name of the matcher.
  * @param  string $actual The actual value.
  * @return string         A matcher class name.
  */
 public function _matcher($matcherName, $actual)
 {
     if (!Matcher::exists($matcherName, true)) {
         throw new Exception("Unexisting matcher attached to `'{$matcherName}'`.");
     }
     $matcher = null;
     foreach (Matcher::get($matcherName, true) as $target => $value) {
         if (!$target) {
             $matcher = $value;
             continue;
         }
         if ($actual instanceof $target) {
             $matcher = $value;
         }
     }
     if (!$matcher) {
         throw new Exception("Unexisting matcher attached to `'{$matcherName}'` for `{$target}`.");
     }
     return $matcher;
 }
Example #7
0
            expect($arg->match(true))->not->toBe(true);
            expect($arg->match(true))->toBe(false);
        });
        it("registers a matcher for a specific class", function () {
            Matcher::register('toEqualCustom', Stub::classname(['extends' => 'kahlan\\matcher\\ToEqual']), 'stdClass');
            $arg = Arg::toEqualCustom(new stdClass());
            expect($arg->match(new stdClass()))->toBe(true);
            $arg = Arg::toEqualCustom(new DateTime());
            expect($arg->match(new stdClass()))->not->toBe(true);
        });
        it("makes registered matchers for a specific class available for sub classes", function () {
            Matcher::register('toEqualCustom', Stub::classname(['extends' => 'kahlan\\matcher\\ToEqual']), 'SplHeap');
            $arg = Arg::toEqualCustom(new SplMaxHeap());
            expect($arg->match(new SplMaxHeap()))->toBe(true);
        });
        it("throws an exception using an undefined matcher name", function () {
            $closure = function () {
                $arg = Arg::toHelloWorld(true);
            };
            expect($closure)->toThrow(new Exception("Unexisting matchers attached to `'toHelloWorld'`."));
        });
        it("throws an exception using an matcher name which doesn't match actual", function () {
            Matcher::register('toEqualCustom', Stub::classname(['extends' => 'kahlan\\matcher\\ToEqual']), 'SplHeap');
            $closure = function () {
                $arg = Arg::toEqualCustom(new SplMaxHeap());
                $arg->match(true);
            };
            expect($closure)->toThrow(new Exception("Unexisting matcher attached to `'toEqualCustom'` for `SplHeap`."));
        });
    });
});
Example #8
0
        fclose($fp);
    } catch (TimeoutException $e) {
        echo "Unable to run the WebDriver binary, abording.\n";
        $process->close();
        exit(-1);
    }
    return $chain->next();
});
Filter::register('register.globals', function ($chain) {
    $root = $this->suite();
    $root->mink = $mink = box('spec')->get('mink');
    $root->afterEach(function () use($mink) {
        $mink->resetSessions();
    });
    return $chain->next();
});
Filter::register('register.matchers', function ($chain) {
    Matcher::register('toContain', 'Testing\\Spec\\Matcher\\ToContain', 'Behat\\Mink\\Element\\Element');
    return $chain->next();
});
Filter::register('cleanup', function ($chain) {
    $box = box('spec');
    $box->get('mink')->stopSessions();
    $box->get('manager')->close();
    return $chain->next();
});
Filter::apply($this, 'interceptor', 'exclude.namespaces');
Filter::apply($this, 'run', 'run.webdriver');
Filter::apply($this, 'run', 'register.globals');
Filter::apply($this, 'run', 'register.matchers');
Filter::apply($this, 'stop', 'cleanup');
Example #9
0
            throw new \Exception("Not a valid Mink session");
        }
        self::$_description = ['description' => 'to be at the expected url.', 'params' => ['actual' => $actual->getCurrentUrl(), 'expected' => $expected]];
        return $actual->getCurrentUrl() === $expected;
    }
    public static function description()
    {
        return self::$_description;
    }
}
class ToMatchUrl
{
    private static $_description;
    public static function match($actual, $expected)
    {
        if (!$actual instanceof Session) {
            throw new \Exception("Not a valid Mink session");
        }
        self::$_description = ['description' => 'to be at an url matching the expected.', 'params' => ['actual' => $actual->getCurrentUrl(), 'expected' => $expected]];
        return preg_match($expected, $actual->getCurrentUrl()) === 1;
    }
    public static function description()
    {
        return self::$_description;
    }
}
Matcher::register('toHaveElement', 'ToHaveElement');
Matcher::register('toContainText', 'ToContainText');
Matcher::register('toBeAtUrl', 'ToBeAtUrl');
Matcher::register('toMatchUrl', 'ToMatchUrl');
Example #10
0
            expect($arg->match(true))->not->toBe(true);
            expect($arg->match(true))->toBe(false);
        });
        it("registers a matcher for a specific class", function () {
            Matcher::register('toEqualCustom', Double::classname(['extends' => 'Kahlan\\Matcher\\ToEqual']), 'stdClass');
            $arg = Arg::toEqualCustom(new stdClass());
            expect($arg->match(new stdClass()))->toBe(true);
            $arg = Arg::toEqualCustom(new DateTime());
            expect($arg->match(new stdClass()))->not->toBe(true);
        });
        it("makes registered matchers for a specific class available for sub classes", function () {
            Matcher::register('toEqualCustom', Double::classname(['extends' => 'Kahlan\\Matcher\\ToEqual']), 'SplHeap');
            $arg = Arg::toEqualCustom(new SplMaxHeap());
            expect($arg->match(new SplMaxHeap()))->toBe(true);
        });
        it("throws an exception using an undefined matcher name", function () {
            $closure = function () {
                $arg = Arg::toHelloWorld(true);
            };
            expect($closure)->toThrow(new Exception("Unexisting matchers attached to `'toHelloWorld'`."));
        });
        it("throws an exception using an matcher name which doesn't match actual", function () {
            Matcher::register('toEqualCustom', Double::classname(['extends' => 'Kahlan\\Matcher\\ToEqual']), 'SplHeap');
            $closure = function () {
                $arg = Arg::toEqualCustom(new SplMaxHeap());
                $arg->match(true);
            };
            expect($closure)->toThrow(new Exception("Unexisting matcher attached to `'toEqualCustom'` for `SplHeap`."));
        });
    });
});