How to override "ModuleCore" class? (/classes/module/Module.php) - prestashop

How to override ModuleCore class located in /classes/module/Module.php?
If I put class ModuleOverride extends ModuleCore (...) in /override/classes/module/Module.php, the file is ignored.

The override of built in class is the same name without the "Core". In ModuleCore the override class would be just class Module extends ModuleCore (...) and the location is correct, the same, but inside the folder override. The Override suffix is for modules classes.
Dont' forget to delete de cache/class_index.php file.

Related

Why don't I need to write property at object class in sealed class by Kotlin?

I'm developing the app by using Kotlin.
sealed class DestinationScreen(val route:String){
object Signup: DestinationScreen(route = "signup")
}
#Composable
fun InstagramApp(){
val navController = rememberNavController()
NavHost(navController = navController, startDestination = DestinationScreen.Signup.route){
composable(DestinationScreen.Signup.route){
SignupScreen(navController = navController)
}
}
}
I don't know Why Signup singleton class can have the property "route" using argv?
I understand it inherits DestinationScreen. So it also has route property.
But Destination class doesn't have concrete the property route.
If Destination class is data class, make sense it doesn't need to declare the property.
No need for{}. And data class has the property not declareing it by using argv.
So I mean DestinationScreen should has concrete property route, if Signup inherit different property's value, it should override.
Why can this codes above work? Does this feature have seal class or object class?
Please teach me. Thank you.
But Destination class doesn't have concrete the property route
Yes, it does. The route property is declared right there in its constructor.
if Signup inherit different property's value, it should override
Not sure what you mean by this, but Signup doesn't need to override the property. It already inherits the property. By passing a value to the super-class's constructor, the existing property gets an initial value as passed by the sub-class without overriding it.
You mention sealed and data class types, but they are irrelevant to this discussion. Inheritance works the same way with sealed and non-sealed classes.
Any time a class extends another class, it also is a type of that class and inherits all of its properties and functions, no overriding needed.

Null controller when test with Mockito

I'm testing a controller that needs a Autowired Service.
I read in many place (example Mockito: How to test my Service with mocking?) I need to do it like this
#RunWith(JUnitPlatform.class)
public class AdminControllerTest {
#Mock
private AdminService service;
#InjectMocks
private AdminController adminController;
#Test
public void registerUser() {
Boolean resultReal = adminController.registerUser();
assertTrue(resultReal);
}
}
But it's fail and I see is because the adminController is null
Instead, if I create the controller like this
AdminController adminController = new AdminController();
It works, but then I can inject the mock.
Maybe I'm forgotten something
Per the documentation for InjectMocks:
MockitoAnnotations.openMocks(this) method has to be called to initialize annotated objects. In above example, openMocks() is called in #Before (JUnit4) method of test's base class. For JUnit3 openMocks() can go to setup() method of a base class. Instead you can also put openMocks() in your JUnit runner (#RunWith) or use the built-in MockitoJUnitRunner.
Thus, either:
call openMocks(this) somewhere before the test is run or
Use #RunWith(MockitoJUnitRunner.class) on the class

How to override PrestShop 1.7.7 module classes?

I want to override class MySQL of PrestaShop module ps_facetedsearch that is located in file: mystore/modules/ps_facetedsearch/src/Adapter/MySQL.php
How do I achieve this?
EDIT:
Class MySQL that I want to override is defined in file mystore/modules/ps_facetedsearch/src/Adapter/MySQL.php.
This class is used by class Search of same module and is defined in file mystore/modules/ps_facetedsearch/src/Product/Search.php. Class Search is used by another module class and so on, so there is a long 'chain' of classes.
Do I need to extend all chained classes or can I somehow override only the MySQL class that really requires modification?
Override Module Classes
To modify behavior of a module’s class you have to put your modified class at
ROOT/override/modules/MODULENAME/OVERRIDECLASS.php
You have to create a PHP file on the location below and call it ps_facetedsearch.php
ROOT/override/modules/ps_facetedsearch/ps_facetedsearch.php
Inside the overriding class you can add the code below.
<?php
class Ps_FacetedsearchOverride extends Ps_Facetedsearch
{
public function getContent()
{
return "Hello store owner! this code is brought to you by crezzur.com";
}
}
?>
After saving your ps_facetedsearch.php override and clearing the Prestashop cache you will see the message "Hello store owner! this code is brought to you by crezzur.com" when you open the module ps_facetedsearch.

How to override the Prestashop 1.7 Controller class

How can I override the default functionality of Prestashop 1.7 using the override feature
To override the Prestashop controller class you have to create a separate file with the same name as the class name and extend it to the core class and put that file in to override folder.
For example: if you want to override the Product class located at
root/classess/Product.php
Create a file with the name Product.php in root/override/classes/ folder.
Now create the Product class and extend it to ProductCore class
class Product extends ProductCore
{
// put the functions and variables that need to be override
}
This is not compulsory to put all the content of ProductCore class into the override class. Just put the required functions that you want to override.
Note: Don't forget to clear the cache.

Why to pass values to the super class? subclass to super

I was checking Equatable package "how to use" examples and they pass values to the super class, and this subject like a blind spot for me, and I see it everywhere:
import 'package:equatable/equatable.dart';
class Person extends Equatable {
final String name;
// why? what's happening behind the scene here?
Person(this.name) : super([name]);
}
another example
#immutable
abstract class MyEvent extends Equatable {
MyEvent([List configs = const []]) : super(configs);
}
1- why we do that? why to pass values for abstract thing? for a blueprint? what's the use of this?
2- why sometimes developers pass values for abstract class like this?
3- what's happening behind the scene here?
4- what is the use cases for such a code?
thanks a lot.
A class being abstract doesn't exclude that it has some concrete members in Dart.
Take the following class as an example:
abstract class Foo {
Foo(this.value);
final int value;
#override
String toString() => value.toString();
}
While it is abstract, it has a concrete implementation of the property value, that is initialized via a custom constructor.
And since the parameter is required, the subclass must call the super constructor like so:
class Subclass extends Foo {
Subclass(): super(42);
}