<?php

/*
 * This file is part of the Lime framework.
 *
 * (c) Fabien Potencier <*****@*****.**>
 * (c) Bernhard Schussek <*****@*****.**>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
require_once dirname(__FILE__) . '/setup.php';
LimeAnnotationSupport::enable();
$t = new LimeTest();
// @Test
$t->expect('RuntimeException');
echo "Test 1\n";
throw new RuntimeException();
// @Test
$t->expect('LogicException');
echo "Test 2\n";
// should not result in "Got: RuntimeException"!
Beispiel #2
0
 * This file is part of the Lime framework.
 *
 * (c) Fabien Potencier <*****@*****.**>
 * (c) Bernhard Schussek <*****@*****.**>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
LimeAnnotationSupport::enable();
$t = new LimeTest();
// @Test: is() throws an exception if keys are missing
// fixtures
$actual = new LimeTesterArray(array());
$expected = new LimeTesterArray(array(0 => 1));
// test
$t->expect('LimeAssertionFailedException');
$actual->is($expected);
// @Test: is() throws an exception if keys are unexpected
// fixtures
$actual = new LimeTesterArray(array(0 => 1));
$expected = new LimeTesterArray(array());
// test
$t->expect('LimeAssertionFailedException');
$actual->is($expected);
// @Test: is() throws an exception if values don't match
// fixtures
$actual = new LimeTesterArray(array(0 => 1));
$expected = new LimeTesterArray(array(0 => 2));
// test
$t->expect('LimeAssertionFailedException');
$actual->is($expected);
Beispiel #3
0
$t->is($value, 'Foobar', 'The correct value has been returned');
// @Test: Return values can be stubbed based on method parameters
// test
$m->testMethod()->returns('Foobar');
$m->testMethod(1)->returns('More foobar');
$m->replay();
$value1 = $m->testMethod();
$value2 = $m->testMethod(1);
// assertions
$t->is($value1, 'Foobar', 'The correct value has been returned');
$t->is($value2, 'More foobar', 'The correct value has been returned');
// @Test: Exceptions can be stubbed
// fixtures
$m->testMethod()->throws('TestException');
$m->replay();
$t->expect('TestException');
// test
$m->testMethod();
// @Test: Exceptions can be stubbed using objects
// fixtures
$m->testMethod()->throws(new TestException());
$m->replay();
$t->expect('TestException');
// TODO: It would be good if we could test for the exact object
// test
$m->testMethod();
// @Test: ->verify() fails if a method was not called
// test
$m->testMethod();
$m->replay();
// assertions
Beispiel #4
0
$resource = tmpfile();
$expected = new LimeTesterResource($resource);
$t->ok(LimeTester::create($resource) == $expected, 'The correct object was created');
fclose($resource);
// @Test: register() registers a new tester for a given class
LimeTester::register('TestClass', 'TestClassTester');
$object = new TestClass();
$expected = new TestClassTester($object);
$t->ok(LimeTester::create($object) == $expected, 'The correct object was created');
// @Test: register() registers a new tester for a given interface
LimeTester::register('TestInterface', 'TestInterfaceTester');
$object = new TestInterfaceClass();
$expected = new TestInterfaceTester($object);
$t->ok(LimeTester::create($object) == $expected, 'The correct object was created');
// @Test: unregister() removes any custom testers for a given class
LimeTester::register('TestClass', 'TestClassTester');
LimeTester::unregister('TestClass');
$object = new TestClass();
$expected = new LimeTesterObject($object);
$t->ok(LimeTester::create($object) == $expected, 'The correct object was created');
// @Test: create() creates a tester also for subclasses of a registered tester
LimeTester::register('TestClass', 'TestClassTester');
$object = new TestSubClass();
$expected = new TestClassTester($object);
$t->ok(LimeTester::create($object) == $expected, 'The correct object was created');
// @Test: register() throws an exception if the tester class does not exist
$t->expect('InvalidArgumentException');
LimeTester::register('TestClass', 'FoobarTester');
// @Test: register() throws an exception if the tester does not implement LimeTesterInterface
$t->expect('InvalidArgumentException');
LimeTester::register('TestClass', 'InvalidTester');
Beispiel #5
0
// test
$r->run();
$t->diag('The exception handlers are called when a test throws an exception');
// fixtures
$mock = $t->mock('Mock', array('strict' => true));
$r = new LimeTestRunner();
$r->addTest(array($mock, 'testThrowsException'), '', '', 0);
$r->addExceptionHandler(array($mock, 'handleExceptionFailed'));
$r->addExceptionHandler(array($mock, 'handleExceptionSuccessful'));
$mock->testThrowsException()->throws('Exception');
$mock->method('handleExceptionFailed')->returns(false);
$mock->method('handleExceptionSuccessful')->returns(true);
$mock->replay();
// test
$r->run();
$t->diag('If no exception handler returns true, the exception is thrown again');
// fixtures
$mock = $t->mock('Mock', array('strict' => true));
$r = new LimeTestRunner();
$r->addTest(array($mock, 'testThrowsException'), '', '', 0);
$r->addExceptionHandler(array($mock, 'handleExceptionFailed'));
$mock->testThrowsException()->throws('Exception');
$mock->method('handleExceptionFailed')->returns(false);
$mock->replay();
// test
$t->expect('Exception');
try {
    $r->run();
    $t->fail('The exception was thrown');
} catch (Exception $e) {
}
Beispiel #6
0
<?php

/*
 * This file is part of the Lime framework.
 *
 * (c) Fabien Potencier <*****@*****.**>
 * (c) Bernhard Schussek <*****@*****.**>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
require_once dirname(__FILE__) . '/setup.php';
LimeAnnotationSupport::enable();
$t = new LimeTest();
// @Test
$t->expect(new RuntimeException("Foobar"));
echo "Test 1\n";
throw new RuntimeException("Foobar");
// @Test
$t->expect(new RuntimeException("Foobar", 1));
echo "Test 2\n";
throw new RuntimeException("Foobar", 0);
// @Test
$t->expect(new RuntimeException("Foobar", 1));
echo "Test 3\n";
throw new RuntimeException("Foobar", 1);
Beispiel #7
0
// @Test: ->verify() passes if a method is expected both with any and with concrete parameters
// test
$m->method('testMethod')->once();
$m->testMethod(1, 'foobar');
$m->replay();
$m->testMethod('ramble on');
$m->testMethod(1, 'foobar');
$m->verify();
// assertions
$t->is(count($m->__lime_getInvocationTrace()), 2, 'Two tests passed');
// @Test: An exception is thrown if methods are called in the wrong order
// fixtures
$m->method1();
$m->method2();
$m->replay();
$t->expect('LimeMockException');
// test
$m->method2();
// @Test: An exception is thrown if too many methods are called
// test
$m->method1();
$m->replay();
$m->method1();
$t->expect('LimeMockException');
$m->method2();
// @Test: If the option "nice" is set, ->verify() fails if methods were called in the wrong order
// test
$m = LimeMock::create('TestClass', array('strict' => true, 'nice' => true));
$m->method1();
$m->method2();
$m->method3();