Completed
Pull Request — master (#8)
by Helpful
02:48
created

CommentNotifierTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 10.13 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 4
lcom 1
cbo 6
dl 8
loc 79
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 8 8 2
A tearDown() 0 6 1
A testSendEmail() 0 52 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class CommentNotifierTest extends SapphireTest {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	protected static $fixture_file = 'CommentNotifications.yml';
6
7
	protected $oldhost = null;
8
9
	protected $extraDataObjects = array(
10
		'CommentNotifiableTest_DataObject'
11
	);
12
	
13 View Code Duplication
	public function setUp() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
setUp uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
14
		parent::setUp();
15
		Email::set_mailer(new EmailTest_Mailer());
0 ignored issues
show
Deprecated Code introduced by
The method Email::set_mailer() has been deprecated with message: since version 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
16
		Config::nest();
17
		Config::inst()->update('Email', 'admin_email', '[email protected]');
18
		$this->oldhost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
19
		$_SERVER['HTTP_HOST'] = 'www.mysite.com';
20
	}
21
22
	public function tearDown() {
0 ignored issues
show
Coding Style introduced by
tearDown uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
23
		$_SERVER['HTTP_HOST'] = $this->oldhost;
24
		Config::unnest();
25
		Email::set_mailer(new Mailer());
0 ignored issues
show
Deprecated Code introduced by
The method Email::set_mailer() has been deprecated with message: since version 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
26
		parent::tearDown();
27
	}
28
29
	public function testSendEmail() {
30
		$author = $this->objFromFixture('Member', 'author');
31
		$item1 = $this->objFromFixture('CommentNotifiableTest_DataObject', 'item1');
32
		$item2 = $this->objFromFixture('CommentNotifiableTest_DataObject', 'item2');
33
		$comment1 = $this->objFromFixture('Comment', 'comment1');
34
		$comment2 = $this->objFromFixture('Comment', 'comment2');
35
		$comment3 = $this->objFromFixture('Comment', 'comment3');
36
		$controller = new CommentNotifierTest_Controller();
37
38
39
		// Commen 1
40
		$result = $controller->notifyCommentRecipient($comment1, $item1, $author);
0 ignored issues
show
Documentation Bug introduced by
The method notifyCommentRecipient does not exist on object<CommentNotifierTest_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
41
		$this->assertEquals('[email protected]', $result['to']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifierTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
		$this->assertEquals('[email protected]', $result['from']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifierTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
		$this->assertEquals('A new comment has been posted', $result['subject']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifierTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
		$this->assertContains('<li>Bob Bobberson</li>', $result['content']);
45
		$this->assertContains('<li>[email protected]</li>', $result['content']);
46
		$this->assertContains('<blockquote>Hey what a lovely comment</blockquote>', $result['content']);
47
		$this->assertContains(
48
			'You can view or moderate this comment at <a href="http://www.mysite.com/item1#comment-' .
49
				$comment1->ID . '">An Object</a>',
50
			$result['content']
51
		);
52
53
		// Comment 2
54
		$result = $controller->notifyCommentRecipient($comment2, $item2, $author);
0 ignored issues
show
Documentation Bug introduced by
The method notifyCommentRecipient does not exist on object<CommentNotifierTest_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
55
		$this->assertEquals('[email protected]', $result['to']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifierTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
		$this->assertEquals('[email protected]', $result['from']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifierTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
		$this->assertEquals('A new comment has been posted', $result['subject']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifierTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
		$this->assertContains('<li>Secret</li>', $result['content']);
59
		$this->assertContains('<li>[email protected]</li>', $result['content']);
60
		$this->assertContains('<blockquote>I don&#039;t want to disclose my details</blockquote>', $result['content']);
61
		$this->assertContains(
62
			'You can view or moderate this comment at <a href="http://www.mysite.com/item2#comment-' .
63
				$comment2->ID . '">Another One</a>',
64
			$result['content']
65
		);
66
67
		// Comment 3
68
		$result = $controller->notifyCommentRecipient($comment3, $item1, $author);
0 ignored issues
show
Documentation Bug introduced by
The method notifyCommentRecipient does not exist on object<CommentNotifierTest_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
69
		$this->assertEquals('[email protected]', $result['to']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifierTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
		$this->assertEquals('[email protected]', $result['from']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifierTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
		$this->assertEquals('A new comment has been posted', $result['subject']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifierTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
		$this->assertContains('<li>Anonymous</li>', $result['content']);
73
		$this->assertContains('<li>[email protected]</li>', $result['content']);
74
		$this->assertContains('<blockquote>I didn&#039;t log in</blockquote>', $result['content']);
75
		$this->assertContains(
76
			'You can view or moderate this comment at <a href="http://www.mysite.com/item1#comment-' .
77
				$comment3->ID . '">An Object</a>',
78
			$result['content']
79
		);
80
	}
81
}
82
83
class CommentNotifierTest_Controller extends Controller implements TestOnly {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
84
	private static $extensions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $extensions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
85
		'CommentNotifier'
86
	);
87
}