Skip to content

Commit 6970a42

Browse files
committed
Fix UTs
1 parent f6961ec commit 6970a42

13 files changed

+75
-43
lines changed

test/Behavioral/Command/CommandInvokerTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hyunk3l\Test\PhpDesignPatterns\Behavioral\Command;
44

5+
use Hyunk3l\PhpDesignPatterns\Behavioral\Command\CommandInterface;
56
use Hyunk3l\PhpDesignPatterns\Behavioral\Command\CommandInvoker;
67
use PHPUnit\Framework\TestCase;
78

@@ -16,12 +17,14 @@ class CommandInvokerTest extends TestCase
1617
*/
1718
public function testCommand()
1819
{
19-
$command = $this->getMock('PhpDesignPatterns\\Behavioral\\Command\\CommandInterface', array('execute'));
20+
$command = $this->getMockBuilder(CommandInterface::class)
21+
->disableOriginalConstructor()
22+
->setMethods(['execute'])
23+
->getMock();
2024
$command
2125
->expects($this->once())
2226
->method('execute')
23-
->will($this->returnValue($expected_output = 'Dummy command!'))
24-
;
27+
->will($this->returnValue($expected_output = 'Dummy command!'));
2528
$invoker = new CommandInvoker;
2629
$invoker->setCommand($command);
2730
$this->assertEquals($expected_output, $invoker->run());

test/Behavioral/Command/SwitchButtonCommandTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hyunk3l\Test\PhpDesignPatterns\Behavioral\Command;
44

5+
use Hyunk3l\PhpDesignPatterns\Behavioral\Command\ReceiverInterface;
56
use Hyunk3l\PhpDesignPatterns\Behavioral\Command\SwitchButtonCommand;
67
use PHPUnit\Framework\TestCase;
78

@@ -17,7 +18,10 @@ class SwitchButtonCommandTest extends TestCase
1718
*/
1819
public function testExecuteCommand()
1920
{
20-
$receiver = $this->getMock("\\PhpDesignPatterns\\Behavioral\\Command\\ReceiverInterface", array("executeCommand"));
21+
$receiver = $this->getMockBuilder(ReceiverInterface::class)
22+
->setMethods(["executeCommand"])
23+
->disableOriginalConstructor()
24+
->getMock();
2125
$receiver
2226
->expects($this->once())
2327
->method("executeCommand")

test/Creational/Builder/ClassicWatchBuilderTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Hyunk3l\Test\PhpDesignPatterns\Creational\Builder;
44

55
use Hyunk3l\PhpDesignPatterns\Creational\Builder\ClassicWatchBuilder;
6+
use Hyunk3l\PhpDesignPatterns\Creational\Builder\Components\Box;
7+
use Hyunk3l\PhpDesignPatterns\Creational\Builder\Watch;
8+
use Hyunk3l\PhpDesignPatterns\Creational\Builder\WatchBuilderInterface;
69
use PHPUnit\Framework\TestCase;
710

811
/**
@@ -37,8 +40,9 @@ public function tearDown()
3740
/**
3841
* Testing interface contract.
3942
*/
40-
public function testInterfaceContract() {
41-
$this->assertInstanceOf('PhpDesignPatterns\Creational\Builder\WatchBuilderInterface', $this->builder);
43+
public function testInterfaceContract()
44+
{
45+
$this->assertInstanceOf(WatchBuilderInterface::class, $this->builder);
4246
}
4347

4448
/**
@@ -47,7 +51,7 @@ public function testInterfaceContract() {
4751
public function testWatchCreation()
4852
{
4953
$this->builder->createWatch();
50-
$this->assertInstanceOf('PhpDesignPatterns\Creational\Builder\Watch', $this->builder->getWatch());
54+
$this->assertInstanceOf(Watch::class, $this->builder->getWatch());
5155
}
5256

5357
/**
@@ -58,8 +62,7 @@ public function testAddBox()
5862
$this->builder->createWatch();
5963
$this->builder->addBox();
6064

61-
$expected = array("watch box" => $this->getMock("PhpDesignPatterns\\Creational\\Builder\\Components\\Box"));
62-
$this->assertInstanceOf('PhpDesignPatterns\Creational\Builder\Watch', $this->builder->getWatch());
65+
$this->assertInstanceOf(Watch::class, $this->builder->getWatch());
6366
}
6467

6568
/**
@@ -70,7 +73,6 @@ public function testAddHands()
7073
$this->builder->createWatch();
7174
$this->builder->addHands();
7275

73-
$expected = array("Minutes Hand" => $this->getMock("PhpDesignPatterns\\Creational\\Builder\\Components\\Hand"));
74-
$this->assertInstanceOf('PhpDesignPatterns\Creational\Builder\Watch', $this->builder->getWatch());
76+
$this->assertInstanceOf(Watch::class, $this->builder->getWatch());
7577
}
7678
}

test/Creational/Builder/ClassicWatchTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Hyunk3l\Test\PhpDesignPatterns\Creational\Builder;
44

55
use Hyunk3l\PhpDesignPatterns\Creational\Builder\ClassicWatch;
6+
use Hyunk3l\PhpDesignPatterns\Creational\Builder\Components\ComponentInterface;
67
use PHPUnit\Framework\TestCase;
78

89
/**
@@ -17,10 +18,12 @@ class ClassicWatchTest extends TestCase
1718
public function testWatchComponent()
1819
{
1920
$watch = new ClassicWatch;
20-
$mock_component = $this->getMock("PhpDesignPatterns\\Creational\\Builder\\Components\\ComponentInterface");
21-
$watch->addComponent("Test component", $mock_component);
21+
$mockedComponent = $this->getMockBuilder(ComponentInterface::class)
22+
->disableOriginalConstructor()
23+
->getMock();
24+
$watch->addComponent("Test component", $mockedComponent);
2225

23-
$expected = array("Test component" => $mock_component);
26+
$expected = array("Test component" => $mockedComponent);
2427
$this->assertEquals($expected, $watch->getWatchComponents(), "the result is not the expected one.");
2528
}
2629
}

test/Creational/Builder/DirectorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Hyunk3l\Test\PhpDesignPatterns\Creational\Builder;
44

55
use Hyunk3l\PhpDesignPatterns\Creational\Builder\Director;
6+
use Hyunk3l\PhpDesignPatterns\Creational\Builder\WatchBuilderInterface;
67
use PHPUnit\Framework\TestCase;
78

89
/**
@@ -29,7 +30,10 @@ public function testBuild()
2930
'getWatch',
3031
'setWatch',
3132
);
32-
$mocked_builder = $this->getMock('PhpDesignPatterns\Creational\Builder\WatchBuilderInterface', $methods);
33+
$mocked_builder = $this->getMockBuilder(WatchBuilderInterface::class)
34+
->disableOriginalConstructor()
35+
->setMethods($methods)
36+
->getMock();
3337
$mocked_builder->expects($this->once())->method('createWatch')->will($this->returnValue($mocked_builder));
3438
$mocked_builder->expects($this->once())->method('addMovements')->will($this->returnValue($mocked_builder));
3539
$mocked_builder->expects($this->once())->method('addHands')->will($this->returnValue($mocked_builder));

test/Creational/Builder/SportWatchBuilderTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Hyunk3l\Test\PhpDesignPatterns\Creational\Builder;
44

55
use Hyunk3l\PhpDesignPatterns\Creational\Builder\SportWatchBuilder;
6+
use Hyunk3l\PhpDesignPatterns\Creational\Builder\Watch;
7+
use Hyunk3l\PhpDesignPatterns\Creational\Builder\WatchBuilderInterface;
68
use PHPUnit\Framework\TestCase;
79

810
/**
@@ -38,7 +40,7 @@ public function tearDown()
3840
* Testing interface contract.
3941
*/
4042
public function testInterfaceContract() {
41-
$this->assertInstanceOf('PhpDesignPatterns\Creational\Builder\WatchBuilderInterface', $this->builder);
43+
$this->assertInstanceOf(WatchBuilderInterface::class, $this->builder);
4244
}
4345

4446
/**
@@ -47,7 +49,7 @@ public function testInterfaceContract() {
4749
public function testWatchCreation()
4850
{
4951
$this->builder->createWatch();
50-
$this->assertInstanceOf('PhpDesignPatterns\Creational\Builder\Watch', $this->builder->getWatch());
52+
$this->assertInstanceOf(Watch::class, $this->builder->getWatch());
5153
}
5254

5355
/**
@@ -58,7 +60,6 @@ public function testAddBox()
5860
$this->builder->createWatch();
5961
$this->builder->addBox();
6062

61-
$expected = array("watch box" => $this->getMock("PhpDesignPatterns\\Creational\\Builder\\Components\\Box"));
62-
$this->assertInstanceOf('PhpDesignPatterns\Creational\Builder\Watch', $this->builder->getWatch());
63+
$this->assertInstanceOf(Watch::class, $this->builder->getWatch());
6364
}
6465
}

test/Creational/Builder/SportWatchTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hyunk3l\Test\PhpDesignPatterns\Creational\Builder;
44

5+
use Hyunk3l\PhpDesignPatterns\Creational\Builder\Components\ComponentInterface;
56
use Hyunk3l\PhpDesignPatterns\Creational\Builder\SportWatch;
67
use PHPUnit\Framework\TestCase;
78

@@ -17,7 +18,9 @@ class SportWatchTest extends TestCase
1718
public function testWatchComponent()
1819
{
1920
$watch = new SportWatch;
20-
$mock_component = $this->getMock("PhpDesignPatterns\Creational\Builder\Components\ComponentInterface");
21+
$mock_component = $this->getMockBuilder(ComponentInterface::class)
22+
->disableOriginalConstructor()
23+
->getMock();
2124
$watch->addComponent("Test component", $mock_component);
2225

2326
$expected = array("Test component" => $mock_component);

test/Structural/Composite/PhoneButtonTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Structural\Composite;
44

55
use Hyunk3l\PhpDesignPatterns\Structural\Composite\PhoneButton;
6+
use Hyunk3l\PhpDesignPatterns\Structural\Composite\PhoneElement;
67
use PHPUnit\Framework\TestCase;
78

89
/**
@@ -47,17 +48,17 @@ public function testPhoneButtonBuild()
4748
*/
4849
public function testAddChild()
4950
{
50-
$expected_instance = "PhpDesignPatterns\\Structural\\Composite\\PhoneElement";
51-
$phone_element = $this->getMock($expected_instance, array("add", "remove", "build"));
52-
$this->assertInstanceOf($expected_instance, $this->phone_button->add($phone_element));
51+
$phone_element = $this->getMockBuilder(PhoneElement::class)
52+
->setMethods(array("add", "remove", "build"))
53+
->getMock();
54+
$this->assertInstanceOf(PhoneElement::class, $this->phone_button->add($phone_element));
5355
}
5456

5557
/**
5658
* Testing remove child.
5759
*/
5860
public function testRemoveChild()
5961
{
60-
$expected_instance = "PhpDesignPatterns\\Structural\\Composite\\PhoneElement";
61-
$this->assertInstanceOf($expected_instance, $this->phone_button->remove("just_a_key"));
62+
$this->assertInstanceOf(PhoneElement::class, $this->phone_button->remove("just_a_key"));
6263
}
6364
}

test/Structural/Composite/PhoneDisplayTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Structural\Composite;
44

55
use Hyunk3l\PhpDesignPatterns\Structural\Composite\PhoneDisplay;
6+
use Hyunk3l\PhpDesignPatterns\Structural\Composite\PhoneElement;
67
use PHPUnit\Framework\TestCase;
78

89
/**
@@ -47,17 +48,15 @@ public function testPhoneDisplayBuild()
4748
*/
4849
public function testAddChild()
4950
{
50-
$expected_instance = "PhpDesignPatterns\\Structural\\Composite\\PhoneElement";
51-
$phone_element = $this->getMock($expected_instance, array("add", "remove", "build"));
52-
$this->assertInstanceOf($expected_instance, $this->phone_display->add($phone_element));
51+
$phone_element = $this->getMockBuilder(PhoneElement::class)->setMethods(array("add", "remove", "build"))->getMock();
52+
$this->assertInstanceOf(PhoneElement::class, $this->phone_display->add($phone_element));
5353
}
5454

5555
/**
5656
* Testing remove child.
5757
*/
5858
public function testRemoveChild()
5959
{
60-
$expected_instance = "PhpDesignPatterns\\Structural\\Composite\\PhoneElement";
61-
$this->assertInstanceOf($expected_instance, $this->phone_display->remove("just_a_key"));
60+
$this->assertInstanceOf(PhoneElement::class, $this->phone_display->remove("just_a_key"));
6261
}
6362
}

test/Structural/Composite/PhoneTest.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Structural\Composite;
44

55
use Hyunk3l\PhpDesignPatterns\Structural\Composite\Phone;
6+
use Hyunk3l\PhpDesignPatterns\Structural\Composite\PhoneElement;
67
use PHPUnit\Framework\TestCase;
78

89
/**
@@ -52,14 +53,15 @@ public function testNotEmptyPhone()
5253
'remove',
5354
'build',
5455
);
55-
$element_interface = "PhpDesignPatterns\\Structural\\Composite\\PhoneElement";
56-
$display_mock = $this->getMock($element_interface, $methods);
56+
$display_mock = $this->getMockBuilder(PhoneElement::class)->setMethods($methods)->getMock();
5757
$display_mock
5858
->expects($this->once())
5959
->method("build")
6060
->will($this->returnValue("This is a mocked display.".PHP_EOL))
6161
;
62-
$button_mock = $this->getMock($element_interface, $methods);
62+
$button_mock = $this->getMockBuilder(PhoneElement::class)
63+
->setMethods($methods)
64+
->getMock();
6365
$button_mock
6466
->expects($this->once())
6567
->method("build")
@@ -82,14 +84,13 @@ public function testRemoveChildrenFromPhone()
8284
'remove',
8385
'build',
8486
);
85-
$element_interface = "PhpDesignPatterns\\Structural\\Composite\\PhoneElement";
86-
$display_mock = $this->getMock($element_interface, $methods);
87+
$display_mock = $this->getMockBuilder(PhoneElement::class)->setMethods($methods)->getMock();
8788
$display_mock
8889
->expects($this->once())
8990
->method("build")
9091
->will($this->returnValue("This is a mocked display.".PHP_EOL))
9192
;
92-
$button_mock = $this->getMock($element_interface, $methods);
93+
$button_mock = $this->getMockBuilder(PhoneElement::class)->setMethods($methods)->getMock();
9394
$button_mock
9495
->expects($this->never())
9596
->method("build")
@@ -112,14 +113,18 @@ public function testRemoveNotExistingChild()
112113
'remove',
113114
'build',
114115
);
115-
$element_interface = "PhpDesignPatterns\\Structural\\Composite\\PhoneElement";
116-
$display_mock = $this->getMock($element_interface, $methods);
116+
$display_mock = $this->getMockBuilder(PhoneElement::class)
117+
->disableOriginalConstructor()
118+
->setMethods($methods)
119+
->getMock();
117120
$display_mock
118121
->expects($this->once())
119122
->method("build")
120123
->will($this->returnValue("This is a mocked display.".PHP_EOL))
121124
;
122-
$button_mock = $this->getMock($element_interface, $methods);
125+
$button_mock = $this->getMockBuilder(PhoneElement::class)
126+
->setMethods($methods)
127+
->getMock();
123128
$button_mock
124129
->expects($this->once())
125130
->method("build")

0 commit comments

Comments
 (0)