Completed
Push — dev2 ( bf3a9f...7a5f92 )
by Gordon
07:26 queued 12s
created

SearchableFieldTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 139
Duplicated Lines 39.57 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.65%
Metric Value
wmc 16
lcom 1
cbo 4
dl 55
loc 139
ccs 83
cts 85
cp 0.9765
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testCMSFields() 0 14 1
B testZeroWeight() 28 28 5
B testNegativeWeight() 27 27 5
A testDefaultWeight() 0 7 1
A testPositiveWeight() 0 8 1
A testHumanReadableSearchable() 0 16 1
A testCanDelete() 0 4 1
A testCanCreate() 0 4 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
/**
4
 * Test the functionality of the Searchable extension
5
 * @package elastica
6
 */
7
class SearchableFieldTest extends ElasticsearchBaseTest {
8
	public static $fixture_file = 'elastica/tests/ElasticaTest.yml';
9
10 1
	public function testCMSFields() {
11 1
		$sf = new SearchableField();
12 1
		$sf->Name = 'TestField';
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<SearchableField>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
13 1
		$sf->ClazzName = 'TestClazz';
0 ignored issues
show
Documentation introduced by
The property ClazzName does not exist on object<SearchableField>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
14 1
		$sf->write();
15
16 1
		$fields = $sf->getCMSFields();
17
18 1
		$tab = $this->checkTabExists($fields,'Main');
19
20
		//Check fields
21 1
		$nf = $this->checkFieldExists($tab, 'Name');
22 1
		$this->assertTrue($nf->isDisabled());
23 1
	}
24
25
26
27
28
	/* Zero weight is pointless as it means not part of the search */
29 2 View Code Duplication
	public function testZeroWeight() {
1 ignored issue
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...
30 1
		$searchPage = $this->objFromFixture('ElasticSearchPage', 'search');
31 1
		$lastEdited = $searchPage->LastEdited;
32
33 1
		$extraFields = array('Searchable' => 1, 'SimilarSearchable' => 1, 'Active' => 1,
34 1
			'Weight' => 0);
35 1
		$esfs = $searchPage->ElasticaSearchableFields();
0 ignored issues
show
Bug introduced by
The method ElasticaSearchableFields() does not exist on DataObject. Did you maybe mean searchableFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
36 1
		foreach ($esfs as $sf) {
37 1
			if ($sf->Name == 'Title' || $sf->Name == 'Description') {
38 1
				$esfs->remove($sf);
39 1
				$esfs->add($sf, $extraFields);
40 1
			}
41 1
		}
42
43
		try {
44 1
			$searchPage->write();
45
			$this->fail('Searchable fail should have failed to write');
46 1
		} catch (ValidationException $e) {
47 1
			$this->assertInstanceOf('ValidationException', $e);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<SearchableFieldTest>.

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...
48
			$expected = 'The field SearchableTestPage.Title has a zero weight. ; The field '.
49 1
			'SiteTree.Title has a zero weight. ; The field Page.Title has a zero weight. ';
50 1
			$this->assertEquals($expected, $e->getMessage());
51
		}
52
53
		//Effectively assert that the search page has not been written by checking LastEdited
54 1
		$pageFromDB = DataObject::get_by_id('ElasticSearchPage', $searchPage->ID);
55 2
		$this->assertEquals($lastEdited, $pageFromDB->LastEdited);
56 2
	}
57
58
59
	/* Weights must be positive */
60 1 View Code Duplication
	public function testNegativeWeight() {
1 ignored issue
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...
61 1
		$searchPage = $this->objFromFixture('ElasticSearchPage', 'search');
62 1
		$lastEdited = $searchPage->LastEdited;
63
64 1
		$extraFields = array('Searchable' => 1, 'SimilarSearchable' => 1, 'Active' => 1,
65 1
			'Weight' => -1);
66 1
		$esfs = $searchPage->ElasticaSearchableFields();
0 ignored issues
show
Bug introduced by
The method ElasticaSearchableFields() does not exist on DataObject. Did you maybe mean searchableFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
67 1
		foreach ($esfs as $sf) {
68 1
			if ($sf->Name == 'Title' || $sf->Name == 'Description') {
69 1
				$esfs->remove($sf);
70 1
				$esfs->add($sf, $extraFields);
71 1
			}
72 1
		}
73
74
		try {
75 1
			$searchPage->write();
76
			$this->fail('Searchable fail should have failed to write');
77 1
		} catch (ValidationException $e) {
78 1
			$this->assertInstanceOf('ValidationException', $e);
79
			$expected = 'The field SearchableTestPage.Title has a negative weight. ; The field '.
80 1
			'SiteTree.Title has a negative weight. ; The field Page.Title has a negative weight. ';
81 1
			$this->assertEquals($expected, $e->getMessage());
82
		}
83
		//Effectively assert that the search page has not been written by checking LastEdited
84 1
		$pageFromDB = DataObject::get_by_id('ElasticSearchPage', $searchPage->ID);
85 1
		$this->assertEquals($lastEdited, $pageFromDB->LastEdited);
86 1
	}
87
88
89 1
	public function testDefaultWeight() {
90 1
		$searchPage = $this->objFromFixture('ElasticSearchPage', 'search');
91
		//$searchPage->write();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
92 1
		$sf = $searchPage->ElasticaSearchableFields()->first();
0 ignored issues
show
Bug introduced by
The method ElasticaSearchableFields() does not exist on DataObject. Did you maybe mean searchableFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
93 1
		$this->assertEquals(1, $sf->Weight);
94 1
		$this->assertTrue($sf->ID > 0);
95 1
	}
96
97
98 1
	public function testPositiveWeight() {
99 1
		$sf = new SearchableField();
100 1
		$sf->Name = 'TestField';
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<SearchableField>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101 1
		$sf->Weight = 10;
0 ignored issues
show
Documentation introduced by
The property Weight does not exist on object<SearchableField>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102 1
		$sf->write();
103 1
		$this->assertEquals(10, $sf->Weight);
0 ignored issues
show
Documentation introduced by
The property Weight does not exist on object<SearchableField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104 1
		$this->assertTrue($sf->ID > 0);
105 1
	}
106
107
108 1
	public function testHumanReadableSearchable() {
109
110 1
		$searchPage = $this->objFromFixture('ElasticSearchPage', 'search');
111
112
		//ensure valid
113 1
		$extraFields = array('Searchable' => 1, 'SimilarSearchable' => 1, 'Active' => 1,
0 ignored issues
show
Unused Code introduced by
$extraFields is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114 1
			'Weight' => 1);
115 1
		$sf = $searchPage->ElasticaSearchableFields()->first();
0 ignored issues
show
Bug introduced by
The method ElasticaSearchableFields() does not exist on DataObject. Did you maybe mean searchableFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
116
117
118 1
		$sf->Name = 'TestField';
119 1
		$sf->Searchable = false;
120 1
		$this->assertEquals('No', $sf->HumanReadableSearchable());
121 1
		$sf->Searchable = true;
122 1
		$this->assertEquals('Yes', $sf->HumanReadableSearchable());
123 1
	}
124
125
126
	// ---- searchable fields are created via a script, so test do not allow creation/deletion ----
127
128
	/*
129
	Ensure CMS users cannot delete searchable fields
130
	 */
131 1
	public function testCanDelete() {
132 1
		$sf = new SearchableField();
133 1
		$this->assertFalse($sf->canDelete());
134 1
	}
135
136
	/*
137
	Ensure CMS users cannot create searchable fields
138
	 */
139 1
	public function testCanCreate() {
140 1
		$sf = new SearchableField();
141 1
		$this->assertFalse($sf->canCreate());
142 1
	}
143
144
145
}
146