Skip to content

Commit

Permalink
Refactoring risky tests (#1226)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
  • Loading branch information
ghostwriter committed Apr 14, 2023
2 parents 9ad050f + a097d47 commit bd8f225
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 79 deletions.
32 changes: 15 additions & 17 deletions tests/Mockery/AllowsExpectsSyntaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace test\Mockery;

use Mockery as m;
use Mockery;
use Mockery\Spy;
use Mockery\Exception\InvalidCountException;
use PHPUnit\Framework\TestCase;
Expand All @@ -43,12 +43,12 @@ public function expects()
}


class AllowsExpectsSyntaxTest extends TestCase
class AllowsExpectsSyntaxTest extends Mockery\Adapter\Phpunit\MockeryTestCase
{
/** @test */
public function allowsSetsUpMethodStub()
{
$stub = m::mock();
$stub = Mockery::mock();
$stub->allows()->foo(123)->andReturns(456);

$this->assertEquals(456, $stub->foo(123));
Expand All @@ -57,7 +57,7 @@ public function allowsSetsUpMethodStub()
/** @test */
public function allowsCanTakeAnArrayOfCalls()
{
$stub = m::mock();
$stub = Mockery::mock();
$stub->allows([
"foo" => "bar",
"bar" => "baz",
Expand All @@ -70,15 +70,15 @@ public function allowsCanTakeAnArrayOfCalls()
/** @test */
public function allowsCanTakeAString()
{
$stub = m::mock();
$stub = Mockery::mock();
$stub->allows("foo")->andReturns("bar");
$this->assertEquals("bar", $stub->foo());
}

/** @test */
public function expects_can_optionally_match_on_any_arguments()
{
$mock = m::mock();
$mock = Mockery::mock();
$mock->allows()->foo()->withAnyArgs()->andReturns(123);

$this->assertEquals(123, $mock->foo(456, 789));
Expand All @@ -87,7 +87,7 @@ public function expects_can_optionally_match_on_any_arguments()
/** @test */
public function expects_can_take_a_string()
{
$mock = m::mock();
$mock = Mockery::mock();
$mock->expects("foo")->andReturns(123);

$this->assertEquals(123, $mock->foo(456, 789));
Expand All @@ -96,40 +96,38 @@ public function expects_can_take_a_string()
/** @test */
public function expectsSetsUpExpectationOfOneCall()
{
$mock = m::mock();
$mock = Mockery::mock();
$mock->expects()->foo(123);

$this->expectException("Mockery\Exception\InvalidCountException");
m::close();
Mockery::close();
}

/** @test */
public function callVerificationCountCanBeOverridenAfterExpectsThrowsExceptionWhenIncorrectNumberOfCalls()
{
$mock = m::mock();
$mock = Mockery::mock();
$mock->expects()->foo(123)->twice();

$mock->foo(123);
$this->expectException("Mockery\Exception\InvalidCountException");
m::close();
$this->expectException(\Mockery\Exception\InvalidCountException::class);
Mockery::close();
}

/** @test */
public function callVerificationCountCanBeOverridenAfterExpects()
{
$mock = m::mock();
$mock = Mockery::mock();
$mock->expects()->foo(123)->twice();

$mock->foo(123);
$mock->foo(123);

m::close();
}

/** @test */
public function generateSkipsAllowsMethodIfAlreadyExists()
{
$stub = m::mock("test\Mockery\ClassWithAllowsMethod");
$stub = Mockery::mock("test\Mockery\ClassWithAllowsMethod");

$stub->shouldReceive('allows')->andReturn(123);

Expand All @@ -139,7 +137,7 @@ public function generateSkipsAllowsMethodIfAlreadyExists()
/** @test */
public function generateSkipsExpectsMethodIfAlreadyExists()
{
$stub = m::mock("test\Mockery\ClassWithExpectsMethod");
$stub = Mockery::mock("test\Mockery\ClassWithExpectsMethod");

$stub->shouldReceive('expects')->andReturn(123);

Expand Down
9 changes: 6 additions & 3 deletions tests/Mockery/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ public function testCanUseBlacklistAndExpectionOnNonBlacklistedMethod()
public function testCanUseEmptyMethodlist()
{
$m = mock('MockeryTest_PartialNormalClass2[]');
$this->assertInstanceOf(MockeryTest_PartialNormalClass2::class, $m);
}

/**
Expand Down Expand Up @@ -512,7 +513,7 @@ public function testCanMockClassContainingAPublicWakeupMethod()
public function testCanMockClassUsingMagicCallMethodsInPlaceOfNormalMethods()
{
$m = Mockery::mock('Gateway');
$m->shouldReceive('iDoSomethingReallyCoolHere');
$m->shouldReceive('iDoSomethingReallyCoolHere')->once();
$m->iDoSomethingReallyCoolHere();
}

Expand All @@ -522,7 +523,7 @@ public function testCanMockClassUsingMagicCallMethodsInPlaceOfNormalMethods()
public function testCanPartialMockObjectUsingMagicCallMethodsInPlaceOfNormalMethods()
{
$m = Mockery::mock(new Gateway());
$m->shouldReceive('iDoSomethingReallyCoolHere');
$m->shouldReceive('iDoSomethingReallyCoolHere')->once();
$m->iDoSomethingReallyCoolHere();
}

Expand Down Expand Up @@ -614,12 +615,14 @@ public function testMockedStaticMethodsObeyMethodCounting()
public function testMockedStaticThrowsExceptionWhenMethodDoesNotExist()
{
$m = mock('alias:MyNamespace\StaticNoMethod');

try {
MyNameSpace\StaticNoMethod::staticFoo();
} catch (BadMethodCallException $e) {
// Mockery + PHPUnit has a fail safe for tests swallowing our
// exceptions
$e->dismiss();
self::assertTrue($e->dismissed());
return;
}

Expand Down Expand Up @@ -780,7 +783,7 @@ public function testInstantiationOfInstanceMockWithConstructorParameterValidatio
$params = [
'value1' => uniqid('test_')
];
$m->shouldReceive('__construct')->with($params);
$m->shouldReceive('__construct')->with($params)->once();

new MyNamespace\MyClass14($params);
}
Expand Down
56 changes: 28 additions & 28 deletions tests/Mockery/ExpectationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public function testMultipleExpectationsWithReturns()

public function testExpectsNoArguments()
{
$this->mock->shouldReceive('foo')->withNoArgs();
$this->mock->shouldReceive('foo')->withNoArgs()->once();
$this->mock->foo();
}

Expand All @@ -404,7 +404,7 @@ public function testExpectsNoArgumentsThrowsExceptionIfAnyPassed()

public function testExpectsArgumentsArray()
{
$this->mock->shouldReceive('foo')->withArgs(array(1, 2));
$this->mock->shouldReceive('foo')->withArgs([1, 2])->once();
$this->mock->foo(1, 2);
}

Expand Down Expand Up @@ -454,7 +454,7 @@ public function testExpectsArgumentsArrayAcceptAClosureThatValidatesPassedArgume
$closure = function ($odd, $even) {
return ($odd % 2 != 0) && ($even % 2 == 0);
};
$this->mock->shouldReceive('foo')->withArgs($closure);
$this->mock->shouldReceive('foo')->withArgs($closure)->once();
$this->mock->foo(1, 2);
}

Expand All @@ -478,7 +478,7 @@ public function testExpectsArgumentsArrayClosureDoesNotThrowExceptionIfOptionalA
}
return $result;
};
$this->mock->shouldReceive('foo')->withArgs($closure);
$this->mock->shouldReceive('foo')->withArgs($closure)->once();
$this->mock->foo(1, 4);
}

Expand All @@ -491,7 +491,7 @@ public function testExpectsArgumentsArrayClosureDoesNotThrowExceptionIfOptionalA
}
return $result;
};
$this->mock->shouldReceive('foo')->withArgs($closure);
$this->mock->shouldReceive('foo')->withArgs($closure)->once();
$this->mock->foo(1, 4, 5);
}

Expand Down Expand Up @@ -531,15 +531,15 @@ public function testExpectsSomeOfArgumentsGivenArgsDoNotMatchRealArgsAndThrowNoM

public function testExpectsAnyArguments()
{
$this->mock->shouldReceive('foo')->withAnyArgs();
$this->mock->shouldReceive('foo')->withAnyArgs()->times(3);
$this->mock->foo();
$this->mock->foo(1);
$this->mock->foo(1, 'k', new stdClass());
}

public function testExpectsArgumentMatchingObjectType()
{
$this->mock->shouldReceive('foo')->with('\stdClass');
$this->mock->shouldReceive('foo')->with('\stdClass')->once();
$this->mock->foo(new stdClass());
}

Expand Down Expand Up @@ -816,8 +816,8 @@ public function testCallCountingThrowsExceptionFirst()

public function testOrderedCallsWithoutError()
{
$this->mock->shouldReceive('foo')->ordered();
$this->mock->shouldReceive('bar')->ordered();
$this->mock->shouldReceive('foo')->ordered()->once();
$this->mock->shouldReceive('bar')->ordered()->once();
$this->mock->foo();
$this->mock->bar();
}
Expand All @@ -834,8 +834,8 @@ public function testOrderedCallsWithOutOfOrderError()

public function testDifferentArgumentsAndOrderingsPassWithoutException()
{
$this->mock->shouldReceive('foo')->with(1)->ordered();
$this->mock->shouldReceive('foo')->with(2)->ordered();
$this->mock->shouldReceive('foo')->with(1)->ordered()->once();
$this->mock->shouldReceive('foo')->with(2)->ordered()->once();
$this->mock->foo(1);
$this->mock->foo(2);
}
Expand All @@ -852,9 +852,9 @@ public function testDifferentArgumentsAndOrderingsThrowExceptionWhenInWrongOrder

public function testUnorderedCallsIgnoredForOrdering()
{
$this->mock->shouldReceive('foo')->with(1)->ordered();
$this->mock->shouldReceive('foo')->with(2);
$this->mock->shouldReceive('foo')->with(3)->ordered();
$this->mock->shouldReceive('foo')->with(1)->ordered()->once();
$this->mock->shouldReceive('foo')->with(2)->times(3);
$this->mock->shouldReceive('foo')->with(3)->ordered()->once();
$this->mock->foo(2);
$this->mock->foo(1);
$this->mock->foo(2);
Expand All @@ -864,8 +864,8 @@ public function testUnorderedCallsIgnoredForOrdering()

public function testOrderingOfDefaultGrouping()
{
$this->mock->shouldReceive('foo')->ordered();
$this->mock->shouldReceive('bar')->ordered();
$this->mock->shouldReceive('foo')->ordered()->once();
$this->mock->shouldReceive('bar')->ordered()->once();
$this->mock->foo();
$this->mock->bar();
}
Expand All @@ -882,10 +882,10 @@ public function testOrderingOfDefaultGroupingThrowsExceptionOnWrongOrder()

public function testOrderingUsingNumberedGroups()
{
$this->mock->shouldReceive('start')->ordered(1);
$this->mock->shouldReceive('foo')->ordered(2);
$this->mock->shouldReceive('bar')->ordered(2);
$this->mock->shouldReceive('final')->ordered();
$this->mock->shouldReceive('start')->ordered(1)->once();
$this->mock->shouldReceive('foo')->ordered(2)->once();
$this->mock->shouldReceive('bar')->ordered(2)->twice();
$this->mock->shouldReceive('final')->ordered()->once();
$this->mock->start();
$this->mock->bar();
$this->mock->foo();
Expand All @@ -895,10 +895,10 @@ public function testOrderingUsingNumberedGroups()

public function testOrderingUsingNamedGroups()
{
$this->mock->shouldReceive('start')->ordered('start');
$this->mock->shouldReceive('foo')->ordered('foobar');
$this->mock->shouldReceive('bar')->ordered('foobar');
$this->mock->shouldReceive('final')->ordered();
$this->mock->shouldReceive('start')->ordered('start')->once();
$this->mock->shouldReceive('foo')->ordered('foobar')->once();
$this->mock->shouldReceive('bar')->ordered('foobar')->twice();
$this->mock->shouldReceive('final')->ordered()->once();
$this->mock->start();
$this->mock->bar();
$this->mock->foo();
Expand Down Expand Up @@ -950,9 +950,9 @@ public function testExpectationMatchingWithAnyArgsOrderings()

public function testEnsuresOrderingIsNotCrossMockByDefault()
{
$this->mock->shouldReceive('foo')->ordered();
$this->mock->shouldReceive('foo')->ordered()->once();
$mock2 = mock('bar');
$mock2->shouldReceive('bar')->ordered();
$mock2->shouldReceive('bar')->ordered()->once();
$mock2->bar();
$this->mock->foo();
}
Expand Down Expand Up @@ -1043,8 +1043,8 @@ public function testDefaultExpectationsCanBeOrderedAndReplaced()
{
$this->mock->shouldReceive('foo')->ordered()->byDefault();
$this->mock->shouldReceive('bar')->ordered()->byDefault();
$this->mock->shouldReceive('bar')->ordered();
$this->mock->shouldReceive('foo')->ordered();
$this->mock->shouldReceive('bar')->ordered()->once();
$this->mock->shouldReceive('foo')->ordered()->once();
$this->mock->bar();
$this->mock->foo();
}
Expand Down
31 changes: 19 additions & 12 deletions tests/Mockery/MockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ class Mockery_MockTest extends MockeryTestCase
public function testAnonymousMockWorksWithNotAllowingMockingOfNonExistentMethods()
{
\Mockery::getConfiguration()->allowMockingNonExistentMethods(false);

$m = mock();
$m->shouldReceive("test123")->andReturn(true);
assertThat($m->test123(), equalTo(true));
self::assertTrue($m->test123());

\Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
}

Expand All @@ -38,8 +40,8 @@ public function testMockWithNotAllowingMockingOfNonExistentMethodsCanBeGivenAddi
\Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
$m = mock('ExampleClassForTestingNonExistentMethod');
$m->shouldAllowMockingMethod('testSomeNonExistentMethod');
$m->shouldReceive("testSomeNonExistentMethod")->andReturn(true);
assertThat($m->testSomeNonExistentMethod(), equalTo(true));
$m->shouldReceive("testSomeNonExistentMethod")->andReturn(true)->once();
self::assertTrue($m->testSomeNonExistentMethod());
\Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
}

Expand All @@ -49,7 +51,7 @@ public function testProtectedMethodMockWithNotAllowingMockingOfNonExistentMethod
$m = mock('ClassWithProtectedMethod');
$m->shouldAllowMockingProtectedMethods();
$m->shouldReceive('foo')->andReturn(true);
assertThat($m->foo(), equalTo(true));
self::assertTrue($m->foo());
\Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
}

Expand Down Expand Up @@ -112,24 +114,29 @@ public function testShouldIgnoreMissingCallingExistentMethods()
{
Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
$mock = mock('ClassWithMethods')->shouldIgnoreMissing();
assertThat(nullValue($mock->foo()));

self::assertNull($mock->foo());

$mock->shouldReceive('bar')->passthru();
assertThat($mock->bar(), equalTo('bar'));

self::assertSame('bar', $mock->bar());
}

public function testShouldIgnoreMissingCallingNonExistentMethods()
{
Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
$mock = mock('ClassWithMethods')->shouldIgnoreMissing();
assertThat(nullValue($mock->foo()));
assertThat(nullValue($mock->bar()));
assertThat(nullValue($mock->nonExistentMethod()));

self::assertNull($mock->foo());
self::assertNull($mock->bar());
self::assertNull($mock->nonExistentMethod());

$mock->shouldReceive(array('foo' => 'new_foo', 'nonExistentMethod' => 'result'));
$mock->shouldReceive('bar')->passthru();
assertThat($mock->foo(), equalTo('new_foo'));
assertThat($mock->bar(), equalTo('bar'));
assertThat($mock->nonExistentMethod(), equalTo('result'));

self::assertSame('new_foo', $mock->foo());
self::assertSame('bar', $mock->bar());
self::assertSame('result', $mock->nonExistentMethod());
}

public function testCanMockException()
Expand Down

0 comments on commit bd8f225

Please sign in to comment.